xref: /petsc/src/sys/fileio/ghome.c (revision 834855d6effb0d027771461c8e947ee1ce5a1e17)
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 @*/
PetscGetHomeDirectory(char dir[],size_t maxlen)26 PetscErrorCode PetscGetHomeDirectory(char dir[], size_t maxlen)
27 {
28   const char *d1;
29 
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 }
36 
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.
40 
41   Not Collective
42 
43   Input Parameter:
44 . filein - name of file to be fixed
45 
46   Output Parameter:
47 . fileout - the fixed name. Should long enough to hold the filename.
48 
49   Level: developer
50 
51 .seealso: `PetscFOpen()`
52 @*/
PetscFixFilename(const char filein[],char fileout[])53 PetscErrorCode PetscFixFilename(const char filein[], char fileout[])
54 {
55   size_t i, n;
56 
57   PetscFunctionBegin;
58   if (!filein || !fileout) PetscFunctionReturn(PETSC_SUCCESS);
59 
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 }
68