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 #include <ctype.h> 10 #include <sys/types.h> 11 #include <sys/stat.h> 12 #if defined(PETSC_HAVE_UNISTD_H) 13 #include <unistd.h> 14 #endif 15 #if defined(PETSC_HAVE_SYS_UTSNAME_H) 16 #include <sys/utsname.h> 17 #endif 18 #include <fcntl.h> 19 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H) 20 #include <sys/systeminfo.h> 21 #endif 22 23 #if defined(PETSC_HAVE_PWD_H) 24 25 #undef __FUNCT__ 26 #define __FUNCT__ "PetscGetFullPath" 27 /*@C 28 PetscGetFullPath - Given a filename, returns the fully qualified file name. 29 30 Not Collective 31 32 Input Parameters: 33 + path - pathname to qualify 34 . fullpath - pointer to buffer to hold full pathname 35 - flen - size of fullpath 36 37 Level: developer 38 39 Concepts: full path 40 Concepts: path^full 41 42 .seealso: PetscGetRelativePath() 43 @*/ 44 PetscErrorCode PetscGetFullPath(const char path[],char fullpath[],size_t flen) 45 { 46 struct passwd *pwde; 47 PetscErrorCode ierr; 48 size_t ln; 49 PetscBool flg; 50 51 PetscFunctionBegin; 52 if (path[0] == '/') { 53 ierr = PetscStrncmp("/tmp_mnt/",path,9,&flg);CHKERRQ(ierr); 54 if (flg) {ierr = PetscStrncpy(fullpath,path + 8,flen);CHKERRQ(ierr);} 55 else {ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr);} 56 PetscFunctionReturn(0); 57 } 58 ierr = PetscGetWorkingDirectory(fullpath,flen);CHKERRQ(ierr); 59 ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); 60 ierr = PetscStrncat(fullpath,"/",flen - ln);CHKERRQ(ierr); 61 if (path[0] == '.' && path[1] == '/') { 62 ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); 63 ierr = PetscStrncat(fullpath,path+2,flen - ln - 1);CHKERRQ(ierr); 64 } else { 65 ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr); 66 ierr = PetscStrncat(fullpath,path,flen - ln - 1);CHKERRQ(ierr); 67 } 68 69 /* Remove the various "special" forms (~username/ and ~/) */ 70 if (fullpath[0] == '~') { 71 char tmppath[PETSC_MAX_PATH_LEN]; 72 if (fullpath[1] == '/') { 73 #if defined(PETSC_HAVE_GETPWUID) 74 pwde = getpwuid(geteuid()); 75 if (!pwde) PetscFunctionReturn(0); 76 ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr); 77 ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr); 78 if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);} 79 ierr = PetscStrcat(tmppath,fullpath + 2);CHKERRQ(ierr); 80 ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr); 81 #else 82 PetscFunctionReturn(0); 83 #endif 84 } else { 85 char *p,*name; 86 87 /* Find username */ 88 name = fullpath + 1; 89 p = name; 90 while (*p && *p != '/') p++; 91 *p = 0; p++; 92 pwde = getpwnam(name); 93 if (!pwde) PetscFunctionReturn(0); 94 95 ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr); 96 ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr); 97 if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);} 98 ierr = PetscStrcat(tmppath,p);CHKERRQ(ierr); 99 ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr); 100 } 101 } 102 /* Remove the automounter part of the path */ 103 ierr = PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);CHKERRQ(ierr); 104 if (flg) { 105 char tmppath[PETSC_MAX_PATH_LEN]; 106 ierr = PetscStrcpy(tmppath,fullpath + 8);CHKERRQ(ierr); 107 ierr = PetscStrcpy(fullpath,tmppath);CHKERRQ(ierr); 108 } 109 /* We could try to handle things like the removal of .. etc */ 110 PetscFunctionReturn(0); 111 } 112 #elif defined(PETSC_HAVE__FULLPATH) 113 #undef __FUNCT__ 114 #define __FUNCT__ "PetscGetFullPath" 115 PetscErrorCode PetscGetFullPath(const char path[],char fullpath[],size_t flen) 116 { 117 PetscFunctionBegin; 118 _fullpath(fullpath,path,flen); 119 PetscFunctionReturn(0); 120 } 121 #else 122 #undef __FUNCT__ 123 #define __FUNCT__ "PetscGetFullPath" 124 PetscErrorCode PetscGetFullPath(const char path[],char fullpath[],size_t flen) 125 { 126 PetscErrorCode ierr; 127 128 PetscFunctionBegin; 129 ierr = PetscStrcpy(fullpath,path);CHKERRQ(ierr); 130 PetscFunctionReturn(0); 131 } 132 #endif 133