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: #ifndef MAXPATHLEN
 34: #define MAXPATHLEN 1024
 35: #endif


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

 41:    Not Collective

 43:    Input Parameter:
 44: .  path - path to resolve

 46:    Output Parameter:
 47: .  rpath - resolved path

 49:    Level: developer

 51:    Notes: 
 52:    rpath is assumed to be of length MAXPATHLEN.

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

 60:    Concepts: real path
 61:    Concepts: path^real

 63: .seealso: PetscGetFullPath()
 64: @*/
 65: int PetscGetRealPath(char path[],char rpath[])
 66: {
 67:   int        ierr;
 68:   char       tmp3[MAXPATHLEN];
 69:   PetscTruth flg;
 70: #if !defined(PETSC_HAVE_REALPATH) && !defined(PARCH_win32) && defined(PETSC_HAVE_READLINK)
 71:   char       tmp1[MAXPATHLEN],tmp4[MAXPATHLEN],*tmp2;
 72:   int        n,m,N,len,len1,len2;
 73: #endif

 76: #if defined(PETSC_HAVE_REALPATH)
 77:   realpath(path,rpath);
 78: #elif defined (PARCH_win32)
 79:   PetscStrcpy(rpath,path);
 80: #elif !defined(PETSC_HAVE_READLINK)
 81:   PetscStrcpy(rpath,path);
 82: #else

 84:   /* Algorithm: we move through the path, replacing links with the real paths.   */
 85:   PetscStrcpy(rpath,path);
 86:   PetscStrlen(rpath,&N);
 87:   while (N) {
 88:     PetscStrncpy(tmp1,rpath,N);
 89:     tmp1[N] = 0;
 90:     n = readlink(tmp1,tmp3,MAXPATHLEN);
 91:     if (n > 0) {
 92:       tmp3[n] = 0; /* readlink does not automatically add 0 to string end */
 93:       if (tmp3[0] != '/') {
 94:         PetscStrchr(tmp1,'/',&tmp2);
 95:         PetscStrlen(tmp1,&len1);
 96:         PetscStrlen(tmp2,&len2);
 97:         m    = len1 - len2;
 98:         PetscStrncpy(tmp4,tmp1,m);
 99:         tmp4[m] = 0;
100:         PetscStrlen(tmp4,&len);
101:         PetscStrncat(tmp4,"/",MAXPATHLEN - len);
102:         PetscStrlen(tmp4,&len);
103:         PetscStrncat(tmp4,tmp3,MAXPATHLEN - len);
104:         PetscGetRealPath(tmp4,rpath);
105:         PetscStrlen(rpath,&len);
106:         PetscStrncat(rpath,path+N,MAXPATHLEN - len);
107:       } else {
108:         PetscGetRealPath(tmp3,tmp1);
109:         PetscStrncpy(rpath,tmp1,MAXPATHLEN);
110:         PetscStrlen(rpath,&len);
111:         PetscStrncat(rpath,path+N,MAXPATHLEN - len);
112:       }
113:       return(0);
114:     }
115:     PetscStrchr(tmp1,'/',&tmp2);
116:     if (tmp2) {
117:       PetscStrlen(tmp1,&len1);
118:       PetscStrlen(tmp2,&len2);
119:       N    = len1 - len2;
120:     } else {
121:       PetscStrlen(tmp1,&N);
122:     }
123:   }
124:   PetscStrncpy(rpath,path,MAXPATHLEN);
125: #endif

127:   /* remove garbage some automounters put at the beginning of the path */
128:   PetscStrncmp("/tmp_mnt/",rpath,9,&flg);
129:   if (flg) {
130:     PetscStrcpy(tmp3,rpath + 8);
131:     PetscStrcpy(rpath,tmp3);
132:   }
133:   return(0);
134: }