BSD 4_4 release
[unix-history] / usr / src / bin / dd / dd.c
index 46fd43e..356b015 100644 (file)
@@ -1,36 +1,64 @@
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1991, 1993
+ *     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.
  *
  *
  * 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
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1991 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1991, 1993\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)dd.c       5.8 (Berkeley) %G%";
+static char sccsid[] = "@(#)dd.c       8.1 (Berkeley) 5/31/93";
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/mtio.h>
 #endif /* not lint */
 
 #include <sys/param.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/mtio.h>
-#include <signal.h>
-#include <fcntl.h>
-#include <unistd.h>
+
+#include <ctype.h>
 #include <errno.h>
 #include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+
 #include "dd.h"
 #include "extern.h"
 
 #include "dd.h"
 #include "extern.h"
 
@@ -40,16 +68,18 @@ static void setup __P((void));
 
 IO     in, out;                /* input/output state */
 STAT   st;                     /* statistics */
 
 IO     in, out;                /* input/output state */
 STAT   st;                     /* statistics */
-void   (*cfunc)();             /* conversion function */
+void   (*cfunc) __P((void));   /* conversion function */
 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_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 */
+int    errstats;               /* show statistics on error */
 u_char *ctab;                  /* conversion table */
 
 u_char *ctab;                  /* conversion table */
 
+int
 main(argc, argv)
        int argc;
 main(argc, argv)
        int argc;
-       char **argv;
+       char *argv[];
 {
        jcl(argv);
        setup();
 {
        jcl(argv);
        setup();
@@ -57,7 +87,7 @@ main(argc, argv)
        (void)signal(SIGINFO, summary);
        (void)signal(SIGINT, terminate);
 
        (void)signal(SIGINFO, summary);
        (void)signal(SIGINT, terminate);
 
-       while (files_cnt--)
+       for (errstats = 1; files_cnt--;)
                dd_in();
 
        dd_close();
                dd_in();
 
        dd_close();
@@ -85,7 +115,7 @@ setup()
                err("%s: %s", in.name, strerror(errno));
        if (S_ISCHR(sb.st_mode))
                in.flags |= ioctl(in.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
                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, 0L, SEEK_CUR) == -1 && errno == ESPIPE)
+       else if (lseek(in.fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
                in.flags |= ISPIPE;             /* XXX fixed in 4.4BSD */
 
        if (files_cnt > 1 && !(in.flags & ISTAPE))
                in.flags |= ISPIPE;             /* XXX fixed in 4.4BSD */
 
        if (files_cnt > 1 && !(in.flags & ISTAPE))
@@ -96,14 +126,16 @@ setup()
                out.fd = STDOUT_FILENO;
                out.name = "stdout";
        } else {
                out.fd = STDOUT_FILENO;
                out.name = "stdout";
        } else {
-               out.fd = open(out.name, O_RDWR|O_CREAT, DEFFILEMODE);
+#define        OFLAGS \
+    (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
+               out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE);
                /*
                 * May not have read access, so try again with write only.
                 * Without read we may have a problem if output also does
                 * not support seeks.
                 */
                if (out.fd < 0) {
                /*
                 * May not have read access, so try again with write only.
                 * Without read we may have a problem if output also does
                 * not support seeks.
                 */
                if (out.fd < 0) {
-                       out.fd = open(out.name, O_WRONLY|O_CREAT, DEFFILEMODE);
+                       out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE);
                        out.flags |= NOREAD;
                }
                if (out.fd < 0)
                        out.flags |= NOREAD;
                }
                if (out.fd < 0)
@@ -114,7 +146,7 @@ setup()
                err("%s: %s", out.name, strerror(errno));
        if (S_ISCHR(sb.st_mode))
                out.flags |= ioctl(out.fd, MTIOCGET, &mt) ? ISCHR : ISTAPE;
                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, 0L, SEEK_CUR) == -1 && errno == ESPIPE)
+       else if (lseek(out.fd, (off_t)0, SEEK_CUR) == -1 && errno == ESPIPE)
                out.flags |= ISPIPE;            /* XXX fixed in 4.4BSD */
 
        /*
                out.flags |= ISPIPE;            /* XXX fixed in 4.4BSD */
 
        /*
@@ -122,13 +154,7 @@ setup()
         * record oriented I/O, only need a single buffer.
         */
        if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
         * record oriented I/O, only need a single buffer.
         */
        if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
-               if (in.dbsz > out.dbsz)
-                       cnt = in.dbsz - 1 - in.dbsz / 2 + in.dbsz;
-               else if (in.dbsz < out.dbsz)
-                       cnt = out.dbsz + in.dbsz - 1;
-               else
-                       cnt = in.dbsz;
-               if ((in.db = malloc(cnt)) == NULL)
+               if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
                        err("%s", strerror(errno));
                out.db = in.db;
        } else if ((in.db =
                        err("%s", strerror(errno));
                out.db = in.db;
        } else if ((in.db =
@@ -144,13 +170,12 @@ setup()
        if (out.offset)
                pos_out();
 
        if (out.offset)
                pos_out();
 
-       /* Truncate the output file. */
-       if (ddflags & C_NOTRUNC) {
-               if (out.flags & ISTAPE)
-                       err("notrunc is not supported for tape devices");
-       } else if (S_ISREG(sb.st_mode) && ftruncate(out.fd,
-           (off_t)out.offset * out.dbsz))
-               err("%s: truncate: %s", out.name, strerror(errno));
+       /*
+        * Truncate the output file; ignore errors because it fails on some
+        * kinds of output files, tapes, for example.
+        */
+       if (ddflags & (C_OF | C_SEEK | C_NOTRUNC) == (C_OF | C_SEEK))
+               (void)ftruncate(out.fd, (off_t)out.offset * out.dbsz);
 
        /*
         * If converting case at the same time as another conversion, build a
 
        /*
         * If converting case at the same time as another conversion, build a
@@ -180,6 +205,7 @@ setup()
                        }
                else
                        ctab = ddflags & C_LCASE ? u2l : l2u;
                        }
                else
                        ctab = ddflags & C_LCASE ? u2l : l2u;
+       (void)time(&st.start);                  /* Statistics timestamp. */
 }
 
 static void
 }
 
 static void
@@ -187,7 +213,7 @@ dd_in()
 {
        register int flags, n;
 
 {
        register int flags, n;
 
-       for(flags = ddflags;;) {
+       for (flags = ddflags;;) {
                if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
                        return;
 
                if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
                        return;
 
@@ -200,7 +226,7 @@ dd_in()
                        if (flags & (C_BLOCK|C_UNBLOCK))
                                memset(in.dbp, ' ', in.dbsz);
                        else
                        if (flags & (C_BLOCK|C_UNBLOCK))
                                memset(in.dbp, ' ', in.dbsz);
                        else
-                               bzero(in.dbp, in.dbsz);
+                               memset(in.dbp, 0, in.dbsz);
 
                n = read(in.fd, in.dbp, in.dbsz);
                if (n == 0) {
 
                n = read(in.fd, in.dbp, in.dbsz);
                if (n == 0) {
@@ -243,7 +269,7 @@ dd_in()
                        ++st.in_full;
 
                /* Handle partial input blocks. */
                        ++st.in_full;
 
                /* Handle partial input blocks. */
-               } else if (n != in.dbsz) {
+               } else {
                        /* If sync, use the entire block. */
                        if (ddflags & C_SYNC)
                                in.dbcnt += in.dbrcnt = in.dbsz;
                        /* If sync, use the entire block. */
                        if (ddflags & C_SYNC)
                                in.dbcnt += in.dbrcnt = in.dbsz;
@@ -252,18 +278,10 @@ dd_in()
                        ++st.in_part;
                }
 
                        ++st.in_part;
                }
 
-               if (ddflags & C_SWAB) {
-                       if ((n = in.dbcnt) & 1) {
-                               ++st.swab;
-                               --n;
-                       }
-                       swab(in.dbp, in.dbp, n);
-               }
-
                /*
                 * POSIX states that if bs is set and no other conversions
                /*
                 * POSIX states that if bs is set and no other conversions
-                * are specified, the block is output without buffering as
-                * it is read.
+                * than noerror, notrunc or sync are specified, the block
+                * is output without buffering as it is read.
                 */
                if (ddflags & C_BS) {
                        out.dbcnt = in.dbcnt;
                 */
                if (ddflags & C_BS) {
                        out.dbcnt = in.dbcnt;
@@ -272,6 +290,14 @@ dd_in()
                        continue;
                }
 
                        continue;
                }
 
+               if (ddflags & C_SWAB) {
+                       if ((n = in.dbcnt) & 1) {
+                               ++st.swab;
+                               --n;
+                       }
+                       swab(in.dbp, in.dbp, n);
+               }
+
                in.dbp += in.dbrcnt;
                (*cfunc)();
        }
                in.dbp += in.dbrcnt;
                (*cfunc)();
        }
@@ -321,7 +347,17 @@ dd_out(force)
        outp = out.db;
        for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
                for (cnt = n;; cnt -= nw) {
        outp = out.db;
        for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
                for (cnt = n;; cnt -= nw) {
-                       outp += nw = write(out.fd, outp, cnt);
+                       nw = write(out.fd, outp, cnt);
+                       if (nw <= 0) {
+                               if (nw == 0)
+                                       err("%s: end of device", out.name);
+                               if (errno != EINTR)
+                                       err("%s: %s",
+                                           out.name, strerror(errno));
+                               nw = 0;
+                       }
+                       outp += nw;
+                       st.bytes += nw;
                        if (nw == n) {
                                if (n != out.dbsz)
                                        ++st.out_part;
                        if (nw == n) {
                                if (n != out.dbsz)
                                        ++st.out_part;
@@ -329,8 +365,6 @@ dd_out(force)
                                        ++st.out_full;
                                break;
                        }
                                        ++st.out_full;
                                break;
                        }
-                       if (nw < 0)
-                               err("%s: %s", out.name, strerror(errno));
                        ++st.out_part;
                        if (nw == cnt)
                                break;
                        ++st.out_part;
                        if (nw == cnt)
                                break;
@@ -348,6 +382,6 @@ dd_out(force)
 
        /* Reassemble the output block. */
        if (out.dbcnt)
 
        /* Reassemble the output block. */
        if (out.dbcnt)
-               bcopy(out.dbp - out.dbcnt, out.db, out.dbcnt);
+               memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
        out.dbp = out.db + out.dbcnt;
 }
        out.dbp = out.db + out.dbcnt;
 }