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)); }
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 error"); } strcpy(fullpath,pathname); return (dopath(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)); }
static int myftw(char *pathname, char *searchstr, struct FlagOpts *flags) { int len; fullpath = malloc(PATH_MAX+1) ; /* malloc's for PATH_MAX+1 bytes */ symlpath = malloc(PATH_MAX+1) ; root = (struct Node *) malloc( sizeof(struct Node) ); root->x = -1; root->next = NULL; strncpy(fullpath, realpath(pathname,NULL), PATH_MAX+1); return(dopath(searchstr, flags)); }
/* * Descend through the hierarchy, starting at "fullpath". * If "fullpath" is anything other than a directory, we lstat() it, * call func(), and return. For a directory, we call ourself * recursively for each name in the directory. */ static int /* we return whatever func() returns */ dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if (lstat(fullpath, &statbuf) < 0) return(func(fullpath, &statbuf, FTW_NS)); /* stat error */ if (S_ISDIR(statbuf.st_mode) == 0) return(func(fullpath, &statbuf, FTW_F)); /* not a directory */ /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ if ( (ret = func(fullpath, &statbuf, FTW_D)) != 0) return(ret); ptr = fullpath + strlen(fullpath); /* point to end of fullpath */ *ptr++ = '/'; *ptr = 0; if ( (dp = opendir(fullpath)) == NULL) return(func(fullpath, &statbuf, FTW_DNR)); /* can't read directory */ while ( (dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; /* ignore dot and dot-dot */ strcpy(ptr, dirp->d_name); /* append name after slash */ if ( (ret = dopath(func)) != 0) /* recursive */ break; /* time to leave */ } ptr[-1] = 0; /* erase everything from slash onwards */ if (closedir(dp) < 0) err_ret("can't close directory %s", fullpath); return(ret); }
static int dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if (lstat(fullpath, &statbuf) < 0) return (func(fullpath, &statbuf, FTW_NS)); // not dir if (S_ISDIR(statbuf.st_mode) == 0) return (func(fullpath, &statbuf, FTW_F)); if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return ret; ptr = fullpath + strlen(fullpath); *ptr++ = '/'; *ptr = 0; // can't read a dir if ((dp = opendir(fullpath)) == NULL) return (func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if ((strcmp(dirp->d_name, ".") == 0) || (strcmp(dirp->d_name, "..") == 0)) continue; strcpy(ptr, dirp->d_name); if ((ret = dopath(func)) != 0) break; } ptr[-1] = 0; if (closedir(dp) < 0) { fprintf(stderr, "can't close dir %s\n", fullpath); exit(2); } return ret; }
struct input_file * fopen_trypath(struct input_file *i, const char *filename) { FILE *f; f = fopen(filename, "r"); if (f != NULL) { set_input(i, f, filename); return i; } if (filename[0] == '/') return NULL; ensure_m4path(); return dopath(i, filename); }
/* * Descend through the hierarchy, starting at "fullpath". * If "fullpath" is anything other than a directory, we lstat() it, * call func(), and return. For a directory, we call ourself * recursively for each name in the directory. */ static int /* we return whatever func() returns */ dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret, n; if (lstat(fullpath, &statbuf) < 0) /* stat error */ return(func(fullpath, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) /* not a directory */ return(func(fullpath, &statbuf, FTW_F)); /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return(ret); n = strlen(fullpath); if (n + NAME_MAX + 2 > pathlen) { /* expand path buffer */ pathlen *= 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } fullpath[n++] = '/'; fullpath[n] = 0; if ((dp = opendir(fullpath)) == NULL) /* can't read directory */ return(func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; /* ignore dot and dot-dot */ strcpy(&fullpath[n], dirp->d_name); /* append name after "/" */ if ((ret = dopath(func)) != 0) /* recursive */ break; /* time to leave */ } fullpath[n-1] = 0; /* erase everything from slash onward */ if (closedir(dp) < 0) err_ret("can't close directory %s", fullpath); return(ret); }
/* * Descend through the hierarchy, starting at "fullpath" * If "fullpath" is anything other than a directory, we lstat if, * call func(), and return. For a directory, we call ourself * recursively for each name in the directory. */ static int dopath(Myfunc* func) { struct stat statbuf; // information of a file struct dirent *dirp; // information of files in a directory DIR *dp; // information of a dir int ret; char *ptr; if (lstat(fullpath, &statbuf) < 0) // stat error return(func(fullpath, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) // not a directory return(func(fullpath, &statbuf, FTW_F)); /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return ret; ptr = fullpath + strlen(fullpath); //point to end of fullpath *ptr++ = '/'; *ptr = 0; if ((dp = opendir(fullpath)) == NULL) // can't read directory return(func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; // ignore dot and dot-dot strcpy(ptr, dirp->d_name); //append name after slash if ((ret = dopath(func)) != 0) // recursive break; // time to leave } ptr[-1] = 0; // erase everything from slash onwards if (closedir(dp) < 0) printf("can't close directory %s", fullpath); return ret; }
static int dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if(lstat(fullpath,&statbuf)<0) return (func(fullpath,&statbuf,FTW_NS)); if(S_ISDIR(statbuf.st_mode)==0) return (func(fullpath,&statbuf,FTW_F)); /* * It's a dir,first call func() for the dir * then process each filename in the dir * */ if((ret=func(fullpath,&statbuf,FTW_D))!=0) return (ret); ptr=fullpath+strlen(fullpath); *(ptr++)='/'; *ptr=0; if((dp=opendir(fullpath))==NULL){ return (func(fullpath,&statbuf,FTW_DNR)); } while((dirp=readdir(dp))!=NULL){ if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0) continue; strcpy(ptr,dirp->d_name);/*ingore dot and dot dot*/ if((ret=dopath(func))!=0)/*recursive*/ break; } ptr[-1]=0; if(closedir(dp)<0){ printf("can't close dir:%s",fullpath); } return (ret); }
static int dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if(lstat(fullpath, &statbuf) < 0) return (func(fullpath, &statbuf, FTW_NS)); if(S_ISDIR(statbuf.st_mode) == 0) return (func(fullpath, &statbuf, FTW_F)); if((ret = func(fullpath, &statbuf, FTW_D)) != 0) return (ret); ptr = fullpath + strlen(fullpath); *ptr++ = '/'; *ptr = 0; if((dp = opendir(fullpath)) == NULL) return (func(fullpath, &statbuf, FTW_DNR)); while((dirp = readdir(dp)) != NULL) { if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; strcpy(ptr, dirp->d_name); if((ret = dopath(func)) != 0) break; } ptr[-1] = 0; if(closedir(dp) < 0) printf("can't close directory %s", fullpath); return (ret); }
int dopath(MyFunc*func) { struct stat statbuf; struct dirent *dirp; DIR *dp;int ret;char *ptr; if(lstat(fullpath,&statbuf)<0) return func(fullpath,&statbuf,FTW_NS); if(S_ISDIR(statbuf.st_mode)==0) return func(fullpath,&statbuf,FTW_F); if((ret=func(fullpath,&statbuf,FTW_D))!=0) return ret; ptr=fullpath+strlen(fullpath);*ptr++='/';*ptr=0; if((dp=opendir(fullpath))==NULL) return func(fullpath,&statbuf,FTW_DNR); while((dirp=readdir(dp))!=NULL) { if(strcmp(dirp->d_name,".")==0||strcmp(dirp->d_name,"..")==0) continue; strcpy(ptr,dirp->d_name); if((ret=dopath(func))!=0) break; } ptr[-1]=0;if(closedir(dp)<0) err_sys("close dp error\n"); return ret; }
/* return text of current dir into specified buffer */ long xgetdir(char *buf, int drv) { DND *p; int len; /* M01.01.1024.02 */ drv = (drv == 0) ? run->p_curdrv : drv-1 ; if( !(Drvmap() & (1<<drv)) || (ckdrv(drv) < 0) ) /* M01.01.1031.01 */ { *buf = 0; return(EDRIVE); } p = dirtbl[(int)(run->p_curdir[drv])]; len = LEN_ZPATH - 3; /* M01.01.1024.02 */ buf = dopath(p,buf,&len); /* M01.01.1024.02 */ *--buf = 0; /* null as last char, not slash */ return(E_OK); }
static int dopath(_myfunc *func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret, n; if (lstat(fullpath, &statbuf) < 0) return (func(fullpath, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) return (func(fullpath, &statbuf, FTW_F)); if ((ret = func(fullpath, &statbuf, FTW_D)) != 0) return (ret); n = strlen(fullpath); if (n + NAME_MAX + 2 > pathlen) { pathlen *= 2; if ((fullpath = realloc(fullpath, pathlen)) == NULL) err_sys("realloc failed"); } fullpath[n++] = '/'; fullpath[n] = 0; if ((dp = opendir(fullpath)) == NULL) return (func(fullpath, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) { continue; } strcpy(&fullpath[n], dirp->d_name); if ((ret = dopath(func)) != 0) break; } fullpath[n-1] = 0; if (closedir(dp) < 0) err_ret("can`t close directory %s", fullpath); return (ret); }
static int dopath(Myfunc* func) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; if(lstat(fullpath, &statbuf) < 0) return (func(fullpath, &statbuf, FTW_NS)); if(S_ISDIR(statbuf.st_mode) == 0)//not a dir return (func(fullpath, &statbuf, FTW_F)); //dir if((ret = func(fullpath, &statbuf, FTW_D)) != 0) return (ret); //printf("%s_______fullpath_in_dopath\n", fullpath); ptr = fullpath + strlen(fullpath);//point to end of path *ptr++ = '/';//这里不需要了,fullpath=realpath,realpath后面有已经有'/' //printf("after_/_fullpath______%s\n", fullpath); *ptr = 0; //if can't read dir if ((dp = opendir(fullpath)) == NULL) return (func(fullpath, &statbuf, FTW_DNR)); //read while((dirp = readdir(dp)) != NULL) { if(strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; strcpy(ptr, dirp->d_name);//append dname after '/' if((ret = dopath(func)) != 0)//recursive 递归,循环 break;//time to leave } //ptr[-1] = 0;//erase everything from slash onwards ??? if(closedir(dp) < 0) err_ret("can't close dir %s", fullpath); return (ret); }
static int dopath(cbfile cbfil){ struct stat statbuf; struct dirent *dirp; DIR *dp; int ret, n; if(lstat(fullpath, &statbuf) < 0)return(cbfil(fullpath, &statbuf, FTW_NS)); if(S_ISDIR(statbuf.st_mode)==0)return(cbfil(fullpath, &statbuf, FTW_F)); if((ret = cbfil(fullpath, &statbuf, FTW_D))!=0)return(ret); n = strlen(fullpath); fullpath[n++]= '/'; fullpath[n] = 0; if((dp = opendir(fullpath)) == NULL) return(cbfil(fullpath, &statbuf, FTW_DNR)); while((dirp = readdir(dp)) != NULL){ if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)continue; strcpy(&fullpath[n], dirp->d_name); if((ret = dopath(cbfil)) != 0)break; } fullpath[n-1] = 0; if(closedir(dp)<0)printf("can't close directory %s", fullpath); return(ret); }
/* * Descend through the hierarchy, starting at "filename". * If "filename" is anything other than a directory, we lstat() it, * call func(), and return. For a directory, we call ourself * recursively for each name in the directory. */ static int dopath(Myfunc *func, char *filename) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; if (lstat(filename, &statbuf) < 0) /* stat error */ return (func(filename, &statbuf, FTW_NS)); if (S_ISDIR(statbuf.st_mode) == 0) /* not a directory */ return (func(filename, &statbuf, FTW_F)); /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ if ((ret = func(filename, &statbuf, FTW_D)) != 0) return ret; if ((dp = opendir(filename)) == NULL) /* can't read directory */ return (func(filename, &statbuf, FTW_DNR)); chdir(filename); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; /* ignore dot and dot-dot */ if ((ret = dopath(func, dirp->d_name)) != 0) /* recursive */ break; /* time to leave; */ } chdir(".."); if (closedir(dp) < 0) err_ret("can't close directory %s", filename); return ret; }
static char *dopath(DND *p, char *buf, int *len) { char temp[LEN_ZFNAME]; char *tp; long tlen; if ( p->d_parent ) buf = dopath(p->d_parent,buf,len); tlen = (long)packit(p->d_name,temp) - (long)temp; tp = temp; while ( *len ) { (*len)--; /* len must never go < 0 */ if ( tlen-- ) *buf++ = *tp++; else { *buf++ = SLASH; break; } } return(buf); }
static int zftw(char *pathname, cbfile cbfil){ strcpy(fullpath, pathname); return(dopath(cbfil)); }
static int myftw(char* pathname,Myfunc* func) { fullpath=(char*)malloc(PATH_MAX+1); strcpy(fullpath,pathname); return (dopath(func)); }
static int /* we return whatever func() returns */ myftw(char *filename, Myfunc *func) { return (dopath(func, filename)); }
/* * Descend through the hierarchy, starting at "fullpath". * If "fullpath" is anything other than a directory, we lstat() it, * call the search function, and return. For a directory, we call ourself * recursively for each name in the directory. */ static int /* we return whatever func() returns */ dopath(char *searchstr, struct FlagOpts *flags) { struct stat statbuf; struct dirent *dirp; DIR *dp; int ret; char *ptr; int errnum; errno = 0; if (lstat(fullpath, &statbuf) < 0) { /* stat error */ errnum = errno; my_errprintf("Error reading file attribute: %s\n", strerror(errnum) ); } if (S_ISLNK(statbuf.st_mode) != 0) { /* symbolic link */ if( (*flags).l == 1) { // search symbolic links int k = readlink( fullpath, symlpath, PATH_MAX+1 ); symlpath[k] = '\0'; if (stat(fullpath, &statbuf) < 0) { /* stat error */ errnum = errno; my_errprintf("Error reading file attribute: %s\n", strerror(errnum) ); } } else { // do not search file or folder return 0; } } // Do not read already visited files if ( alreadyVisited(statbuf.st_ino) == 1 ) { return 0; } if (S_ISDIR(statbuf.st_mode) == 0) { /* not a directory */ // Skip certain files according to [-f] flags used if ( fileTypesFlag == 1 ) { if ( checkEnding('c',fullpath)==1 ) { if( flags->c == 0) return 0; } else if ( checkEnding('h',fullpath)==1 ) { if( flags->h == 0) return 0; } else if ( checkEnding('S',fullpath)==1 ) { if( flags->S == 0) return 0; } else { //other type of file - skip since flag opt used return 0; } } // add regular file i-node to list of files visited markVisited(statbuf.st_ino); return(readfile(fullpath, searchstr, &statbuf, FTW_F)); } /* * It's a directory. First call func() for the directory, * then process each filename in the directory. */ // add directory i-node to list of files visited markVisited(statbuf.st_ino); if ((ret = readfile(fullpath, searchstr, &statbuf, FTW_D)) != 0) return(ret); ptr = fullpath + strlen(fullpath); /* point to end of fullpath */ *ptr++ = '/'; *ptr = 0; if ((dp = opendir(fullpath)) == NULL) /* can't read directory */ return(readfile(fullpath, searchstr, &statbuf, FTW_DNR)); while ((dirp = readdir(dp)) != NULL) { if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0) continue; /* ignore dot and dot-dot */ strcpy(ptr, dirp->d_name); /* append name after slash */ if ((ret = dopath(searchstr, flags)) != 0) /* recursive */ break; /* time to leave */ } ptr[-1] = 0; /* erase everything from slash onwards */ if (closedir(dp) < 0) ; return(ret); }