Actual source code: grpath.c

 2:  #include petsc.h
 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
 22: #include "petscfix.h"

 26: /*@C
 27:    PetscGetRealPath - Get the path without symbolic links etc. and in absolute form.

 29:    Not Collective

 31:    Input Parameter:
 32: .  path - path to resolve

 34:    Output Parameter:
 35: .  rpath - resolved path

 37:    Level: developer

 39:    Notes: 
 40:    rpath is assumed to be of length PETSC_MAX_PATH_LEN.

 42:    Systems that use the automounter often generate absolute paths
 43:    of the form "/tmp_mnt....".  However, the automounter will fail to
 44:    mount this path if it is not already mounted, so we remove this from
 45:    the head of the line.  This may cause problems if, for some reason,
 46:    /tmp_mnt is valid and not the result of the automounter.

 48:    Concepts: real path
 49:    Concepts: path^real

 51: .seealso: PetscGetFullPath()
 52: @*/
 53: PetscErrorCode PetscGetRealPath(char path[],char rpath[])
 54: {
 56:   char           tmp3[PETSC_MAX_PATH_LEN];
 57:   PetscTruth     flg;
 58: #if !defined(PETSC_HAVE_REALPATH) && defined(PETSC_HAVE_READLINK)
 59:   char           tmp1[PETSC_MAX_PATH_LEN],tmp4[PETSC_MAX_PATH_LEN],*tmp2;
 60:   size_t         N,len,len1,len2;
 61:   int            n,m;
 62: #endif

 65: #if defined(PETSC_HAVE_REALPATH)
 66:   realpath(path,rpath);
 67: #elif defined(PETSC_HAVE_READLINK)
 68:   /* Algorithm: we move through the path, replacing links with the real paths.   */
 69:   PetscStrcpy(rpath,path);
 70:   PetscStrlen(rpath,&N);
 71:   while (N) {
 72:     PetscStrncpy(tmp1,rpath,N);
 73:     tmp1[N] = 0;
 74:     n = readlink(tmp1,tmp3,PETSC_MAX_PATH_LEN);
 75:     if (n > 0) {
 76:       tmp3[n] = 0; /* readlink does not automatically add 0 to string end */
 77:       if (tmp3[0] != '/') {
 78:         PetscStrchr(tmp1,'/',&tmp2);
 79:         PetscStrlen(tmp1,&len1);
 80:         PetscStrlen(tmp2,&len2);
 81:         m    = len1 - len2;
 82:         PetscStrncpy(tmp4,tmp1,m);
 83:         tmp4[m] = 0;
 84:         PetscStrlen(tmp4,&len);
 85:         PetscStrncat(tmp4,"/",PETSC_MAX_PATH_LEN - len);
 86:         PetscStrlen(tmp4,&len);
 87:         PetscStrncat(tmp4,tmp3,PETSC_MAX_PATH_LEN - len);
 88:         PetscGetRealPath(tmp4,rpath);
 89:         PetscStrlen(rpath,&len);
 90:         PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
 91:       } else {
 92:         PetscGetRealPath(tmp3,tmp1);
 93:         PetscStrncpy(rpath,tmp1,PETSC_MAX_PATH_LEN);
 94:         PetscStrlen(rpath,&len);
 95:         PetscStrncat(rpath,path+N,PETSC_MAX_PATH_LEN - len);
 96:       }
 97:       return(0);
 98:     }
 99:     PetscStrchr(tmp1,'/',&tmp2);
100:     if (tmp2) {
101:       PetscStrlen(tmp1,&len1);
102:       PetscStrlen(tmp2,&len2);
103:       N    = len1 - len2;
104:     } else {
105:       PetscStrlen(tmp1,&N);
106:     }
107:   }
108:   PetscStrncpy(rpath,path,PETSC_MAX_PATH_LEN);
109: #else /* Just punt */
110:   PetscStrcpy(rpath,path);
111: #endif

113:   /* remove garbage some automounters put at the beginning of the path */
114:   PetscStrncmp("/tmp_mnt/",rpath,9,&flg);
115:   if (flg) {
116:     PetscStrcpy(tmp3,rpath + 8);
117:     PetscStrcpy(rpath,tmp3);
118:   }
119:   return(0);
120: }