prettyness police
[unix-history] / usr / src / bin / dd / dd.c
index 01d0675..ed7ba8b 100644 (file)
@@ -16,7 +16,7 @@ static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)dd.c       8.2 (Berkeley) %G%";
+static char sccsid[] = "@(#)dd.c       8.3 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -25,6 +25,7 @@ static char sccsid[] = "@(#)dd.c      8.2 (Berkeley) %G%";
 #include <sys/mtio.h>
 
 #include <ctype.h>
 #include <sys/mtio.h>
 
 #include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <signal.h>
@@ -47,7 +48,6 @@ u_long        cpy_cnt;                /* # of blocks to copy */
 u_int  ddflags;                /* conversion options */
 u_int  cbsz;                   /* conversion block size */
 u_int  files_cnt = 1;          /* # of files to copy */
 u_int  ddflags;                /* conversion options */
 u_int  cbsz;                   /* conversion block size */
 u_int  files_cnt = 1;          /* # of files to copy */
-int    errstats;               /* show statistics on error */
 u_char *ctab;                  /* conversion table */
 
 int
 u_char *ctab;                  /* conversion table */
 
 int
@@ -58,23 +58,37 @@ main(argc, argv)
        jcl(argv);
        setup();
 
        jcl(argv);
        setup();
 
-       (void)signal(SIGINFO, summary);
+       (void)signal(SIGINFO, summaryx);
        (void)signal(SIGINT, terminate);
 
        (void)signal(SIGINT, terminate);
 
-       for (errstats = 1; files_cnt--;)
+       atexit(summary);
+
+       while (files_cnt--)
                dd_in();
 
        dd_close();
                dd_in();
 
        dd_close();
-       summary(0);
        exit(0);
 }
 
 static void
        exit(0);
 }
 
 static void
-setup()
+getfdtype(io)
+       IO *io;
 {
 {
-       register u_int cnt;
-       struct stat sb;
        struct mtget mt;
        struct mtget mt;
+       struct stat sb;
+
+       if (fstat(io->fd, &sb))
+               err(1, "%s", io->name);
+       if (S_ISCHR(sb.st_mode))
+               io->flags |= ioctl(io->fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
+       else if (lseek(io->fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
+               io->flags |= ISPIPE;            /* XXX fixed in 4.4BSD */
+}
+
+static void
+setup()
+{
+       u_int cnt;
 
        if (in.name == NULL) {
                in.name = "stdin";
 
        if (in.name == NULL) {
                in.name = "stdin";
@@ -82,18 +96,13 @@ setup()
        } else {
                in.fd = open(in.name, O_RDONLY, 0);
                if (in.fd < 0)
        } else {
                in.fd = open(in.name, O_RDONLY, 0);
                if (in.fd < 0)
-                       err("%s: %s", in.name, strerror(errno));
+                       err(1, "%s", in.name);
        }
 
        }
 
-       if (fstat(in.fd, &sb))
-               err("%s: %s", in.name, strerror(errno));
-       if (S_ISCHR(sb.st_mode))
-               in.flags |= ioctl(in.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
-       else if (lseek(in.fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
-               in.flags |= ISPIPE;             /* XXX fixed in 4.4BSD */
+       getfdtype(&in);
 
        if (files_cnt > 1 && !(in.flags & ISTAPE))
 
        if (files_cnt > 1 && !(in.flags & ISTAPE))
-               err("files is not supported for non-tape devices");
+               errx(1, "files is not supported for non-tape devices");
 
        if (out.name == NULL) {
                /* No way to check for read access here. */
 
        if (out.name == NULL) {
                /* No way to check for read access here. */
@@ -113,15 +122,10 @@ setup()
                        out.flags |= NOREAD;
                }
                if (out.fd < 0)
                        out.flags |= NOREAD;
                }
                if (out.fd < 0)
-                       err("%s: %s", out.name, strerror(errno));
+                       err(1, "%s", out.name);
        }
 
        }
 
-       if (fstat(out.fd, &sb))
-               err("%s: %s", out.name, strerror(errno));
-       if (S_ISCHR(sb.st_mode))
-               out.flags |= ioctl(out.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
-       else if (lseek(out.fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
-               out.flags |= ISPIPE;            /* XXX fixed in 4.4BSD */
+       getfdtype(&out);
 
        /*
         * Allocate space for the input and output buffers.  If not doing
 
        /*
         * Allocate space for the input and output buffers.  If not doing
@@ -129,12 +133,12 @@ setup()
         */
        if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
                if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
         */
        if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
                if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
-                       err("%s", strerror(errno));
+                       err(1, NULL);
                out.db = in.db;
        } else if ((in.db =
            malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
            (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
                out.db = in.db;
        } else if ((in.db =
            malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
            (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL)
-               err("%s", strerror(errno));
+               err(1, NULL);
        in.dbp = in.db;
        out.dbp = out.db;
 
        in.dbp = in.db;
        out.dbp = out.db;
 
@@ -185,7 +189,7 @@ setup()
 static void
 dd_in()
 {
 static void
 dd_in()
 {
-       register int flags, n;
+       int flags, n;
 
        for (flags = ddflags;;) {
                if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
 
        for (flags = ddflags;;) {
                if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
@@ -215,9 +219,9 @@ dd_in()
                         * the warning message be followed by an I/O display.
                         */
                        if (!(flags & C_NOERROR))
                         * the warning message be followed by an I/O display.
                         */
                        if (!(flags & C_NOERROR))
-                               err("%s: %s", in.name, strerror(errno));
-                       warn("%s: %s", in.name, strerror(errno));
-                       summary(0);
+                               err(1, "%s", in.name);
+                       warn("%s", in.name);
+                       summary();
 
                        /*
                         * If it's not a tape drive or a pipe, seek past the
 
                        /*
                         * If it's not a tape drive or a pipe, seek past the
@@ -227,7 +231,7 @@ dd_in()
                         */
                        if (!(in.flags & (ISPIPE|ISTAPE)) &&
                            lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
                         */
                        if (!(in.flags & (ISPIPE|ISTAPE)) &&
                            lseek(in.fd, (off_t)in.dbsz, SEEK_CUR))
-                               warn("%s: %s", in.name, strerror(errno));
+                               warn("%s", in.name);
 
                        /* If sync not specified, omit block and continue. */
                        if (!(ddflags & C_SYNC))
 
                        /* If sync not specified, omit block and continue. */
                        if (!(ddflags & C_SYNC))
@@ -291,7 +295,7 @@ dd_close()
        else if (cfunc == unblock)
                unblock_close();
        if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
        else if (cfunc == unblock)
                unblock_close();
        if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
-               bzero(out.dbp, out.dbsz - out.dbcnt);
+               memset(out.dbp, 0, out.dbsz - out.dbcnt);
                out.dbcnt = out.dbsz;
        }
        if (out.dbcnt)
                out.dbcnt = out.dbsz;
        }
        if (out.dbcnt)
@@ -303,8 +307,8 @@ dd_out(force)
        int force;
 {
        static int warned;
        int force;
 {
        static int warned;
-       register int cnt, n, nw;
-       register u_char *outp;
+       int cnt, n, nw;
+       u_char *outp;
 
        /*
         * Write one or more blocks out.  The common case is writing a full
 
        /*
         * Write one or more blocks out.  The common case is writing a full
@@ -328,10 +332,9 @@ dd_out(force)
                        nw = write(out.fd, outp, cnt);
                        if (nw <= 0) {
                                if (nw == 0)
                        nw = write(out.fd, outp, cnt);
                        if (nw <= 0) {
                                if (nw == 0)
-                                       err("%s: end of device", out.name);
+                                       errx(1, "%s: end of device", out.name);
                                if (errno != EINTR)
                                if (errno != EINTR)
-                                       err("%s: %s",
-                                           out.name, strerror(errno));
+                                       err(1, "%s", out.name);
                                nw = 0;
                        }
                        outp += nw;
                                nw = 0;
                        }
                        outp += nw;
@@ -348,11 +351,11 @@ dd_out(force)
                                break;
                        if (out.flags & ISCHR && !warned) {
                                warned = 1;
                                break;
                        if (out.flags & ISCHR && !warned) {
                                warned = 1;
-                               warn("%s: short write on character device",
+                               warnx("%s: short write on character device",
                                    out.name);
                        }
                        if (out.flags & ISTAPE)
                                    out.name);
                        }
                        if (out.flags & ISTAPE)
-                               err("%s: short write on tape device", out.name);
+                               errx(1, "%s: short write on tape device", out.name);
                }
                if ((out.dbcnt -= n) < out.dbsz)
                        break;
                }
                if ((out.dbcnt -= n) < out.dbsz)
                        break;