xref: /petsc/src/sys/fileio/ghome.c (revision 58d68138c660dfb4e9f5b03334792cd4f2ffd7cc)
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 lengh 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    Note:
21    If PETSc cannot determine the home directory it makes dir a null string
22 
23    On Windows machines the enviornmental variable HOME specifies the home directory.
24 
25 @*/
26 PetscErrorCode PetscGetHomeDirectory(char dir[], size_t maxlen) {
27   const char *d1;
28 
29   PetscFunctionBegin;
30   d1 = getenv("HOME");
31   if (d1) {
32     PetscCall(PetscStrncpy(dir, d1, maxlen));
33   } else if (maxlen > 0) dir[0] = 0;
34   PetscFunctionReturn(0);
35 }
36 
37 /*@C
38     PetscFixFilename - Fixes a file name so that it is correct for both Unix and
39     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: advanced
50 
51    Notes:
52    Call PetscFixFilename() just before calling fopen().
53 @*/
54 PetscErrorCode PetscFixFilename(const char filein[], char fileout[]) {
55   size_t i, n;
56 
57   PetscFunctionBegin;
58   if (!filein || !fileout) PetscFunctionReturn(0);
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(0);
67 }
68