Actual source code: ghome.c
petsc-3.3-p7 2013-05-11
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_STDLIB_H)
16: #include <stdlib.h>
17: #endif
18: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
19: #include <sys/utsname.h>
20: #endif
21: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
22: #include <sys/systeminfo.h>
23: #endif
27: /*@C
28: PetscGetHomeDirectory - Returns home directory name.
30: Not Collective
32: Input Parameter:
33: . maxlen - maximum lengh allowed
35: Output Parameter:
36: . dir - contains the home directory. Must be long enough to hold the name.
38: Level: developer
40: Note:
41: If PETSc cannot determine the home directory it makes dir a null string
43: On Windows machines the enviornmental variable HOME specifies the home directory.
45: Concepts: home directory
46: @*/
47: PetscErrorCode PetscGetHomeDirectory(char dir[],size_t maxlen)
48: {
50: char *d1 = 0;
51: #if defined(PETSC_HAVE_GETPWUID)
52: struct passwd *pw = 0;
53: #endif
56: #if defined(PETSC_HAVE_GETPWUID)
57: pw = getpwuid(getuid());
58: if (pw) {
59: d1 = pw->pw_dir;
60: }
61: #else
62: d1 = getenv("HOME");
63: #endif
64: if (d1) {
65: PetscStrncpy(dir,d1,maxlen);
66: } else if (maxlen > 0) {
67: dir[0] = 0;
68: }
69: return(0);
70: }
74: /*@C
75: PetscFixFilename - Fixes a file name so that it is correct for both Unix and
76: Windows by using the correct / or \ to separate directories.
78: Not Collective
80: Input Parameter:
81: . filein - name of file to be fixed
83: Output Parameter:
84: . fileout - the fixed name. Should long enough to hold the filename.
86: Level: advanced
88: Notes:
89: Call PetscFixFilename() just before calling fopen().
90: @*/
91: PetscErrorCode PetscFixFilename(const char filein[],char fileout[])
92: {
94: size_t i,n;
97: if (!filein || !fileout) return(0);
99: PetscStrlen(filein,&n);
100: for (i=0; i<n; i++) {
101: if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR;
102: else fileout[i] = filein[i];
103: }
104: fileout[n] = 0;
106: return(0);
107: }