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)
37: static int PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg) {
38: int m;
39:
41: if (mode == 'r') m = R_OK;
42: else if (mode == 'w') m = W_OK;
43: else if (mode == 'x') m = X_OK;
44: else SETERRQ(PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
45: #if defined(PETSC_HAVE_ACCESS)
46: if(!access(fname, m)) *flg = PETSC_TRUE;
47: #else
48: if (m == X_OK) SETERRQ1(PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
49: if(!_access(fname, m)) *flg = PETSC_TRUE;
50: #endif
51: return(0);
52: }
54: #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */
56: static int PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg) {
57: uid_t uid;
58: gid_t *gid = PETSC_NULL;
59: int numGroups;
60: int rbit = S_IROTH;
61: int wbit = S_IWOTH;
62: int ebit = S_IXOTH;
63: int ierr;
66: /* Get the number of supplementary group IDs */
67: #if !defined(PETSC_MISSING_GETGROUPS)
68: numGroups = getgroups(0, gid); if (numGroups < 0) {SETERRQ(numGroups, "Unable to count supplementary group IDs");}
69: PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);
70: #else
71: numGroups = 0;
72: #endif
74: /* Get the (effective) user and group of the caller */
75: uid = geteuid();
76: gid[0] = getegid();
78: /* Get supplementary group IDs */
79: #if !defined(PETSC_MISSING_GETGROUPS)
80: getgroups(numGroups, gid+1); if (ierr < 0) {SETERRQ(ierr, "Unable to obtain supplementary group IDs");}
81: #endif
83: /* Test for accessibility */
84: if (fuid == uid) {
85: rbit = S_IRUSR;
86: wbit = S_IWUSR;
87: ebit = S_IXUSR;
88: } else {
89: int g;
91: for(g = 0; g <= numGroups; g++) {
92: if (fgid == gid[g]) {
93: rbit = S_IRGRP;
94: wbit = S_IWGRP;
95: ebit = S_IXGRP;
96: break;
97: }
98: }
99: }
100: PetscFree(gid);
102: if (mode == 'r') {
103: if (fmode & rbit) *flg = PETSC_TRUE;
104: } else if (mode == 'w') {
105: if (fmode & wbit) *flg = PETSC_TRUE;
106: } else if (mode == 'x') {
107: if (fmode & ebit) *flg = PETSC_TRUE;
108: }
109: return(0);
110: }
112: #endif /* PETSC_HAVE_ACCESS */
114: static int PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscTruth *exists) {
115: struct stat statbuf;
116: int ierr;
119: #if defined(PETSC_HAVE_STAT_NO_CONST)
120: stat((char*) fname, &statbuf);
121: #else
122: stat(fname, &statbuf);
123: #endif
124: if (ierr) {
125: *exists = PETSC_FALSE;
126: } else {
127: *exists = PETSC_TRUE;
128: *fileUid = statbuf.st_uid;
129: *fileGid = statbuf.st_gid;
130: *fileMode = statbuf.st_mode;
131: }
132: return(0);
133: }
135: int PetscTestFile(const char fname[], char mode, PetscTruth *flg)
136: {
137: uid_t fuid;
138: gid_t fgid;
139: int fmode;
140: int ierr;
141: PetscTruth exists;
144: *flg = PETSC_FALSE;
145: if (!fname) return(0);
147: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
148: if (!exists) return(0);
149: /* Except for systems that have this broken stat macros (rare), this
150: is the correct way to check for a regular file */
151: if (!S_ISREG(fmode)) return(0);
153: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
154: return(0);
155: }
157: int PetscTestDirectory(const char fname[],char mode,PetscTruth *flg)
158: {
159: uid_t fuid;
160: gid_t fgid;
161: int fmode;
162: int ierr;
163: PetscTruth exists;
166: *flg = PETSC_FALSE;
167: if (!fname) return(0);
169: PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
170: if (!exists) return(0);
171: /* Except for systems that have this broken stat macros (rare), this
172: is the correct way to check for a directory */
173: if (!S_ISDIR(fmode)) return(0);
175: PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
176: return(0);
177: }
179: int PetscLs(MPI_Comm comm,const char libname[],char *found,int tlen,PetscTruth *flg)
180: {
181: int ierr,len;
182: char *f,program[1024];
183: FILE *fp;
186: ierr = PetscStrcpy(program,"ls ");
187: ierr = PetscStrcat(program,libname);
188: ierr = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);
189: f = fgets(found,tlen,fp);
190: if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
191: while (f) {
192: ierr = PetscStrlen(found,&len);
193: f = fgets(found+len,tlen-len,fp);
194: }
195: if (*flg) PetscLogInfo(0,"ls on %s gives n%sn",libname,found);
196: return(0);
197: }