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) { 59 d1 = pw->pw_dir; 60 } 61 #else 62 d1 = getenv("HOME"); 63 #endif 64 if (d1) { 65 ierr = PetscStrncpy(dir,d1,maxlen);CHKERRQ(ierr); 66 } else if (maxlen > 0) { 67 dir[0] = 0; 68 } 69 PetscFunctionReturn(0); 70 } 71 72 #undef __FUNCT__ 73 #define __FUNCT__ "PetscFixFilename" 74 /*@C 75 PetscFixFilename - Fixes a file name so that it is correct for both Unix and 76 Windows by using the correct / or \ to separate directories. 77 78 Not Collective 79 80 Input Parameter: 81 . filein - name of file to be fixed 82 83 Output Parameter: 84 . fileout - the fixed name. Should long enough to hold the filename. 85 86 Level: advanced 87 88 Notes: 89 Call PetscFixFilename() just before calling fopen(). 90 @*/ 91 PetscErrorCode PetscFixFilename(const char filein[],char fileout[]) 92 { 93 PetscErrorCode ierr; 94 size_t i,n; 95 96 PetscFunctionBegin; 97 if (!filein || !fileout) PetscFunctionReturn(0); 98 99 ierr = PetscStrlen(filein,&n);CHKERRQ(ierr); 100 for (i=0; i<n; i++) { 101 if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR; 102 else fileout[i] = filein[i]; 103 } 104 fileout[n] = 0; 105 106 PetscFunctionReturn(0); 107 } 108