Actual source code: ftest.c

  1: #define PETSC_DLL

 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

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

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

 48: #else  /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */

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

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

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

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

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

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

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

109: #endif /* PETSC_HAVE_ACCESS */

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

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: }

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

146:   *flg = PETSC_FALSE;
147:   if (!fname) return(0);

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

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

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

170:   *flg = PETSC_FALSE;
171:   if (!fname) return(0);

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

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

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

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