Actual source code: fpath.c

  1: /*$Id: fpath.c,v 1.39 2001/03/23 23:20:30 balay Exp $*/
  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(PARCH_win32)
 20: #include <sys/utsname.h>
 21: #endif
 22: #if defined(PARCH_win32)
 23: #include <windows.h>
 24: #include <io.h>
 25: #include <direct.h>
 26: #endif
 27: #if defined (PARCH_win32_gnu)
 28: #include <windows.h>
 29: #endif
 30: #include <fcntl.h>
 31: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 32: #include <sys/systeminfo.h>
 33: #endif
 34: #include "petscfix.h"

 36: #if defined(PETSC_HAVE_PWD_H)

 38: #undef __FUNCT__  
 40: /*@C
 41:    PetscGetFullPath - Given a filename, returns the fully qualified file name.

 43:    Not Collective

 45:    Input Parameters:
 46: +  path     - pathname to qualify
 47: .  fullpath - pointer to buffer to hold full pathname
 48: -  flen     - size of fullpath

 50:    Level: developer

 52:    Concepts: full path
 53:    Concepts: path^full

 55: .seealso: PetscGetRelativePath()
 56: @*/
 57: int PetscGetFullPath(const char path[],char fullpath[],int flen)
 58: {
 59:   struct passwd *pwde;
 60:   int           ierr,ln;
 61:   PetscTruth    flg;

 64:   if (path[0] == '/') {
 65:     PetscStrncmp("/tmp_mnt/",path,9,&flg);
 66:     if (flg) {PetscStrncpy(fullpath,path + 8,flen);}
 67:     else      {PetscStrncpy(fullpath,path,flen);}
 68:     return(0);
 69:   }
 70:   PetscGetWorkingDirectory(fullpath,flen);
 71:   PetscStrlen(fullpath,&ln);
 72:   PetscStrncat(fullpath,"/",flen - ln);
 73:   if (path[0] == '.' && path[1] == '/') {
 74:     PetscStrlen(fullpath,&ln);
 75:     PetscStrncat(fullpath,path+2,flen - ln - 1);
 76:   } else {
 77:     PetscStrlen(fullpath,&ln);
 78:     PetscStrncat(fullpath,path,flen - ln - 1);
 79:   }

 81:   /* Remove the various "special" forms (~username/ and ~/) */
 82:   if (fullpath[0] == '~') {
 83:     char tmppath[PETSC_MAX_PATH_LEN];
 84:     if (fullpath[1] == '/') {
 85: #if !defined(PETSC_MISSING_GETPWUID)
 86:         pwde = getpwuid(geteuid());
 87:         if (!pwde) return(0);
 88:         PetscStrcpy(tmppath,pwde->pw_dir);
 89:         PetscStrlen(tmppath,&ln);
 90:         if (tmppath[ln-1] != '/') {PetscStrcat(tmppath+ln-1,"/");}
 91:         PetscStrcat(tmppath,fullpath + 2);
 92:         PetscStrncpy(fullpath,tmppath,flen);
 93: #else
 94:         return(0);
 95: #endif
 96:     } else {
 97:         char *p,*name;

 99:         /* Find username */
100:         name = fullpath + 1;
101:         p    = name;
102:         while (*p && isalnum((int)(*p))) p++;
103:         *p = 0; p++;
104:         pwde = getpwnam(name);
105:         if (!pwde) return(0);
106: 
107:         PetscStrcpy(tmppath,pwde->pw_dir);
108:         PetscStrlen(tmppath,&ln);
109:         if (tmppath[ln-1] != '/') {PetscStrcat(tmppath+ln-1,"/");}
110:         PetscStrcat(tmppath,p);
111:         PetscStrncpy(fullpath,tmppath,flen);
112:     }
113:   }
114:   /* Remove the automounter part of the path */
115:   PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);
116:   if (flg) {
117:     char tmppath[PETSC_MAX_PATH_LEN];
118:     PetscStrcpy(tmppath,fullpath + 8);
119:     PetscStrcpy(fullpath,tmppath);
120:   }
121:   /* We could try to handle things like the removal of .. etc */
122:   return(0);
123: }
124: #elif defined (PARCH_win32)
125: #undef __FUNCT__  
127: int PetscGetFullPath(const char path[],char fullpath[],int flen)
128: {
130:   _fullpath(fullpath,path,flen);
131:   return(0);
132: }
133: #else
134: #undef __FUNCT__  
136: int PetscGetFullPath(const char path[],char fullpath[],int flen)
137: {

141:   PetscStrcpy(fullpath,path);
142:   return(0);
143: }
144: #endif