BSD 4_4 release
[unix-history] / usr / src / bin / dd / dd.c
index d199c90..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.12 (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,7 +68,7 @@ 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_long cpy_cnt;                /* # of blocks to copy */
 u_int  ddflags;                /* conversion options */
 u_int  cbsz;                   /* conversion block size */
@@ -48,9 +76,10 @@ u_int        files_cnt = 1;          /* # of files to copy */
 int    errstats;               /* show statistics on error */
 u_char *ctab;                  /* conversion table */
 
 int    errstats;               /* show statistics on error */
 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();
@@ -86,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))
@@ -117,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 */
 
        /*
@@ -184,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;
 
@@ -197,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) {
@@ -240,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;
@@ -318,7 +347,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)
+                                       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.bytes += nw;
                        if (nw == n) {
                                if (n != out.dbsz)
@@ -327,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;
@@ -346,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;
 }