Actual source code: grpath.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: PetscGetRealPath - Get the path without symbolic links etc. and in absolute form.
28: Not Collective
30: Input Parameter:
31: . path - path to resolve
33: Output Parameter:
34: . rpath - resolved path
36: Level: developer
38: Notes:
39: rpath is assumed to be of length PETSC_MAX_PATH_LEN.
41: Systems that use the automounter often generate absolute paths
42: of the form "/tmp_mnt....". However, the automounter will fail to
43: mount this path if it is not already mounted, so we remove this from
44: the head of the line. This may cause problems if, for some reason,
45: /tmp_mnt is valid and not the result of the automounter.
47: Concepts: real path
48: Concepts: path^real
50: .seealso: PetscGetFullPath()
51: @*/
52: PetscErrorCode PetscGetRealPath(const char path[],char rpath[])
53: {
55: char tmp3[PETSC_MAX_PATH_LEN];
56: PetscTruth flg;
57: #if !defined(PETSC_HAVE_REALPATH) && defined(PETSC_HAVE_READLINK)
58: char tmp1[PETSC_MAX_PATH_LEN],tmp4[PETSC_MAX_PATH_LEN],*tmp2;
59: size_t N,len,len1,len2;
60: int n,m;
61: #endif
64: #if defined(PETSC_HAVE_REALPATH)
65: realpath(path,rpath);
66: #elif defined(PETSC_HAVE_READLINK)
67: /* Algorithm: we move through the path, replacing links with the real paths. */
68: PetscStrcpy(rpath,path);
69: PetscStrlen(rpath,&N);
70: while (N) {
71: PetscStrncpy(tmp1,rpath,N);
72: tmp1[N] = 0;
73: n = readlink(tmp1,tmp3,PETSC_MAX_PATH_LEN);
74: if (n > 0) {
75: tmp3[n] = 0; /* readlink does not automatically add 0 to string end */
76: if (tmp3[0] != '/') {
77: PetscStrchr(tmp1,'/',&tmp2);
78: PetscStrlen(tmp1,&len1);
79: PetscStrlen(tmp2,&len2);
80: m = len1 - len2;
81: PetscStrncpy(tmp4,tmp1,m);
82: tmp4[m] = 0;
83: PetscStrlen(tmp4,&len);
84: PetscStrncat(tmp4,"/",PETSC_MAX_PATH_LEN - len);
85: PetscStrlen(tmp4,&len);
86: PetscStrncat(tmp4,tmp3,PETSC_MAX_PATH_LEN - len);
87: PetscGetRealPath(tmp4,rpath);
88: PetscStrlen(rpath,&len);
89: PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
90: } else {
91: PetscGetRealPath(tmp3,tmp1);
92: PetscStrncpy(rpath,tmp1,PETSC_MAX_PATH_LEN);
93: PetscStrlen(rpath,&len);
94: PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
95: }
96: return(0);
97: }
98: PetscStrchr(tmp1,'/',&tmp2);
99: if (tmp2) {
100: PetscStrlen(tmp1,&len1);
101: PetscStrlen(tmp2,&len2);
102: N = len1 - len2;
103: } else {
104: PetscStrlen(tmp1,&N);
105: }
106: }
107: PetscStrncpy(rpath,path,PETSC_MAX_PATH_LEN);
108: #else /* Just punt */
109: PetscStrcpy(rpath,path);
110: #endif
112: /* remove garbage some automounters put at the beginning of the path */
113: PetscStrncmp("/tmp_mnt/",rpath,9,&flg);
114: if (flg) {
115: PetscStrcpy(tmp3,rpath + 8);
116: PetscStrcpy(rpath,tmp3);
117: }
118: return(0);
119: }