Actual source code: fpath.c

petsc-3.3-p7 2013-05-11
  2: /*
  3:       Code for opening and closing files.
  4: */
  5: #include <petscsys.h>
  6: #if defined(PETSC_HAVE_PWD_H)
  7: #include <pwd.h>
  8: #endif
  9: #include <ctype.h>
 10: #include <sys/types.h>
 11: #include <sys/stat.h>
 12: #if defined(PETSC_HAVE_UNISTD_H)
 13: #include <unistd.h>
 14: #endif
 15: #if defined(PETSC_HAVE_STDLIB_H)
 16: #include <stdlib.h>
 17: #endif
 18: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 19: #include <sys/utsname.h>
 20: #endif
 21: #include <fcntl.h>
 22: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 23: #include <sys/systeminfo.h>
 24: #endif

 26: #if defined(PETSC_HAVE_PWD_H)

 30: /*@C
 31:    PetscGetFullPath - Given a filename, returns the fully qualified file name.

 33:    Not Collective

 35:    Input Parameters:
 36: +  path     - pathname to qualify
 37: .  fullpath - pointer to buffer to hold full pathname
 38: -  flen     - size of fullpath

 40:    Level: developer

 42:    Concepts: full path
 43:    Concepts: path^full

 45: .seealso: PetscGetRelativePath()
 46: @*/
 47: PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
 48: {
 49:   struct passwd *pwde;
 51:   size_t        ln;
 52:   PetscBool     flg;

 55:   if (path[0] == '/') {
 56:     PetscStrncmp("/tmp_mnt/",path,9,&flg);
 57:     if (flg) {PetscStrncpy(fullpath,path + 8,flen);}
 58:     else      {PetscStrncpy(fullpath,path,flen);}
 59:     return(0);
 60:   }
 61:   PetscGetWorkingDirectory(fullpath,flen);
 62:   PetscStrlen(fullpath,&ln);
 63:   PetscStrncat(fullpath,"/",flen - ln);
 64:   if (path[0] == '.' && path[1] == '/') {
 65:     PetscStrlen(fullpath,&ln);
 66:     PetscStrncat(fullpath,path+2,flen - ln - 1);
 67:   } else {
 68:     PetscStrlen(fullpath,&ln);
 69:     PetscStrncat(fullpath,path,flen - ln - 1);
 70:   }

 72:   /* Remove the various "special" forms (~username/ and ~/) */
 73:   if (fullpath[0] == '~') {
 74:     char tmppath[PETSC_MAX_PATH_LEN];
 75:     if (fullpath[1] == '/') {
 76: #if defined(PETSC_HAVE_GETPWUID)
 77:         pwde = getpwuid(geteuid());
 78:         if (!pwde) return(0);
 79:         PetscStrcpy(tmppath,pwde->pw_dir);
 80:         PetscStrlen(tmppath,&ln);
 81:         if (tmppath[ln-1] != '/') {PetscStrcat(tmppath+ln-1,"/");}
 82:         PetscStrcat(tmppath,fullpath + 2);
 83:         PetscStrncpy(fullpath,tmppath,flen);
 84: #else
 85:         return(0);
 86: #endif
 87:     } else {
 88:         char *p,*name;

 90:         /* Find username */
 91:         name = fullpath + 1;
 92:         p    = name;
 93:         while (*p && *p != '/') p++;
 94:         *p = 0; p++;
 95:         pwde = getpwnam(name);
 96:         if (!pwde) return(0);
 97: 
 98:         PetscStrcpy(tmppath,pwde->pw_dir);
 99:         PetscStrlen(tmppath,&ln);
100:         if (tmppath[ln-1] != '/') {PetscStrcat(tmppath+ln-1,"/");}
101:         PetscStrcat(tmppath,p);
102:         PetscStrncpy(fullpath,tmppath,flen);
103:     }
104:   }
105:   /* Remove the automounter part of the path */
106:   PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);
107:   if (flg) {
108:     char tmppath[PETSC_MAX_PATH_LEN];
109:     PetscStrcpy(tmppath,fullpath + 8);
110:     PetscStrcpy(fullpath,tmppath);
111:   }
112:   /* We could try to handle things like the removal of .. etc */
113:   return(0);
114: }
115: #elif defined(PETSC_HAVE__FULLPATH)
118: PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
119: {
121:   _fullpath(fullpath,path,flen);
122:   return(0);
123: }
124: #else
127: PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
128: {

132:   PetscStrcpy(fullpath,path);
133:   return(0);
134: }
135: #endif