Actual source code: fpath.c
petsc-3.7.3 2016-08-01
2: /*
3: Code for opening and closing files.
4: */
5: #include <petscsys.h>
6: #if defined(PETSC_HAVE_PWD_H)
7: #include <pwd.h>
8: #endif
12: /*@C
13: PetscGetFullPath - Given a filename, returns the fully qualified file name.
15: Not Collective
17: Input Parameters:
18: + path - pathname to qualify
19: . fullpath - pointer to buffer to hold full pathname
20: - flen - size of fullpath
22: Level: developer
24: Concepts: full path
25: Concepts: path^full
27: .seealso: PetscGetRelativePath()
28: @*/
29: PetscErrorCode PetscGetFullPath(const char path[],char fullpath[],size_t flen)
30: {
32: size_t ln;
33: PetscBool flg;
36: if (path[0] == '/') {
37: PetscStrncmp("/tmp_mnt/",path,9,&flg);
38: if (flg) {PetscStrncpy(fullpath,path + 8,flen);}
39: else {PetscStrncpy(fullpath,path,flen);}
40: fullpath[flen-1] = 0;
41: return(0);
42: }
44: PetscStrncpy(fullpath,path,flen);
45: fullpath[flen-1] = 0;
46: /* Remove the various "special" forms (~username/ and ~/) */
47: if (fullpath[0] == '~') {
48: char tmppath[PETSC_MAX_PATH_LEN],*rest;
49: if (fullpath[1] == '/') {
50: PetscGetHomeDirectory(tmppath,PETSC_MAX_PATH_LEN);
51: rest = fullpath + 2;
52: } else {
53: #if defined(PETSC_HAVE_PWD_H)
54: struct passwd *pwde;
55: char *p,*name;
57: /* Find username */
58: name = fullpath + 1;
59: p = name;
60: while (*p && *p != '/') p++;
61: *p = 0;
62: rest = p + 1;
63: pwde = getpwnam(name);
64: if (!pwde) return(0);
66: PetscStrcpy(tmppath,pwde->pw_dir);
67: #else
68: return(0);
69: #endif
70: }
71: PetscStrlen(tmppath,&ln);
72: if (tmppath[ln-1] != '/') {PetscStrcat(tmppath+ln-1,"/");}
73: PetscStrcat(tmppath,rest);
74: PetscStrncpy(fullpath,tmppath,flen);
75: fullpath[flen-1] = 0;
76: } else {
77: PetscGetWorkingDirectory(fullpath,flen);
78: PetscStrlen(fullpath,&ln);
79: PetscStrncpy(fullpath+ln,"/",flen - ln);
80: fullpath[flen-1] = 0;
81: PetscStrlen(fullpath,&ln);
82: if (path[0] == '.' && path[1] == '/') {
83: PetscStrncat(fullpath,path+2,flen - ln - 1);
84: } else {
85: PetscStrncat(fullpath,path,flen - ln - 1);
86: }
87: fullpath[flen-1] = 0;
88: }
90: /* Remove the automounter part of the path */
91: PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);
92: if (flg) {
93: char tmppath[PETSC_MAX_PATH_LEN];
94: PetscStrcpy(tmppath,fullpath + 8);
95: PetscStrcpy(fullpath,tmppath);
96: }
97: /* We could try to handle things like the removal of .. etc */
98: return(0);
99: }