make sleep spin-wait during autoconfiguration
[unix-history] / usr / src / bin / rmdir / rmdir.c
index 301787b..284e622 100644 (file)
-static char *sccsid = "@(#)rmdir.c     4.1 (Berkeley) %G%";
 /*
 /*
- * Remove directory
+ * Copyright (c) 1983 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
  */
 
  */
 
-#include <sys/types.h>
-#include <sys/dir.h>
-#include <sys/stat.h>
-#include <stdio.h>
-
-int    Errors = 0;
-char   *rindex();
-char   *strcat();
-char   *strcpy();
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1983 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
 
 
-main(argc,argv)
-int argc;
-char **argv;
-{
+#ifndef lint
+static char sccsid[] = "@(#)rmdir.c    5.1 (Berkeley) %G%";
+#endif not lint
 
 
-       if(argc < 2) {
-               fprintf(stderr, "rmdir: arg count\n");
-               exit(1);
-       }
-       while(--argc)
-               rmdir(*++argv);
-       exit(Errors!=0);
-}
+/*
+ * Remove directory
+ */
+#include <stdio.h>
 
 
-rmdir(d)
-char *d;
+main(argc,argv)
+       int argc;
+       char **argv;
 {
 {
-       int     fd;
-       char    *np, name[500];
-       struct  stat    st, cst;
-       struct  direct  dir;
+       int errors = 0;
 
 
-       strcpy(name, d);
-       if((np = rindex(name, '/')) == NULL)
-               np = name;
-       if(stat(name,&st) < 0) {
-               fprintf(stderr, "rmdir: %s non-existent\n", name);
-               ++Errors;
-               return;
-       }
-       if (stat("", &cst) < 0) {
-               fprintf(stderr, "rmdir: cannot stat \"\"");
-               ++Errors;
+       if (argc < 2) {
+               fprintf(stderr, "usage: %s directory ...\n", argv[0]);
                exit(1);
        }
                exit(1);
        }
-       if((st.st_mode & S_IFMT) != S_IFDIR) {
-               fprintf(stderr, "rmdir: %s not a directory\n", name);
-               ++Errors;
-               return;
-       }
-       if(st.st_ino==cst.st_ino &&st.st_dev==cst.st_dev) {
-               fprintf(stderr, "rmdir: cannot remove current directory\n");
-               ++Errors;
-               return;
-       }
-       if((fd = open(name,0)) < 0) {
-               fprintf(stderr, "rmdir: %s unreadable\n", name);
-               ++Errors;
-               return;
-       }
-       while(read(fd, (char *)&dir, sizeof dir) == sizeof dir) {
-               if(dir.d_ino == 0) continue;
-               if(!strcmp(dir.d_name, ".") || !strcmp(dir.d_name, ".."))
-                       continue;
-               fprintf(stderr, "rmdir: %s not empty\n", name);
-               ++Errors;
-               close(fd);
-               return;
-       }
-       close(fd);
-       if(!strcmp(np, ".") || !strcmp(np, "..")) {
-               fprintf(stderr, "rmdir: cannot remove . or ..\n");
-               ++Errors;
-               return;
-       }
-       strcat(name, "/.");
-       if((access(name, 0)) < 0) {             /* name/. non-existent */
-               strcat(name, ".");
-               goto unl;
-       }
-       strcat(name, ".");
-       if((access(name, 0)) < 0)               /* name/.. non-existent */
-               goto unl2;
-       if(access(name, 02)) {
-               name[strlen(name)-3] = '\0';
-               fprintf(stderr, "rmdir: %s: no permission\n", name);
-               ++Errors;
-               return;
-       }
-unl:
-       unlink(name);   /* unlink name/.. */
-unl2:
-       name[strlen(name)-1] = '\0';
-       unlink(name);   /* unlink name/.  */
-       name[strlen(name)-2] = '\0';
-       if (unlink(name) < 0) {
-               fprintf(stderr, "rmdir: %s not removed\n", name);
-               ++Errors;
-       }
+       while (--argc)
+               if (rmdir(*++argv) < 0) {
+                       fprintf(stderr, "rmdir: ");
+                       perror(*argv);;
+                       errors++;
+               }
+       exit(errors != 0);
 }
 }