Actual source code: ffpath.c
1: #define PETSC_DLL
3: #include petscsys.h
4: #if defined(PETSC_HAVE_PWD_H)
5: #include <pwd.h>
6: #endif
7: #include <ctype.h>
8: #include <sys/types.h>
9: #include <sys/stat.h>
10: #if defined(PETSC_HAVE_UNISTD_H)
11: #include <unistd.h>
12: #endif
13: #if defined(PETSC_HAVE_STDLIB_H)
14: #include <stdlib.h>
15: #endif
16: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
17: #include <sys/utsname.h>
18: #endif
19: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
20: #include <sys/systeminfo.h>
21: #endif
25: /*@C
26: PetscGetFileFromPath - Finds a file from a name and a path string. A
27: default can be provided.
29: Not Collective
31: Input Parameters:
32: + path - A string containing "directory:directory:..." (without the
33: quotes, of course).
34: As a special case, if the name is a single FILE, that file is
35: used.
36: . defname - default name
37: . name - file name to use with the directories from env
38: - mode - file mode desired (usually r for readable, w for writable, or e for
39: executable)
41: Output Parameter:
42: . fname - qualified file name
44: Level: developer
46: Concepts: files^finding in path
47: Concepts: path^searching for file
49: @*/
50: PetscErrorCode PetscGetFileFromPath(char *path,char *defname,char *name,char *fname,char mode)
51: {
52: char *p,*cdir,trial[PETSC_MAX_PATH_LEN],*senv,*env;
53: size_t ln;
55: PetscTruth flg;
58: /* Setup default */
59: PetscGetFullPath(defname,fname,PETSC_MAX_PATH_LEN);
61: if (path) {
62: /* Check to see if the path is a valid regular FILE */
63: PetscTestFile(path,mode,&flg);
64: if (flg) {
65: PetscStrcpy(fname,path);
66: PetscFunctionReturn(1);
67: }
68:
69: /* Make a local copy of path and mangle it */
70: PetscStrallocpy(path,&senv);
71: env = senv;
72: while (env) {
73: /* Find next directory in env */
74: cdir = env;
75: PetscStrchr(env,PETSC_PATH_SEPARATOR,&p);
76: if (p) {
77: *p = 0;
78: env = p + 1;
79: } else
80: env = 0;
82: /* Form trial file name */
83: PetscStrcpy(trial,cdir);
84: PetscStrlen(trial,&ln);
85: if (trial[ln-1] != '/') trial[ln++] = '/';
86:
87: PetscStrcpy(trial + ln,name);
89: PetscTestFile(path,mode,&flg);
90: if (flg) {
91: /* need PetscGetFullPath rather then copy in case path has . in it */
92: PetscGetFullPath(trial,fname,PETSC_MAX_PATH_LEN);
93: PetscFree(senv);
94: PetscFunctionReturn(1);
95: }
96: }
97: PetscFree(senv);
98: }
100: PetscTestFile(path,mode,&flg);
101: if (flg) PetscFunctionReturn(1);
102: return(0);
103: }