use library versions of err/warn
[unix-history] / usr / src / usr.bin / du / du.c
index 056ce59..2cece57 100644 (file)
@@ -15,22 +15,25 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)du.c       5.14 (Berkeley) %G%";
+static char sccsid[] = "@(#)du.c       5.20 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
-#include <sys/errno.h>
+
 #include <dirent.h>
 #include <dirent.h>
-#include <stdio.h>
+#include <err.h>
+#include <errno.h>
 #include <fts.h>
 #include <fts.h>
-#include <string.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
+#include <string.h>
 
 
-char   *getbsize __P((char *, int *, int *));
+char   *getbsize __P((char *, int *, long *));
 int     linkchk __P((FTSENT *));
 void    usage __P((void));
 
 int     linkchk __P((FTSENT *));
 void    usage __P((void));
 
+int
 main(argc, argv)
        int argc;
        char *argv[];
 main(argc, argv)
        int argc;
        char *argv[];
@@ -38,19 +41,24 @@ main(argc, argv)
        register FTS *fts;
        register FTSENT *p;
        register int listdirs, listfiles;
        register FTS *fts;
        register FTSENT *p;
        register int listdirs, listfiles;
-       int aflag, blocksize, ch, ftsoptions, notused, sflag;
+       long blocksize;
+       int aflag, ch, ftsoptions, notused, sflag;
        char **save;
 
        ftsoptions = FTS_PHYSICAL;
        save = argv;
        aflag = sflag = 0;
        char **save;
 
        ftsoptions = FTS_PHYSICAL;
        save = argv;
        aflag = sflag = 0;
-       while ((ch = getopt(argc, argv, "aksx")) != EOF)
+       while ((ch = getopt(argc, argv, "Hahsx")) != EOF)
                switch(ch) {
                switch(ch) {
+               case 'H':
+                       ftsoptions |= FTS_COMFOLLOW;
+                       break;
                case 'a':
                        aflag = 1;
                        break;
                case 'a':
                        aflag = 1;
                        break;
-               case 'k':               /* Delete before 4.4BSD. */
-                       (void)fprintf(stderr, "du: -k no longer supported\n");
+               case 'h':
+                       ftsoptions &= ~FTS_PHYSICAL;
+                       ftsoptions |= FTS_LOGICAL;
                        break;
                case 's':
                        sflag = 1;
                        break;
                case 's':
                        sflag = 1;
@@ -84,10 +92,8 @@ main(argc, argv)
        (void)getbsize("du", &notused, &blocksize);
        blocksize /= 512;
 
        (void)getbsize("du", &notused, &blocksize);
        blocksize /= 512;
 
-       if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL) {
-               (void)fprintf(stderr, "du: %s\n", strerror(errno));
-               exit(1);
-       }
+       if ((fts = fts_open(argv, ftsoptions, NULL)) == NULL)
+               err(1, "");
 
        while (p = fts_read(fts))
                switch(p->fts_info) {
 
        while (p = fts_read(fts))
                switch(p->fts_info) {
@@ -109,15 +115,8 @@ main(argc, argv)
                case FTS_DNR:
                case FTS_ERR:
                case FTS_NS:
                case FTS_DNR:
                case FTS_ERR:
                case FTS_NS:
-                       (void)fprintf(stderr,
-                           "du: %s: %s\n", p->fts_path, strerror(errno));
+                       warn("%s", p->fts_path);
                        break;
                        break;
-               case FTS_SL:
-                       if (p->fts_level == FTS_ROOTLEVEL) {
-                               (void)fts_set(fts, p, FTS_FOLLOW);
-                               break;
-                       }
-                       /* FALLTHROUGH */
                default:
                        if (p->fts_statp->st_nlink > 1 && linkchk(p))
                                break;
                default:
                        if (p->fts_statp->st_nlink > 1 && linkchk(p))
                                break;
@@ -126,11 +125,13 @@ main(argc, argv)
                         * the root of a traversal, display the total.
                         */
                        if (listfiles || !p->fts_level)
                         * the root of a traversal, display the total.
                         */
                        if (listfiles || !p->fts_level)
-                               (void)printf("%ld\t%s\n",
+                               (void)printf("%qd\t%s\n",
                                    howmany(p->fts_statp->st_blocks, blocksize),
                                    p->fts_path);
                        p->fts_parent->fts_number += p->fts_statp->st_blocks;
                }
                                    howmany(p->fts_statp->st_blocks, blocksize),
                                    p->fts_path);
                        p->fts_parent->fts_number += p->fts_statp->st_blocks;
                }
+       if (errno)
+               err(1, "");
        exit(0);
 }
 
        exit(0);
 }
 
@@ -157,10 +158,8 @@ linkchk(p)
                                return(1);
 
        if (nfiles == maxfiles && (files = realloc((char *)files,
                                return(1);
 
        if (nfiles == maxfiles && (files = realloc((char *)files,
-           (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL) {
-               (void)fprintf(stderr, "du: %s\n", strerror(errno));
-               exit(1);
-       }
+           (u_int)(sizeof(ID) * (maxfiles += 128)))) == NULL)
+               err(1, "");
        files[nfiles].inode = ino;
        files[nfiles].dev = dev;
        ++nfiles;
        files[nfiles].inode = ino;
        files[nfiles].dev = dev;
        ++nfiles;
@@ -170,6 +169,6 @@ linkchk(p)
 void
 usage()
 {
 void
 usage()
 {
-       (void)fprintf(stderr, "usage: du [-a | -s] [-x] [file ...]\n");
+       (void)fprintf(stderr, "usage: du [-a | -s] [-Hhx] [file ...]\n");
        exit(1);
 }
        exit(1);
 }