summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2018-04-20 19:46:03 -0500
committerElliott Hughes2018-04-20 19:55:44 -0500
commit21ceafbaacbfec47bc26f6c500847fd09c3924e0 (patch)
tree54f978b47210b8260ed48566ff5d218899abd063 /toolbox
parentb1677c15c266e428cf656e546cd7cc19de1e21e8 (diff)
downloadplatform-system-core-21ceafbaacbfec47bc26f6c500847fd09c3924e0.tar.gz
platform-system-core-21ceafbaacbfec47bc26f6c500847fd09c3924e0.tar.xz
platform-system-core-21ceafbaacbfec47bc26f6c500847fd09c3924e0.zip
Remove BSD dd.
Bug: N/A Test: builds Change-Id: Id80364241434abfc216149b4ce7440cef700abef
Diffstat (limited to 'toolbox')
-rw-r--r--toolbox/Android.bp28
-rw-r--r--toolbox/bsd-compatibility.h84
-rw-r--r--toolbox/tools.h1
-rw-r--r--toolbox/upstream-netbsd/bin/dd/args.c391
-rw-r--r--toolbox/upstream-netbsd/bin/dd/conv.c283
-rw-r--r--toolbox/upstream-netbsd/bin/dd/dd.c598
-rw-r--r--toolbox/upstream-netbsd/bin/dd/dd.h127
-rw-r--r--toolbox/upstream-netbsd/bin/dd/dd_hostops.c53
-rw-r--r--toolbox/upstream-netbsd/bin/dd/extern.h82
-rw-r--r--toolbox/upstream-netbsd/bin/dd/misc.c342
-rw-r--r--toolbox/upstream-netbsd/bin/dd/position.c185
-rw-r--r--toolbox/upstream-netbsd/include/namespace.h0
-rw-r--r--toolbox/upstream-netbsd/include/sys/extattr.h0
-rw-r--r--toolbox/upstream-netbsd/include/util.h0
-rw-r--r--toolbox/upstream-netbsd/lib/libc/gen/getbsize.c118
-rw-r--r--toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c161
-rw-r--r--toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c249
-rw-r--r--toolbox/upstream-netbsd/lib/libc/string/swab.c80
-rw-r--r--toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c117
19 files changed, 1 insertions, 2898 deletions
diff --git a/toolbox/Android.bp b/toolbox/Android.bp
index c6d4d9b84..232dd6332 100644
--- a/toolbox/Android.bp
+++ b/toolbox/Android.bp
@@ -5,32 +5,8 @@ cc_defaults {
5 "-Werror", 5 "-Werror",
6 "-Wno-unused-parameter", 6 "-Wno-unused-parameter",
7 "-Wno-unused-const-variable", 7 "-Wno-unused-const-variable",
8 "-include bsd-compatibility.h",
9 "-D_FILE_OFFSET_BITS=64", 8 "-D_FILE_OFFSET_BITS=64",
10 ], 9 "-DWITHOUT_NLS",
11 local_include_dirs: ["upstream-netbsd/include/"],
12}
13
14cc_library_static {
15 name: "libtoolbox_dd",
16 defaults: ["toolbox_defaults"],
17 vendor_available: true,
18 srcs: [
19 "upstream-netbsd/bin/dd/args.c",
20 "upstream-netbsd/bin/dd/conv.c",
21 "upstream-netbsd/bin/dd/dd.c",
22 "upstream-netbsd/bin/dd/dd_hostops.c",
23 "upstream-netbsd/bin/dd/misc.c",
24 "upstream-netbsd/bin/dd/position.c",
25 "upstream-netbsd/lib/libc/gen/getbsize.c",
26 "upstream-netbsd/lib/libc/gen/humanize_number.c",
27 "upstream-netbsd/lib/libc/stdlib/strsuftoll.c",
28 "upstream-netbsd/lib/libc/string/swab.c",
29 "upstream-netbsd/lib/libutil/raise_default_signal.c",
30 ],
31 cflags: [
32 "-Dmain=dd_main",
33 "-DNO_CONV",
34 ], 10 ],
35} 11}
36 12
@@ -55,10 +31,8 @@ cc_defaults {
55 generated_headers: [ 31 generated_headers: [
56 "toolbox_input_labels", 32 "toolbox_input_labels",
57 ], 33 ],
58 whole_static_libs: ["libtoolbox_dd"],
59 shared_libs: [ 34 shared_libs: [
60 "libbase", 35 "libbase",
61 "libcutils",
62 ], 36 ],
63 static_libs: ["libpropertyinfoparser"], 37 static_libs: ["libpropertyinfoparser"],
64 38
diff --git a/toolbox/bsd-compatibility.h b/toolbox/bsd-compatibility.h
deleted file mode 100644
index 7c3ddd452..000000000
--- a/toolbox/bsd-compatibility.h
+++ /dev/null
@@ -1,84 +0,0 @@
1/*
2 * Copyright (C) 2014, The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <stdbool.h>
30#include <sys/types.h>
31
32/* We want chown to support user.group as well as user:group. */
33#define SUPPORT_DOT
34
35/* We don't localize /system/bin! */
36#define WITHOUT_NLS
37
38// NetBSD uses _DIAGASSERT to null-check arguments and the like.
39#include <assert.h>
40#define _DIAGASSERT(e) ((e) ? (void) 0 : __assert2(__FILE__, __LINE__, __func__, #e))
41
42// TODO: update our <sys/cdefs.h> to support this properly.
43#define __type_fit(t, a) (0 == 0)
44
45// TODO: should this be in our <sys/cdefs.h>?
46#define __arraycount(a) (sizeof(a) / sizeof((a)[0]))
47
48// This at least matches GNU dd(1) behavior.
49#define SIGINFO SIGUSR1
50
51#define S_ISWHT(x) false
52
53__BEGIN_DECLS
54
55/* From NetBSD <stdlib.h>. */
56#define HN_DECIMAL 0x01
57#define HN_NOSPACE 0x02
58#define HN_B 0x04
59#define HN_DIVISOR_1000 0x08
60#define HN_GETSCALE 0x10
61#define HN_AUTOSCALE 0x20
62int humanize_number(char *, size_t, int64_t, const char *, int, int);
63int dehumanize_number(const char *, int64_t *);
64char *getbsize(int *, long *);
65long long strsuftoll(const char *, const char *, long long, long long);
66long long strsuftollx(const char *, const char *, long long, long long,
67 char *, size_t);
68
69/* From NetBSD <string.h>. */
70void strmode(mode_t, char*);
71
72/* From NetBSD <sys/param.h>. */
73#define MAXBSIZE 65536
74
75/* From NetBSD <sys/stat.h>. */
76#define DEFFILEMODE (S_IRUSR | S_IWUSR)
77
78/* From NetBSD <unistd.h>. */
79void swab(const void * __restrict, void * __restrict, ssize_t);
80
81/* From NetBSD <util.h>. */
82int raise_default_signal(int);
83
84__END_DECLS
diff --git a/toolbox/tools.h b/toolbox/tools.h
index 505f5281b..3d4bc3e12 100644
--- a/toolbox/tools.h
+++ b/toolbox/tools.h
@@ -1,4 +1,3 @@
1TOOL(dd)
2TOOL(getevent) 1TOOL(getevent)
3TOOL(getprop) 2TOOL(getprop)
4TOOL(newfs_msdos) 3TOOL(newfs_msdos)
diff --git a/toolbox/upstream-netbsd/bin/dd/args.c b/toolbox/upstream-netbsd/bin/dd/args.c
deleted file mode 100644
index 207e30081..000000000
--- a/toolbox/upstream-netbsd/bin/dd/args.c
+++ /dev/null
@@ -1,391 +0,0 @@
1/* $NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
40#else
41__RCSID("$NetBSD: args.c,v 1.38 2013/07/17 12:55:48 christos Exp $");
42#endif
43#endif /* not lint */
44
45#include <sys/types.h>
46#include <sys/time.h>
47
48#include <err.h>
49#include <errno.h>
50#include <limits.h>
51#include <stdio.h>
52#include <stdlib.h>
53#include <string.h>
54
55#include "dd.h"
56#include "extern.h"
57
58static int c_arg(const void *, const void *);
59
60#ifdef NO_MSGFMT
61static void f_msgfmt(char *) __dead;
62#else
63static void f_msgfmt(char *);
64#endif /* NO_MSGFMT */
65
66#ifdef NO_CONV
67static void f_conv(char *) __dead;
68#else
69static void f_conv(char *);
70static int c_conv(const void *, const void *);
71#endif /* NO_CONV */
72
73static void f_bs(char *);
74static void f_cbs(char *);
75static void f_count(char *);
76static void f_files(char *);
77static void f_ibs(char *);
78static void f_if(char *);
79static void f_obs(char *);
80static void f_of(char *);
81static void f_seek(char *);
82static void f_skip(char *);
83static void f_progress(char *);
84
85static const struct arg {
86 const char *name;
87 void (*f)(char *);
88 u_int set, noset;
89} args[] = {
90 /* the array needs to be sorted by the first column so
91 bsearch() can be used to find commands quickly */
92 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
93 { "cbs", f_cbs, C_CBS, C_CBS },
94 { "conv", f_conv, 0, 0 },
95 { "count", f_count, C_COUNT, C_COUNT },
96 { "files", f_files, C_FILES, C_FILES },
97 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
98 { "if", f_if, C_IF, C_IF },
99 { "iseek", f_skip, C_SKIP, C_SKIP },
100 { "msgfmt", f_msgfmt, 0, 0 },
101 { "obs", f_obs, C_OBS, C_BS|C_OBS },
102 { "of", f_of, C_OF, C_OF },
103 { "oseek", f_seek, C_SEEK, C_SEEK },
104 { "progress", f_progress, 0, 0 },
105 { "seek", f_seek, C_SEEK, C_SEEK },
106 { "skip", f_skip, C_SKIP, C_SKIP },
107};
108
109/*
110 * args -- parse JCL syntax of dd.
111 */
112void
113jcl(char **argv)
114{
115 struct arg *ap, tmp;
116 char *oper, *arg;
117
118 in.dbsz = out.dbsz = 512;
119
120 while ((oper = *++argv) != NULL) {
121 if ((oper = strdup(oper)) == NULL) {
122 errx(EXIT_FAILURE,
123 "unable to allocate space for the argument %s",
124 *argv);
125 /* NOTREACHED */
126 }
127 if ((arg = strchr(oper, '=')) == NULL) {
128 errx(EXIT_FAILURE, "unknown operand %s", oper);
129 /* NOTREACHED */
130 }
131 *arg++ = '\0';
132 if (!*arg) {
133 errx(EXIT_FAILURE, "no value specified for %s", oper);
134 /* NOTREACHED */
135 }
136 tmp.name = oper;
137 if (!(ap = bsearch(&tmp, args,
138 __arraycount(args), sizeof(*args), c_arg))) {
139 errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
140 /* NOTREACHED */
141 }
142 if (ddflags & ap->noset) {
143 errx(EXIT_FAILURE,
144 "%s: illegal argument combination or already set",
145 tmp.name);
146 /* NOTREACHED */
147 }
148 ddflags |= ap->set;
149 ap->f(arg);
150 }
151
152 /* Final sanity checks. */
153
154 if (ddflags & C_BS) {
155 /*
156 * Bs is turned off by any conversion -- we assume the user
157 * just wanted to set both the input and output block sizes
158 * and didn't want the bs semantics, so we don't warn.
159 */
160 if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
161 C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
162 ddflags &= ~C_BS;
163 ddflags |= C_IBS|C_OBS;
164 }
165
166 /* Bs supersedes ibs and obs. */
167 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
168 warnx("bs supersedes ibs and obs");
169 }
170
171 /*
172 * Ascii/ebcdic and cbs implies block/unblock.
173 * Block/unblock requires cbs and vice-versa.
174 */
175 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
176 if (!(ddflags & C_CBS)) {
177 errx(EXIT_FAILURE, "record operations require cbs");
178 /* NOTREACHED */
179 }
180 cfunc = ddflags & C_BLOCK ? block : unblock;
181 } else if (ddflags & C_CBS) {
182 if (ddflags & (C_ASCII|C_EBCDIC)) {
183 if (ddflags & C_ASCII) {
184 ddflags |= C_UNBLOCK;
185 cfunc = unblock;
186 } else {
187 ddflags |= C_BLOCK;
188 cfunc = block;
189 }
190 } else {
191 errx(EXIT_FAILURE,
192 "cbs meaningless if not doing record operations");
193 /* NOTREACHED */
194 }
195 } else
196 cfunc = def;
197
198 /* Read, write and seek calls take off_t as arguments.
199 *
200 * The following check is not done because an off_t is a quad
201 * for current NetBSD implementations.
202 *
203 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
204 * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
205 */
206}
207
208static int
209c_arg(const void *a, const void *b)
210{
211
212 return (strcmp(((const struct arg *)a)->name,
213 ((const struct arg *)b)->name));
214}
215
216static void
217f_bs(char *arg)
218{
219
220 in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
221}
222
223static void
224f_cbs(char *arg)
225{
226
227 cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
228}
229
230static void
231f_count(char *arg)
232{
233
234 cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
235 if (!cpy_cnt)
236 terminate(0);
237}
238
239static void
240f_files(char *arg)
241{
242
243 files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
244 if (!files_cnt)
245 terminate(0);
246}
247
248static void
249f_ibs(char *arg)
250{
251
252 if (!(ddflags & C_BS))
253 in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
254}
255
256static void
257f_if(char *arg)
258{
259
260 in.name = arg;
261}
262
263#ifdef NO_MSGFMT
264/* Build a small version (i.e. for a ramdisk root) */
265static void
266f_msgfmt(char *arg)
267{
268
269 errx(EXIT_FAILURE, "msgfmt option disabled");
270 /* NOTREACHED */
271}
272#else /* NO_MSGFMT */
273static void
274f_msgfmt(char *arg)
275{
276
277 /*
278 * If the format string is not valid, dd_write_msg() will print
279 * an error and exit.
280 */
281 dd_write_msg(arg, 0);
282
283 msgfmt = arg;
284}
285#endif /* NO_MSGFMT */
286
287static void
288f_obs(char *arg)
289{
290
291 if (!(ddflags & C_BS))
292 out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
293}
294
295static void
296f_of(char *arg)
297{
298
299 out.name = arg;
300}
301
302static void
303f_seek(char *arg)
304{
305
306 out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
307}
308
309static void
310f_skip(char *arg)
311{
312
313 in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
314}
315
316static void
317f_progress(char *arg)
318{
319
320 progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
321}
322
323#ifdef NO_CONV
324/* Build a small version (i.e. for a ramdisk root) */
325static void
326f_conv(char *arg)
327{
328
329 errx(EXIT_FAILURE, "conv option disabled");
330 /* NOTREACHED */
331}
332#else /* NO_CONV */
333
334static const struct conv {
335 const char *name;
336 u_int set, noset;
337 const u_char *ctab;
338} clist[] = {
339 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
340 { "block", C_BLOCK, C_UNBLOCK, NULL },
341 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
342 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
343 { "lcase", C_LCASE, C_UCASE, NULL },
344 { "noerror", C_NOERROR, 0, NULL },
345 { "notrunc", C_NOTRUNC, 0, NULL },
346 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
347 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
348 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
349 { "osync", C_OSYNC, C_BS, NULL },
350 { "sparse", C_SPARSE, 0, NULL },
351 { "swab", C_SWAB, 0, NULL },
352 { "sync", C_SYNC, 0, NULL },
353 { "ucase", C_UCASE, C_LCASE, NULL },
354 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
355 /* If you add items to this table, be sure to add the
356 * conversions to the C_BS check in the jcl routine above.
357 */
358};
359
360static void
361f_conv(char *arg)
362{
363 struct conv *cp, tmp;
364
365 while (arg != NULL) {
366 tmp.name = strsep(&arg, ",");
367 if (!(cp = bsearch(&tmp, clist,
368 __arraycount(clist), sizeof(*clist), c_conv))) {
369 errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
370 /* NOTREACHED */
371 }
372 if (ddflags & cp->noset) {
373 errx(EXIT_FAILURE,
374 "%s: illegal conversion combination", tmp.name);
375 /* NOTREACHED */
376 }
377 ddflags |= cp->set;
378 if (cp->ctab)
379 ctab = cp->ctab;
380 }
381}
382
383static int
384c_conv(const void *a, const void *b)
385{
386
387 return (strcmp(((const struct conv *)a)->name,
388 ((const struct conv *)b)->name));
389}
390
391#endif /* NO_CONV */
diff --git a/toolbox/upstream-netbsd/bin/dd/conv.c b/toolbox/upstream-netbsd/bin/dd/conv.c
deleted file mode 100644
index d4a8a0952..000000000
--- a/toolbox/upstream-netbsd/bin/dd/conv.c
+++ /dev/null
@@ -1,283 +0,0 @@
1/* $NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)conv.c 8.3 (Berkeley) 4/2/94";
40#else
41__RCSID("$NetBSD: conv.c,v 1.17 2003/08/07 09:05:10 agc Exp $");
42#endif
43#endif /* not lint */
44
45#include <sys/param.h>
46#include <sys/time.h>
47
48#include <err.h>
49#include <string.h>
50#include <stdlib.h>
51
52#include "dd.h"
53#include "extern.h"
54
55/*
56 * def --
57 * Copy input to output. Input is buffered until reaches obs, and then
58 * output until less than obs remains. Only a single buffer is used.
59 * Worst case buffer calculation is (ibs + obs - 1).
60 */
61void
62def(void)
63{
64 uint64_t cnt;
65 u_char *inp;
66 const u_char *t;
67
68 if ((t = ctab) != NULL)
69 for (inp = in.dbp - (cnt = in.dbrcnt); cnt--; ++inp)
70 *inp = t[*inp];
71
72 /* Make the output buffer look right. */
73 out.dbp = in.dbp;
74 out.dbcnt = in.dbcnt;
75
76 if (in.dbcnt >= out.dbsz) {
77 /* If the output buffer is full, write it. */
78 dd_out(0);
79
80 /*
81 * Ddout copies the leftover output to the beginning of
82 * the buffer and resets the output buffer. Reset the
83 * input buffer to match it.
84 */
85 in.dbp = out.dbp;
86 in.dbcnt = out.dbcnt;
87 }
88}
89
90void
91def_close(void)
92{
93
94 /* Just update the count, everything is already in the buffer. */
95 if (in.dbcnt)
96 out.dbcnt = in.dbcnt;
97}
98
99#ifdef NO_CONV
100/* Build a smaller version (i.e. for a miniroot) */
101/* These can not be called, but just in case... */
102static const char no_block[] = "unblock and -DNO_CONV?";
103void block(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
104void block_close(void) { errx(EXIT_FAILURE, "%s", no_block + 2); }
105void unblock(void) { errx(EXIT_FAILURE, "%s", no_block); }
106void unblock_close(void) { errx(EXIT_FAILURE, "%s", no_block); }
107#else /* NO_CONV */
108
109/*
110 * Copy variable length newline terminated records with a max size cbsz
111 * bytes to output. Records less than cbs are padded with spaces.
112 *
113 * max in buffer: MAX(ibs, cbsz)
114 * max out buffer: obs + cbsz
115 */
116void
117block(void)
118{
119 static int intrunc;
120 int ch = 0; /* pacify gcc */
121 uint64_t cnt, maxlen;
122 u_char *inp, *outp;
123 const u_char *t;
124
125 /*
126 * Record truncation can cross block boundaries. If currently in a
127 * truncation state, keep tossing characters until reach a newline.
128 * Start at the beginning of the buffer, as the input buffer is always
129 * left empty.
130 */
131 if (intrunc) {
132 for (inp = in.db, cnt = in.dbrcnt;
133 cnt && *inp++ != '\n'; --cnt);
134 if (!cnt) {
135 in.dbcnt = 0;
136 in.dbp = in.db;
137 return;
138 }
139 intrunc = 0;
140 /* Adjust the input buffer numbers. */
141 in.dbcnt = cnt - 1;
142 in.dbp = inp + cnt - 1;
143 }
144
145 /*
146 * Copy records (max cbsz size chunks) into the output buffer. The
147 * translation is done as we copy into the output buffer.
148 */
149 for (inp = in.dbp - in.dbcnt, outp = out.dbp; in.dbcnt;) {
150 maxlen = MIN(cbsz, in.dbcnt);
151 if ((t = ctab) != NULL)
152 for (cnt = 0;
153 cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
154 *outp++ = t[ch];
155 else
156 for (cnt = 0;
157 cnt < maxlen && (ch = *inp++) != '\n'; ++cnt)
158 *outp++ = ch;
159 /*
160 * Check for short record without a newline. Reassemble the
161 * input block.
162 */
163 if (ch != '\n' && in.dbcnt < cbsz) {
164 (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
165 break;
166 }
167
168 /* Adjust the input buffer numbers. */
169 in.dbcnt -= cnt;
170 if (ch == '\n')
171 --in.dbcnt;
172
173 /* Pad short records with spaces. */
174 if (cnt < cbsz)
175 (void)memset(outp, ctab ? ctab[' '] : ' ', cbsz - cnt);
176 else {
177 /*
178 * If the next character wouldn't have ended the
179 * block, it's a truncation.
180 */
181 if (!in.dbcnt || *inp != '\n')
182 ++st.trunc;
183
184 /* Toss characters to a newline. */
185 for (; in.dbcnt && *inp++ != '\n'; --in.dbcnt);
186 if (!in.dbcnt)
187 intrunc = 1;
188 else
189 --in.dbcnt;
190 }
191
192 /* Adjust output buffer numbers. */
193 out.dbp += cbsz;
194 if ((out.dbcnt += cbsz) >= out.dbsz)
195 dd_out(0);
196 outp = out.dbp;
197 }
198 in.dbp = in.db + in.dbcnt;
199}
200
201void
202block_close(void)
203{
204
205 /*
206 * Copy any remaining data into the output buffer and pad to a record.
207 * Don't worry about truncation or translation, the input buffer is
208 * always empty when truncating, and no characters have been added for
209 * translation. The bottom line is that anything left in the input
210 * buffer is a truncated record. Anything left in the output buffer
211 * just wasn't big enough.
212 */
213 if (in.dbcnt) {
214 ++st.trunc;
215 (void)memmove(out.dbp, in.dbp - in.dbcnt, in.dbcnt);
216 (void)memset(out.dbp + in.dbcnt,
217 ctab ? ctab[' '] : ' ', cbsz - in.dbcnt);
218 out.dbcnt += cbsz;
219 }
220}
221
222/*
223 * Convert fixed length (cbsz) records to variable length. Deletes any
224 * trailing blanks and appends a newline.
225 *
226 * max in buffer: MAX(ibs, cbsz) + cbsz
227 * max out buffer: obs + cbsz
228 */
229void
230unblock(void)
231{
232 uint64_t cnt;
233 u_char *inp;
234 const u_char *t;
235
236 /* Translation and case conversion. */
237 if ((t = ctab) != NULL)
238 for (cnt = in.dbrcnt, inp = in.dbp - 1; cnt--; inp--)
239 *inp = t[*inp];
240 /*
241 * Copy records (max cbsz size chunks) into the output buffer. The
242 * translation has to already be done or we might not recognize the
243 * spaces.
244 */
245 for (inp = in.db; in.dbcnt >= cbsz; inp += cbsz, in.dbcnt -= cbsz) {
246 for (t = inp + cbsz - 1; t >= inp && *t == ' '; --t);
247 if (t >= inp) {
248 cnt = t - inp + 1;
249 (void)memmove(out.dbp, inp, cnt);
250 out.dbp += cnt;
251 out.dbcnt += cnt;
252 }
253 ++out.dbcnt;
254 *out.dbp++ = '\n';
255 if (out.dbcnt >= out.dbsz)
256 dd_out(0);
257 }
258 if (in.dbcnt)
259 (void)memmove(in.db, in.dbp - in.dbcnt, in.dbcnt);
260 in.dbp = in.db + in.dbcnt;
261}
262
263void
264unblock_close(void)
265{
266 uint64_t cnt;
267 u_char *t;
268
269 if (in.dbcnt) {
270 warnx("%s: short input record", in.name);
271 for (t = in.db + in.dbcnt - 1; t >= in.db && *t == ' '; --t);
272 if (t >= in.db) {
273 cnt = t - in.db + 1;
274 (void)memmove(out.dbp, in.db, cnt);
275 out.dbp += cnt;
276 out.dbcnt += cnt;
277 }
278 ++out.dbcnt;
279 *out.dbp++ = '\n';
280 }
281}
282
283#endif /* NO_CONV */
diff --git a/toolbox/upstream-netbsd/bin/dd/dd.c b/toolbox/upstream-netbsd/bin/dd/dd.c
deleted file mode 100644
index 03d080cd4..000000000
--- a/toolbox/upstream-netbsd/bin/dd/dd.c
+++ /dev/null
@@ -1,598 +0,0 @@
1/* $NetBSD: dd.c,v 1.49 2012/02/21 01:49:01 matt Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38__COPYRIGHT("@(#) Copyright (c) 1991, 1993, 1994\
39 The Regents of the University of California. All rights reserved.");
40#endif /* not lint */
41
42#ifndef lint
43#if 0
44static char sccsid[] = "@(#)dd.c 8.5 (Berkeley) 4/2/94";
45#else
46__RCSID("$NetBSD: dd.c,v 1.49 2012/02/21 01:49:01 matt Exp $");
47#endif
48#endif /* not lint */
49
50#include <sys/param.h>
51#include <sys/stat.h>
52#include <sys/ioctl.h>
53#include <sys/mtio.h>
54#include <sys/time.h>
55
56#include <ctype.h>
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <locale.h>
61#include <signal.h>
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
66
67#include "dd.h"
68#include "extern.h"
69
70static void dd_close(void);
71static void dd_in(void);
72static void getfdtype(IO *);
73static void redup_clean_fd(IO *);
74static void setup(void);
75
76int main(int, char *[]);
77
78IO in, out; /* input/output state */
79STAT st; /* statistics */
80void (*cfunc)(void); /* conversion function */
81uint64_t cpy_cnt; /* # of blocks to copy */
82static off_t pending = 0; /* pending seek if sparse */
83u_int ddflags; /* conversion options */
84uint64_t cbsz; /* conversion block size */
85u_int files_cnt = 1; /* # of files to copy */
86uint64_t progress = 0; /* display sign of life */
87const u_char *ctab; /* conversion table */
88sigset_t infoset; /* a set blocking SIGINFO */
89const char *msgfmt = "posix"; /* default summary() message format */
90
91/*
92 * Ops for stdin/stdout and crunch'd dd. These are always host ops.
93 */
94static const struct ddfops ddfops_stdfd = {
95 .op_open = open,
96 .op_close = close,
97 .op_fcntl = fcntl,
98 .op_ioctl = ioctl,
99 .op_fstat = fstat,
100 .op_fsync = fsync,
101 .op_ftruncate = ftruncate,
102 .op_lseek = lseek,
103 .op_read = read,
104 .op_write = write,
105};
106extern const struct ddfops ddfops_prog;
107
108int
109main(int argc, char *argv[])
110{
111 int ch;
112
113 setprogname(argv[0]);
114 (void)setlocale(LC_ALL, "");
115
116 while ((ch = getopt(argc, argv, "")) != -1) {
117 switch (ch) {
118 default:
119 errx(EXIT_FAILURE, "usage: dd [operand ...]");
120 /* NOTREACHED */
121 }
122 }
123 argc -= (optind - 1);
124 argv += (optind - 1);
125
126 jcl(argv);
127#ifndef CRUNCHOPS
128 if (ddfops_prog.op_init && ddfops_prog.op_init() == -1)
129 err(1, "prog init");
130#endif
131 setup();
132
133 (void)signal(SIGINFO, summaryx);
134 (void)signal(SIGINT, terminate);
135 (void)sigemptyset(&infoset);
136 (void)sigaddset(&infoset, SIGINFO);
137
138 (void)atexit(summary);
139
140 while (files_cnt--)
141 dd_in();
142
143 dd_close();
144 exit(0);
145 /* NOTREACHED */
146}
147
148static void
149setup(void)
150{
151#ifdef CRUNCHOPS
152 const struct ddfops *prog_ops = &ddfops_stdfd;
153#else
154 const struct ddfops *prog_ops = &ddfops_prog;
155#endif
156
157 if (in.name == NULL) {
158 in.name = "stdin";
159 in.fd = STDIN_FILENO;
160 in.ops = &ddfops_stdfd;
161 } else {
162 in.ops = prog_ops;
163 in.fd = ddop_open(in, in.name, O_RDONLY, 0);
164 if (in.fd < 0)
165 err(EXIT_FAILURE, "%s", in.name);
166 /* NOTREACHED */
167
168 /* Ensure in.fd is outside the stdio descriptor range */
169 redup_clean_fd(&in);
170 }
171
172 getfdtype(&in);
173
174 if (files_cnt > 1 && !(in.flags & ISTAPE)) {
175 errx(EXIT_FAILURE, "files is not supported for non-tape devices");
176 /* NOTREACHED */
177 }
178
179 if (out.name == NULL) {
180 /* No way to check for read access here. */
181 out.fd = STDOUT_FILENO;
182 out.name = "stdout";
183 out.ops = &ddfops_stdfd;
184 } else {
185 out.ops = prog_ops;
186#define OFLAGS \
187 (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC))
188 out.fd = ddop_open(out, out.name, O_RDWR | OFLAGS, DEFFILEMODE);
189 /*
190 * May not have read access, so try again with write only.
191 * Without read we may have a problem if output also does
192 * not support seeks.
193 */
194 if (out.fd < 0) {
195 out.fd = ddop_open(out, out.name, O_WRONLY | OFLAGS,
196 DEFFILEMODE);
197 out.flags |= NOREAD;
198 }
199 if (out.fd < 0) {
200 err(EXIT_FAILURE, "%s", out.name);
201 /* NOTREACHED */
202 }
203
204 /* Ensure out.fd is outside the stdio descriptor range */
205 redup_clean_fd(&out);
206 }
207
208 getfdtype(&out);
209
210 /*
211 * Allocate space for the input and output buffers. If not doing
212 * record oriented I/O, only need a single buffer.
213 */
214 if (!(ddflags & (C_BLOCK|C_UNBLOCK))) {
215 size_t dbsz = out.dbsz;
216 if (!(ddflags & C_BS))
217 dbsz += in.dbsz - 1;
218 if ((in.db = malloc(dbsz)) == NULL) {
219 err(EXIT_FAILURE, NULL);
220 /* NOTREACHED */
221 }
222 out.db = in.db;
223 } else if ((in.db =
224 malloc((u_int)(MAX(in.dbsz, cbsz) + cbsz))) == NULL ||
225 (out.db = malloc((u_int)(out.dbsz + cbsz))) == NULL) {
226 err(EXIT_FAILURE, NULL);
227 /* NOTREACHED */
228 }
229 in.dbp = in.db;
230 out.dbp = out.db;
231
232 /* Position the input/output streams. */
233 if (in.offset)
234 pos_in();
235 if (out.offset)
236 pos_out();
237
238 /*
239 * Truncate the output file; ignore errors because it fails on some
240 * kinds of output files, tapes, for example.
241 */
242 if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK))
243 (void)ddop_ftruncate(out, out.fd, (off_t)out.offset * out.dbsz);
244
245 /*
246 * If converting case at the same time as another conversion, build a
247 * table that does both at once. If just converting case, use the
248 * built-in tables.
249 */
250 if (ddflags & (C_LCASE|C_UCASE)) {
251#ifdef NO_CONV
252 /* Should not get here, but just in case... */
253 errx(EXIT_FAILURE, "case conv and -DNO_CONV");
254 /* NOTREACHED */
255#else /* NO_CONV */
256 u_int cnt;
257
258 if (ddflags & C_ASCII || ddflags & C_EBCDIC) {
259 if (ddflags & C_LCASE) {
260 for (cnt = 0; cnt < 256; ++cnt)
261 casetab[cnt] = tolower(ctab[cnt]);
262 } else {
263 for (cnt = 0; cnt < 256; ++cnt)
264 casetab[cnt] = toupper(ctab[cnt]);
265 }
266 } else {
267 if (ddflags & C_LCASE) {
268 for (cnt = 0; cnt < 256; ++cnt)
269 casetab[cnt] = tolower(cnt);
270 } else {
271 for (cnt = 0; cnt < 256; ++cnt)
272 casetab[cnt] = toupper(cnt);
273 }
274 }
275
276 ctab = casetab;
277#endif /* NO_CONV */
278 }
279
280 (void)gettimeofday(&st.start, NULL); /* Statistics timestamp. */
281}
282
283static void
284getfdtype(IO *io)
285{
286 struct mtget mt;
287 struct stat sb;
288
289 if (io->ops->op_fstat(io->fd, &sb)) {
290 err(EXIT_FAILURE, "%s", io->name);
291 /* NOTREACHED */
292 }
293 if (S_ISCHR(sb.st_mode))
294 io->flags |= io->ops->op_ioctl(io->fd, MTIOCGET, &mt)
295 ? ISCHR : ISTAPE;
296 else if (io->ops->op_lseek(io->fd, (off_t)0, SEEK_CUR) == -1
297 && errno == ESPIPE)
298 io->flags |= ISPIPE; /* XXX fixed in 4.4BSD */
299}
300
301/*
302 * Move the parameter file descriptor to a descriptor that is outside the
303 * stdio descriptor range, if necessary. This is required to avoid
304 * accidentally outputting completion or error messages into the
305 * output file that were intended for the tty.
306 */
307static void
308redup_clean_fd(IO *io)
309{
310 int fd = io->fd;
311 int newfd;
312
313 if (fd != STDIN_FILENO && fd != STDOUT_FILENO &&
314 fd != STDERR_FILENO)
315 /* File descriptor is ok, return immediately. */
316 return;
317
318 /*
319 * 3 is the first descriptor greater than STD*_FILENO. Any
320 * free descriptor valued 3 or above is acceptable...
321 */
322 newfd = io->ops->op_fcntl(fd, F_DUPFD, 3);
323 if (newfd < 0) {
324 err(EXIT_FAILURE, "dupfd IO");
325 /* NOTREACHED */
326 }
327
328 io->ops->op_close(fd);
329 io->fd = newfd;
330}
331
332static void
333dd_in(void)
334{
335 int flags;
336 int64_t n;
337
338 for (flags = ddflags;;) {
339 if (cpy_cnt && (st.in_full + st.in_part) >= cpy_cnt)
340 return;
341
342 /*
343 * Clear the buffer first if doing "sync" on input.
344 * If doing block operations use spaces. This will
345 * affect not only the C_NOERROR case, but also the
346 * last partial input block which should be padded
347 * with zero and not garbage.
348 */
349 if (flags & C_SYNC) {
350 if (flags & (C_BLOCK|C_UNBLOCK))
351 (void)memset(in.dbp, ' ', in.dbsz);
352 else
353 (void)memset(in.dbp, 0, in.dbsz);
354 }
355
356 n = ddop_read(in, in.fd, in.dbp, in.dbsz);
357 if (n == 0) {
358 in.dbrcnt = 0;
359 return;
360 }
361
362 /* Read error. */
363 if (n < 0) {
364
365 /*
366 * If noerror not specified, die. POSIX requires that
367 * the warning message be followed by an I/O display.
368 */
369 if (!(flags & C_NOERROR)) {
370 err(EXIT_FAILURE, "%s", in.name);
371 /* NOTREACHED */
372 }
373 warn("%s", in.name);
374 summary();
375
376 /*
377 * If it's not a tape drive or a pipe, seek past the
378 * error. If your OS doesn't do the right thing for
379 * raw disks this section should be modified to re-read
380 * in sector size chunks.
381 */
382 if (!(in.flags & (ISPIPE|ISTAPE)) &&
383 ddop_lseek(in, in.fd, (off_t)in.dbsz, SEEK_CUR))
384 warn("%s", in.name);
385
386 /* If sync not specified, omit block and continue. */
387 if (!(ddflags & C_SYNC))
388 continue;
389
390 /* Read errors count as full blocks. */
391 in.dbcnt += in.dbrcnt = in.dbsz;
392 ++st.in_full;
393
394 /* Handle full input blocks. */
395 } else if ((uint64_t)n == in.dbsz) {
396 in.dbcnt += in.dbrcnt = n;
397 ++st.in_full;
398
399 /* Handle partial input blocks. */
400 } else {
401 /* If sync, use the entire block. */
402 if (ddflags & C_SYNC)
403 in.dbcnt += in.dbrcnt = in.dbsz;
404 else
405 in.dbcnt += in.dbrcnt = n;
406 ++st.in_part;
407 }
408
409 /*
410 * POSIX states that if bs is set and no other conversions
411 * than noerror, notrunc or sync are specified, the block
412 * is output without buffering as it is read.
413 */
414 if (ddflags & C_BS) {
415 out.dbcnt = in.dbcnt;
416 dd_out(1);
417 in.dbcnt = 0;
418 continue;
419 }
420
421 if (ddflags & C_SWAB) {
422 if ((n = in.dbrcnt) & 1) {
423 ++st.swab;
424 --n;
425 }
426 swab(in.dbp, in.dbp, n);
427 }
428
429 in.dbp += in.dbrcnt;
430 (*cfunc)();
431 }
432}
433
434/*
435 * Cleanup any remaining I/O and flush output. If necessary, output file
436 * is truncated.
437 */
438static void
439dd_close(void)
440{
441
442 if (cfunc == def)
443 def_close();
444 else if (cfunc == block)
445 block_close();
446 else if (cfunc == unblock)
447 unblock_close();
448 if (ddflags & C_OSYNC && out.dbcnt < out.dbsz) {
449 (void)memset(out.dbp, 0, out.dbsz - out.dbcnt);
450 out.dbcnt = out.dbsz;
451 }
452 /* If there are pending sparse blocks, make sure
453 * to write out the final block un-sparse
454 */
455 if ((out.dbcnt == 0) && pending) {
456 memset(out.db, 0, out.dbsz);
457 out.dbcnt = out.dbsz;
458 out.dbp = out.db + out.dbcnt;
459 pending -= out.dbsz;
460 }
461 if (out.dbcnt)
462 dd_out(1);
463
464 /*
465 * Reporting nfs write error may be deferred until next
466 * write(2) or close(2) system call. So, we need to do an
467 * extra check. If an output is stdout, the file structure
468 * may be shared with other processes and close(2) just
469 * decreases the reference count.
470 */
471 if (out.fd == STDOUT_FILENO && ddop_fsync(out, out.fd) == -1
472 && errno != EINVAL) {
473 err(EXIT_FAILURE, "fsync stdout");
474 /* NOTREACHED */
475 }
476 if (ddop_close(out, out.fd) == -1) {
477 err(EXIT_FAILURE, "close");
478 /* NOTREACHED */
479 }
480}
481
482void
483dd_out(int force)
484{
485 static int warned;
486 int64_t cnt, n, nw;
487 u_char *outp;
488
489 /*
490 * Write one or more blocks out. The common case is writing a full
491 * output block in a single write; increment the full block stats.
492 * Otherwise, we're into partial block writes. If a partial write,
493 * and it's a character device, just warn. If a tape device, quit.
494 *
495 * The partial writes represent two cases. 1: Where the input block
496 * was less than expected so the output block was less than expected.
497 * 2: Where the input block was the right size but we were forced to
498 * write the block in multiple chunks. The original versions of dd(1)
499 * never wrote a block in more than a single write, so the latter case
500 * never happened.
501 *
502 * One special case is if we're forced to do the write -- in that case
503 * we play games with the buffer size, and it's usually a partial write.
504 */
505 outp = out.db;
506 for (n = force ? out.dbcnt : out.dbsz;; n = out.dbsz) {
507 for (cnt = n;; cnt -= nw) {
508
509 if (!force && ddflags & C_SPARSE) {
510 int sparse, i;
511 sparse = 1; /* Is buffer sparse? */
512 for (i = 0; i < cnt; i++)
513 if (outp[i] != 0) {
514 sparse = 0;
515 break;
516 }
517 if (sparse) {
518 pending += cnt;
519 outp += cnt;
520 nw = 0;
521 break;
522 }
523 }
524 if (pending != 0) {
525 if (ddop_lseek(out,
526 out.fd, pending, SEEK_CUR) == -1)
527 err(EXIT_FAILURE, "%s: seek error creating sparse file",
528 out.name);
529 }
530 nw = bwrite(&out, outp, cnt);
531 if (nw <= 0) {
532 if (nw == 0)
533 errx(EXIT_FAILURE,
534 "%s: end of device", out.name);
535 /* NOTREACHED */
536 if (errno != EINTR)
537 err(EXIT_FAILURE, "%s", out.name);
538 /* NOTREACHED */
539 nw = 0;
540 }
541 if (pending) {
542 st.bytes += pending;
543 st.sparse += pending/out.dbsz;
544 st.out_full += pending/out.dbsz;
545 pending = 0;
546 }
547 outp += nw;
548 st.bytes += nw;
549 if (nw == n) {
550 if ((uint64_t)n != out.dbsz)
551 ++st.out_part;
552 else
553 ++st.out_full;
554 break;
555 }
556 ++st.out_part;
557 if (nw == cnt)
558 break;
559 if (out.flags & ISCHR && !warned) {
560 warned = 1;
561 warnx("%s: short write on character device", out.name);
562 }
563 if (out.flags & ISTAPE)
564 errx(EXIT_FAILURE,
565 "%s: short write on tape device", out.name);
566 /* NOTREACHED */
567
568 }
569 if ((out.dbcnt -= n) < out.dbsz)
570 break;
571 }
572
573 /* Reassemble the output block. */
574 if (out.dbcnt)
575 (void)memmove(out.db, out.dbp - out.dbcnt, out.dbcnt);
576 out.dbp = out.db + out.dbcnt;
577
578 if (progress && (st.out_full + st.out_part) % progress == 0)
579 (void)write(STDERR_FILENO, ".", 1);
580}
581
582/*
583 * A protected against SIGINFO write
584 */
585ssize_t
586bwrite(IO *io, const void *buf, size_t len)
587{
588 sigset_t oset;
589 ssize_t rv;
590 int oerrno;
591
592 (void)sigprocmask(SIG_BLOCK, &infoset, &oset);
593 rv = io->ops->op_write(io->fd, buf, len);
594 oerrno = errno;
595 (void)sigprocmask(SIG_SETMASK, &oset, NULL);
596 errno = oerrno;
597 return (rv);
598}
diff --git a/toolbox/upstream-netbsd/bin/dd/dd.h b/toolbox/upstream-netbsd/bin/dd/dd.h
deleted file mode 100644
index b01c7b303..000000000
--- a/toolbox/upstream-netbsd/bin/dd/dd.h
+++ /dev/null
@@ -1,127 +0,0 @@
1/* $NetBSD: dd.h,v 1.15 2011/02/04 19:42:12 pooka Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)dd.h 8.3 (Berkeley) 4/2/94
36 */
37
38#include <sys/stat.h>
39
40struct ddfops {
41 int (*op_init)(void);
42
43 int (*op_open)(const char *, int, ...);
44 int (*op_close)(int);
45
46 int (*op_fcntl)(int, int, ...);
47#ifdef __ANDROID__
48 int (*op_ioctl)(int, int, ...);
49#else
50 int (*op_ioctl)(int, unsigned long, ...);
51#endif
52
53 int (*op_fstat)(int, struct stat *);
54 int (*op_fsync)(int);
55 int (*op_ftruncate)(int, off_t);
56
57 off_t (*op_lseek)(int, off_t, int);
58
59 ssize_t (*op_read)(int, void *, size_t);
60 ssize_t (*op_write)(int, const void *, size_t);
61};
62
63#define ddop_open(dir, a1, a2, ...) dir.ops->op_open(a1, a2, __VA_ARGS__)
64#define ddop_close(dir, a1) dir.ops->op_close(a1)
65#define ddop_fcntl(dir, a1, a2, ...) dir.ops->op_fcntl(a1, a2, __VA_ARGS__)
66#define ddop_ioctl(dir, a1, a2, ...) dir.ops->op_ioctl(a1, a2, __VA_ARGS__)
67#define ddop_fsync(dir, a1) dir.ops->op_fsync(a1)
68#define ddop_ftruncate(dir, a1, a2) dir.ops->op_ftruncate(a1, a2)
69#define ddop_lseek(dir, a1, a2, a3) dir.ops->op_lseek(a1, a2, a3)
70#define ddop_read(dir, a1, a2, a3) dir.ops->op_read(a1, a2, a3)
71#define ddop_write(dir, a1, a2, a3) dir.ops->op_write(a1, a2, a3)
72
73/* Input/output stream state. */
74typedef struct {
75 u_char *db; /* buffer address */
76 u_char *dbp; /* current buffer I/O address */
77 uint64_t dbcnt; /* current buffer byte count */
78 int64_t dbrcnt; /* last read byte count */
79 uint64_t dbsz; /* buffer size */
80
81#define ISCHR 0x01 /* character device (warn on short) */
82#define ISPIPE 0x02 /* pipe (not truncatable) */
83#define ISTAPE 0x04 /* tape (not seekable) */
84#define NOREAD 0x08 /* not readable */
85 u_int flags;
86
87 const char *name; /* name */
88 int fd; /* file descriptor */
89 uint64_t offset; /* # of blocks to skip */
90 struct ddfops const *ops; /* ops to use with fd */
91} IO;
92
93typedef struct {
94 uint64_t in_full; /* # of full input blocks */
95 uint64_t in_part; /* # of partial input blocks */
96 uint64_t out_full; /* # of full output blocks */
97 uint64_t out_part; /* # of partial output blocks */
98 uint64_t trunc; /* # of truncated records */
99 uint64_t swab; /* # of odd-length swab blocks */
100 uint64_t sparse; /* # of sparse output blocks */
101 uint64_t bytes; /* # of bytes written */
102 struct timeval start; /* start time of dd */
103} STAT;
104
105/* Flags (in ddflags). */
106#define C_ASCII 0x00001
107#define C_BLOCK 0x00002
108#define C_BS 0x00004
109#define C_CBS 0x00008
110#define C_COUNT 0x00010
111#define C_EBCDIC 0x00020
112#define C_FILES 0x00040
113#define C_IBS 0x00080
114#define C_IF 0x00100
115#define C_LCASE 0x00200
116#define C_NOERROR 0x00400
117#define C_NOTRUNC 0x00800
118#define C_OBS 0x01000
119#define C_OF 0x02000
120#define C_SEEK 0x04000
121#define C_SKIP 0x08000
122#define C_SWAB 0x10000
123#define C_SYNC 0x20000
124#define C_UCASE 0x40000
125#define C_UNBLOCK 0x80000
126#define C_OSYNC 0x100000
127#define C_SPARSE 0x200000
diff --git a/toolbox/upstream-netbsd/bin/dd/dd_hostops.c b/toolbox/upstream-netbsd/bin/dd/dd_hostops.c
deleted file mode 100644
index d6e7a89e7..000000000
--- a/toolbox/upstream-netbsd/bin/dd/dd_hostops.c
+++ /dev/null
@@ -1,53 +0,0 @@
1/* $NetBSD: dd_hostops.c,v 1.1 2011/02/04 19:42:12 pooka Exp $ */
2
3/*-
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30#ifndef lint
31__RCSID("$NetBSD: dd_hostops.c,v 1.1 2011/02/04 19:42:12 pooka Exp $");
32#endif /* !lint */
33
34#include <sys/types.h>
35#include <sys/ioctl.h>
36
37#include <fcntl.h>
38#include <unistd.h>
39
40#include "dd.h"
41
42const struct ddfops ddfops_prog = {
43 .op_open = open,
44 .op_close = close,
45 .op_fcntl = fcntl,
46 .op_ioctl = ioctl,
47 .op_fstat = fstat,
48 .op_fsync = fsync,
49 .op_ftruncate = ftruncate,
50 .op_lseek = lseek,
51 .op_read = read,
52 .op_write = write,
53};
diff --git a/toolbox/upstream-netbsd/bin/dd/extern.h b/toolbox/upstream-netbsd/bin/dd/extern.h
deleted file mode 100644
index 9c5902186..000000000
--- a/toolbox/upstream-netbsd/bin/dd/extern.h
+++ /dev/null
@@ -1,82 +0,0 @@
1/* $NetBSD: extern.h,v 1.22 2011/11/07 22:24:23 jym Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)extern.h 8.3 (Berkeley) 4/2/94
36 */
37
38#include <sys/cdefs.h>
39
40#ifdef NO_CONV
41__dead void block(void);
42__dead void block_close(void);
43__dead void unblock(void);
44__dead void unblock_close(void);
45#else
46void block(void);
47void block_close(void);
48void unblock(void);
49void unblock_close(void);
50#endif
51
52#ifndef NO_MSGFMT
53int dd_write_msg(const char *, int);
54#endif
55
56void dd_out(int);
57void def(void);
58void def_close(void);
59void jcl(char **);
60void pos_in(void);
61void pos_out(void);
62void summary(void);
63void summaryx(int);
64__dead void terminate(int);
65void unblock(void);
66void unblock_close(void);
67ssize_t bwrite(IO *, const void *, size_t);
68
69extern IO in, out;
70extern STAT st;
71extern void (*cfunc)(void);
72extern uint64_t cpy_cnt;
73extern uint64_t cbsz;
74extern u_int ddflags;
75extern u_int files_cnt;
76extern uint64_t progress;
77extern const u_char *ctab;
78extern const u_char a2e_32V[], a2e_POSIX[];
79extern const u_char e2a_32V[], e2a_POSIX[];
80extern const u_char a2ibm_32V[], a2ibm_POSIX[];
81extern u_char casetab[];
82extern const char *msgfmt;
diff --git a/toolbox/upstream-netbsd/bin/dd/misc.c b/toolbox/upstream-netbsd/bin/dd/misc.c
deleted file mode 100644
index 0fac98bed..000000000
--- a/toolbox/upstream-netbsd/bin/dd/misc.c
+++ /dev/null
@@ -1,342 +0,0 @@
1/* $NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)misc.c 8.3 (Berkeley) 4/2/94";
40#else
41__RCSID("$NetBSD: misc.c,v 1.23 2011/11/07 22:24:23 jym Exp $");
42#endif
43#endif /* not lint */
44
45#include <sys/param.h>
46#include <sys/types.h>
47#include <sys/time.h>
48
49#include <err.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53#include <unistd.h>
54#include <util.h>
55#include <inttypes.h>
56
57#include "dd.h"
58#include "extern.h"
59
60#define tv2mS(tv) ((tv).tv_sec * 1000LL + ((tv).tv_usec + 500) / 1000)
61
62static void posix_summary(void);
63#ifndef NO_MSGFMT
64static void custom_summary(void);
65static void human_summary(void);
66static void quiet_summary(void);
67
68static void buffer_write(const char *, size_t, int);
69#endif /* NO_MSGFMT */
70
71void
72summary(void)
73{
74
75 if (progress)
76 (void)write(STDERR_FILENO, "\n", 1);
77
78#ifdef NO_MSGFMT
79 return posix_summary();
80#else /* NO_MSGFMT */
81 if (strncmp(msgfmt, "human", sizeof("human")) == 0)
82 return human_summary();
83
84 if (strncmp(msgfmt, "posix", sizeof("posix")) == 0)
85 return posix_summary();
86
87 if (strncmp(msgfmt, "quiet", sizeof("quiet")) == 0)
88 return quiet_summary();
89
90 return custom_summary();
91#endif /* NO_MSGFMT */
92}
93
94static void
95posix_summary(void)
96{
97 char buf[100];
98 int64_t mS;
99 struct timeval tv;
100
101 if (progress)
102 (void)write(STDERR_FILENO, "\n", 1);
103
104 (void)gettimeofday(&tv, NULL);
105 mS = tv2mS(tv) - tv2mS(st.start);
106 if (mS == 0)
107 mS = 1;
108
109 /* Use snprintf(3) so that we don't reenter stdio(3). */
110 (void)snprintf(buf, sizeof(buf),
111 "%llu+%llu records in\n%llu+%llu records out\n",
112 (unsigned long long)st.in_full, (unsigned long long)st.in_part,
113 (unsigned long long)st.out_full, (unsigned long long)st.out_part);
114 (void)write(STDERR_FILENO, buf, strlen(buf));
115 if (st.swab) {
116 (void)snprintf(buf, sizeof(buf), "%llu odd length swab %s\n",
117 (unsigned long long)st.swab,
118 (st.swab == 1) ? "block" : "blocks");
119 (void)write(STDERR_FILENO, buf, strlen(buf));
120 }
121 if (st.trunc) {
122 (void)snprintf(buf, sizeof(buf), "%llu truncated %s\n",
123 (unsigned long long)st.trunc,
124 (st.trunc == 1) ? "block" : "blocks");
125 (void)write(STDERR_FILENO, buf, strlen(buf));
126 }
127 if (st.sparse) {
128 (void)snprintf(buf, sizeof(buf), "%llu sparse output %s\n",
129 (unsigned long long)st.sparse,
130 (st.sparse == 1) ? "block" : "blocks");
131 (void)write(STDERR_FILENO, buf, strlen(buf));
132 }
133 (void)snprintf(buf, sizeof(buf),
134 "%llu bytes transferred in %lu.%03d secs (%llu bytes/sec)\n",
135 (unsigned long long) st.bytes,
136 (long) (mS / 1000),
137 (int) (mS % 1000),
138 (unsigned long long) (st.bytes * 1000LL / mS));
139 (void)write(STDERR_FILENO, buf, strlen(buf));
140}
141
142/* ARGSUSED */
143void
144summaryx(int notused)
145{
146
147 summary();
148}
149
150/* ARGSUSED */
151void
152terminate(int signo)
153{
154
155 summary();
156 (void)raise_default_signal(signo);
157 _exit(127);
158}
159
160#ifndef NO_MSGFMT
161/*
162 * Buffer write(2) calls
163 */
164static void
165buffer_write(const char *str, size_t size, int flush)
166{
167 static char wbuf[128];
168 static size_t cnt = 0; /* Internal counter to allow wbuf to wrap */
169
170 unsigned int i;
171
172 for (i = 0; i < size; i++) {
173 if (str != NULL) {
174 wbuf[cnt++] = str[i];
175 }
176 if (cnt >= sizeof(wbuf)) {
177 (void)write(STDERR_FILENO, wbuf, cnt);
178 cnt = 0;
179 }
180 }
181
182 if (flush != 0) {
183 (void)write(STDERR_FILENO, wbuf, cnt);
184 cnt = 0;
185 }
186}
187
188/*
189 * Write summary to stderr according to format 'fmt'. If 'enable' is 0, it
190 * will not attempt to write anything. Can be used to validate the
191 * correctness of the 'fmt' string.
192 */
193int
194dd_write_msg(const char *fmt, int enable)
195{
196 char hbuf[7], nbuf[32];
197 const char *ptr;
198 int64_t mS;
199 struct timeval tv;
200
201 (void)gettimeofday(&tv, NULL);
202 mS = tv2mS(tv) - tv2mS(st.start);
203 if (mS == 0)
204 mS = 1;
205
206#define ADDC(c) do { if (enable != 0) buffer_write(&c, 1, 0); } \
207 while (/*CONSTCOND*/0)
208#define ADDS(p) do { if (enable != 0) buffer_write(p, strlen(p), 0); } \
209 while (/*CONSTCOND*/0)
210
211 for (ptr = fmt; *ptr; ptr++) {
212 if (*ptr != '%') {
213 ADDC(*ptr);
214 continue;
215 }
216
217 switch (*++ptr) {
218 case 'b':
219 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
220 (unsigned long long)st.bytes);
221 ADDS(nbuf);
222 break;
223 case 'B':
224 if (humanize_number(hbuf, sizeof(hbuf),
225 st.bytes, "B",
226 HN_AUTOSCALE, HN_DECIMAL) == -1)
227 warnx("humanize_number (bytes transferred)");
228 ADDS(hbuf);
229 break;
230 case 'e':
231 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
232 (unsigned long long) (st.bytes * 1000LL / mS));
233 ADDS(nbuf);
234 break;
235 case 'E':
236 if (humanize_number(hbuf, sizeof(hbuf),
237 st.bytes * 1000LL / mS, "B",
238 HN_AUTOSCALE, HN_DECIMAL) == -1)
239 warnx("humanize_number (bytes per second)");
240 ADDS(hbuf); ADDS("/sec");
241 break;
242 case 'i':
243 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
244 (unsigned long long)st.in_part);
245 ADDS(nbuf);
246 break;
247 case 'I':
248 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
249 (unsigned long long)st.in_full);
250 ADDS(nbuf);
251 break;
252 case 'o':
253 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
254 (unsigned long long)st.out_part);
255 ADDS(nbuf);
256 break;
257 case 'O':
258 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
259 (unsigned long long)st.out_full);
260 ADDS(nbuf);
261 break;
262 case 's':
263 (void)snprintf(nbuf, sizeof(nbuf), "%li.%03d",
264 (long) (mS / 1000), (int) (mS % 1000));
265 ADDS(nbuf);
266 break;
267 case 'p':
268 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
269 (unsigned long long)st.sparse);
270 ADDS(nbuf);
271 break;
272 case 't':
273 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
274 (unsigned long long)st.trunc);
275 ADDS(nbuf);
276 break;
277 case 'w':
278 (void)snprintf(nbuf, sizeof(nbuf), "%llu",
279 (unsigned long long)st.swab);
280 ADDS(nbuf);
281 break;
282 case 'P':
283 ADDS("block");
284 if (st.sparse != 1) ADDS("s");
285 break;
286 case 'T':
287 ADDS("block");
288 if (st.trunc != 1) ADDS("s");
289 break;
290 case 'W':
291 ADDS("block");
292 if (st.swab != 1) ADDS("s");
293 break;
294 case '%':
295 ADDC(*ptr);
296 break;
297 default:
298 if (*ptr == '\0')
299 goto done;
300 errx(EXIT_FAILURE, "unknown specifier '%c' in "
301 "msgfmt string", *ptr);
302 /* NOTREACHED */
303 }
304 }
305
306done:
307 /* flush buffer */
308 buffer_write(NULL, 0, 1);
309 return 0;
310}
311
312static void
313custom_summary(void)
314{
315
316 dd_write_msg(msgfmt, 1);
317}
318
319static void
320human_summary(void)
321{
322 (void)dd_write_msg("%I+%i records in\n%O+%o records out\n", 1);
323 if (st.swab) {
324 (void)dd_write_msg("%w odd length swab %W\n", 1);
325 }
326 if (st.trunc) {
327 (void)dd_write_msg("%t truncated %T\n", 1);
328 }
329 if (st.sparse) {
330 (void)dd_write_msg("%p sparse output %P\n", 1);
331 }
332 (void)dd_write_msg("%b bytes (%B) transferred in %s secs "
333 "(%e bytes/sec - %E)\n", 1);
334}
335
336static void
337quiet_summary(void)
338{
339
340 /* stay quiet */
341}
342#endif /* NO_MSGFMT */
diff --git a/toolbox/upstream-netbsd/bin/dd/position.c b/toolbox/upstream-netbsd/bin/dd/position.c
deleted file mode 100644
index 36dd580a3..000000000
--- a/toolbox/upstream-netbsd/bin/dd/position.c
+++ /dev/null
@@ -1,185 +0,0 @@
1/* $NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Keith Muller of the University of California, San Diego and Lance
9 * Visser of Convex Computer Corporation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38#if 0
39static char sccsid[] = "@(#)position.c 8.3 (Berkeley) 4/2/94";
40#else
41__RCSID("$NetBSD: position.c,v 1.18 2010/11/22 21:04:28 pooka Exp $");
42#endif
43#endif /* not lint */
44
45#include <sys/types.h>
46#include <sys/stat.h>
47#include <sys/ioctl.h>
48#include <sys/mtio.h>
49#include <sys/time.h>
50
51#include <err.h>
52#include <errno.h>
53#include <stdlib.h>
54#include <string.h>
55#include <unistd.h>
56
57#include "dd.h"
58#include "extern.h"
59
60/*
61 * Position input/output data streams before starting the copy. Device type
62 * dependent. Seekable devices use lseek, and the rest position by reading.
63 * Seeking past the end of file can cause null blocks to be written to the
64 * output.
65 */
66void
67pos_in(void)
68{
69 int bcnt, cnt, nr, warned;
70
71 /* If not a pipe or tape device, try to seek on it. */
72 if (!(in.flags & (ISPIPE|ISTAPE))) {
73 if (ddop_lseek(in, in.fd,
74 (off_t)in.offset * (off_t)in.dbsz, SEEK_CUR) == -1) {
75 err(EXIT_FAILURE, "%s", in.name);
76 /* NOTREACHED */
77 }
78 return;
79 /* NOTREACHED */
80 }
81
82 /*
83 * Read the data. If a pipe, read until satisfy the number of bytes
84 * being skipped. No differentiation for reading complete and partial
85 * blocks for other devices.
86 */
87 for (bcnt = in.dbsz, cnt = in.offset, warned = 0; cnt;) {
88 if ((nr = ddop_read(in, in.fd, in.db, bcnt)) > 0) {
89 if (in.flags & ISPIPE) {
90 if (!(bcnt -= nr)) {
91 bcnt = in.dbsz;
92 --cnt;
93 }
94 } else
95 --cnt;
96 continue;
97 }
98
99 if (nr == 0) {
100 if (files_cnt > 1) {
101 --files_cnt;
102 continue;
103 }
104 errx(EXIT_FAILURE, "skip reached end of input");
105 /* NOTREACHED */
106 }
107
108 /*
109 * Input error -- either EOF with no more files, or I/O error.
110 * If noerror not set die. POSIX requires that the warning
111 * message be followed by an I/O display.
112 */
113 if (ddflags & C_NOERROR) {
114 if (!warned) {
115
116 warn("%s", in.name);
117 warned = 1;
118 summary();
119 }
120 continue;
121 }
122 err(EXIT_FAILURE, "%s", in.name);
123 /* NOTREACHED */
124 }
125}
126
127void
128pos_out(void)
129{
130 struct mtop t_op;
131 int n;
132 uint64_t cnt;
133
134 /*
135 * If not a tape, try seeking on the file. Seeking on a pipe is
136 * going to fail, but don't protect the user -- they shouldn't
137 * have specified the seek operand.
138 */
139 if (!(out.flags & ISTAPE)) {
140 if (ddop_lseek(out, out.fd,
141 (off_t)out.offset * (off_t)out.dbsz, SEEK_SET) == -1)
142 err(EXIT_FAILURE, "%s", out.name);
143 /* NOTREACHED */
144 return;
145 }
146
147 /* If no read access, try using mtio. */
148 if (out.flags & NOREAD) {
149 t_op.mt_op = MTFSR;
150 t_op.mt_count = out.offset;
151
152 if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) < 0)
153 err(EXIT_FAILURE, "%s", out.name);
154 /* NOTREACHED */
155 return;
156 }
157
158 /* Read it. */
159 for (cnt = 0; cnt < out.offset; ++cnt) {
160 if ((n = ddop_read(out, out.fd, out.db, out.dbsz)) > 0)
161 continue;
162
163 if (n < 0)
164 err(EXIT_FAILURE, "%s", out.name);
165 /* NOTREACHED */
166
167 /*
168 * If reach EOF, fill with NUL characters; first, back up over
169 * the EOF mark. Note, cnt has not yet been incremented, so
170 * the EOF read does not count as a seek'd block.
171 */
172 t_op.mt_op = MTBSR;
173 t_op.mt_count = 1;
174 if (ddop_ioctl(out, out.fd, MTIOCTOP, &t_op) == -1)
175 err(EXIT_FAILURE, "%s", out.name);
176 /* NOTREACHED */
177
178 while (cnt++ < out.offset)
179 if ((uint64_t)(n = bwrite(&out,
180 out.db, out.dbsz)) != out.dbsz)
181 err(EXIT_FAILURE, "%s", out.name);
182 /* NOTREACHED */
183 break;
184 }
185}
diff --git a/toolbox/upstream-netbsd/include/namespace.h b/toolbox/upstream-netbsd/include/namespace.h
deleted file mode 100644
index e69de29bb..000000000
--- a/toolbox/upstream-netbsd/include/namespace.h
+++ /dev/null
diff --git a/toolbox/upstream-netbsd/include/sys/extattr.h b/toolbox/upstream-netbsd/include/sys/extattr.h
deleted file mode 100644
index e69de29bb..000000000
--- a/toolbox/upstream-netbsd/include/sys/extattr.h
+++ /dev/null
diff --git a/toolbox/upstream-netbsd/include/util.h b/toolbox/upstream-netbsd/include/util.h
deleted file mode 100644
index e69de29bb..000000000
--- a/toolbox/upstream-netbsd/include/util.h
+++ /dev/null
diff --git a/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c b/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c
deleted file mode 100644
index a9ce2c14e..000000000
--- a/toolbox/upstream-netbsd/lib/libc/gen/getbsize.c
+++ /dev/null
@@ -1,118 +0,0 @@
1/* $NetBSD: getbsize.c,v 1.17 2012/06/25 22:32:43 abs Exp $ */
2
3/*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33#if defined(LIBC_SCCS) && !defined(lint)
34#if 0
35static char sccsid[] = "@(#)getbsize.c 8.1 (Berkeley) 6/4/93";
36#else
37__RCSID("$NetBSD: getbsize.c,v 1.17 2012/06/25 22:32:43 abs Exp $");
38#endif
39#endif /* not lint */
40
41#include "namespace.h"
42
43#include <assert.h>
44#include <err.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48
49#ifdef __weak_alias
50__weak_alias(getbsize,_getbsize)
51#endif
52
53char *
54getbsize(int *headerlenp, long *blocksizep)
55{
56 static char header[20];
57 long n, max, mul, blocksize;
58 char *ep, *p;
59 const char *form;
60
61#define KB (1024L)
62#define MB (1024L * 1024L)
63#define GB (1024L * 1024L * 1024L)
64#define MAXB GB /* No tera, peta, nor exa. */
65 form = "";
66 if ((p = getenv("BLOCKSIZE")) != NULL && *p != '\0') {
67 if ((n = strtol(p, &ep, 10)) < 0)
68 goto underflow;
69 if (n == 0)
70 n = 1;
71 if (*ep && ep[1])
72 goto fmterr;
73 switch (*ep) {
74 case 'G': case 'g':
75 form = "G";
76 max = MAXB / GB;
77 mul = GB;
78 break;
79 case 'K': case 'k':
80 form = "K";
81 max = MAXB / KB;
82 mul = KB;
83 break;
84 case 'M': case 'm':
85 form = "M";
86 max = MAXB / MB;
87 mul = MB;
88 break;
89 case '\0':
90 max = MAXB;
91 mul = 1;
92 break;
93 default:
94fmterr: warnx("%s: unknown blocksize", p);
95 n = 512;
96 mul = 1;
97 max = 0;
98 break;
99 }
100 if (n > max) {
101 warnx("maximum blocksize is %ldG", MAXB / GB);
102 n = max;
103 }
104 if ((blocksize = n * mul) < 512) {
105underflow: warnx("%s: minimum blocksize is 512", p);
106 form = "";
107 blocksize = n = 512;
108 }
109 } else
110 blocksize = n = 512;
111
112 if (headerlenp)
113 *headerlenp =
114 snprintf(header, sizeof(header), "%ld%s-blocks", n, form);
115 if (blocksizep)
116 *blocksizep = blocksize;
117 return (header);
118}
diff --git a/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c b/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c
deleted file mode 100644
index 533560f31..000000000
--- a/toolbox/upstream-netbsd/lib/libc/gen/humanize_number.c
+++ /dev/null
@@ -1,161 +0,0 @@
1/* $NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $ */
2
3/*
4 * Copyright (c) 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center, by Luke Mewburn and by Tomas Svensson.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34#if defined(LIBC_SCCS) && !defined(lint)
35__RCSID("$NetBSD: humanize_number.c,v 1.16 2012/03/17 20:01:14 christos Exp $");
36#endif /* LIBC_SCCS and not lint */
37
38#include "namespace.h"
39#include <assert.h>
40#include <inttypes.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <locale.h>
45
46int
47humanize_number(char *buf, size_t len, int64_t bytes,
48 const char *suffix, int scale, int flags)
49{
50 const char *prefixes, *sep;
51 int b, r, s1, s2, sign;
52 int64_t divisor, max, post = 1;
53 size_t i, baselen, maxscale;
54
55 _DIAGASSERT(buf != NULL);
56 _DIAGASSERT(suffix != NULL);
57 _DIAGASSERT(scale >= 0);
58
59 if (flags & HN_DIVISOR_1000) {
60 /* SI for decimal multiplies */
61 divisor = 1000;
62 if (flags & HN_B)
63 prefixes = "B\0k\0M\0G\0T\0P\0E";
64 else
65 prefixes = "\0\0k\0M\0G\0T\0P\0E";
66 } else {
67 /*
68 * binary multiplies
69 * XXX IEC 60027-2 recommends Ki, Mi, Gi...
70 */
71 divisor = 1024;
72 if (flags & HN_B)
73 prefixes = "B\0K\0M\0G\0T\0P\0E";
74 else
75 prefixes = "\0\0K\0M\0G\0T\0P\0E";
76 }
77
78#define SCALE2PREFIX(scale) (&prefixes[(scale) << 1])
79 maxscale = 7;
80
81 if ((size_t)scale >= maxscale &&
82 (scale & (HN_AUTOSCALE | HN_GETSCALE)) == 0)
83 return (-1);
84
85 if (buf == NULL || suffix == NULL)
86 return (-1);
87
88 if (len > 0)
89 buf[0] = '\0';
90 if (bytes < 0) {
91 sign = -1;
92 baselen = 3; /* sign, digit, prefix */
93 if (-bytes < INT64_MAX / 100)
94 bytes *= -100;
95 else {
96 bytes = -bytes;
97 post = 100;
98 baselen += 2;
99 }
100 } else {
101 sign = 1;
102 baselen = 2; /* digit, prefix */
103 if (bytes < INT64_MAX / 100)
104 bytes *= 100;
105 else {
106 post = 100;
107 baselen += 2;
108 }
109 }
110 if (flags & HN_NOSPACE)
111 sep = "";
112 else {
113 sep = " ";
114 baselen++;
115 }
116 baselen += strlen(suffix);
117
118 /* Check if enough room for `x y' + suffix + `\0' */
119 if (len < baselen + 1)
120 return (-1);
121
122 if (scale & (HN_AUTOSCALE | HN_GETSCALE)) {
123 /* See if there is additional columns can be used. */
124 for (max = 100, i = len - baselen; i-- > 0;)
125 max *= 10;
126
127 /*
128 * Divide the number until it fits the given column.
129 * If there will be an overflow by the rounding below,
130 * divide once more.
131 */
132 for (i = 0; bytes >= max - 50 && i < maxscale; i++)
133 bytes /= divisor;
134
135 if (scale & HN_GETSCALE) {
136 _DIAGASSERT(__type_fit(int, i));
137 return (int)i;
138 }
139 } else
140 for (i = 0; i < (size_t)scale && i < maxscale; i++)
141 bytes /= divisor;
142 bytes *= post;
143
144 /* If a value <= 9.9 after rounding and ... */
145 if (bytes < 995 && i > 0 && flags & HN_DECIMAL) {
146 /* baselen + \0 + .N */
147 if (len < baselen + 1 + 2)
148 return (-1);
149 b = ((int)bytes + 5) / 10;
150 s1 = b / 10;
151 s2 = b % 10;
152 r = snprintf(buf, len, "%d%s%d%s%s%s",
153 sign * s1, localeconv()->decimal_point, s2,
154 sep, SCALE2PREFIX(i), suffix);
155 } else
156 r = snprintf(buf, len, "%" PRId64 "%s%s%s",
157 sign * ((bytes + 50) / 100),
158 sep, SCALE2PREFIX(i), suffix);
159
160 return (r);
161}
diff --git a/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c b/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c
deleted file mode 100644
index 80fc52fc9..000000000
--- a/toolbox/upstream-netbsd/lib/libc/stdlib/strsuftoll.c
+++ /dev/null
@@ -1,249 +0,0 @@
1/* $NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $ */
2/*-
3 * Copyright (c) 2001-2002,2004 The NetBSD Foundation, Inc.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Luke Mewburn.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30/*-
31 * Copyright (c) 1991, 1993, 1994
32 * The Regents of the University of California. All rights reserved.
33 *
34 * This code is derived from software contributed to Berkeley by
35 * Keith Muller of the University of California, San Diego and Lance
36 * Visser of Convex Computer Corporation.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * SUCH DAMAGE.
61 */
62
63#if HAVE_NBTOOL_CONFIG_H
64#include "nbtool_config.h"
65#endif
66
67#include <sys/cdefs.h>
68
69#if defined(LIBC_SCCS) && !defined(lint)
70__RCSID("$NetBSD: strsuftoll.c,v 1.9 2011/10/22 22:08:47 christos Exp $");
71#endif /* LIBC_SCCS and not lint */
72
73#ifdef _LIBC
74#include "namespace.h"
75#endif
76
77#if !HAVE_STRSUFTOLL
78
79#include <sys/types.h>
80#include <sys/time.h>
81
82#include <assert.h>
83#include <ctype.h>
84#include <err.h>
85#include <errno.h>
86#include <limits.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90
91#ifdef _LIBC
92# ifdef __weak_alias
93__weak_alias(strsuftoll, _strsuftoll)
94__weak_alias(strsuftollx, _strsuftollx)
95# endif
96#endif /* LIBC */
97
98/*
99 * Convert an expression of the following forms to a (u)int64_t.
100 * 1) A positive decimal number.
101 * 2) A positive decimal number followed by a b (mult by 512).
102 * 3) A positive decimal number followed by a k (mult by 1024).
103 * 4) A positive decimal number followed by a m (mult by 1048576).
104 * 5) A positive decimal number followed by a g (mult by 1073741824).
105 * 6) A positive decimal number followed by a t (mult by 1099511627776).
106 * 7) A positive decimal number followed by a w (mult by sizeof int)
107 * 8) Two or more positive decimal numbers (with/without k,b or w).
108 * separated by x (also * for backwards compatibility), specifying
109 * the product of the indicated values.
110 * Returns the result upon successful conversion, or exits with an
111 * appropriate error.
112 *
113 */
114/* LONGLONG */
115long long
116strsuftoll(const char *desc, const char *val,
117 long long min, long long max)
118{
119 long long result;
120 char errbuf[100];
121
122 result = strsuftollx(desc, val, min, max, errbuf, sizeof(errbuf));
123 if (*errbuf != '\0')
124 errx(EXIT_FAILURE, "%s", errbuf);
125 return result;
126}
127
128/*
129 * As strsuftoll(), but returns the error message into the provided buffer
130 * rather than exiting with it.
131 */
132/* LONGLONG */
133static long long
134__strsuftollx(const char *desc, const char *val,
135 long long min, long long max, char *ebuf, size_t ebuflen, size_t depth)
136{
137 long long num, t;
138 char *expr;
139
140 _DIAGASSERT(desc != NULL);
141 _DIAGASSERT(val != NULL);
142 _DIAGASSERT(ebuf != NULL);
143
144 if (depth > 16) {
145 snprintf(ebuf, ebuflen, "%s: Recursion limit exceeded", desc);
146 return 0;
147 }
148
149 while (isspace((unsigned char)*val)) /* Skip leading space */
150 val++;
151
152 errno = 0;
153 num = strtoll(val, &expr, 10);
154 if (errno == ERANGE)
155 goto erange; /* Overflow */
156
157 if (expr == val) /* No digits */
158 goto badnum;
159
160 switch (*expr) {
161 case 'b':
162 t = num;
163 num *= 512; /* 1 block */
164 if (t > num)
165 goto erange;
166 ++expr;
167 break;
168 case 'k':
169 t = num;
170 num *= 1024; /* 1 kibibyte */
171 if (t > num)
172 goto erange;
173 ++expr;
174 break;
175 case 'm':
176 t = num;
177 num *= 1048576; /* 1 mebibyte */
178 if (t > num)
179 goto erange;
180 ++expr;
181 break;
182 case 'g':
183 t = num;
184 num *= 1073741824; /* 1 gibibyte */
185 if (t > num)
186 goto erange;
187 ++expr;
188 break;
189 case 't':
190 t = num;
191 num *= 1099511627776LL; /* 1 tebibyte */
192 if (t > num)
193 goto erange;
194 ++expr;
195 break;
196 case 'w':
197 t = num;
198 num *= sizeof(int); /* 1 word */
199 if (t > num)
200 goto erange;
201 ++expr;
202 break;
203 }
204
205 switch (*expr) {
206 case '\0':
207 break;
208 case '*': /* Backward compatible */
209 case 'x':
210 t = num;
211 num *= __strsuftollx(desc, expr + 1, min, max, ebuf, ebuflen,
212 depth + 1);
213 if (*ebuf != '\0')
214 return 0;
215 if (t > num) {
216 erange:
217 errno = ERANGE;
218 snprintf(ebuf, ebuflen, "%s: %s", desc, strerror(errno));
219 return 0;
220 }
221 break;
222 default:
223 badnum:
224 snprintf(ebuf, ebuflen, "%s `%s': illegal number", desc, val);
225 return 0;
226 }
227 if (num < min) {
228 /* LONGLONG */
229 snprintf(ebuf, ebuflen, "%s %lld is less than %lld.",
230 desc, (long long)num, (long long)min);
231 return 0;
232 }
233 if (num > max) {
234 /* LONGLONG */
235 snprintf(ebuf, ebuflen, "%s %lld is greater than %lld.",
236 desc, (long long)num, (long long)max);
237 return 0;
238 }
239 *ebuf = '\0';
240 return num;
241}
242
243long long
244strsuftollx(const char *desc, const char *val,
245 long long min, long long max, char *ebuf, size_t ebuflen)
246{
247 return __strsuftollx(desc, val, min, max, ebuf, ebuflen, 0);
248}
249#endif /* !HAVE_STRSUFTOLL */
diff --git a/toolbox/upstream-netbsd/lib/libc/string/swab.c b/toolbox/upstream-netbsd/lib/libc/string/swab.c
deleted file mode 100644
index 392b1868e..000000000
--- a/toolbox/upstream-netbsd/lib/libc/string/swab.c
+++ /dev/null
@@ -1,80 +0,0 @@
1/* $NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $ */
2
3/*
4 * Copyright (c) 1988, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jeffrey Mogul.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35#include <sys/cdefs.h>
36#if defined(LIBC_SCCS) && !defined(lint)
37#if 0
38static char sccsid[] = "@(#)swab.c 8.1 (Berkeley) 6/4/93";
39#else
40__RCSID("$NetBSD: swab.c,v 1.18 2011/01/04 17:14:07 martin Exp $");
41#endif
42#endif /* LIBC_SCCS and not lint */
43
44#include <assert.h>
45#include <unistd.h>
46
47void
48swab(const void * __restrict from, void * __restrict to, ssize_t len)
49{
50 char temp;
51 const char *fp;
52 char *tp;
53
54 if (len <= 1)
55 return;
56
57 _DIAGASSERT(from != NULL);
58 _DIAGASSERT(to != NULL);
59
60 len /= 2;
61 fp = (const char *)from;
62 tp = (char *)to;
63#define STEP temp = *fp++,*tp++ = *fp++,*tp++ = temp
64
65 if (__predict_false(len == 1)) {
66 STEP;
67 return;
68 }
69
70 /* round to multiple of 8 */
71 while ((--len % 8) != 0)
72 STEP;
73 len /= 8;
74 if (len == 0)
75 return;
76 while (len-- != 0) {
77 STEP; STEP; STEP; STEP;
78 STEP; STEP; STEP; STEP;
79 }
80}
diff --git a/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c b/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c
deleted file mode 100644
index 50cffd440..000000000
--- a/toolbox/upstream-netbsd/lib/libutil/raise_default_signal.c
+++ /dev/null
@@ -1,117 +0,0 @@
1/* $NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $ */
2
3/*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Luke Mewburn.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#if HAVE_NBTOOL_CONFIG_H
33#include "nbtool_config.h"
34#endif
35
36#include <sys/cdefs.h>
37#if defined(LIBC_SCCS) && !defined(lint)
38__RCSID("$NetBSD: raise_default_signal.c,v 1.3 2008/04/28 20:23:03 martin Exp $");
39#endif
40
41#include <errno.h>
42#include <signal.h>
43#include <stdio.h>
44#include <string.h>
45#include <util.h>
46
47#if ! HAVE_RAISE_DEFAULT_SIGNAL
48/*
49 * raise_default_signal sig
50 * Raise the default signal handler for sig, by
51 * - block all signals
52 * - set the signal handler to SIG_DFL
53 * - raise the signal
54 * - unblock the signal to deliver it
55 *
56 * The original signal mask and signal handler is restored on exit
57 * (whether successful or not).
58 *
59 * Returns 0 on success, or -1 on failure with errno set to
60 * on of the values for sigemptyset(), sigaddset(), sigprocmask(),
61 * sigaction(), or raise().
62 */
63int
64raise_default_signal(int sig)
65{
66 struct sigaction origact, act;
67 sigset_t origmask, fullmask, mask;
68 int retval, oerrno;
69
70 retval = -1;
71
72 /* Setup data structures */
73 /* XXX memset(3) isn't async-safe according to signal(7) */
74 (void)memset(&act, 0, sizeof(act));
75 act.sa_handler = SIG_DFL;
76 act.sa_flags = 0;
77 if ((sigemptyset(&act.sa_mask) == -1) ||
78 (sigfillset(&fullmask) == -1) ||
79 (sigemptyset(&mask) == -1) ||
80 (sigaddset(&mask, sig) == -1))
81 goto restore_none;
82
83 /* Block all signals */
84 if (sigprocmask(SIG_BLOCK, &fullmask, &origmask) == -1)
85 goto restore_none;
86 /* (use 'goto restore_mask' to restore state) */
87
88 /* Enable the SIG_DFL handler */
89 if (sigaction(sig, &act, &origact) == -1)
90 goto restore_mask;
91 /* (use 'goto restore_act' to restore state) */
92
93 /* Raise the signal, and unblock the signal to deliver it */
94 if ((raise(sig) == -1) ||
95 (sigprocmask(SIG_UNBLOCK, &mask, NULL) == -1))
96 goto restore_act;
97
98 /* Flag successful raise() */
99 retval = 0;
100
101 /* Restore the original handler */
102 restore_act:
103 oerrno = errno;
104 (void)sigaction(sig, &origact, NULL);
105 errno = oerrno;
106
107 /* Restore the original mask */
108 restore_mask:
109 oerrno = errno;
110 (void)sigprocmask(SIG_SETMASK, &origmask, NULL);
111 errno = oerrno;
112
113 restore_none:
114 return retval;
115}
116
117#endif /* ! HAVE_RAISE_DEFAULT_SIGNAL */