1: #ifndef lint
   2: static char *sccsid = "@(#)rmdir.c	4.2 (Berkeley) 11/10/80";
   3: #endif
   4: /*
   5:  * Remove directory
   6:  */
   7: 
   8: #include <sys/types.h>
   9: #include <sys/dir.h>
  10: #include <sys/stat.h>
  11: #include <stdio.h>
  12: 
  13: int Errors = 0;
  14: char    *rindex();
  15: char    *strcat();
  16: char    *strcpy();
  17: 
  18: main(argc,argv)
  19: int argc;
  20: char **argv;
  21: {
  22: 
  23:     if(argc < 2) {
  24:         fprintf(stderr, "rmdir: arg count\n");
  25:         exit(1);
  26:     }
  27:     while(--argc)
  28:         rmdir(*++argv);
  29:     exit(Errors!=0);
  30: }
  31: 
  32: rmdir(d)
  33: char *d;
  34: {
  35:     int fd;
  36:     char    *np, name[500];
  37:     struct  stat    st, cst;
  38:     struct  direct  dir;
  39: 
  40:     strcpy(name, d);
  41: 
  42:     /* eat trailing slashes */
  43:     np = &(name[strlen(name)-1]);
  44:     while (*np == '/' && np != name) {
  45:         *np = '\0';
  46:         np--;
  47:     }
  48: 
  49:     /* point after last slash */
  50:     if((np = rindex(name, '/')) == NULL)
  51:         np = name;
  52:     else
  53:         np++;
  54: 
  55:     if(!strcmp(np, ".") || !strcmp(np, "..")) {
  56:         fprintf(stderr, "rmdir: cannot remove . or ..\n");
  57:         ++Errors;
  58:         return;
  59:     }
  60:     if(stat(name,&st) < 0) {
  61:         fprintf(stderr, "rmdir: %s non-existent\n", name);
  62:         ++Errors;
  63:         return;
  64:     }
  65:     if (stat("", &cst) < 0) {
  66:         fprintf(stderr, "rmdir: cannot stat \"\"");
  67:         ++Errors;
  68:         exit(1);
  69:     }
  70:     if((st.st_mode & S_IFMT) != S_IFDIR) {
  71:         fprintf(stderr, "rmdir: %s not a directory\n", name);
  72:         ++Errors;
  73:         return;
  74:     }
  75:     if(st.st_ino==cst.st_ino &&st.st_dev==cst.st_dev) {
  76:         fprintf(stderr, "rmdir: cannot remove current directory\n");
  77:         ++Errors;
  78:         return;
  79:     }
  80:     if((fd = open(name,0)) < 0) {
  81:         fprintf(stderr, "rmdir: %s unreadable\n", name);
  82:         ++Errors;
  83:         return;
  84:     }
  85:     while(read(fd, (char *)&dir, sizeof dir) == sizeof dir) {
  86:         if(dir.d_ino == 0) continue;
  87:         if(!strcmp(dir.d_name, ".") || !strcmp(dir.d_name, ".."))
  88:             continue;
  89:         fprintf(stderr, "rmdir: %s not empty\n", name);
  90:         ++Errors;
  91:         close(fd);
  92:         return;
  93:     }
  94:     close(fd);
  95:     strcat(name, "/.");
  96:     if((access(name, 0)) < 0) {     /* name/. non-existent */
  97:         strcat(name, ".");
  98:         goto unl;
  99:     }
 100:     strcat(name, ".");
 101:     if((access(name, 0)) < 0)       /* name/.. non-existent */
 102:         goto unl2;
 103:     if(access(name, 02)) {
 104:         name[strlen(name)-3] = '\0';
 105:         fprintf(stderr, "rmdir: %s: no permission\n", name);
 106:         ++Errors;
 107:         return;
 108:     }
 109: unl:
 110:     unlink(name);   /* unlink name/.. */
 111: unl2:
 112:     name[strlen(name)-1] = '\0';
 113:     unlink(name);   /* unlink name/.  */
 114:     name[strlen(name)-2] = '\0';
 115:     if (unlink(name) < 0) {
 116:         fprintf(stderr, "rmdir: %s not removed\n", name);
 117:         ++Errors;
 118:     }
 119: }

Defined functions

main defined in line 18; never used
rmdir defined in line 32; used 1 times
  • in line 28

Defined variables

Errors defined in line 13; used 10 times
sccsid defined in line 2; never used
Last modified: 1982-09-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 929
Valid CSS Valid XHTML 1.0 Strict