Actual source code: fpath.c


  2: #include <petscsys.h>
  3: #if defined(PETSC_HAVE_PWD_H)
  4: #include <pwd.h>
  5: #endif

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

 10:    Not Collective

 12:    Input Parameters:
 13: +  path     - pathname to qualify
 14: .  fullpath - pointer to buffer to hold full pathname
 15: -  flen     - size of fullpath

 17:    Level: developer

 19: .seealso: PetscGetRelativePath()
 20: @*/
 21: PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
 22: {
 24:   size_t         ln;
 25:   PetscBool      flg;

 28:   if (path[0] == '/') {
 29:     PetscStrncmp("/tmp_mnt/",path,9,&flg);
 30:     if (flg) {PetscStrncpy(fullpath,path + 8,flen);}
 31:     else     {PetscStrncpy(fullpath,path,flen);}
 32:     fullpath[flen-1] = 0;
 33:     return(0);
 34:   }

 36:   PetscStrncpy(fullpath,path,flen);
 37:   fullpath[flen-1] = 0;
 38:   /* Remove the various "special" forms (~username/ and ~/) */
 39:   if (fullpath[0] == '~') {
 40:     char tmppath[PETSC_MAX_PATH_LEN],*rest;
 41:     if (fullpath[1] == '/') {
 42:       PetscGetHomeDirectory(tmppath,PETSC_MAX_PATH_LEN);
 43:       rest = fullpath + 2;
 44:     } else {
 45: #if defined(PETSC_HAVE_PWD_H)
 46:       struct passwd  *pwde;
 47:       char *p,*name;

 49:       /* Find username */
 50:       name = fullpath + 1;
 51:       p    = name;
 52:       while (*p && *p != '/') p++;
 53:       *p   = 0;
 54:       rest = p + 1;
 55:       pwde = getpwnam(name);
 56:       if (!pwde) return(0);

 58:       PetscStrcpy(tmppath,pwde->pw_dir);
 59: #else
 60:       return(0);
 61: #endif
 62:     }
 63:     PetscStrlen(tmppath,&ln);
 64:     if (tmppath[ln-1] != '/') {PetscStrcat(tmppath+ln-1,"/");}
 65:     PetscStrcat(tmppath,rest);
 66:     PetscStrncpy(fullpath,tmppath,flen);
 67:     fullpath[flen-1] = 0;
 68:   } else {
 69:     PetscGetWorkingDirectory(fullpath,flen);
 70:     PetscStrlen(fullpath,&ln);
 71:     PetscStrncpy(fullpath+ln,"/",flen - ln);
 72:     fullpath[flen-1] = 0;
 73:     PetscStrlen(fullpath,&ln);
 74:     if (path[0] == '.' && path[1] == '/') {
 75:       PetscStrlcat(fullpath,path+2,flen);
 76:     } else {
 77:       PetscStrlcat(fullpath,path,flen);
 78:     }
 79:     fullpath[flen-1] = 0;
 80:   }

 82:   /* Remove the automounter part of the path */
 83:   PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);
 84:   if (flg) {
 85:     char tmppath[PETSC_MAX_PATH_LEN];
 86:     PetscStrcpy(tmppath,fullpath + 8);
 87:     PetscStrcpy(fullpath,tmppath);
 88:   }
 89:   /* We could try to handle things like the removal of .. etc */
 90:   return(0);
 91: }