Actual source code: grpath.c
1: /*$Id: grpath.c,v 1.40 2001/03/23 23:20:30 balay Exp $*/
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(PARCH_win32)
18: #include <sys/utsname.h>
19: #endif
20: #if defined(PARCH_win32)
21: #include <windows.h>
22: #include <io.h>
23: #include <direct.h>
24: #endif
25: #if defined (PARCH_win32_gnu)
26: #include <windows.h>
27: #endif
28: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
29: #include <sys/systeminfo.h>
30: #endif
31: #include "petscfix.h"
33: #undef __FUNCT__
35: /*@C
36: PetscGetRealPath - Get the path without symbolic links etc. and in absolute form.
38: Not Collective
40: Input Parameter:
41: . path - path to resolve
43: Output Parameter:
44: . rpath - resolved path
46: Level: developer
48: Notes:
49: rpath is assumed to be of length PETSC_MAX_PATH_LEN.
51: Systems that use the automounter often generate absolute paths
52: of the form "/tmp_mnt....". However, the automounter will fail to
53: mount this path if it is not already mounted, so we remove this from
54: the head of the line. This may cause problems if, for some reason,
55: /tmp_mnt is valid and not the result of the automounter.
57: Concepts: real path
58: Concepts: path^real
60: .seealso: PetscGetFullPath()
61: @*/
62: int PetscGetRealPath(char path[],char rpath[])
63: {
64: int ierr;
65: char tmp3[PETSC_MAX_PATH_LEN];
66: PetscTruth flg;
67: #if !defined(PETSC_HAVE_REALPATH) && !defined(PARCH_win32) && defined(PETSC_HAVE_READLINK)
68: char tmp1[PETSC_MAX_PATH_LEN],tmp4[PETSC_MAX_PATH_LEN],*tmp2;
69: int n,m,N,len,len1,len2;
70: #endif
73: #if defined(PETSC_HAVE_REALPATH)
74: realpath(path,rpath);
75: #elif defined (PARCH_win32)
76: PetscStrcpy(rpath,path);
77: #elif !defined(PETSC_HAVE_READLINK)
78: PetscStrcpy(rpath,path);
79: #else
81: /* Algorithm: we move through the path, replacing links with the real paths. */
82: PetscStrcpy(rpath,path);
83: PetscStrlen(rpath,&N);
84: while (N) {
85: PetscStrncpy(tmp1,rpath,N);
86: tmp1[N] = 0;
87: n = readlink(tmp1,tmp3,PETSC_MAX_PATH_LEN);
88: if (n > 0) {
89: tmp3[n] = 0; /* readlink does not automatically add 0 to string end */
90: if (tmp3[0] != '/') {
91: PetscStrchr(tmp1,'/',&tmp2);
92: PetscStrlen(tmp1,&len1);
93: PetscStrlen(tmp2,&len2);
94: m = len1 - len2;
95: PetscStrncpy(tmp4,tmp1,m);
96: tmp4[m] = 0;
97: PetscStrlen(tmp4,&len);
98: PetscStrncat(tmp4,"/",PETSC_MAX_PATH_LEN - len);
99: PetscStrlen(tmp4,&len);
100: PetscStrncat(tmp4,tmp3,PETSC_MAX_PATH_LEN - len);
101: PetscGetRealPath(tmp4,rpath);
102: PetscStrlen(rpath,&len);
103: PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
104: } else {
105: PetscGetRealPath(tmp3,tmp1);
106: PetscStrncpy(rpath,tmp1,PETSC_MAX_PATH_LEN);
107: PetscStrlen(rpath,&len);
108: PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
109: }
110: return(0);
111: }
112: PetscStrchr(tmp1,'/',&tmp2);
113: if (tmp2) {
114: PetscStrlen(tmp1,&len1);
115: PetscStrlen(tmp2,&len2);
116: N = len1 - len2;
117: } else {
118: PetscStrlen(tmp1,&N);
119: }
120: }
121: PetscStrncpy(rpath,path,PETSC_MAX_PATH_LEN);
122: #endif
124: /* remove garbage some automounters put at the beginning of the path */
125: PetscStrncmp("/tmp_mnt/",rpath,9,&flg);
126: if (flg) {
127: PetscStrcpy(tmp3,rpath + 8);
128: PetscStrcpy(rpath,tmp3);
129: }
130: return(0);
131: }