Actual source code: ftest.c
1: /*$Id: ftest.c,v 1.39 2001/04/04 21:18:39 bsmith Exp $*/
3: #include "petsc.h"
4: #include "petscsys.h"
5: #if defined(PETSC_HAVE_PWD_H)
6: #include <pwd.h>
7: #endif
8: #include <ctype.h>
9: #include <sys/types.h>
10: #include <sys/stat.h>
11: #if defined(PETSC_HAVE_UNISTD_H)
12: #include <unistd.h>
13: #endif
14: #if defined(PETSC_HAVE_STDLIB_H)
15: #include <stdlib.h>
16: #endif
17: #if !defined(PARCH_win32)
18: #include <sys/utsname.h>
19: #endif
20: #if defined(PARCH_win32)
21: #include <windows.h>
22: #include <io.h>
23: #include <direct.h>
24: #endif
25: #if defined (PARCH_win32_gnu)
26: #include <windows.h>
27: #endif
28: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
29: #include <sys/systeminfo.h>
30: #endif
31: #include "petscfix.h"
33: #if defined (PETSC_HAVE_U_ACCESS) || defined(PETSC_HAVE_ACCESS)
34: #if !defined(R_OK)
35: #define R_OK 04
36: #endif
37: #if !defined(W_OK)
38: #define W_OK 02
39: #endif
40: #if !defined(X_OK)
41: #define X_OK 01
42: #endif
44: /*@C
45: PetscTestFile - Test for a file existing with a specified mode.
47: Input Parameters:
48: + fname - name of file
49: - mode - mode. One of r, w, or x
51: Output Parameter:
52: . flg - PETSC_TRUE if file exists with given mode, PETSC_FALSE otherwise.
54: Level: intermediate
56: @*/
57: int PetscTestFile(const char fname[],char mode,PetscTruth *flg)
58: {
59: int m;
60:
62: *flg = PETSC_FALSE;
63: if (!fname) return(0);
64:
65: if (mode == 'r') m = R_OK;
66: else if (mode == 'w') m = W_OK;
67: else if (mode == 'x') m = X_OK;
68: else SETERRQ(1,"Mode must be one of r, w, or x");
69: #if defined(PETSC_HAVE_U_ACCESS)
70: if (m == X_OK) SETERRQ1(PETSC_ERR_SUP,"Unable to check execute permission for file %s",fname);
71: if(!_access(fname,m)) *flg = PETSC_TRUE;
72: #else
73: if(!access(fname,m)) *flg = PETSC_TRUE;
74: #endif
75: return(0);
76: }
77: #else
78: int PetscTestFile(const char fname[],char mode,PetscTruth *flg)
79: {
80: struct stat statbuf;
81: int err,stmode,rbit,wbit,ebit;
82: uid_t uid;
83: gid_t gid;
86: *flg = PETSC_FALSE;
87: if (!fname) return(0);
89: /* Get the (effective) user and group of the caller */
90: uid = geteuid();
91: gid = getegid();
93: #if defined(PETSC_HAVE_STAT_NO_CONST)
94: err = stat((char*)fname,&statbuf);
95: #else
96: err = stat(fname,&statbuf);
97: #endif
98: if (err != 0) return(0);
100: /* At least the file exists ... */
101: stmode = statbuf.st_mode;
102: /*
103: Except for systems that have this broken stat macros (rare), this
104: is the correct way to check for a (not) regular file */
105: if (!S_ISREG(stmode)) return(0);
107: /* Test for accessible. */
108: if (statbuf.st_uid == uid) {
109: rbit = S_IRUSR;
110: wbit = S_IWUSR;
111: ebit = S_IXUSR;
112: } else if (statbuf.st_gid == gid) {
113: rbit = S_IRGRP;
114: wbit = S_IWGRP;
115: ebit = S_IXGRP;
116: } else {
117: rbit = S_IROTH;
118: wbit = S_IWOTH;
119: ebit = S_IXOTH;
120: }
121: if (mode == 'r') {
122: if ((stmode & rbit)) *flg = PETSC_TRUE;
123: } else if (mode == 'w') {
124: if ((stmode & wbit)) *flg = PETSC_TRUE;
125: } else if (mode == 'x') {
126: if ((stmode & ebit)) *flg = PETSC_TRUE;
127: }
128: return(0);
129: }
131: #endif