tftp: optional tftp-hpa compat
[oweals/busybox.git] / coreutils / dd.c
index 39ce5019f82e41fcb9d6111aa7e6aad7b5a9206b..b5f3cbec5d4aa4297a97f18d9dc5fd99dabf073c 100644 (file)
@@ -7,7 +7,7 @@
  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  */
 //config:config DD
-//config:      bool "dd (7.1 kb)"
+//config:      bool "dd (7.5 kb)"
 //config:      default y
 //config:      help
 //config:      dd copies a file (from standard input to standard output,
@@ -59,7 +59,7 @@
 //usage:       "[if=FILE] [of=FILE] [" IF_FEATURE_DD_IBS_OBS("ibs=N obs=N/") "bs=N] [count=N] [skip=N] [seek=N]\n"
 //usage:       IF_FEATURE_DD_IBS_OBS(
 //usage:       "       [conv=notrunc|noerror|sync|fsync]\n"
-//usage:       "       [iflag=skip_bytes|fullblock] [oflag=seek_bytes]"
+//usage:       "       [iflag=skip_bytes|fullblock] [oflag=seek_bytes|append]"
 //usage:       )
 //usage:#define dd_full_usage "\n\n"
 //usage:       "Copy a file with converting and formatting\n"
@@ -84,6 +84,7 @@
 //usage:     "\n       iflag=skip_bytes        skip=N is in bytes"
 //usage:     "\n       iflag=fullblock Read full blocks"
 //usage:     "\n       oflag=seek_bytes        seek=N is in bytes"
+//usage:     "\n       oflag=append    Open output file in append mode"
 //usage:       )
 //usage:       IF_FEATURE_DD_STATUS(
 //usage:     "\n       status=noxfer   Suppress rate output"
@@ -140,11 +141,12 @@ enum {
        /* start of output flags */
        FLAG_OFLAG_SHIFT = 7,
        FLAG_SEEK_BYTES = (1 << 7) * ENABLE_FEATURE_DD_IBS_OBS,
+       FLAG_APPEND = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
        /* end of output flags */
-       FLAG_TWOBUFS = (1 << 8) * ENABLE_FEATURE_DD_IBS_OBS,
-       FLAG_COUNT   = 1 << 9,
-       FLAG_STATUS_NONE = 1 << 10,
-       FLAG_STATUS_NOXFER = 1 << 11,
+       FLAG_TWOBUFS = (1 << 9) * ENABLE_FEATURE_DD_IBS_OBS,
+       FLAG_COUNT   = 1 << 10,
+       FLAG_STATUS_NONE = 1 << 11,
+       FLAG_STATUS_NOXFER = 1 << 12,
 };
 
 static void dd_output_status(int UNUSED_PARAM cur_signal)
@@ -190,23 +192,15 @@ static void dd_output_status(int UNUSED_PARAM cur_signal)
 #endif
 }
 
-static ssize_t full_write_or_warn(const void *buf, size_t len,
-       const char *const filename)
-{
-       ssize_t n = full_write(ofd, buf, len);
-       if (n < 0)
-               bb_perror_msg("writing '%s'", filename);
-       return n;
-}
-
 static bool write_and_stats(const void *buf, size_t len, size_t obs,
        const char *filename)
 {
-       ssize_t n = full_write_or_warn(buf, len, filename);
-       if (n < 0)
-               return 1;
+       ssize_t n;
+
+       n = full_write(ofd, buf, len);
 #if ENABLE_FEATURE_DD_THIRD_STATUS_LINE
-       G.total_bytes += n;
+       if (n > 0)
+               G.total_bytes += n;
 #endif
        if ((size_t)n == obs) {
                G.out_full++;
@@ -216,6 +210,14 @@ static bool write_and_stats(const void *buf, size_t len, size_t obs,
                G.out_part++;
                return 0;
        }
+       /* n is < len (and possibly is -1).
+        * Even if n >= 0, errno is usually set correctly.
+        * For example, if writing to block device and getting ENOSPC,
+        * full_write() first sees a short write, then tries to write
+        * the remainder and gets errno set to ENOSPC.
+        * It returns n > 0 (the amount which it did write).
+        */
+       bb_perror_msg("error writing '%s'", filename);
        return 1;
 }
 
@@ -267,7 +269,7 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
        static const char iflag_words[] ALIGN1 =
                "skip_bytes\0""fullblock\0";
        static const char oflag_words[] ALIGN1 =
-               "seek_bytes\0";
+               "seek_bytes\0append\0";
 #endif
 #if ENABLE_FEATURE_DD_STATUS
        static const char status_words[] ALIGN1 =
@@ -451,6 +453,8 @@ int dd_main(int argc UNUSED_PARAM, char **argv)
 
                if (!seek && !(G.flags & FLAG_NOTRUNC))
                        oflag |= O_TRUNC;
+               if (G.flags & FLAG_APPEND)
+                       oflag |= O_APPEND;
 
                xmove_fd(xopen(outfile, oflag), ofd);