xref: /petsc/src/sys/fileio/ghome.c (revision 98d129c30f3ee9fdddc40fdbc5a989b7be64f888)
1 /*
2       Code for manipulating files.
3 */
4 #include <petscsys.h>
5 
6 /*@C
7   PetscGetHomeDirectory - Returns the name of the user's home directory
8 
9   Not Collective
10 
11   Input Parameter:
12 . maxlen - maximum length allowed
13 
14   Output Parameter:
15 . dir - contains the home directory. Must be long enough to hold the name.
16 
17   Level: developer
18 
19   Notes:
20   If PETSc cannot determine the home directory it makes `dir` an empty string
21 
22   On Microsoft Windows machines the environmental variable `HOME` specifies the home directory.
23 
24 .seealso: `PetscGetTmp()`, `PetscSharedTmp()`, `PetscGetWorkingDirectory()`
25 @*/
26 PetscErrorCode PetscGetHomeDirectory(char dir[], size_t maxlen)
27 {
28   const char *d1;
29 
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 }
37 
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.
41 
42   Not Collective
43 
44   Input Parameter:
45 . filein - name of file to be fixed
46 
47   Output Parameter:
48 . fileout - the fixed name. Should long enough to hold the filename.
49 
50   Level: developer
51 
52 .seealso: `PetscFOpen()`
53 @*/
54 PetscErrorCode PetscFixFilename(const char filein[], char fileout[])
55 {
56   size_t i, n;
57 
58   PetscFunctionBegin;
59   if (!filein || !fileout) PetscFunctionReturn(PETSC_SUCCESS);
60 
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 }
69