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