Actual source code: ftest.c

  1: #define PETSC_DLL

 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(PETSC_HAVE_SYS_UTSNAME_H)
 18: #include <sys/utsname.h>
 19: #endif
 20: #if defined(PETSC_HAVE_IO_H)
 21: #include <io.h>
 22: #endif
 23: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 24: #include <sys/systeminfo.h>
 25: #endif
 26: #include "petscfix.h"

 28: #if defined (PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS)

 32: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg)
 33: {
 34:   int m = R_OK;
 35: 
 37:   if (mode == 'r') m = R_OK;
 38:   else if (mode == 'w') m = W_OK;
 39:   else if (mode == 'x') m = X_OK;
 40:   else SETERRQ(PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x");
 41: #if defined(PETSC_HAVE_ACCESS)
 42:   if(!access(fname, m))  *flg = PETSC_TRUE;
 43: #else
 44:   if (m == X_OK) SETERRQ1(PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname);
 45:   if(!_access(fname, m)) *flg = PETSC_TRUE;
 46: #endif
 47:   return(0);
 48: }

 50: #else  /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */

 54: static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscTruth *flg)
 55: {
 56:   uid_t          uid;
 57:   gid_t          *gid = PETSC_NULL;
 58:   int            numGroups;
 59:   int            rbit = S_IROTH;
 60:   int            wbit = S_IWOTH;
 61:   int            ebit = S_IXOTH;

 65:   /* Get the number of supplementary group IDs */
 66: #if !defined(PETSC_MISSING_GETGROUPS)
 67:   numGroups = getgroups(0, gid); if (numGroups < 0) {SETERRQ(numGroups, "Unable to count supplementary group IDs");}
 68:   PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);
 69: #else
 70:   numGroups = 0;
 71: #endif

 73:   /* Get the (effective) user and group of the caller */
 74:   uid    = geteuid();
 75:   gid[0] = getegid();

 77:   /* Get supplementary group IDs */
 78: #if !defined(PETSC_MISSING_GETGROUPS)
 79:   getgroups(numGroups, gid+1); if (ierr < 0) {SETERRQ(ierr, "Unable to obtain supplementary group IDs");}
 80: #endif

 82:   /* Test for accessibility */
 83:   if (fuid == uid) {
 84:     rbit = S_IRUSR;
 85:     wbit = S_IWUSR;
 86:     ebit = S_IXUSR;
 87:   } else {
 88:     int g;

 90:     for(g = 0; g <= numGroups; g++) {
 91:       if (fgid == gid[g]) {
 92:         rbit = S_IRGRP;
 93:         wbit = S_IWGRP;
 94:         ebit = S_IXGRP;
 95:         break;
 96:       }
 97:     }
 98:   }
 99:   PetscFree(gid);

101:   if (mode == 'r') {
102:     if (fmode & rbit) *flg = PETSC_TRUE;
103:   } else if (mode == 'w') {
104:     if (fmode & wbit) *flg = PETSC_TRUE;
105:   } else if (mode == 'x') {
106:     if (fmode & ebit) *flg = PETSC_TRUE;
107:   }
108:   return(0);
109: }

111: #endif /* PETSC_HAVE_ACCESS */

115: static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscTruth *exists)
116: {
117:   struct stat    statbuf;

121: #if defined(PETSC_HAVE_STAT_NO_CONST)
122:   stat((char*) fname, &statbuf);
123: #else
124:   stat(fname, &statbuf);
125: #endif
126:   if (ierr) {
127:     *exists = PETSC_FALSE;
128:   } else {
129:     *exists = PETSC_TRUE;
130:     *fileUid  = statbuf.st_uid;
131:     *fileGid  = statbuf.st_gid;
132:     *fileMode = statbuf.st_mode;
133:   }
134:   return(0);
135: }

139: PetscErrorCode  PetscTestFile(const char fname[], char mode, PetscTruth *flg)
140: {
141:   uid_t          fuid;
142:   gid_t          fgid;
143:   int            fmode;
145:   PetscTruth     exists;

148:   *flg = PETSC_FALSE;
149:   if (!fname) return(0);

151:   PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
152:   if (!exists) return(0);
153:   /* Except for systems that have this broken stat macros (rare), this
154:      is the correct way to check for a regular file */
155:   if (!S_ISREG(fmode)) return(0);

157:   PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
158:   return(0);
159: }

163: PetscErrorCode  PetscTestDirectory(const char fname[],char mode,PetscTruth *flg)
164: {
165:   uid_t          fuid;
166:   gid_t          fgid;
167:   int            fmode;
169:   PetscTruth     exists;

172:   *flg = PETSC_FALSE;
173:   if (!fname) return(0);

175:   PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
176:   if (!exists) return(0);
177:   /* Except for systems that have this broken stat macros (rare), this
178:      is the correct way to check for a directory */
179:   if (!S_ISDIR(fmode)) return(0);

181:   PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
182:   return(0);
183: }

187: PetscErrorCode  PetscLs(MPI_Comm comm,const char libname[],char *found,size_t tlen,PetscTruth *flg)
188: {
190:   size_t         len;
191:   char           *f,program[PETSC_MAX_PATH_LEN];
192:   FILE           *fp;

195:   PetscStrcpy(program,"ls ");
196:   PetscStrcat(program,libname);
197: #if defined(PETSC_HAVE_POPEN)
198:   PetscPOpen(comm,PETSC_NULL,program,"r",&fp);
199: #else
200:   SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
201: #endif
202:   f      = fgets(found,tlen,fp);
203:   if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
204:   while (f) {
205:     PetscStrlen(found,&len);
206:     f     = fgets(found+len,tlen-len,fp);
207:   }
208:   if (*flg) {PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);}
209: #if defined(PETSC_HAVE_POPEN)
210:   PetscPClose(comm,fp);
211: #else
212:   SETERRQ(PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine");
213: #endif
214:   return(0);
215: }