1: /* 2: Code for manipulating files. 3: */ 4: #include <petscsys.h> 6: /*@C 7: PetscGetHomeDirectory - Returns the name of the user's home directory 9: Not Collective 11: Input Parameter: 12: . maxlen - maximum length allowed 14: Output Parameter: 15: . dir - contains the home directory. Must be long enough to hold the name. 17: Level: developer 19: Notes: 20: If PETSc cannot determine the home directory it makes `dir` an empty string 22: On Microsoft Windows machines the environmental variable `HOME` specifies the home directory. 24: .seealso: `PetscGetTmp()`, `PetscSharedTmp()`, `PetscGetWorkingDirectory()` 25: @*/ 26: PetscErrorCode PetscGetHomeDirectory(char dir[], size_t maxlen) 27: { 28: const char *d1; 30: PetscFunctionBegin; 31: d1 = getenv("HOME"); 32: if (d1) PetscCall(PetscStrncpy(dir, d1, maxlen)); 33: else if (maxlen > 0) dir[0] = 0; 34: PetscFunctionReturn(PETSC_SUCCESS); 35: } 37: /*@C 38: PetscFixFilename - Fixes a file name so that it is correct for both Unix and 39: Microsoft Windows by using the correct / or \ to separate directories. 41: Not Collective 43: Input Parameter: 44: . filein - name of file to be fixed 46: Output Parameter: 47: . fileout - the fixed name. Should long enough to hold the filename. 49: Level: developer 51: .seealso: `PetscFOpen()` 52: @*/ 53: PetscErrorCode PetscFixFilename(const char filein[], char fileout[]) 54: { 55: size_t i, n; 57: PetscFunctionBegin; 58: if (!filein || !fileout) PetscFunctionReturn(PETSC_SUCCESS); 60: PetscCall(PetscStrlen(filein, &n)); 61: for (i = 0; i < n; i++) { 62: if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR; 63: else fileout[i] = filein[i]; 64: } 65: fileout[n] = 0; 66: PetscFunctionReturn(PETSC_SUCCESS); 67: }