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