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