xref: /petsc/src/sys/fileio/ghome.c (revision ce94432eddcd14845bc7e8083b7f8ea723b9bf7d)
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   char           *d1 = 0;
47 #if defined(PETSC_HAVE_GETPWUID)
48   struct passwd *pw = 0;
49 #endif
50 
51   PetscFunctionBegin;
52 #if defined(PETSC_HAVE_GETPWUID)
53   pw = getpwuid(getuid());
54   if (pw) d1 = pw->pw_dir;
55 #else
56   d1 = getenv("HOME");
57 #endif
58   if (d1) {
59     ierr = PetscStrncpy(dir,d1,maxlen);CHKERRQ(ierr);
60   } else if (maxlen > 0) dir[0] = 0;
61   PetscFunctionReturn(0);
62 }
63 
64 #undef __FUNCT__
65 #define __FUNCT__ "PetscFixFilename"
66 /*@C
67     PetscFixFilename - Fixes a file name so that it is correct for both Unix and
68     Windows by using the correct / or \ to separate directories.
69 
70    Not Collective
71 
72    Input Parameter:
73 .  filein - name of file to be fixed
74 
75    Output Parameter:
76 .  fileout - the fixed name. Should long enough to hold the filename.
77 
78    Level: advanced
79 
80    Notes:
81    Call PetscFixFilename() just before calling fopen().
82 @*/
83 PetscErrorCode  PetscFixFilename(const char filein[],char fileout[])
84 {
85   PetscErrorCode ierr;
86   size_t         i,n;
87 
88   PetscFunctionBegin;
89   if (!filein || !fileout) PetscFunctionReturn(0);
90 
91   ierr = PetscStrlen(filein,&n);CHKERRQ(ierr);
92   for (i=0; i<n; i++) {
93     if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR;
94     else fileout[i] = filein[i];
95   }
96   fileout[n] = 0;
97   PetscFunctionReturn(0);
98 }
99