Actual source code: fpath.c

  1: /*
  2:       Code for opening and closing files.
  3: */
 4:  #include petsc.h
 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
 25: #include "petscfix.h"

 27: #if defined(PETSC_HAVE_PWD_H)

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

 34:    Not Collective

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

 41:    Level: developer

 43:    Concepts: full path
 44:    Concepts: path^full

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

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

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

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

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