date and time created 83/02/11 15:44:33 by rrh
[unix-history] / usr / src / usr.bin / du / du.c
index b8db4a2..4089ba7 100644 (file)
@@ -1,5 +1,5 @@
 #ifndef lint
 #ifndef lint
-static char *sccsid = "@(#)du.c        4.5 (Berkeley) %G%";
+static char *sccsid = "@(#)du.c        4.8 (Berkeley) %G%";
 #endif
 
 #include <stdio.h>
 #endif
 
 #include <stdio.h>
@@ -7,8 +7,6 @@ static char *sccsid = "@(#)du.c 4.5 (Berkeley) %G%";
 #include <sys/stat.h>
 #include <dir.h>
 
 #include <sys/stat.h>
 #include <dir.h>
 
-#define howmany(x, y)  (((x) + (y) - 1) / (y))
-
 char   path[BUFSIZ], name[BUFSIZ];
 int    aflg;
 int    sflg;
 char   path[BUFSIZ], name[BUFSIZ];
 int    aflg;
 int    sflg;
@@ -30,6 +28,7 @@ main(argc, argv)
 {
        long kbytes = 0;
        register char *np;
 {
        long kbytes = 0;
        register char *np;
+       int pid;
 
        argc--, argv++;
 again:
 
        argc--, argv++;
 again:
@@ -48,19 +47,32 @@ again:
                argc = 1;
        }
        do {
                argc = 1;
        }
        do {
-               (void) strcpy(path, *argv);
-               (void) strcpy(name, *argv);
-               if (np = rindex(name, '/')) {
-                       *np++ = '\0';
-                       if (chdir(*name ? name : "/") < 0) {
-                               perror(*name ? name : "/");
+               if (argc > 1) {
+                       pid = fork();
+                       if (pid == -1) {
+                               fprintf(stderr, "No more processes.\n");
                                exit(1);
                        }
                                exit(1);
                        }
-               } else
-                       np = path;
-               kbytes = descend(path, *np ? np : ".");
-               if (sflg)
-                       printf("%ld\t%s\n", kbytes, path);
+                       if (pid != 0)
+                               wait((int *)0);
+               }
+               if (argc == 1 || pid == 0) {
+                       (void) strcpy(path, *argv);
+                       (void) strcpy(name, *argv);
+                       if (np = rindex(name, '/')) {
+                               *np++ = '\0';
+                               if (chdir(*name ? name : "/") < 0) {
+                                       perror(*name ? name : "/");
+                                       exit(1);
+                               }
+                       } else
+                               np = path;
+                       kbytes = descend(path, *np ? np : ".");
+                       if (sflg)
+                               printf("%ld\t%s\n", kbytes, path);
+                       if (argc > 1)
+                               exit(1);
+               }
                argc--, argv++;
        } while (argc > 0);
        exit(0);
                argc--, argv++;
        } while (argc > 0);
        exit(0);
@@ -103,16 +115,21 @@ descend(base, name)
                        printf("%ld\t%s\n", kbytes, base);
                return (kbytes);
        }
                        printf("%ld\t%s\n", kbytes, base);
                return (kbytes);
        }
-       if (chdir(name) < 0)
-               return (0);
        if (dirp != NULL)
                closedir(dirp);
        if (dirp != NULL)
                closedir(dirp);
-       dirp = opendir(".");
+       dirp = opendir(name);
        if (dirp == NULL) {
                perror(base);
                *ebase0 = 0;
                return (0);
        }
        if (dirp == NULL) {
                perror(base);
                *ebase0 = 0;
                return (0);
        }
+       if (chdir(name) < 0) {
+               perror(base);
+               *ebase0 = 0;
+               closedir(dirp);
+               dirp = NULL;
+               return (0);
+       }
        while (dp = readdir(dirp)) {
                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                        continue;
        while (dp = readdir(dirp)) {
                if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
                        continue;
@@ -122,6 +139,10 @@ descend(base, name)
                *ebase = 0;
                if (dirp == NULL) {
                        dirp = opendir(".");
                *ebase = 0;
                if (dirp == NULL) {
                        dirp = opendir(".");
+                       if (dirp == NULL) {
+                               perror(".");
+                               return (0);
+                       }
                        seekdir(dirp, curoff);
                }
        }
                        seekdir(dirp, curoff);
                }
        }