Actual source code: fpath.c

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

 28: #if defined(PETSC_HAVE_PWD_H)

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

 35:    Not Collective

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

 42:    Level: developer

 44:    Concepts: full path
 45:    Concepts: path^full

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

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

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

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

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