Date: 2013-07-02T14:38:46+00:00

Actual source code: fpath.c
petsc-3.4.2 2013-07-02

      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/stat.h>
     11: #if defined(PETSC_HAVE_UNISTD_H)
     12: #include <unistd.h>
     13: #endif
     14: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
     15: #include <sys/utsname.h>
     16: #endif
     17: #include <fcntl.h>
     18: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
     19: #include <sys/systeminfo.h>
     20: #endif

     22: #if defined(PETSC_HAVE_PWD_H)

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

     29:    Not Collective

     31:    Input Parameters:
     32: +  path     - pathname to qualify
     33: .  fullpath - pointer to buffer to hold full pathname
     34: -  flen     - size of fullpath

     36:    Level: developer

     38:    Concepts: full path
     39:    Concepts: path^full

     41: .seealso: PetscGetRelativePath()
     42: @*/
     43: PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
     44: {
     45:   struct passwd  *pwde;
     47:   size_t         ln;
     48:   PetscBool      flg;

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

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

     86:       /* Find username */
     87:       name = fullpath + 1;
     88:       p    = name;
     89:       while (*p && *p != '/') p++;
     90:       *p   = 0; p++;
     91:       pwde = getpwnam(name);
     92:       if (!pwde) return(0);

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

    128:   PetscStrcpy(fullpath,path);
    129:   return(0);
    130: }
    131: #endif