📄 find_name.c
字号:
/*==========================================================================*//* DMC Interim out | find_name.c | Utility *//*==========================================================================*//* Name: find_name.c Purpose:look for a given file in a series of one or more locations specified by a circular linked list of path names and return the absolute file name if found. Usage: int find_name (req, data, name) struct DMC_request req struct path_list **data char *name int ret ret = find_name (req, data, name) Input: struct DMC_request req; request file entry struct path_list **data; ptr to ptr to circular list of directory paths char *name; ptr to absolute file name Output: int find_name ()= 0, file exists = 1, file does not exist = 2, file status error char *name ptr to absolute file name struct path_list **data; new ptr to ptr to circular list of directory paths Externals:Debug - setting of environment variable DEBUG (globals.h) Messages: Warnings: Errors: Fatals: Called by: Calls to:fexist tests existence of path plus file and returns full name Algorithm: Notes: Problems: Debug: level D_MIN - print out start and finish notices level D_MED - level D_MAX - References: none Language: ANSI standard C, under Sun OS 3.5 Revisions: 03/06/89 Kevin MacKenzie original version*/#include <dirent.h>#include <sys/param.h>#include "output.h" /*=====================================*//*=================| |=================*/ /*=====================================*/ int find_name (req, data, name)struct DMC_request *req; /* request file entry */struct path_list **data; /* ptr to ptr to circular list of */ /* directory paths */char *name; /* ptr to absolute file name */{ register struct path_list *p; /* ptr to circular list */ int err; /* result */ if (Debug >= D_MIN) fprintf (D_OUT, "[find_name] Started.\n"); p = *data; /* point to circular list */ while (((err = fexist (p->path, req->entry.file, name)) == 1) && ((p = p->next) != *data) ); if (err == 0) *data = p; /* save location in circular list */ if (err != 0) /* file not found, look into dirs */ err = scan_DATA_PATH(req, data, name); if (Debug >= D_MIN) fprintf (D_OUT, "[find_name] Finished.\n"); return (err);}/* ------------------------------------------------------------------------ */int scan_DATA_PATH(req, data, name)struct DMC_request *req; /* request file entry */struct path_list **data; /* ptr to ptr to circular list of */ /* directory paths */char *name; /* ptr to absolute file name */{ register struct path_list *p = *data; /* ptr to circular list */ int result; /* 0 = found, 1 not found */ DIR *dirp; struct dirent *direntp; do { result = scan_this_path(p->path, req->entry.file, name); } while (((p = p->next) != *data) && (result != 0)); return result;}int scan_this_path(char *path, char *file, char *fname){ DIR *dirp; struct dirent *direntp; int result; char full_name[MAXPATHLEN]; /* see if file is in this level of directory */ if (fexist(path, file, fname) == 0) return 0; dirp = opendir(path); if (dirp == NULL) { fprintf(stderr, "Error, scan_DATA_PATH(): Unable to open directory\n"); fprintf(stderr, "for directory: %s\n", path); perror("scan_this_path()"); return 1; /* file not found */ } while ((direntp = readdir(dirp)) != NULL) { /* filter out the "." and the ".."'s */ if ((strcmp(direntp->d_name, ".") == 0) || (strcmp(direntp->d_name, "..") == 0)) continue; /* check for sub directory */ if (is_dir(path, direntp->d_name)) { sprintf(full_name, "%s/%s", path, direntp->d_name); result = scan_this_path(full_name, file, fname); if (result == 0) { closedir(dirp); return result; } continue; } } closedir(dirp); return 1;} /* ------------------------------------------------------------------------ */int dump_name(struct path_list *pl){ struct path_list *start; start = pl; do { printf("Path list=%s\n", pl->path); pl = pl->next; } while (pl != start);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -