1 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 10 #undef __FUNCT__ 11 #define __FUNCT__ "PetscGetFullPath" 12 /*@C 13 PetscGetFullPath - Given a filename, returns the fully qualified file name. 14 15 Not Collective 16 17 Input Parameters: 18 + path - pathname to qualify 19 . fullpath - pointer to buffer to hold full pathname 20 - flen - size of fullpath 21 22 Level: developer 23 24 Concepts: full path 25 Concepts: path^full 26 27 .seealso: PetscGetRelativePath() 28 @*/ 29 PetscErrorCode PetscGetFullPath(const char path[],char fullpath[],size_t flen) 30 { 31 PetscErrorCode ierr; 32 size_t ln; 33 PetscBool flg; 34 35 PetscFunctionBegin; 36 if (path[0] == '/') { 37 ierr = PetscStrncmp("/tmp_mnt/",path,9,&flg);CHKERRQ(ierr); 38 if (flg) {ierr = PetscStrncpy(fullpath,path + 8,flen);CHKERRQ(ierr);} 39 else {ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr);} 40 fullpath[flen-1] = 0; 41 PetscFunctionReturn(0); 42 } 43 44 ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr); 45 fullpath[flen-1] = 0; 46 /* Remove the various "special" forms (~username/ and ~/) */ 47 if (fullpath[0] == '~') { 48 char tmppath[PETSC_MAX_PATH_LEN],*rest; 49 if (fullpath[1] == '/') { 50 ierr = PetscGetHomeDirectory(tmppath,PETSC_MAX_PATH_LEN);CHKERRQ(ierr); 51 rest = fullpath + 2; 52 } else { 53 #if defined(PETSC_HAVE_PWD_H) 54 struct passwd *pwde; 55 char *p,*name; 56 57 /* Find username */ 58 name = fullpath + 1; 59 p = name; 60 while (*p && *p != '/') p++; 61 *p = 0; 62 rest = p + 1; 63 pwde = getpwnam(name); 64 if (!pwde) PetscFunctionReturn(0); 65 66 ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr); 67 #else 68 PetscFunctionReturn(0); 69 #endif 70 } 71 ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr); 72 if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);} 73 ierr = PetscStrcat(tmppath,rest);CHKERRQ(ierr); 74 ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr); 75 fullpath[flen-1] = 0; 76 } else { 77 ierr = PetscGetWorkingDirectory(fullpath,flen);CHKERRQ(ierr); 78 ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); 79 ierr = PetscStrncpy(fullpath+ln,"/",flen - ln);CHKERRQ(ierr); 80 fullpath[flen-1] = 0; 81 ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); 82 if (path[0] == '.' && path[1] == '/') { 83 ierr = PetscStrncat(fullpath,path+2,flen - ln - 1);CHKERRQ(ierr); 84 } else { 85 ierr = PetscStrncat(fullpath,path,flen - ln - 1);CHKERRQ(ierr); 86 } 87 fullpath[flen-1] = 0; 88 } 89 90 /* Remove the automounter part of the path */ 91 ierr = PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);CHKERRQ(ierr); 92 if (flg) { 93 char tmppath[PETSC_MAX_PATH_LEN]; 94 ierr = PetscStrcpy(tmppath,fullpath + 8);CHKERRQ(ierr); 95 ierr = PetscStrcpy(fullpath,tmppath);CHKERRQ(ierr); 96 } 97 /* We could try to handle things like the removal of .. etc */ 98 PetscFunctionReturn(0); 99 } 100