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: 
   9: /*
  10: **  DESTROY DATA BASE
  11: **
  12: **	This program destroys an existing database.  To be able
  13: **	to wield this awesome power, you must be the dba for
  14: **	the database.  Also, anyone has this power if the admin
  15: **	the database, or the ingres superuser, and have the "-s"
  16: **	flag requested.  If admin is trashed, the INGRES superuser
  17: **	must either destroy the database or recover it.
  18: **
  19: **	If -m is specified, then the directory is not removed.
  20: **	This is useful if the directory is a mounted file system.
  21: **
  22: **	History:
  23: **		2/8/78 [eric] -- changed to use fread, and to not
  24: **			try to unlink files with i# = 0 (null
  25: **			entries).
  26: */
  27: 
  28: extern char *Usercode;
  29: extern int  Status;
  30: extern char *Pathname;
  31: extern char *Parmvect[];
  32: extern char *Flagvect[];
  33: extern char *Dbpath;
  34: struct admin    Admin;
  35: 
  36: main(argc, argv)
  37: int argc;
  38: char    *argv[];
  39: {
  40:     register int    i;
  41:     register char   *dbase;
  42:     int     superuser, mounted;
  43:     char        **av;
  44:     register char   *p;
  45:     char        *q;
  46: 
  47: #	ifdef xSTR1
  48:     tTrace(&argc, argv, 'T');
  49: #	endif
  50: 
  51:     i = initucode(argc, argv, TRUE, NULL, -1);
  52:     dbase = Parmvect[0];
  53:     switch (i)
  54:     {
  55:       case 0:
  56:       case 5:
  57:         break;
  58: 
  59:       case 1:
  60:       case 6:
  61:         printf("Database %s does not exist\n", dbase);
  62:         exit(-1);
  63: 
  64:       case 2:
  65:         printf("You are not authorized to access database %s\n", dbase);
  66:         exit(-1);
  67: 
  68:       case 3:
  69:         printf("You are not an authorized INGRES user\n");
  70:         exit(-1);
  71: 
  72:       case 4:
  73:         printf("No database name specified\n");
  74:     usage:
  75:         printf("Usage: destroydb [-s] [-m] dbname\n");
  76:         exit(-1);
  77: 
  78:       default:
  79:         syserr("initucode %d", i);
  80:     }
  81: 
  82:     mounted = superuser = 0;
  83:     for (av = Flagvect; (p = *av) != NULL; av++)
  84:     {
  85:         if (p[0] != '-')
  86:         {
  87:         badflag:
  88:             printf("Bad flag %s\n", p);
  89:             goto usage;
  90:         }
  91:         switch (p[1])
  92:         {
  93: 
  94:           case 's':
  95:             superuser++;
  96:             break;
  97: 
  98:           case 'm':
  99:             mounted++;
 100:             break;
 101: 
 102:           default:
 103:             goto badflag;
 104:         }
 105:     }
 106: 
 107:     if (Parmvect[1] != NULL)
 108:     {
 109:         printf("Too many parameters to destroydb\n");
 110:         goto usage;
 111:     }
 112:     if (length(dbase) > 14)
 113:         syserr(0, "invalid dbname %s", dbase);
 114:     if (superuser && (Status & U_SUPER) == 0)
 115:         syserr(0, "you may not use the -s flag");
 116: 
 117:     if (!superuser)
 118:     {
 119:         if (!bequal(Admin.adhdr.adowner, Usercode, 2))
 120:         {
 121:             printf("You are not the DBA for %s\n", dbase);
 122:             exit(-1);
 123:         }
 124:     }
 125: 
 126:     if (chdir(Dbpath) < 0)
 127:         syserr("chdir %s", Dbpath);
 128: 
 129:     clean(".");
 130: 
 131:     if (!mounted)
 132:     {
 133:         /* find end of Dbpath and trim it off. */
 134:         for (p = q = Dbpath; *p != '\0'; p++)
 135:             if (*p == '/')
 136:                 q = p;
 137:         *q++ = '\0';
 138:         if (chdir(Dbpath) < 0)
 139:             syserr("chdir(%s)", Dbpath);
 140:         execl("/bin/rmdir", "/bin/rmdir", q, 0);
 141:     }
 142: }
 143: 
 144: 
 145: 
 146: clean(dirname)
 147: char    *dirname;
 148: {
 149:     DIR     *dirp;
 150:     struct direct   *directp;
 151: 
 152:     if ((dirp = opendir(dirname)) == NULL)
 153:         syserr("can't open \".\" for cleaning");
 154: 
 155:     while ((directp = readdir(dirp)) != NULL) {
 156:         if (!strcmp(directp->d_name, "."))
 157:             continue;
 158:         if (!strcmp(directp->d_name, ".."))
 159:             continue;
 160:         unlink(directp->d_name);
 161:     }
 162: 
 163:     closedir(dirp);
 164: }
 165: 
 166: 
 167: 
 168: /*
 169: **  Rubout processing.
 170: */
 171: 
 172: rubproc()
 173: {
 174:     exit(-2);
 175: }

Defined functions

clean defined in line 146; used 1 times
main defined in line 36; never used
rubproc defined in line 172; never used

Defined variables

Admin defined in line 34; used 1 times
Last modified: 1995-02-12
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3744
Valid CSS Valid XHTML 1.0 Strict