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: {
 23:   size_t         ln;
 24:   PetscBool      flg;

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

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

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

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

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