annotate toys/pending/dd.c @ 1276:d48bdc1cb017 draft

Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
author Rob Landley <rob@landley.net>
date Tue, 06 May 2014 06:31:28 -0500
parents b2a8f64a564b
children 8f59893165fa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1116
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* dd.c - program to convert and copy a file.
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * Copyright 2013 Ashwini Kumar <ak.ashwini@gmail.com>
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 * Copyright 2013 Kyungwan Han <asura321@gmail.com>
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 *
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 * See http://opengroup.org/onlinepubs/9699919799/utilities/dd.html
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 USE_DD(NEWTOY(dd, NULL, TOYFLAG_USR|TOYFLAG_BIN))
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 config DD
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
10 bool "dd"
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 default n
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 help
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 usage: dd [if=FILE] [of=FILE] [ibs=N] [obs=N] [bs=N] [count=N] [skip=N]
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 [seek=N] [conv=notrunc|noerror|sync|fsync]
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
15
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Options:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 if=FILE Read from FILE instead of stdin
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 of=FILE Write to FILE instead of stdout
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 bs=N Read and write N bytes at a time
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 ibs=N Read N bytes at a time
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
21 obs=N Write N bytes at a time
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 count=N Copy only N input blocks
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
23 skip=N Skip N input blocks
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 seek=N Skip N output blocks
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 conv=notrunc Don't truncate output file
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 conv=noerror Continue after read errors
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 conv=sync Pad blocks with zeros
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 conv=fsync Physically write data out before finishing
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
29
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 Numbers may be suffixed by c (x1), w (x2), b (x512), kD (x1000), k (x1024),
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 MD (x1000000), M (x1048576), GD (x1000000000) or G (x1073741824)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 Copy a file, converting and formatting according to the operands.
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 */
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 #define FOR_dd
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 #include "toys.h"
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
36
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 GLOBALS(
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 int sig;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 )
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 #define C_CONV 0x0000
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 #define C_BS 0x0001
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 #define C_COUNT 0x0002
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
43 #define C_IBS 0x0004
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
44 #define C_OBS 0x0008
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
45 #define C_IF 0x0010
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
46 #define C_OF 0x0020
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
47 #define C_SEEK 0x0040
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
48 #define C_SKIP 0x0080
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
49 #define C_SYNC 0x0100
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
50 #define C_FSYNC 0x0200
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
51 #define C_NOERROR 0x0400
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
52 #define C_NOTRUNC 0x0800
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
53
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
54 struct io {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
55 char *name;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
56 int fd;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
57 unsigned char *buff, *bp;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
58 long sz, count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
59 unsigned long long offset;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
60 };
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
61
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
62 struct iostat {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
63 unsigned long long in_full, in_part, out_full, out_part, bytes;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
64 struct timeval start;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
65 };
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
66
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
67 struct pair {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
68 char *name;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
69 unsigned val;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
70 };
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
71
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
72 static struct pair suffixes[] = {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
73 { "c", 1 }, { "w", 2 }, { "b", 512 },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
74 { "kD", 1000 }, { "k", 1024 }, { "K", 1024 },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
75 { "MD", 1000000 }, { "M", 1048576 },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
76 { "GD", 1000000000 }, { "G", 1073741824 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
77 };
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
78
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
79 static struct pair clist[] = {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
80 { "fsync", C_FSYNC },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
81 { "noerror", C_NOERROR },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
82 { "notrunc", C_NOTRUNC },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
83 { "sync", C_SYNC },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
84 };
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
85
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
86 static struct pair operands[] = {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
87 // keep the array sorted by name, bsearch() can be used.
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
88 { "bs", C_BS },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
89 { "conv", C_CONV },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
90 { "count", C_COUNT},
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
91 { "ibs", C_IBS },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
92 { "if", C_IF },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
93 { "obs", C_OBS },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
94 { "of", C_OF },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
95 { "seek", C_SEEK },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
96 { "skip", C_SKIP },
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
97 };
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
98
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
99 static struct io in, out;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
100 static struct iostat st;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
101 static unsigned long long c_count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
102
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
103 static unsigned long long strsuftoll(char* arg, int def, unsigned long long max)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
104 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
105 unsigned long long result;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
106 char *endp, *ch = arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
107 int i, idx = -1;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
108 errno = 0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
109
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
110 while (isspace(*ch)) ch++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
111 if (*ch == '-') error_exit("invalid number '%s'",arg);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
112 result = strtoull(arg, &endp, 10);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
113 if (errno == ERANGE || result > max || result < def)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
114 perror_exit("invalid number '%s'",arg);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
115 if (*endp != '\0') {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
116 for (i = 0; i < ARRAY_LEN(suffixes); i++)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
117 if (!strcmp(endp, suffixes[i].name)) idx = i;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
118 if (idx == -1 || (max/suffixes[idx].val < result))
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
119 error_exit("invalid number '%s'",arg);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
120 result = result* suffixes[idx].val;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
121 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
122 return result;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
123 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
124
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
125 static void summary()
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
126 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
127 double seconds = 5.0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
128 struct timeval now;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
129
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
130 gettimeofday(&now, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
131 seconds = ((now.tv_sec * 1000000 + now.tv_usec) - (st.start.tv_sec * 1000000
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
132 + st.start.tv_usec))/1000000.0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
133 //out to STDERR
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
134 fprintf(stderr,"%llu+%llu records in\n%llu+%llu records out\n", st.in_full, st.in_part,
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
135 st.out_full, st.out_part);
1276
d48bdc1cb017 Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents: 1116
diff changeset
136 human_readable(toybuf, st.bytes);
d48bdc1cb017 Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents: 1116
diff changeset
137 fprintf(stderr, "%llu bytes (%s) copied,",st.bytes, toybuf);
d48bdc1cb017 Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents: 1116
diff changeset
138 human_readable(toybuf, st.bytes/seconds);
d48bdc1cb017 Switch human_readable() to just outputing decimal kilo/mega/gigabytes, make du use it, move it from lib/pending.c to lib.c.
Rob Landley <rob@landley.net>
parents: 1116
diff changeset
139 fprintf(stderr, "%f seconds, %s/s\n", seconds, toybuf);
1116
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
141
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
142 static void sig_handler(int sig)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
143 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
144 TT.sig = sig;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
146
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
147 static int xmove_fd(int fd)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
148 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
149 int newfd;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
151 if (fd > STDERR_FILENO) return fd;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 if ((newfd = fcntl(fd, F_DUPFD, 3) < 0)) perror_exit("dupfd IO");
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 close(fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
154 return newfd;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
156
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
157 static void setup_inout()
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
158 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
159 ssize_t n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
160
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
161 /* for C_BS, in/out is done as it is. so only in.sz is enough.
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 * With Single buffer there will be overflow in a read following partial read
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
163 */
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 in.buff = out.buff = xmalloc(in.sz + ((toys.optflags & C_BS)? 0: out.sz));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 in.bp = out.bp = in.buff;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 atexit(summary);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 //setup input
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 if (!in.name) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 in.name = "stdin";
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 in.fd = STDIN_FILENO;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 } else {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 in.fd = xopen(in.name, O_RDONLY);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 in.fd = xmove_fd(in.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 //setup outout
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 if (!out.name) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 out.name = "stdout";
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 out.fd = STDOUT_FILENO;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 } else {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 out.fd = xcreate(out.name, O_WRONLY | O_CREAT, 0666);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
181 out.fd = xmove_fd(out.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
183
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
184 if (in.offset) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 if (lseek(in.fd, (off_t)(in.offset * in.sz), SEEK_CUR) < 0) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 while (in.offset--) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 if ((n = read(in.fd, in.bp, in.sz)) < 0) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 if (toys.optflags & C_NOERROR) { //warn message and summary
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 error_msg("%s: read error", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 summary();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 } else perror_exit("%s: read error", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 } else if (!n) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 xprintf("%s: Can't skip\n", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 exit(0);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
195 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
196 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
197 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
198 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
199
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
200 if (out.offset) xlseek(out.fd, (off_t)(out.offset * out.sz), SEEK_CUR);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
202
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
203 static void write_out(int all)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 ssize_t nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 out.bp = out.buff;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 while (out.count) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 nw = writeall(out.fd, out.bp, ((all)? out.count : out.sz));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 all = 0; //further writes will be on obs
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 if (nw <= 0) perror_exit("%s: write error",out.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 if (nw == out.sz) st.out_full++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 else st.out_part++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 out.count -= nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 out.bp += nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 st.bytes += nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 if (out.count < out.sz) break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
217 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
218 if (out.count) memmove(out.buff, out.bp, out.count); //move remainder to front
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
220
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
221 static void do_dd(void)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
223 ssize_t n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 struct sigaction sa;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
225
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 memset(&sa, 0, sizeof(sa));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 sa.sa_handler = sig_handler;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 sigaction(SIGINT, &sa, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 sigaction(SIGUSR1, &sa, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
230 setup_inout();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 gettimeofday(&st.start, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
232
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
233 if (toys.optflags & (C_OF | C_SEEK) && !(toys.optflags & C_NOTRUNC))
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 ftruncate(out.fd, (off_t)out.offset * out.sz);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
235
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 while (!(toys.optflags & C_COUNT) || (st.in_full + st.in_part) < c_count) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 if (TT.sig == SIGUSR1) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 summary();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 TT.sig = 0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 } else if (TT.sig == SIGINT) exit(TT.sig | 128);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 in.bp = in.buff + in.count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 if (toys.optflags & C_SYNC) memset(in.bp, 0, in.sz);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 if (!(n = read(in.fd, in.bp, in.sz))) break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 if (n < 0) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 if (errno == EINTR) continue;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 //read error case.
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 perror_msg("%s: read error", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 if (!(toys.optflags & C_NOERROR)) exit(1);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 summary();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 xlseek(in.fd, in.sz, SEEK_CUR);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 if (!(toys.optflags & C_SYNC)) continue;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 // if SYNC, then treat as full block of nuls
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 n = in.sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 if (n == in.sz) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 st.in_full++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
257 in.count += n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 } else {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 st.in_part++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 if (toys.optflags & C_SYNC) in.count += in.sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
261 else in.count += n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
263
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 out.count = in.count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 if (toys.optflags & C_BS) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 write_out(1);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 in.count = 0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
268 continue;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
270
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
271 if (in.count >= out.sz) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 write_out(0);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 in.count = out.count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
275 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 if (out.count) write_out(1); //write any remaining input blocks
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
277 if (toys.optflags & C_FSYNC && fsync(out.fd) < 0)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 perror_exit("%s: fsync fail", out.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
279
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 close(in.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 close(out.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
282 if (in.buff) free(in.buff);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
284
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 static int comp(const void *a, const void *b) //const to shut compiler up
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
286 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
287 return strcmp(((struct pair*)a)->name, ((struct pair*)b)->name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
289
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
290 void dd_main()
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 struct pair *res, key;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
293 char *arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 long sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
295
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 in.sz = out.sz = 512; //default io block size
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
297 while (*toys.optargs) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
298 if (!(arg = strchr(*toys.optargs, '='))) error_exit("unknown arg %s", *toys.optargs);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 *arg++ = '\0';
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 if (!*arg) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 toys.exithelp = 1;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 error_exit("");
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 key.name = *toys.optargs;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
305 if (!(res = bsearch(&key, operands, ARRAY_LEN(operands), sizeof(struct pair),
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 comp))) error_exit("unknown arg %s", key.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
307
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
308 toys.optflags |= res->val;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 switch(res->val) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 case C_BS:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 in.sz = out.sz = strsuftoll(arg, 1, LONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 case C_IBS:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 sz = strsuftoll(arg, 1, LONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 if (!(toys.optflags & C_BS)) in.sz = sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 case C_OBS:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 sz = strsuftoll(arg, 1, LONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 if (!(toys.optflags & C_BS)) out.sz = sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 case C_COUNT:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 c_count = strsuftoll(arg, 0, ULLONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 case C_IF:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 in.name = arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 case C_OF:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 out.name = arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 case C_SEEK:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 out.offset = strsuftoll(arg, 0, ULLONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
333 case C_SKIP:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 in.offset = strsuftoll(arg, 0, ULLONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 case C_CONV:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 while (arg) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 key.name = strsep(&arg, ",");
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 if (!(res = bsearch(&key, clist, ARRAY_LEN(clist),
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
340 sizeof(struct pair), comp)))
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 error_exit("unknown conversion %s", key.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
342
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
343 toys.optflags |= res->val;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
344 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
345 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
346 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
347 toys.optargs++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
349
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
350 do_dd();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
351 }