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