Pavel Roskin <proski@gnu.org> just found a nasty memory
[oweals/busybox.git] / dd.c
diff --git a/dd.c b/dd.c
index 0d5b3e8ab9867bc7918208da17b763275e4b72de..5d9993d8b1ef644a51ff6f95d0b444ee1859303f 100644 (file)
--- a/dd.c
+++ b/dd.c
@@ -2,13 +2,13 @@
 /*
  * Mini dd implementation for busybox
  *
- * Copyright (C) 1999 by Lineo, inc.
+ * Copyright (C) 1999, 2000 by Lineo, inc.
  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
- * based in part on code taken from sash. 
  *
- * Copyright (c) 1999 by David I. Bell
- * Permission is granted to use, distribute, or modify this source,
- * provided that this copyright notice remains intact.
+ * Based in part on code taken from sash. 
+ *   Copyright (c) 1999 by David I. Bell
+ *   Permission is granted to use, distribute, or modify this source,
+ *   provided that this copyright notice remains intact.
  *
  * Permission to distribute this code under the GPL has been granted.
  *
@@ -41,24 +41,26 @@ typedef unsigned long long int uintmax_t;
 #endif
 
 static const char dd_usage[] =
-       "dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n"
-       "Copy a file, converting and formatting according to options\n\n"
+       "dd [if=FILE] [of=FILE] [bs=N] [count=N] [skip=N] [seek=N]\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nCopy a file, converting and formatting according to options\n\n"
        "\tif=FILE\tread from FILE instead of stdin\n"
        "\tof=FILE\twrite to FILE instead of stdout\n"
-       "\tbs=n\tread and write n bytes at a time\n"
-       "\tcount=n\tcopy only n input blocks\n"
-       "\tskip=n\tskip n input blocks\n"
-       "\tseek=n\tskip n output blocks\n"
-
+       "\tbs=N\tread and write N bytes at a time\n"
+       "\tcount=N\tcopy only N input blocks\n"
+       "\tskip=N\tskip N input blocks\n"
+       "\tseek=N\tskip N output blocks\n"
        "\n"
-       "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
+       "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n"
+#endif
+       ;
 
 
 
 extern int dd_main(int argc, char **argv)
 {
-       const char *inFile = NULL;
-       const char *outFile = NULL;
+       char *inFile = NULL;
+       char *outFile = NULL;
        char *cp;
        int inFd;
        int outFd;
@@ -114,11 +116,7 @@ extern int dd_main(int argc, char **argv)
                argv++;
        }
 
-       buf = malloc(blockSize);
-       if (buf == NULL) {
-               fprintf(stderr, "Cannot allocate buffer\n");
-               exit(FALSE);
-       }
+       buf = xmalloc(blockSize);
 
        intotal = 0;
        outTotal = 0;
@@ -129,9 +127,12 @@ extern int dd_main(int argc, char **argv)
                inFd = open(inFile, 0);
 
        if (inFd < 0) {
-               perror(inFile);
-               free(buf);
-               exit(FALSE);
+               /* Note that we are not freeing buf or closing
+                * files here to save a few bytes. This exits
+                * here anyways... */
+
+               /* free(buf); */
+               fatalError( inFile);
        }
 
        if (outFile == NULL)
@@ -140,10 +141,13 @@ extern int dd_main(int argc, char **argv)
                outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
 
        if (outFd < 0) {
-               perror(outFile);
-               close(inFd);
-               free(buf);
-               exit(FALSE);
+               /* Note that we are not freeing buf or closing
+                * files here to save a few bytes. This exits
+                * here anyways... */
+
+               /* close(inFd);
+                  free(buf); */
+               fatalError( outFile);
        }
 
        lseek(inFd, skipBlocks * blockSize, SEEK_SET);
@@ -184,9 +188,13 @@ extern int dd_main(int argc, char **argv)
                perror(inFile);
 
   cleanup:
+       /* Note that we are not freeing memory or closing
+        * files here, to save a few bytes. */
+#if 0
        close(inFd);
        close(outFd);
        free(buf);
+#endif
 
        printf("%ld+%d records in\n", (long) (intotal / blockSize),
                   (intotal % blockSize) != 0);