BSD 4_4_Lite1 release
[unix-history] / usr / src / bin / dd / dd.c
index 816ea71..05ac7a9 100644 (file)
 /*-
 /*-
- * Copyright (c) 1991 The Regents of the University of California.
- * All rights reserved.
+ * 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.
  *
  *
  * 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, 1994\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.9 (Berkeley) %G%";
+static char sccsid[] = "@(#)dd.c       8.5 (Berkeley) 4/2/94";
 #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 <err.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"
 
 static void dd_close __P((void));
 static void dd_in __P((void));
 #include "dd.h"
 #include "extern.h"
 
 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 */
 STAT   st;                     /* statistics */
 static void setup __P((void));
 
 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_char *ctab;                  /* conversion table */
 
 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_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();
 
-       (void)signal(SIGINFO, summary);
+       (void)signal(SIGINFO, summaryx);
        (void)signal(SIGINT, terminate);
 
        (void)signal(SIGINT, terminate);
 
+       atexit(summary);
+
        while (files_cnt--)
                dd_in();
 
        dd_close();
        while (files_cnt--)
                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";
@@ -78,63 +108,49 @@ 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, 0L, 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. */
                out.fd = STDOUT_FILENO;
                out.name = "stdout";
        } else {
 
        if (out.name == NULL) {
                /* No way to check for read access here. */
                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)
-                       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, 0L, 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
         * record oriented I/O, only need a single buffer.
         */
        if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
 
        /*
         * Allocate space for the input and output buffers.  If not doing
         * 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)
-                       err("%s", strerror(errno));
+               if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL)
+                       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;
 
@@ -144,13 +160,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,14 +195,30 @@ setup()
                        }
                else
                        ctab = ddflags & C_LCASE ? u2l : l2u;
                        }
                else
                        ctab = ddflags & C_LCASE ? u2l : l2u;
+       (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;;) {
+       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;
 
@@ -196,11 +227,11 @@ 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
-                               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) {
@@ -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))
@@ -243,7 +274,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;
@@ -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
@@ -321,7 +356,16 @@ 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)
+                                       errx(1, "%s: end of device", out.name);
+                               if (errno != EINTR)
+                                       err(1, "%s", out.name);
+                               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,18 +373,16 @@ 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;
                        if (out.flags & ISCHR && !warned) {
                                warned = 1;
                        ++st.out_part;
                        if (nw == cnt)
                                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;
@@ -348,6 +390,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;
 }