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: #if defined(PETSC_HAVE_SYS_STAT_H)
11: #include <sys/stat.h>
12: #endif
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(PARCH_win32)
20: #include <sys/utsname.h>
21: #endif
22: #if defined(PARCH_win32)
23: #include <windows.h>
24: #include <io.h>
25: #include <direct.h>
26: #endif
27: #if defined (PARCH_win32_gnu)
28: #include <windows.h>
29: #endif
30: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
31: #include <sys/systeminfo.h>
32: #endif
33: #include "petscfix.h"
35: #if defined (PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
39: static int PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg) {
40: int m;
41:
43: if (mode == 'r') m = R_OK;
44: else if (mode == 'w') m = W_OK;
45: else if (mode == 'x') m = X_OK;
46: else SETERRQ(PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
47: #if defined(PETSC_HAVE_ACCESS)
48: if(!access(fname, m)) *flg = PETSC_TRUE;
49: #else
50: if (m == X_OK) SETERRQ1(PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
51: if(!_access(fname, m)) *flg = PETSC_TRUE;
52: #endif
53: return(0);
54: }
56: #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
60: static int PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg) {
61: uid_t uid;
62: gid_t *gid = PETSC_NULL;
63: int numGroups;
64: int rbit = S_IROTH;
65: int wbit = S_IWOTH;
66: int ebit = S_IXOTH;
67: int ierr;
70: /* Get the number of supplementary group IDs */
71: #if !defined(PETSC_MISSING_GETGROUPS)
72: numGroups = getgroups(0, gid); if (numGroups < 0) {SETERRQ(numGroups, "Unable to count supplementary group IDs");}
73: PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);
74: #else
75: numGroups = 0;
76: #endif
78: /* Get the (effective) user and group of the caller */
79: uid = geteuid();
80: gid[0] = getegid();
82: /* Get supplementary group IDs */
83: #if !defined(PETSC_MISSING_GETGROUPS)
84: getgroups(numGroups, gid+1); if (ierr < 0) {SETERRQ(ierr, "Unable to obtain supplementary group IDs");}
85: #endif
87: /* Test for accessibility */
88: if (fuid == uid) {
89: rbit = S_IRUSR;
90: wbit = S_IWUSR;
91: ebit = S_IXUSR;
92: } else {
93: int g;
95: for(g = 0; g <= numGroups; g++) {
96: if (fgid == gid[g]) {
97: rbit = S_IRGRP;
98: wbit = S_IWGRP;
99: ebit = S_IXGRP;
100: break;
101: }
102: }
103: }
104: PetscFree(gid);
106: if (mode == 'r') {
107: if (fmode & rbit) *flg = PETSC_TRUE;
108: } else if (mode == 'w') {
109: if (fmode & wbit) *flg = PETSC_TRUE;
110: } else if (mode == 'x') {
111: if (fmode & ebit) *flg = PETSC_TRUE;
112: }
113: return(0);
114: }
116: #endif /* PETSC_HAVE_ACCESS */
120: static int PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscTruth *exists) {
121: struct stat statbuf;
122: int ierr;
125: #if defined(PETSC_HAVE_STAT_NO_CONST)
126: stat((char*) fname, &statbuf);
127: #else
128: stat(fname, &statbuf);
129: #endif
130: if (ierr) {
131: *exists = PETSC_FALSE;
132: } else {
133: *exists = PETSC_TRUE;
134: *fileUid = statbuf.st_uid;
135: *fileGid = statbuf.st_gid;
136: *fileMode = statbuf.st_mode;
137: }
138: return(0);
139: }
143: int PetscTestFile(const char fname[], char mode, PetscTruth *flg)
144: {
145: uid_t fuid;
146: gid_t fgid;
147: int fmode;
148: int ierr;
149: PetscTruth exists;
152: *flg = PETSC_FALSE;
153: if (!fname) return(0);
155: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
156: if (!exists) return(0);
157: /* Except for systems that have this broken stat macros (rare), this
158: is the correct way to check for a regular file */
159: if (!S_ISREG(fmode)) return(0);
161: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
162: return(0);
163: }
167: int PetscTestDirectory(const char fname[],char mode,PetscTruth *flg)
168: {
169: uid_t fuid;
170: gid_t fgid;
171: int fmode;
172: int ierr;
173: PetscTruth exists;
176: *flg = PETSC_FALSE;
177: if (!fname) return(0);
179: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
180: if (!exists) return(0);
181: /* Except for systems that have this broken stat macros (rare), this
182: is the correct way to check for a directory */
183: if (!S_ISDIR(fmode)) return(0);
185: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
186: return(0);
187: }
191: int PetscLs(MPI_Comm comm,const char libname[],char *found,int tlen,PetscTruth *flg)
192: {
193: int ierr,len;
194: char *f,program[PETSC_MAX_PATH_LEN];
195: FILE *fp;
198: PetscStrcpy(program,"ls ");
199: PetscStrcat(program,libname);
200: PetscPOpen(comm,PETSC_NULL,program,"r",&fp);
201: f = fgets(found,tlen,fp);
202: if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
203: while (f) {
204: PetscStrlen(found,&len);
205: f = fgets(found+len,tlen-len,fp);
206: }
207: if (*flg) PetscLogInfo(0,"ls on %s gives \n%s\n",libname,found);
208: return(0);
209: }