1: # include   <stdio.h>
   2: # include   <sys/types.h>
   3: # include   <sys/dir.h>
   4: 
   5: # include   "../ingres.h"
   6: # include   "../aux.h"
   7: # include   "../access.h"
   8: # include   "../lock.h"
   9: 
  10: /*
  11: ** These are subroutines common to RESTORE and PURGE.
  12: */
  13: 
  14: char        All;
  15: char        Qrymod;
  16: char        Superuser;
  17: char        Ask;
  18: char        Purge;
  19: char        Clean;
  20: char        Lastflag;
  21: DIR     *dirp;
  22: struct direct   *directp;
  23: extern int  Status;
  24: extern char *Usercode;
  25: char        **Dblist;
  26: 
  27: 
  28: 
  29: 
  30: /*
  31: **  INITIALIZE GLOBALS
  32: **
  33: **	Set up Usercode and Status
  34: */
  35: 
  36: initialize(argc, argv)
  37: int argc;
  38: char    **argv;
  39: {
  40:     register int    i;
  41:     long        l;
  42:     extern char *Flagvect[];
  43:     extern char *Parmvect[];
  44:     register char   *p;
  45:     register char   **av;
  46:     char        datadir[MAXLINE];
  47: 
  48: #	ifdef xTTR2
  49:     tTfp(40, 0, "entered initialize\n");
  50: #	endif
  51:     i = initucode(argc, argv, FALSE, NULL, -1);
  52: #	ifdef xTTR2
  53:     tTfp(40, 1, "initucode ret:%d\n", i);
  54: #	endif
  55:     switch (i)
  56:     {
  57:       case 0:
  58:         break;
  59: 
  60:       case 3:
  61:         printf("You are not a valid INGRES user\n");
  62:         exit(-1);
  63: 
  64:       default:
  65:         syserr("initucode %d", i);
  66:     }
  67:     initdbpath(NULL, datadir, FALSE);
  68: 
  69:     /* scan flags */
  70: #	ifdef xTTR2
  71:     tTfp(40, 2, "scanning flags\n");
  72: #	endif
  73:     for (av = Flagvect;  *av != NULL; av++)
  74:     {
  75:         p = *av;
  76:         if (p[0] != '-')
  77:         {
  78:         badflag:
  79:             printf("Bad flag: %s\n", p);
  80:             return (-1);
  81:         }
  82:         switch (p[1])
  83:         {
  84:           case 'a':
  85:             Ask++;
  86:             break;
  87: 
  88:           case 'p':
  89:             Purge++;
  90:             break;
  91: 
  92:           case 's':
  93:             if (sucheck())
  94:                 Superuser++;
  95:             else
  96:             {
  97:                 printf("You may not use the -s flag\n");
  98:                 exit(-1);
  99:             }
 100:             break;
 101: 
 102:           case 'f':
 103:             Clean++;
 104:             break;
 105: 
 106:           default:
 107:             goto badflag;
 108:         }
 109:     }
 110:     Dblist = Parmvect;
 111:     if (*Dblist == 0)
 112:     {
 113: #		ifdef    xTTR2
 114:         tTfp(40, 3, "doing all\n");
 115: #		endif
 116:         All++;
 117:         dirp = opendir(datadir);
 118:         if (dirp == NULL)
 119:         {
 120:             syserr("cannot read .../data/base");
 121:         }
 122:         readdir(dirp);  /* skip "." */
 123:         readdir(dirp);  /* skip ".." */
 124:     }
 125: #	ifdef xTTR2
 126:     tTfp(40, 0, "leaving initialize\n");
 127: #	endif
 128: }
 129: 
 130: 
 131: /*
 132: **  CHECK FOR SUPERUSER
 133: **
 134: **	The user has requested the -s flag.  Can he do it?  Will Martha
 135: **	recover from cancer?  Will Dick get the girl?  Stay tuned for
 136: **	"sucheck".
 137: **
 138: **	Permission is based on the U_SUPER bit in the status field
 139: **	in the users file.
 140: */
 141: 
 142: sucheck()
 143: {
 144:     return (Status & U_SUPER);
 145: }
 146: 
 147: 
 148: /*
 149: **  GET NEXT DATABASE
 150: **
 151: **	The next database to be purged is selected.  It comes from
 152: **	either the directory or the database list.
 153: **
 154: **	Getnxtdb() leaves the user in the database directory.
 155: */
 156: 
 157: char *getnxtdb()
 158: {
 159:     register char       *db;
 160:     register FILE       *fd;
 161:     register int        i;
 162:     extern struct admin Admin;
 163:     static char     dbpbuf[MAXLINE];
 164: 
 165: #	ifdef xTTR2
 166:     tTfp(41, 0, "entered getnxtdb\n");
 167: #	endif
 168:     for (;;)
 169:     {
 170:         if (All)
 171:         {
 172:             directp = readdir(dirp);
 173:             if (directp == NULL)
 174:                 db = NULL;
 175:             else
 176:                 db = directp->d_name;
 177:         }
 178:         else
 179:         {
 180:             db = *Dblist++;
 181:         }
 182:         if (db == NULL)
 183:             return (NULL);
 184: #		ifdef    xTTR2
 185:         tTfp(41, 1, "using %s as Database\n", db);
 186: #		endif
 187:         i = initdbpath(db, dbpbuf, TRUE);
 188: #		ifdef    xTTR2
 189:         tTfp(41, 3, "initdbpath ret: %d, %s\n", i, dbpbuf);
 190: #		endif
 191:         switch (i)
 192:         {
 193:           case 0:
 194:           case 1:
 195:             break;
 196: 
 197:           case 2:
 198:           case 3:
 199:             printf("Database %s does not exist\n", db);
 200:             continue;
 201: 
 202:           default:
 203:             syserr("initdbpath %d", i);
 204:         }
 205:         if (chdir(dbpbuf) < 0)
 206:         {
 207:             printf("Cannot enter %s", dbpbuf);
 208:             continue;
 209:         }
 210: #		ifdef    xTTR2
 211:         tTfp(41, 4, "chdir ok, Superuser: %d\n", Superuser);
 212: #		endif
 213:         fd = fopen("admin", "r");
 214:         if (fd == NULL)
 215:         {
 216:             printf("Cannot open %s/admin\n", dbpbuf);
 217:             continue;
 218:         }
 219:         fread(&Admin.adhdr, sizeof Admin.adhdr, 1, fd);
 220:         fclose(fd);
 221: #		ifdef    xTTR2
 222:         tTfp(41, 5, "user: %.2s\n", Admin.adhdr.adowner);
 223: #		endif
 224: 
 225:         /* set qrymod flag from database status */
 226:         Qrymod = ((Admin.adhdr.adflags & A_QRYMOD) == A_QRYMOD);
 227: 
 228:         /* check for dba of database if not superuser */
 229:         if (Superuser || bequal(Admin.adhdr.adowner, Usercode, 2))
 230:             break;
 231: 
 232:         /*
 233: 		** not dba isn't an error if running in all mode since user
 234: 		** couln't have specified the database
 235: 		*/
 236:         if (All)
 237:             continue;
 238:         printf("You are not the dba for %s\n", db);
 239:     }
 240: #	ifdef xTTR2
 241:     tTfp(41, 6, "leaving getnxtdb, %s ok\n", db);
 242: #	endif
 243:     return (db);
 244: }
 245: 
 246: 
 247: 
 248: /*
 249: ** ASK
 250: **	If Ask is set desplay prompt and look for 'y' and return TRUE
 251: **	If Ask is not set return TRUE
 252: */
 253: ask(prompt)
 254: char    *prompt;
 255: {
 256:     register char   *p;
 257:     char        line[MAXLINE];
 258:     extern char Ask;
 259: 
 260:     if (!Ask)
 261:         return (TRUE);
 262:     p = prompt;
 263: 
 264:     while (*p)
 265:     {
 266:         putchar(*p);
 267:         p++;
 268:     }
 269: 
 270:     if (fgetline(line, MAXLINE, stdin) == NULL)
 271:         syserr("cannot getline in ask()");
 272:     return (line[0] == 'y');
 273: }

Defined functions

sucheck defined in line 142; used 1 times
  • in line 93

Defined variables

All defined in line 14; used 3 times
Ask defined in line 17; used 3 times
Clean defined in line 19; used 1 times
Dblist defined in line 25; used 3 times
Lastflag defined in line 20; never used
Purge defined in line 18; used 1 times
  • in line 89
Qrymod defined in line 15; used 3 times
Superuser defined in line 16; used 3 times
directp defined in line 22; used 3 times
dirp defined in line 21; used 5 times
Last modified: 1995-02-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 844
Valid CSS Valid XHTML 1.0 Strict