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