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