aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/dd.c
diff options
context:
space:
mode:
Diffstat (limited to 'toys/pending/dd.c')
-rw-r--r--toys/pending/dd.c152
1 files changed, 96 insertions, 56 deletions
diff --git a/toys/pending/dd.c b/toys/pending/dd.c
index 0c447f74..80a7595f 100644
--- a/toys/pending/dd.c
+++ b/toys/pending/dd.c
@@ -4,8 +4,6 @@
* Copyright 2013 Kyungwan Han <asura321@gmail.com>
*
* See http://opengroup.org/onlinepubs/9699919799/utilities/dd.html
- *
- * todo: ctrl-c doesn't work, the read() is restarting.
USE_DD(NEWTOY(dd, 0, TOYFLAG_USR|TOYFLAG_BIN))
@@ -13,19 +11,22 @@ config DD
bool "dd"
default n
help
- usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N] [skip=N]
- [seek=N] [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
+ usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [iflag=FLAGS] [oflag=FLAGS]
+ [bs=N] [count=N] [seek=N] [skip=N]
+ [conv=notrunc|noerror|sync|fsync] [status=noxfer|none]
Copy/convert files.
if=FILE Read from FILE instead of stdin
of=FILE Write to FILE instead of stdout
bs=N Read and write N bytes at a time
- ibs=N Read N bytes at a time
- obs=N Write N bytes at a time
+ ibs=N Input block size
+ obs=N Output block size
count=N Copy only N input blocks
skip=N Skip N input blocks
seek=N Skip N output blocks
+ iflag=FLAGS Set input flags
+ oflag=FLAGS Set output flags
conv=notrunc Don't truncate output file
conv=noerror Continue after read errors
conv=sync Pad blocks with zeros
@@ -33,6 +34,12 @@ config DD
status=noxfer Don't show transfer rate
status=none Don't show transfer rate or records in/out
+ FLAGS is a comma-separated list of:
+
+ count_bytes (iflag) interpret count=N in bytes, not blocks
+ seek_bytes (oflag) interpret seek=N in bytes, not blocks
+ skip_bytes (iflag) interpret skip=N in bytes, not blocks
+
Numbers may be suffixed by c (*1), w (*2), b (*512), kD (*1000), k (*1024),
MD (*1000*1000), M (*1024*1024), GD (*1000*1000*1000) or G (*1024*1024*1024).
*/
@@ -51,12 +58,24 @@ GLOBALS(
long sz, count;
unsigned long long offset;
} in, out;
+ unsigned conv, iflag, oflag;
);
-#define C_FSYNC 1
-#define C_NOERROR 2
-#define C_NOTRUNC 4
-#define C_SYNC 8
+struct dd_flag {
+ char *name;
+};
+
+static const struct dd_flag dd_conv[] = TAGGED_ARRAY(DD_conv,
+ {"fsync"}, {"noerror"}, {"notrunc"}, {"sync"},
+);
+
+static const struct dd_flag dd_iflag[] = TAGGED_ARRAY(DD_iflag,
+ {"count_bytes"}, {"skip_bytes"},
+);
+
+static const struct dd_flag dd_oflag[] = TAGGED_ARRAY(DD_oflag,
+ {"seek_bytes"},
+);
static void status()
{
@@ -79,6 +98,12 @@ static void status()
}
}
+static void dd_sigint(int sig) {
+ status();
+ toys.exitval = sig|128;
+ xexit();
+}
+
static void write_out(int all)
{
TT.out.bp = TT.out.buff;
@@ -97,18 +122,24 @@ static void write_out(int all)
if (TT.out.count) memmove(TT.out.buff, TT.out.bp, TT.out.count); //move remainder to front
}
-int strstarteq(char **a, char *b)
+static void parse_flags(char *what, char *arg,
+ const struct dd_flag* flags, int flag_count, unsigned *result)
{
- char *aa = *a;
+ char *pre = xstrdup(arg);
+ int i;
- return strstart(&aa, b) && *aa == '=' && (*a = aa+1);
+ for (i=0; i<flag_count; ++i) {
+ while (comma_remove(pre, flags[i].name)) *result |= 1<<i;
+ }
+ if (*pre) error_exit("bad %s=%s", what, pre);
+ free(pre);
}
void dd_main()
{
char **args;
unsigned long long bs = 0;
- int trunc = O_TRUNC, conv = 0;
+ int trunc = O_TRUNC;
TT.show_xfer = TT.show_records = 1;
TT.c_count = ULLONG_MAX;
@@ -117,51 +148,46 @@ void dd_main()
for (args = toys.optargs; *args; args++) {
char *arg = *args;
- if (strstarteq(&arg, "bs")) bs = atolx_range(arg, 1, LONG_MAX);
- else if (strstarteq(&arg, "ibs")) TT.in.sz = atolx_range(arg, 1, LONG_MAX);
- else if (strstarteq(&arg, "obs")) TT.out.sz = atolx_range(arg, 1, LONG_MAX);
- else if (strstarteq(&arg, "count"))
+ if (strstart(&arg, "bs=")) bs = atolx_range(arg, 1, LONG_MAX);
+ else if (strstart(&arg, "ibs=")) TT.in.sz = atolx_range(arg, 1, LONG_MAX);
+ else if (strstart(&arg, "obs=")) TT.out.sz = atolx_range(arg, 1, LONG_MAX);
+ else if (strstart(&arg, "count="))
TT.c_count = atolx_range(arg, 0, LLONG_MAX);
- else if (strstarteq(&arg, "if")) TT.in.name = arg;
- else if (strstarteq(&arg, "of")) TT.out.name = arg;
- else if (strstarteq(&arg, "seek"))
+ else if (strstart(&arg, "if=")) TT.in.name = arg;
+ else if (strstart(&arg, "of=")) TT.out.name = arg;
+ else if (strstart(&arg, "seek="))
TT.out.offset = atolx_range(arg, 0, LLONG_MAX);
- else if (strstarteq(&arg, "skip"))
+ else if (strstart(&arg, "skip="))
TT.in.offset = atolx_range(arg, 0, LLONG_MAX);
- else if (strstarteq(&arg, "status")) {
+ else if (strstart(&arg, "status=")) {
if (!strcmp(arg, "noxfer")) TT.show_xfer = 0;
else if (!strcmp(arg, "none")) TT.show_xfer = TT.show_records = 0;
else error_exit("unknown status '%s'", arg);
- } else if (strstarteq(&arg, "conv")) {
- char *ss, *convs[] = {"fsync", "noerror", "notrunc", "sync"};
- int i, len;
-
- while ((ss = comma_iterate(&arg, &len))) {
- for (i = 0; i<ARRAY_LEN(convs); i++)
- if (len == strlen(convs[i]) && !strncmp(ss, convs[i], len)) break;
- if (i == ARRAY_LEN(convs)) error_exit("bad conv=%.*s", len, ss);
- conv |= 1<<i;
- }
- } else error_exit("bad arg %s", arg);
+ } else if (strstart(&arg, "conv=")) {
+ parse_flags("conv", arg, dd_conv, ARRAY_LEN(dd_conv), &TT.conv);
+ fprintf(stderr, "conv=%x\n", TT.conv);
+ } else if (strstart(&arg, "iflag="))
+ parse_flags("iflag", arg, dd_iflag, ARRAY_LEN(dd_iflag), &TT.iflag);
+ else if (strstart(&arg, "oflag="))
+ parse_flags("oflag", arg, dd_oflag, ARRAY_LEN(dd_oflag), &TT.oflag);
+ else error_exit("bad arg %s", arg);
}
if (bs) TT.in.sz = TT.out.sz = bs;
- signal(SIGINT, generic_signal);
+ signal(SIGINT, dd_sigint);
signal(SIGUSR1, generic_signal);
gettimeofday(&TT.start, NULL);
- /* for bs=, in/out is done as it is. so only in.sz is enough.
- * With Single buffer there will be overflow in a read following partial read
- */
+ // For bs=, in/out is done as it is. so only in.sz is enough.
+ // With Single buffer there will be overflow in a read following partial read.
TT.in.buff = TT.out.buff = xmalloc(TT.in.sz + (bs ? 0 : TT.out.sz));
TT.in.bp = TT.out.bp = TT.in.buff;
- //setup input
+
if (!TT.in.name) TT.in.name = "stdin";
else TT.in.fd = xopenro(TT.in.name);
- if (conv&C_NOTRUNC) trunc = 0;
+ if (TT.conv & _DD_conv_notrunc) trunc = 0;
- //setup output
if (!TT.out.name) {
TT.out.name = "stdout";
TT.out.fd = 1;
@@ -170,30 +196,43 @@ void dd_main()
// Implement skip=
if (TT.in.offset) {
- if (lseek(TT.in.fd, (off_t)(TT.in.offset * TT.in.sz), SEEK_CUR) < 0) {
- while (TT.in.offset--) {
- ssize_t n = read(TT.in.fd, TT.in.bp, TT.in.sz);
+ off_t off = TT.in.offset;
+
+ if (!(TT.iflag & _DD_iflag_skip_bytes)) off *= TT.in.sz;
+ if (lseek(TT.in.fd, off, SEEK_CUR) < 0) {
+ while (off > 0) {
+ int chunk = off < TT.in.sz ? off : TT.in.sz;
+ ssize_t n = read(TT.in.fd, TT.in.bp, chunk);
if (n < 0) {
perror_msg("%s", TT.in.name);
- if (conv&C_NOERROR) status();
+ if (TT.conv & _DD_conv_noerror) status();
else return;
} else if (!n) {
xprintf("%s: Can't skip\n", TT.in.name);
return;
}
+ off -= chunk;
}
}
}
- // seek/truncate as necessary. We handled position zero truncate with
- // O_TRUNC on open, so output to /dev/null and such doesn't error.
- if ((bs = TT.out.offset*TT.out.sz)) {
+ // Implement seek= and truncate as necessary. We handled position zero
+ // truncate with O_TRUNC on open, so output to /dev/null and such doesn't
+ // error.
+ bs = TT.out.offset;
+ if (!(TT.oflag & _DD_oflag_seek_bytes)) bs *= TT.out.sz;
+ if (bs) {
xlseek(TT.out.fd, bs, SEEK_CUR);
if (trunc && ftruncate(TT.out.fd, bs)) perror_exit("ftruncate");
}
- while (TT.c_count==ULLONG_MAX || (TT.in_full + TT.in_part) < TT.c_count) {
+ unsigned long long bytes_left = TT.c_count;
+ if (TT.c_count != ULLONG_MAX && !(TT.iflag & _DD_iflag_count_bytes)) {
+ bytes_left *= TT.in.sz;
+ }
+ while (bytes_left) {
+ int chunk = bytes_left < TT.in.sz ? bytes_left : TT.in.sz;
ssize_t n;
// Show progress and exit on SIGINT or just continue on SIGUSR1.
@@ -204,16 +243,16 @@ void dd_main()
}
TT.in.bp = TT.in.buff + TT.in.count;
- if (conv&C_SYNC) memset(TT.in.bp, 0, TT.in.sz);
- if (!(n = read(TT.in.fd, TT.in.bp, TT.in.sz))) break;
- if (n < 0) {
+ if (TT.conv & _DD_conv_sync) memset(TT.in.bp, 0, TT.in.sz);
+ if (!(n = read(TT.in.fd, TT.in.bp, chunk))) break;
+ if (n < 0) {
if (errno == EINTR) continue;
//read error case.
perror_msg("%s: read error", TT.in.name);
- if (!(conv&C_NOERROR)) exit(1);
+ if (!(TT.conv & _DD_conv_noerror)) exit(1);
status();
xlseek(TT.in.fd, TT.in.sz, SEEK_CUR);
- if (!(conv&C_SYNC)) continue;
+ if (!(TT.conv & _DD_conv_sync)) continue;
// if SYNC, then treat as full block of nuls
n = TT.in.sz;
}
@@ -222,9 +261,10 @@ void dd_main()
TT.in.count += n;
} else {
TT.in_part++;
- if (conv&C_SYNC) TT.in.count += TT.in.sz;
+ if (TT.conv & _DD_conv_sync) TT.in.count += TT.in.sz;
else TT.in.count += n;
}
+ bytes_left -= n;
TT.out.count = TT.in.count;
if (bs) {
@@ -239,7 +279,7 @@ void dd_main()
}
}
if (TT.out.count) write_out(1); //write any remaining input blocks
- if ((conv&C_FSYNC) && fsync(TT.out.fd)<0)
+ if ((TT.conv & _DD_conv_fsync) && fsync(TT.out.fd)<0)
perror_exit("%s: fsync", TT.out.name);
close(TT.in.fd);