Actual source code: grpath.c
1: #define PETSC_DLL
3: #include petsc.h
4: #include petscsys.h
5: #if defined(PETSC_HAVE_PWD_H)
6: #include <pwd.h>
7: #endif
8: #include <ctype.h>
9: #include <sys/types.h>
10: #include <sys/stat.h>
11: #if defined(PETSC_HAVE_UNISTD_H)
12: #include <unistd.h>
13: #endif
14: #if defined(PETSC_HAVE_STDLIB_H)
15: #include <stdlib.h>
16: #endif
17: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
18: #include <sys/utsname.h>
19: #endif
20: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
21: #include <sys/systeminfo.h>
22: #endif
23: #include "petscfix.h"
27: /*@C
28: PetscGetRealPath - Get the path without symbolic links etc. and in absolute form.
30: Not Collective
32: Input Parameter:
33: . path - path to resolve
35: Output Parameter:
36: . rpath - resolved path
38: Level: developer
40: Notes:
41: rpath is assumed to be of length PETSC_MAX_PATH_LEN.
43: Systems that use the automounter often generate absolute paths
44: of the form "/tmp_mnt....". However, the automounter will fail to
45: mount this path if it is not already mounted, so we remove this from
46: the head of the line. This may cause problems if, for some reason,
47: /tmp_mnt is valid and not the result of the automounter.
49: Concepts: real path
50: Concepts: path^real
52: .seealso: PetscGetFullPath()
53: @*/
54: PetscErrorCode PetscGetRealPath(char path[],char rpath[])
55: {
57: char tmp3[PETSC_MAX_PATH_LEN];
58: PetscTruth flg;
59: #if !defined(PETSC_HAVE_REALPATH) && defined(PETSC_HAVE_READLINK)
60: char tmp1[PETSC_MAX_PATH_LEN],tmp4[PETSC_MAX_PATH_LEN],*tmp2;
61: size_t N,len,len1,len2;
62: int n,m;
63: #endif
66: #if defined(PETSC_HAVE_REALPATH)
67: realpath(path,rpath);
68: #elif defined(PETSC_HAVE_READLINK)
69: /* Algorithm: we move through the path, replacing links with the real paths. */
70: PetscStrcpy(rpath,path);
71: PetscStrlen(rpath,&N);
72: while (N) {
73: PetscStrncpy(tmp1,rpath,N);
74: tmp1[N] = 0;
75: n = readlink(tmp1,tmp3,PETSC_MAX_PATH_LEN);
76: if (n > 0) {
77: tmp3[n] = 0; /* readlink does not automatically add 0 to string end */
78: if (tmp3[0] != '/') {
79: PetscStrchr(tmp1,'/',&tmp2);
80: PetscStrlen(tmp1,&len1);
81: PetscStrlen(tmp2,&len2);
82: m = len1 - len2;
83: PetscStrncpy(tmp4,tmp1,m);
84: tmp4[m] = 0;
85: PetscStrlen(tmp4,&len);
86: PetscStrncat(tmp4,"/",PETSC_MAX_PATH_LEN - len);
87: PetscStrlen(tmp4,&len);
88: PetscStrncat(tmp4,tmp3,PETSC_MAX_PATH_LEN - len);
89: PetscGetRealPath(tmp4,rpath);
90: PetscStrlen(rpath,&len);
91: PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
92: } else {
93: PetscGetRealPath(tmp3,tmp1);
94: PetscStrncpy(rpath,tmp1,PETSC_MAX_PATH_LEN);
95: PetscStrlen(rpath,&len);
96: PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
97: }
98: return(0);
99: }
100: PetscStrchr(tmp1,'/',&tmp2);
101: if (tmp2) {
102: PetscStrlen(tmp1,&len1);
103: PetscStrlen(tmp2,&len2);
104: N = len1 - len2;
105: } else {
106: PetscStrlen(tmp1,&N);
107: }
108: }
109: PetscStrncpy(rpath,path,PETSC_MAX_PATH_LEN);
110: #else /* Just punt */
111: PetscStrcpy(rpath,path);
112: #endif
114: /* remove garbage some automounters put at the beginning of the path */
115: PetscStrncmp("/tmp_mnt/",rpath,9,&flg);
116: if (flg) {
117: PetscStrcpy(tmp3,rpath + 8);
118: PetscStrcpy(rpath,tmp3);
119: }
120: return(0);
121: }