Actual source code: ghome.c

  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) {
 33:     PetscCall(PetscStrncpy(dir, d1, maxlen));
 34:   } else if (maxlen > 0) dir[0] = 0;
 35:   PetscFunctionReturn(PETSC_SUCCESS);
 36: }

 38: /*@C
 39:   PetscFixFilename - Fixes a file name so that it is correct for both Unix and
 40:   Microsoft Windows by using the correct / or \ to separate directories.

 42:   Not Collective

 44:   Input Parameter:
 45: . filein - name of file to be fixed

 47:   Output Parameter:
 48: . fileout - the fixed name. Should long enough to hold the filename.

 50:   Level: developer

 52: .seealso: `PetscFOpen()`
 53: @*/
 54: PetscErrorCode PetscFixFilename(const char filein[], char fileout[])
 55: {
 56:   size_t i, n;

 58:   PetscFunctionBegin;
 59:   if (!filein || !fileout) PetscFunctionReturn(PETSC_SUCCESS);

 61:   PetscCall(PetscStrlen(filein, &n));
 62:   for (i = 0; i < n; i++) {
 63:     if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR;
 64:     else fileout[i] = filein[i];
 65:   }
 66:   fileout[n] = 0;
 67:   PetscFunctionReturn(PETSC_SUCCESS);
 68: }