Actual source code: ffpath.c
petsc-3.4.5 2014-06-29
2: #include <petscsys.h>
3: #if defined(PETSC_HAVE_PWD_H)
4: #include <pwd.h>
5: #endif
6: #include <ctype.h>
7: #include <sys/stat.h>
8: #if defined(PETSC_HAVE_UNISTD_H)
9: #include <unistd.h>
10: #endif
11: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
12: #include <sys/utsname.h>
13: #endif
14: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
15: #include <sys/systeminfo.h>
16: #endif
20: /*@C
21: PetscGetFileFromPath - Finds a file from a name and a path string. A
22: default can be provided.
24: Not Collective
26: Input Parameters:
27: + path - A string containing "directory:directory:..." (without the
28: quotes, of course).
29: As a special case, if the name is a single FILE, that file is
30: used.
31: . defname - default name
32: . name - file name to use with the directories from env
33: - mode - file mode desired (usually r for readable, w for writable, or e for
34: executable)
36: Output Parameter:
37: . fname - qualified file name
39: Level: developer
41: Developer Notes: Wrongly returns 1 as an error code sometimes. Maybe should have additional flag argument indicating
42: if it found it. Most arguments likely should be const.
44: Concepts: files^finding in path
45: Concepts: path^searching for file
47: @*/
48: PetscErrorCode PetscGetFileFromPath(char *path,char *defname,char *name,char *fname,char mode)
49: {
50: char *p,*cdir,trial[PETSC_MAX_PATH_LEN],*senv,*env;
51: size_t ln;
53: PetscBool flg;
56: /* Setup default */
57: PetscGetFullPath(defname,fname,PETSC_MAX_PATH_LEN);
59: if (path) {
60: /* Check to see if the path is a valid regular FILE */
61: PetscTestFile(path,mode,&flg);
62: if (flg) {
63: PetscStrcpy(fname,path);
64: PetscFunctionReturn(1);
65: }
67: /* Make a local copy of path and mangle it */
68: PetscStrallocpy(path,&senv);
69: env = senv;
70: while (env) {
71: /* Find next directory in env */
72: cdir = env;
73: PetscStrchr(env,PETSC_PATH_SEPARATOR,&p);
74: if (p) {
75: *p = 0;
76: env = p + 1;
77: } else env = 0;
79: /* Form trial file name */
80: PetscStrcpy(trial,cdir);
81: PetscStrlen(trial,&ln);
82: if (trial[ln-1] != '/') trial[ln++] = '/';
84: PetscStrcpy(trial + ln,name);
86: PetscTestFile(path,mode,&flg);
87: if (flg) {
88: /* need PetscGetFullPath rather then copy in case path has . in it */
89: PetscGetFullPath(trial,fname,PETSC_MAX_PATH_LEN);
90: PetscFree(senv);
91: PetscFunctionReturn(1);
92: }
93: }
94: PetscFree(senv);
95: }
97: PetscTestFile(path,mode,&flg);
98: if (flg) PetscFunctionReturn(1);
99: return(0);
100: }