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