xref: /petsc/src/sys/fileio/ghome.c (revision feff33ee0b5b037fa8f9f294dede656a2f85cc47)
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    Concepts: home directory
26 @*/
27 PetscErrorCode  PetscGetHomeDirectory(char dir[],size_t maxlen)
28 {
29   PetscErrorCode ierr;
30   const char     *d1;
31 
32   PetscFunctionBegin;
33   d1 = getenv("HOME");
34   if (d1) {
35     ierr = PetscStrncpy(dir,d1,maxlen);CHKERRQ(ierr);
36   } else if (maxlen > 0) dir[0] = 0;
37   PetscFunctionReturn(0);
38 }
39 
40 /*@C
41     PetscFixFilename - Fixes a file name so that it is correct for both Unix and
42     Windows by using the correct / or \ to separate directories.
43 
44    Not Collective
45 
46    Input Parameter:
47 .  filein - name of file to be fixed
48 
49    Output Parameter:
50 .  fileout - the fixed name. Should long enough to hold the filename.
51 
52    Level: advanced
53 
54    Notes:
55    Call PetscFixFilename() just before calling fopen().
56 @*/
57 PetscErrorCode  PetscFixFilename(const char filein[],char fileout[])
58 {
59   PetscErrorCode ierr;
60   size_t         i,n;
61 
62   PetscFunctionBegin;
63   if (!filein || !fileout) PetscFunctionReturn(0);
64 
65   ierr = PetscStrlen(filein,&n);CHKERRQ(ierr);
66   for (i=0; i<n; i++) {
67     if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR;
68     else fileout[i] = filein[i];
69   }
70   fileout[n] = 0;
71   PetscFunctionReturn(0);
72 }
73