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