annotate toys/pending/dd.c @ 1116:b2a8f64a564b draft

du from Ashwini Sharma.
author Rob Landley <rob@landley.net>
date Sun, 10 Nov 2013 18:23:19 -0600
parents
children d48bdc1cb017
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);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
136 fprintf(stderr, "%llu bytes (%sB) copied,",st.bytes, human_readable(st.bytes));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
137 fprintf(stderr, "%f seconds, %sB/s\n", seconds, human_readable(st.bytes/seconds));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
138 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
139
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
140 static void sig_handler(int sig)
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 TT.sig = 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
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
145 static int xmove_fd(int fd)
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 int newfd;
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 if (fd > STDERR_FILENO) return fd;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
150 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
151 close(fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
152 return newfd;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
153 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
154
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
155 static void setup_inout()
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 ssize_t n;
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 /* 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
160 * 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
161 */
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
162 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
163 in.bp = out.bp = in.buff;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
164 atexit(summary);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
165 //setup input
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
166 if (!in.name) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
167 in.name = "stdin";
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
168 in.fd = STDIN_FILENO;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
169 } else {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
170 in.fd = xopen(in.name, O_RDONLY);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
171 in.fd = xmove_fd(in.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
172 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
173 //setup outout
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
174 if (!out.name) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
175 out.name = "stdout";
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
176 out.fd = STDOUT_FILENO;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
177 } else {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
178 out.fd = xcreate(out.name, O_WRONLY | O_CREAT, 0666);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
179 out.fd = xmove_fd(out.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
180 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
181
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
182 if (in.offset) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
183 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
184 while (in.offset--) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
185 if ((n = read(in.fd, in.bp, in.sz)) < 0) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
186 if (toys.optflags & C_NOERROR) { //warn message and summary
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
187 error_msg("%s: read error", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
188 summary();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
189 } else perror_exit("%s: read error", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
190 } else if (!n) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
191 xprintf("%s: Can't skip\n", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
192 exit(0);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
193 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
194 }
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 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
199 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
200
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
201 static void write_out(int all)
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 ssize_t nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
204 out.bp = out.buff;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
205 while (out.count) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
206 nw = writeall(out.fd, out.bp, ((all)? out.count : out.sz));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
207 all = 0; //further writes will be on obs
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
208 if (nw <= 0) perror_exit("%s: write error",out.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
209 if (nw == out.sz) st.out_full++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
210 else st.out_part++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
211 out.count -= nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
212 out.bp += nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
213 st.bytes += nw;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
214 if (out.count < out.sz) break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
215 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
216 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
217 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
218
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
219 static void do_dd(void)
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 ssize_t n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
222 struct sigaction sa;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
223
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
224 memset(&sa, 0, sizeof(sa));
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
225 sa.sa_handler = sig_handler;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
226 sigaction(SIGINT, &sa, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
227 sigaction(SIGUSR1, &sa, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
228 setup_inout();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
229 gettimeofday(&st.start, NULL);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
230
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
231 if (toys.optflags & (C_OF | C_SEEK) && !(toys.optflags & C_NOTRUNC))
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
232 ftruncate(out.fd, (off_t)out.offset * out.sz);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
233
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
234 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
235 if (TT.sig == SIGUSR1) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
236 summary();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
237 TT.sig = 0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
238 } else if (TT.sig == SIGINT) exit(TT.sig | 128);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
239 in.bp = in.buff + in.count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
240 if (toys.optflags & C_SYNC) memset(in.bp, 0, in.sz);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
241 if (!(n = read(in.fd, in.bp, in.sz))) break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
242 if (n < 0) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
243 if (errno == EINTR) continue;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
244 //read error case.
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
245 perror_msg("%s: read error", in.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
246 if (!(toys.optflags & C_NOERROR)) exit(1);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
247 summary();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
248 xlseek(in.fd, in.sz, SEEK_CUR);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
249 if (!(toys.optflags & C_SYNC)) continue;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
250 // if SYNC, then treat as full block of nuls
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
251 n = in.sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
252 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
253 if (n == in.sz) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
254 st.in_full++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
255 in.count += n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
256 } else {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
257 st.in_part++;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
258 if (toys.optflags & C_SYNC) in.count += in.sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
259 else in.count += n;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
260 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
261
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
262 out.count = in.count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
263 if (toys.optflags & C_BS) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
264 write_out(1);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
265 in.count = 0;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
266 continue;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
267 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
268
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
269 if (in.count >= out.sz) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
270 write_out(0);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
271 in.count = out.count;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
272 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
273 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
274 if (out.count) write_out(1); //write any remaining input blocks
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
275 if (toys.optflags & C_FSYNC && fsync(out.fd) < 0)
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
276 perror_exit("%s: fsync fail", out.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
277
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
278 close(in.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
279 close(out.fd);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
280 if (in.buff) free(in.buff);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
281 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
282
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
283 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
284 {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
285 return strcmp(((struct pair*)a)->name, ((struct pair*)b)->name);
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
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
288 void dd_main()
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 struct pair *res, key;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
291 char *arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
292 long sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
293
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
294 in.sz = out.sz = 512; //default io block size
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
295 while (*toys.optargs) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
296 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
297 *arg++ = '\0';
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
298 if (!*arg) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
299 toys.exithelp = 1;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
300 error_exit("");
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
301 }
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
302 key.name = *toys.optargs;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
303 if (!(res = bsearch(&key, operands, ARRAY_LEN(operands), sizeof(struct pair),
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
304 comp))) error_exit("unknown arg %s", key.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
305
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
306 toys.optflags |= res->val;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
307 switch(res->val) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
308 case C_BS:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
309 in.sz = out.sz = strsuftoll(arg, 1, LONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
310 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
311 case C_IBS:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
312 sz = strsuftoll(arg, 1, LONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
313 if (!(toys.optflags & C_BS)) in.sz = sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
314 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
315 case C_OBS:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
316 sz = strsuftoll(arg, 1, LONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
317 if (!(toys.optflags & C_BS)) out.sz = sz;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
318 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
319 case C_COUNT:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
320 c_count = strsuftoll(arg, 0, ULLONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
321 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
322 case C_IF:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
323 in.name = arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
324 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
325 case C_OF:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
326 out.name = arg;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
327 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
328 case C_SEEK:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
329 out.offset = strsuftoll(arg, 0, ULLONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
330 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
331 case C_SKIP:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
332 in.offset = strsuftoll(arg, 0, ULLONG_MAX);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
333 break;
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
334 case C_CONV:
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
335 while (arg) {
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
336 key.name = strsep(&arg, ",");
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
337 if (!(res = bsearch(&key, clist, ARRAY_LEN(clist),
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
338 sizeof(struct pair), comp)))
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
339 error_exit("unknown conversion %s", key.name);
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
340
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
341 toys.optflags |= res->val;
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 break;
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 toys.optargs++;
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
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
348 do_dd();
b2a8f64a564b du from Ashwini Sharma.
Rob Landley <rob@landley.net>
parents:
diff changeset
349 }