/*获得指定文件的绝对路径*/ static char* getRealPath(const char *pathname, char* realpath) { int len; char *dirpath, *ptr; dirpath = path_alloc(&len); /*记录原来的目录*/ if(getcwd(dirpath, len) == NULL) err_sys("getcwd failed\n"); // printf("dirpath_____________%s\n", dirpath); if(chdir(pathname) < 0) err_sys("chdir failed\n"); if(getcwd(realpath, len) == NULL) /*记录指定目录的绝对路径*/ err_sys("getcwd failed\n"); ptr = realpath + strlen(realpath); if(*(ptr-1) != '/') /*保证目录的绝对路径以斜杠结尾*/ { *ptr++ = '/'; *ptr = 0; } if(chdir(dirpath) < 0) /*回到之前记录的原来目录*/ err_sys("chdir failed\n"); //printf("realpath_____________%s\n", realpath); return realpath; }
bool build_apps_config_obj_list () { char * cwd = getcwd (NULL, 0); size_t path_size = 0; if (!cwd) { fprintf (stderr, "Cannot get current working directory!\n"); exit (1); } if (chdir (sdvos_root) < 0) { fprintf (stderr, "Cannot change directory to %s!\n", sdvos_root); exit (1); } memset (apps_obj_list, 0, sizeof (apps_obj_list)); cur_apps_path = path_alloc (&path_size); memset (cur_apps_path, 0, path_size); /* Our current directory should already be SDVOS root */ walk_source_tree ("apps"); if (chdir (cwd) < 0) { fprintf (stderr, "Cannot change directory to %s!\n", cwd); exit (1); } return TRUE; }
int myftw(char*pathname,MyFunc *func) { int len;fullpath=path_alloc(&len); strncpy(fullpath,pathname,len); fullpath[len-1]=0; return dopath(func); }
static int myftw(char *pathname, Myfunc *func) /* return whatever func() returns */ { int len; fullpath = path_alloc(&len); /* malloc's for PATH_MAX+1 bytes */ /* pathalloc.c */ strncpy(fullpath, pathname, len); /* protect against */ fullpath[len-1] = 0; /* buffer overrun */ return (dopath(func)); }
static int /* we return whatever func() returns */ myftw(char *pathname, Myfunc *func) { fullpath = path_alloc(NULL); /* malloc's for PATH_MAX+1 bytes */ /* ({Prog pathalloc}) */ strcpy(fullpath, pathname); /* initialize fullpath */ return(dopath(func)); }
static int myftw(char* pathname, Myfunc *func) { int len; fullpath = path_alloc(&len); //PATH_MAX + 1 bytes strncpy(fullpath, pathname, len); fullpath[len-1] = 0; //printf("fullpath__________%s\n", fullpath);//same as pathname, with '/' in the end return (dopath(func)); }
/* * Descend through the hierarchy, starting at "pathname". * The caller's func() is called for every file. */ static char *fullpath; /* contains full pathname for every fole */ static int /* we return whatever func() returns */ myftw(char *pathname, Myfunc * func) { int len; /* allocate memory for path */ fullpath = path_alloc(&len); strncpy(fullpath, pathname, len); fullpath[len-1] = 0; /* buffer overrun */ return(dopath(func)); }
static int myftw(char *pathname, Myfunc *func) { fullpath = path_alloc(&pathlen); if(pathlen <= strlen(pathname)) { pathlen = strlen(pathname) * 2; if((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } strcpy(fullpath, pathname); return (dopath(func)); }
static int myftw (char *pathname, Myfunc *func) { int len; fullpath = path_alloc (&len); /* malloc's for PATH_MAX + 1 bytes */ strncpy (fullpath, pathname, len); /* protect against */ fullpath[len - 1] = 0; return (dopath (func)); }
void path_append(path_t* p, int x, int y){ if (p == NULL) return; while (p->next != NULL) p = p->next; p->next = path_alloc(x, y); if (p->next == NULL) perror("append path"); }
static int myftw(char *pathname, Myfunc * func) { static char *fullpath = NULL; int len = 0; fullpath = path_alloc(&len); strncpy(fullpath, pathname, len); fullpath[len - 1] = 0; return (dopath(fullpath, func)); }
int main(void) { char *ptr; size_t size; if (chdir("/usr/local") < 0) err_sys("chidr failed"); ptr = path_alloc(&size); /* our own funcation */ if (getcwd(ptr, size) == NULL) err_sys("getcwd failed"); printf("cwd = %s\n", ptr); exit(0); }
static int locftw(char *pathname, Func *func) { fullpath = path_alloc(&pathlen); /* malloc PATH_MAX + 1 */ if (pathlen <= strlen(pathname)) { /* ??? when can this occur ??? */ pathlen = strlen(pathname) * 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } strcpy(fullpath, pathname); return (dopath(func)); }
path_t* path_copy(path_t* src){ path_t** dest_p; path_t* dest; dest_p = &dest; while (src != NULL){ *dest_p = path_alloc(src->cell.x, src->cell.y); dest_p = &((*dest_p)->next); src = src->next; } return dest; }
static int my_ftw(const char *pathname, myfunc *func) { /* 拷贝pathname是递归的需要 */ fullpath = path_alloc(&pathlen); if (pathlen <= strlen(pathname)) { pathlen = strlen(pathname) * 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } strcpy(fullpath, pathname); return (do_path(func)); }
int main(int argc, char *argv[]) { char *ptr; int size; if (chdir("./test/2") < 0) err_sys("chdir failed"); ptr = path_alloc(&size); /* our own function */ if (getcwd(ptr, size) == NULL) err_sys("getcwd failed"); printf("cwd = %s\n", ptr); exit(0); }
/** * #include <unistd.h> * char *getcwd(char *buf,size_t size); **/ int main(void) { char *ptr; int size; if(chdir("/var/www/working") < 0) err_sys("chdir failed"); ptr = path_alloc(&size); if(getcwd(ptr,size) == NULL) err_sys("getcwd failed"); printf("cwd = %s\n",ptr); exit(0); }
//--------------------------------------------------------------------- myftw(char *pathname, Myfunc *func) { fprintf(stderr, "Searching for %s in: %s\n",ar.sval, pathname); ar.pval = (char*)path_alloc(&pathlen); /* malloc PATH_MAX+1 bytes */ if (pathlen <= strlen(pathname)) { pathlen = strlen(pathname) * 2; if ((ar.pval = realloc(ar.pval, pathlen)) == NULL) fprintf(stderr, "%s\n", "Alloc failed"); } strcpy(ar.pval, pathname); return(getPaths(func)); }
static int /* we return whatever func() returns */ myftw(char *pathname, Myfunc *func) { fullpath = path_alloc(&pathlen); /* malloc PATH_MAX+1 bytes */ /* ({Prog pathalloc}) */ if (pathlen <= strlen(pathname)) { pathlen = strlen(pathname) * 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } strcpy(fullpath, pathname); return(dopath(func)); }
kr_adapter_path *kr_adapter_mkpath(kr_adapter *adapter, kr_adapter_path_setup *setup) { kr_adapter_path *path; path = NULL; if (adapter == NULL) return NULL; if (setup == NULL) return NULL; if (adapter->handle.exists == NULL) return NULL; if (adapter->info.api != setup->info.api) return NULL; if (path_setup_check(setup)) return NULL; path = path_alloc(adapter); if (path == NULL) return NULL; path_create(path, setup); return path; }
static int myfunc2(const char *pathname, const struct stat *statptr, int type) { int fd1, fd2; char *buf1, *buf2; char *resolved_path = path_alloc(NULL); int i; buf1 = malloc(sizeof(char) * bufsize); buf2 = malloc(sizeof(char) * bufsize); if ((type == FTW_F) && ((statptr->st_mode & S_IFMT) == S_IFREG) && (filestat.st_size == statptr->st_size)) { /* 文件大小相等,比较文件内容 */ if ((fd1 = open(filename, O_RDONLY)) < 0) { err_quit("cannot open inputfile"); } if ((fd2 = open(pathname, O_RDONLY)) >= 0) { #ifdef DEBUG printf("now begin compare the two files\n"); #endif /* 开始读取两个文件的内容并比较 */ while ((read(fd1, buf1, bufsize) > 0) && (read(fd2, buf2, bufsize) > 0)) { for (i = 0; i < bufsize; i++) { if (buf1[i] != buf2[i]) { /* 文件内容不同,直接返回 */ free(buf1); free(buf2); close(fd1); close(fd2); return 0; } } } /* 文件内容相同 */ if (realpath(pathname, resolved_path) == NULL) { err_quit("realpath error"); } printf("%s\n", resolved_path); } if (fd1 >= 0) { close(fd1); } if (fd2 >= 0){ close(fd2); } } free(buf1); free(buf2); free(resolved_path); return 0; }
int main(void) { char*ptr; size_t size; if(chdir("/usr/include")<0) err_sys("chdir error"); ptr = path_alloc(&size); if(getcwd(ptr,size)==NULL) err_sys("getcwd error"); printf("cwd=%s\n",ptr); exit(0); }
int main(int argc, const char *argv[]) { char *ptr; size_t size; if (chdir("/") < 0) err_sys("chdir failed"); ptr = path_alloc(&size); if (getcwd(ptr, size) == NULL) err_sys("getcwd failed"); printf("cwd = %s\n", ptr); return 0; }
static int myfunc3(const char *pathname, const struct stat *statptr, int type) { int i; char *resolved_path = path_alloc(NULL); /* 因为basename()函数可能改变pathname的值,因此先复制 */ char *pathname_copy = path_alloc(NULL); strcpy(pathname_copy, pathname); char *name = basename(pathname_copy); if (type == FTW_F) { for (i = 0; i < strnum; i++) { if (strcmp(name, str[i]) == 0) { /* 两个文件名相同 */ if (realpath(pathname, resolved_path) == NULL) { err_quit("realpath error"); } printf("%s\n", resolved_path); break; } } } free(pathname_copy); free(resolved_path); return 0; }
int main(void) { char* ptr; size_t size; if(chdir("/usr") < 0){ err_sys("chdir failed"); } ptr = path_alloc(&size); //our own function if(getcwd(ptr,size) == NULL){ err_sys("getcwd failed"); } printf("cwd = %s\n",ptr); exit(0); }
int main (int argc, char* argv[]) { char *ptr; int size; if (chdir("/var/log/news/") < 0) err_sys("chdir /var/log/news failed\n"); ptr = path_alloc(&size); if (getcwd(ptr, size) == NULL) err_sys("get cwd error \n"); printf("cwd is : %s \n", ptr); exit(0); }
int main(void) { char *ptr; int size; if (chdir("/home/skull/.ssh") < 0) err_sys("chdir failed"); ptr = path_alloc(&size); /* our own function */ if (getcwd(ptr, size) == NULL) err_sys("getcwd failed"); printf("cwd = %s\n", ptr); exit(0); }
int main( int argc, char *argv[] ) { #if 0 if ( argc != 3 ){ fprintf ( stderr, "usage: $ %s <device> <text>\n", argv[0] ); fprintf ( stderr, "\t like: $ %s /dev/ttyS0 \"this content will show on termial of /dev/ttyS0\"\n", argv[0]); exit(219); } #else if ( argc < 2 ){ fprintf ( stderr, "usage: $ %s <TTY device> [option...]\n", argv[0] ); fprintf ( stderr, "\t like: $ %s /dev/ttyS0\n", argv[0]); fprintf ( stderr, "\n\t [Option...]: Enter the path which is CGIDebugLogd.py file exist on. \n" "\t this program will default open CGIDebugLogd.py on the same PATH with \"%s\"! \n\n", argv[0]); exit(CMD_ERR); } #endif char *ptr=NULL; size_t size; ptr = path_alloc(&size); /* APUE funciton. */ if (getcwd(ptr, size) == NULL){ fprintf ( stderr, "getcwd() failed!\n"); exit(BUILDIN_FUNC_ERR); } if (doDebug){ fprintf (stdout, "cwd = %s\n", /*current work path*/ptr); } char PathArray[256] = {'\0'}; snprintf(PathArray, 256, "%s", argv[0] ); getCMDPath(PathArray, 256); char absolutePath[512] = {'\0'}; snprintf(absolutePath, 512, "%s%s", ptr, PathArray ); free(ptr); becomeDaemon(0); //run(argc, argv); run(argc, argv, absolutePath); exit(0); }
int main(void) { int i, size; char *path; if (chdir(MYHOME) < 0) err_sys("chdir error"); for (i = 0; i < DEPTH; i++) { if (mkdir(NAME, DIR_MODE) < 0) err_sys("mkdir failed, i = %d", i); if (chdir(NAME) < 0) err_sys("chdir failed, i = %d", i); } if (creat("afile", FILE_MODE) < 0) err_sys("creat error"); /* * The deep directory is created, with a file at the leaf. * Now let's try and obtain its pathname. */ path = path_alloc(&size); // printf ( "Path Size %ld\n", size ) ; for ( ; ; ) { if (getcwd(path, size) != NULL) break; else { err_ret("getcwd failed, size = %d", size); size += 100; if ( (path = realloc(path, size)) == NULL) err_sys("realloc error"); } } printf("length = %d\n%s\n", strlen(path), path); // printf("%s", path); exit(0); }
int main(int argc, char **argv) { char *actualpath, *sympath, *path_buf; int buf_size; if (argc != 3) { err_quit("usage: %s <actualpath> <sympath>", argv[0]); } actualpath = argv[1]; sympath = argv[2]; if (symlink(actualpath, sympath) < 0) { err_sys("symlink error for %s to %s", actualpath, sympath); } path_buf = path_alloc(&buf_size); if (readlink(sympath, path_buf, buf_size) < 0) { err_sys("readlink error for %s", sympath); } printf("%s -> %s\n", sympath, path_buf); exit(0); }