Actual source code: fpath.c
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
19: .seealso: `PetscGetRelativePath()`
20: @*/
21: PetscErrorCode PetscGetFullPath(const char path[], char fullpath[], size_t flen)
22: {
23: size_t ln;
24: PetscBool flg;
26: if (path[0] == '/') {
27: PetscStrncmp("/tmp_mnt/", path, 9, &flg);
28: if (flg) PetscStrncpy(fullpath, path + 8, flen);
29: else PetscStrncpy(fullpath, path, flen);
30: fullpath[flen - 1] = 0;
31: return 0;
32: }
33: if (path[0] == '.' && path[1] == '/') {
34: PetscGetWorkingDirectory(fullpath, flen);
35: PetscStrlcat(fullpath, path + 1, flen);
36: return 0;
37: }
39: PetscStrncpy(fullpath, path, flen);
40: fullpath[flen - 1] = 0;
41: /* Remove the various "special" forms (~username/ and ~/) */
42: if (fullpath[0] == '~') {
43: char tmppath[PETSC_MAX_PATH_LEN], *rest;
44: if (fullpath[1] == '/') {
45: PetscGetHomeDirectory(tmppath, PETSC_MAX_PATH_LEN);
46: rest = fullpath + 2;
47: } else {
48: #if defined(PETSC_HAVE_PWD_H)
49: struct passwd *pwde;
50: char *p, *name;
52: /* Find username */
53: name = fullpath + 1;
54: p = name;
55: while (*p && *p != '/') p++;
56: *p = 0;
57: rest = p + 1;
58: pwde = getpwnam(name);
59: if (!pwde) return 0;
61: PetscStrcpy(tmppath, pwde->pw_dir);
62: #else
63: return 0;
64: #endif
65: }
66: PetscStrlen(tmppath, &ln);
67: if (tmppath[ln - 1] != '/') PetscStrcat(tmppath + ln - 1, "/");
68: PetscStrcat(tmppath, rest);
69: PetscStrncpy(fullpath, tmppath, flen);
70: fullpath[flen - 1] = 0;
71: } else {
72: PetscGetWorkingDirectory(fullpath, flen);
73: PetscStrlen(fullpath, &ln);
74: PetscStrncpy(fullpath + ln, "/", flen - ln);
75: fullpath[flen - 1] = 0;
76: PetscStrlen(fullpath, &ln);
77: if (path[0] == '.' && path[1] == '/') {
78: PetscStrlcat(fullpath, path + 2, flen);
79: } else {
80: PetscStrlcat(fullpath, path, flen);
81: }
82: fullpath[flen - 1] = 0;
83: }
85: /* Remove the automounter part of the path */
86: PetscStrncmp(fullpath, "/tmp_mnt/", 9, &flg);
87: if (flg) {
88: char tmppath[PETSC_MAX_PATH_LEN];
89: PetscStrcpy(tmppath, fullpath + 8);
90: PetscStrcpy(fullpath, tmppath);
91: }
92: /* We could try to handle things like the removal of .. etc */
93: return 0;
94: }