int get_current_path()

in agent/src/c/common/fsof_util.c [116:149]


int get_current_path(char buf[],char *pFileName,int depth) {
    char pidfile[64] = {0};  
    char *p = NULL;
    int bytes = 0;  
    int fd = 0;  
    int cur_dp = 0;

    sprintf(pidfile, "/proc/%d/cmdline", getpid());  
    fd = open(pidfile, O_RDONLY, 0);  
    bytes = read(fd, buf, 256);  
    close(fd);

    p = &buf[strlen(buf)];  
_AGAIN:
    while ('/' != *p) {
        *p = '\0';
        if ((p - buf) == 0) {
            return  1; //path now null,can't get depth dir
        }
        p--;
    }

    cur_dp++;
    if (cur_dp < depth) {
        p--;
        goto  _AGAIN;
    }
    p++;

    memcpy(p,pFileName,strlen(pFileName));  
	p += strlen(pFileName);
	*p = '\0';
    return 0;
}