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