BSD 4_4_Lite1 release
[unix-history] / usr / src / bin / dd / dd.c
index c4bd89b..05ac7a9 100644 (file)
@@ -1,22 +1,48 @@
 /*-
 /*-
- * Copyright (c) 1991, 1993
+ * Copyright (c) 1991, 1993, 1994
  *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Keith Muller of the University of California, San Diego and Lance
  * Visser of Convex Computer Corporation.
  *
  *     The Regents of the University of California.  All rights reserved.
  *
  * This code is derived from software contributed to Berkeley by
  * Keith Muller of the University of California, San Diego and Lance
  * Visser of Convex Computer Corporation.
  *
- * %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
 static char copyright[] =
  */
 
 #ifndef lint
 static char copyright[] =
-"@(#) Copyright (c) 1991, 1993\n\
+"@(#) Copyright (c) 1991, 1993, 1994\n\
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
        The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)dd.c       8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)dd.c       8.5 (Berkeley) 4/2/94";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -25,6 +51,7 @@ static char sccsid[] = "@(#)dd.c      8.1 (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>
@@ -38,6 +65,7 @@ static char sccsid[] = "@(#)dd.c      8.1 (Berkeley) %G%";
 
 static void dd_close __P((void));
 static void dd_in __P((void));
 
 static void dd_close __P((void));
 static void dd_in __P((void));
+static void getfdtype __P((IO *));
 static void setup __P((void));
 
 IO     in, out;                /* input/output state */
 static void setup __P((void));
 
 IO     in, out;                /* input/output state */
@@ -47,7 +75,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 +85,22 @@ 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
 setup()
 {
        exit(0);
 }
 
 static void
 setup()
 {
-       register u_int cnt;
-       struct stat sb;
-       struct mtget mt;
+       u_int cnt;
 
        if (in.name == NULL) {
                in.name = "stdin";
 
        if (in.name == NULL) {
                in.name = "stdin";
@@ -82,18 +108,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 +134,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 +145,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;
 
@@ -182,10 +198,25 @@ setup()
        (void)time(&st.start);                  /* Statistics timestamp. */
 }
 
        (void)time(&st.start);                  /* Statistics timestamp. */
 }
 
+static void
+getfdtype(io)
+       IO *io;
+{
+       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
 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)
@@ -196,7 +227,7 @@ dd_in()
                 * lose the minimum amount of data.  If doing block operations
                 * use spaces.
                 */
                 * lose the minimum amount of data.  If doing block operations
                 * use spaces.
                 */
-               if (flags & (C_NOERROR|C_SYNC))
+               if ((flags & (C_NOERROR|C_SYNC)) == (C_NOERROR|C_SYNC))
                        if (flags & (C_BLOCK|C_UNBLOCK))
                                memset(in.dbp, ' ', in.dbsz);
                        else
                        if (flags & (C_BLOCK|C_UNBLOCK))
                                memset(in.dbp, ' ', in.dbsz);
                        else
@@ -215,9 +246,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 +258,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))
@@ -290,6 +321,10 @@ dd_close()
                block_close();
        else if (cfunc == unblock)
                unblock_close();
                block_close();
        else if (cfunc == unblock)
                unblock_close();
+       if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
+               memset(out.dbp, 0, out.dbsz - out.dbcnt);
+               out.dbcnt = out.dbsz;
+       }
        if (out.dbcnt)
                dd_out(1);
 }
        if (out.dbcnt)
                dd_out(1);
 }
@@ -299,8 +334,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
@@ -324,10 +359,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;
@@ -344,11 +378,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;