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: #ifndef MAXPATHLEN
 37: #define MAXPATHLEN 1024
 38: #endif

 40: #if defined(PETSC_HAVE_PWD_H)

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

 45:    Not Collective

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

 52:    Level: developer

 54:    Concepts: full path
 55:    Concepts: path^full

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

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

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

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

139:   PetscStrcpy(fullpath,path);
140:   return(0);
141: }
142: #endif