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