BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / du / du.c
index e906b98..fd3babf 100644 (file)
@@ -5,7 +5,33 @@
  * This code is derived from software contributed to Berkeley by
  * Chris Newcomb.
  *
  * This code is derived from software contributed to Berkeley by
  * Chris Newcomb.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -15,7 +41,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)du.c       5.10 (Berkeley) %G%";
+static char sccsid[] = "@(#)du.c       5.12 (Berkeley) 6/20/91";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -35,35 +61,44 @@ main(argc, argv)
        register FTS *fts;
        register FTSENT *p;
        register int kvalue, listdirs, listfiles;
        register FTS *fts;
        register FTSENT *p;
        register int kvalue, listdirs, listfiles;
-       int ch, ftsoptions;
+       int aflag, ch, ftsoptions, sflag;
        char **save;
 
        ftsoptions = FTS_PHYSICAL;
        char **save;
 
        ftsoptions = FTS_PHYSICAL;
-       kvalue = listfiles = 0;
-       listdirs = 1;
+       kvalue = 0;
        save = argv;
        save = argv;
+       aflag = sflag = 0;
        while ((ch = getopt(argc, argv, "aksx")) != EOF)
                switch(ch) {
                case 'a':
        while ((ch = getopt(argc, argv, "aksx")) != EOF)
                switch(ch) {
                case 'a':
-                       listfiles = 1;
+                       aflag = 1;
                        break;
                case 'k':
                        kvalue = 1;
                        break;
                case 's':
                        break;
                case 'k':
                        kvalue = 1;
                        break;
                case 's':
-                       listfiles = listdirs = 0;
+                       sflag = 1;
                        break;
                case 'x':
                        ftsoptions |= FTS_XDEV;
                        break;
                case '?':
                default:
                        break;
                case 'x':
                        ftsoptions |= FTS_XDEV;
                        break;
                case '?':
                default:
-                       (void)fprintf(stderr,
-                           "usage: du [-aksx] [name ...]\n");
-                       exit(1);
+                       usage();
                }
        argv += optind;
 
                }
        argv += optind;
 
+       if (aflag) {
+               if (sflag)
+                       usage();
+               listdirs = listfiles = 1;
+       } else if (sflag)
+               listdirs = listfiles = 0;
+       else {
+               listfiles = 0;
+               listdirs = 1;
+       }
+
        if (!*argv) {
                argv = save;
                argv[0] = ".";
        if (!*argv) {
                argv = save;
                argv[0] = ".";
@@ -98,6 +133,12 @@ main(argc, argv)
                        (void)fprintf(stderr,
                            "du: %s: %s.\n", p->fts_path, strerror(errno));
                        break;
                        (void)fprintf(stderr,
                            "du: %s: %s.\n", p->fts_path, strerror(errno));
                        break;
+               case FTS_SL:
+                       if (p->fts_level == FTS_ROOTLEVEL) {
+                               (void)fts_set(fts, p, FTS_FOLLOW);
+                               break;
+                       }
+                       /* FALLTHROUGH */
                default:
                        if (p->fts_statb.st_nlink > 1 && linkchk(p))
                                break;
                default:
                        if (p->fts_statb.st_nlink > 1 && linkchk(p))
                                break;
@@ -145,3 +186,9 @@ linkchk(p)
        ++nfiles;
        return(0);
 }
        ++nfiles;
        return(0);
 }
+
+usage()
+{
+       (void)fprintf(stderr, "usage: du [-a | -s] [-kx] [file ...]\n");
+       exit(1);
+}