Actual source code: ftest.c
2: #include petsc.h
3: #include petscsys.h
4: #if defined(PETSC_HAVE_PWD_H)
5: #include <pwd.h>
6: #endif
7: #include <ctype.h>
8: #include <sys/types.h>
9: #include <sys/stat.h>
10: #if defined(PETSC_HAVE_UNISTD_H)
11: #include <unistd.h>
12: #endif
13: #if defined(PETSC_HAVE_STDLIB_H)
14: #include <stdlib.h>
15: #endif
16: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
17: #include <sys/utsname.h>
18: #endif
19: #if defined(PETSC_HAVE_IO_H)
20: #include <io.h>
21: #endif
22: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
23: #include <sys/systeminfo.h>
24: #endif
25: #include "petscfix.h"
27: #if defined (PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)
31: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg)
32: {
33: int m;
34:
36: if (mode == 'r') m = R_OK;
37: else if (mode == 'w') m = W_OK;
38: else if (mode == 'x') m = X_OK;
39: else SETERRQ(PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
40: #if defined(PETSC_HAVE_ACCESS)
41: if(!access(fname, m)) *flg = PETSC_TRUE;
42: #else
43: if (m == X_OK) SETERRQ1(PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
44: if(!_access(fname, m)) *flg = PETSC_TRUE;
45: #endif
46: return(0);
47: }
49: #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
53: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg)
54: {
55: uid_t uid;
56: gid_t *gid = PETSC_NULL;
57: int numGroups;
58: int rbit = S_IROTH;
59: int wbit = S_IWOTH;
60: int ebit = S_IXOTH;
64: /* Get the number of supplementary group IDs */
65: #if !defined(PETSC_MISSING_GETGROUPS)
66: numGroups = getgroups(0, gid); if (numGroups < 0) {SETERRQ(numGroups, "Unable to count supplementary group IDs");}
67: PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);
68: #else
69: numGroups = 0;
70: #endif
72: /* Get the (effective) user and group of the caller */
73: uid = geteuid();
74: gid[0] = getegid();
76: /* Get supplementary group IDs */
77: #if !defined(PETSC_MISSING_GETGROUPS)
78: getgroups(numGroups, gid+1); if (ierr < 0) {SETERRQ(ierr, "Unable to obtain supplementary group IDs");}
79: #endif
81: /* Test for accessibility */
82: if (fuid == uid) {
83: rbit = S_IRUSR;
84: wbit = S_IWUSR;
85: ebit = S_IXUSR;
86: } else {
87: int g;
89: for(g = 0; g <= numGroups; g++) {
90: if (fgid == gid[g]) {
91: rbit = S_IRGRP;
92: wbit = S_IWGRP;
93: ebit = S_IXGRP;
94: break;
95: }
96: }
97: }
98: PetscFree(gid);
100: if (mode == 'r') {
101: if (fmode & rbit) *flg = PETSC_TRUE;
102: } else if (mode == 'w') {
103: if (fmode & wbit) *flg = PETSC_TRUE;
104: } else if (mode == 'x') {
105: if (fmode & ebit) *flg = PETSC_TRUE;
106: }
107: return(0);
108: }
110: #endif /* PETSC_HAVE_ACCESS */
114: static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscTruth *exists)
115: {
116: struct stat statbuf;
120: #if defined(PETSC_HAVE_STAT_NO_CONST)
121: stat((char*) fname, &statbuf);
122: #else
123: stat(fname, &statbuf);
124: #endif
125: if (ierr) {
126: *exists = PETSC_FALSE;
127: } else {
128: *exists = PETSC_TRUE;
129: *fileUid = statbuf.st_uid;
130: *fileGid = statbuf.st_gid;
131: *fileMode = statbuf.st_mode;
132: }
133: return(0);
134: }
138: PetscErrorCode PetscTestFile(const char fname[], char mode, PetscTruth *flg)
139: {
140: uid_t fuid;
141: gid_t fgid;
142: int fmode;
144: PetscTruth exists;
147: *flg = PETSC_FALSE;
148: if (!fname) return(0);
150: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
151: if (!exists) return(0);
152: /* Except for systems that have this broken stat macros (rare), this
153: is the correct way to check for a regular file */
154: if (!S_ISREG(fmode)) return(0);
156: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
157: return(0);
158: }
162: PetscErrorCode PetscTestDirectory(const char fname[],char mode,PetscTruth *flg)
163: {
164: uid_t fuid;
165: gid_t fgid;
166: int fmode;
168: PetscTruth exists;
171: *flg = PETSC_FALSE;
172: if (!fname) return(0);
174: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
175: if (!exists) return(0);
176: /* Except for systems that have this broken stat macros (rare), this
177: is the correct way to check for a directory */
178: if (!S_ISDIR(fmode)) return(0);
180: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
181: return(0);
182: }
186: PetscErrorCode PetscLs(MPI_Comm comm,const char libname[],char *found,size_t tlen,PetscTruth *flg)
187: {
189: size_t len;
190: char *f,program[PETSC_MAX_PATH_LEN];
191: FILE *fp;
194: PetscStrcpy(program,"ls ");
195: PetscStrcat(program,libname);
196: PetscPOpen(comm,PETSC_NULL,program,"r",&fp);
197: f = fgets(found,tlen,fp);
198: if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
199: while (f) {
200: PetscStrlen(found,&len);
201: f = fgets(found+len,tlen-len,fp);
202: }
203: if (*flg) PetscLogInfo(0,"ls on %s gives \n%s\n",libname,found);
204: return(0);
205: }