1 2 /* 3 Code for manipulating 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_STDLIB_H) 16 #include <stdlib.h> 17 #endif 18 #if defined(PETSC_HAVE_SYS_UTSNAME_H) 19 #include <sys/utsname.h> 20 #endif 21 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H) 22 #include <sys/systeminfo.h> 23 #endif 24 25 #undef __FUNCT__ 26 #define __FUNCT__ "PetscGetHomeDirectory" 27 /*@C 28 PetscGetHomeDirectory - Returns home directory name. 29 30 Not Collective 31 32 Input Parameter: 33 . maxlen - maximum lengh allowed 34 35 Output Parameter: 36 . dir - contains the home directory. Must be long enough to hold the name. 37 38 Level: developer 39 40 Note: 41 If PETSc cannot determine the home directory it makes dir a null string 42 43 On Windows machines the enviornmental variable HOME specifies the home directory. 44 45 Concepts: home directory 46 @*/ 47 PetscErrorCode PetscGetHomeDirectory(char dir[],size_t maxlen) 48 { 49 PetscErrorCode ierr; 50 char *d1 = 0; 51 #if defined(PETSC_HAVE_GETPWUID) 52 struct passwd *pw = 0; 53 #endif 54 55 PetscFunctionBegin; 56 #if defined(PETSC_HAVE_GETPWUID) 57 pw = getpwuid(getuid()); 58 if (pw) d1 = pw->pw_dir; 59 #else 60 d1 = getenv("HOME"); 61 #endif 62 if (d1) { 63 ierr = PetscStrncpy(dir,d1,maxlen);CHKERRQ(ierr); 64 } else if (maxlen > 0) dir[0] = 0; 65 PetscFunctionReturn(0); 66 } 67 68 #undef __FUNCT__ 69 #define __FUNCT__ "PetscFixFilename" 70 /*@C 71 PetscFixFilename - Fixes a file name so that it is correct for both Unix and 72 Windows by using the correct / or \ to separate directories. 73 74 Not Collective 75 76 Input Parameter: 77 . filein - name of file to be fixed 78 79 Output Parameter: 80 . fileout - the fixed name. Should long enough to hold the filename. 81 82 Level: advanced 83 84 Notes: 85 Call PetscFixFilename() just before calling fopen(). 86 @*/ 87 PetscErrorCode PetscFixFilename(const char filein[],char fileout[]) 88 { 89 PetscErrorCode ierr; 90 size_t i,n; 91 92 PetscFunctionBegin; 93 if (!filein || !fileout) PetscFunctionReturn(0); 94 95 ierr = PetscStrlen(filein,&n);CHKERRQ(ierr); 96 for (i=0; i<n; i++) { 97 if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR; 98 else fileout[i] = filein[i]; 99 } 100 fileout[n] = 0; 101 PetscFunctionReturn(0); 102 } 103