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:   numGroups = getgroups(0, gid); if (numGroups < 0) {SETERRQ(numGroups, "Unable to count supplementary group IDs");}
 68:   PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);

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

 74:   /* Get supplementary group IDs */
 75:   getgroups(numGroups, gid+1); if (ierr < 0) {SETERRQ(ierr, "Unable to obtain supplementary group IDs");}

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

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

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

106: #endif /* PETSC_HAVE_ACCESS */

108: static int PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscTruth *exists) {
109:   struct stat statbuf;
110:   int         ierr;

113: #if defined(PETSC_HAVE_STAT_NO_CONST)
114:   stat((char*) fname, &statbuf);
115: #else
116:   stat(fname, &statbuf);
117: #endif
118:   if (ierr) {
119:     *exists = PETSC_FALSE;
120:   } else {
121:     *exists = PETSC_TRUE;
122:     *fileUid  = statbuf.st_uid;
123:     *fileGid  = statbuf.st_gid;
124:     *fileMode = statbuf.st_mode;
125:   }
126:   return(0);
127: }

129: int PetscTestFile(const char fname[], char mode, PetscTruth *flg)
130: {
131:   uid_t      fuid;
132:   gid_t      fgid;
133:   int        fmode;
134:   int        ierr;
135:   PetscTruth exists;

138:   *flg = PETSC_FALSE;
139:   if (!fname) return(0);

141:   PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
142:   if (!exists) return(0);
143:   /* Except for systems that have this broken stat macros (rare), this
144:      is the correct way to check for a regular file */
145:   if (!S_ISREG(fmode)) return(0);

147:   PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
148:   return(0);
149: }

151: int PetscTestDirectory(const char fname[],char mode,PetscTruth *flg)
152: {
153:   uid_t      fuid;
154:   gid_t      fgid;
155:   int        fmode;
156:   int        ierr;
157:   PetscTruth exists;

160:   *flg = PETSC_FALSE;
161:   if (!fname) return(0);

163:   PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);
164:   if (!exists) return(0);
165:   /* Except for systems that have this broken stat macros (rare), this
166:      is the correct way to check for a directory */
167:   if (!S_ISDIR(fmode)) return(0);

169:   PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);
170:   return(0);
171: }

173: int PetscLs(MPI_Comm comm,const char libname[],char *found,int tlen,PetscTruth *flg)
174: {
175:   int   ierr,len;
176:   char  *f,program[1024];
177:   FILE  *fp;

180:   ierr   = PetscStrcpy(program,"ls ");
181:   ierr   = PetscStrcat(program,libname);
182:   ierr   = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);
183:   f      = fgets(found,tlen,fp);
184:   if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE;
185:   while (f) {
186:     ierr  = PetscStrlen(found,&len);
187:     f     = fgets(found+len,tlen-len,fp);
188:   }
189:   if (*flg) PetscLogInfo(0,"ls on %s gives n%sn",libname,found);
190:   return(0);
191: }