Drop main() prototype. Syncs with NetBSD-8
[minix.git] / bin / dd / args.c
blob8f4ed964eae9a90ad78d531791dd9095aba16403
1 /* $NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
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.
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.
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.
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
40 #else
41 __RCSID("$NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $");
42 #endif
43 #endif /* not lint */
45 #include <sys/types.h>
46 #include <sys/time.h>
48 #ifndef NO_IOFLAG
49 #include <fcntl.h>
50 #endif /* NO_IOFLAG */
51 #include <err.h>
52 #include <errno.h>
53 #include <limits.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
58 #include "dd.h"
59 #include "extern.h"
61 static int c_arg(const void *, const void *);
63 #ifdef NO_MSGFMT
64 static void f_msgfmt(char *) __dead;
65 #else
66 static void f_msgfmt(char *);
67 #endif /* NO_MSGFMT */
69 #ifdef NO_CONV
70 static void f_conv(char *) __dead;
71 #else
72 static void f_conv(char *);
73 static int c_conv(const void *, const void *);
74 #endif /* NO_CONV */
76 #ifdef NO_IOFLAG
77 static void f_iflag(char *) __dead;
78 static void f_oflag(char *) __dead;
79 #else
80 static void f_iflag(char *);
81 static void f_oflag(char *);
82 static u_int f_ioflag(char *, u_int);
83 static int c_ioflag(const void *, const void *);
84 #endif /* NO_IOFLAG */
86 static void f_bs(char *);
87 static void f_cbs(char *);
88 static void f_count(char *);
89 static void f_files(char *);
90 static void f_ibs(char *);
91 static void f_if(char *);
92 static void f_obs(char *);
93 static void f_of(char *);
94 static void f_seek(char *);
95 static void f_skip(char *);
96 static void f_progress(char *);
98 static const struct arg {
99 const char *name;
100 void (*f)(char *);
101 u_int set, noset;
102 } args[] = {
103 /* the array needs to be sorted by the first column so
104 bsearch() can be used to find commands quickly */
105 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
106 { "cbs", f_cbs, C_CBS, C_CBS },
107 { "conv", f_conv, 0, 0 },
108 { "count", f_count, C_COUNT, C_COUNT },
109 { "files", f_files, C_FILES, C_FILES },
110 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
111 { "if", f_if, C_IF, C_IF },
112 { "iflag", f_iflag, C_IFLAG, C_IFLAG },
113 { "iseek", f_skip, C_SKIP, C_SKIP },
114 { "msgfmt", f_msgfmt, 0, 0 },
115 { "obs", f_obs, C_OBS, C_BS|C_OBS },
116 { "of", f_of, C_OF, C_OF },
117 { "oflag", f_oflag, C_OFLAG, C_OFLAG },
118 { "oseek", f_seek, C_SEEK, C_SEEK },
119 { "progress", f_progress, 0, 0 },
120 { "seek", f_seek, C_SEEK, C_SEEK },
121 { "skip", f_skip, C_SKIP, C_SKIP },
125 * args -- parse JCL syntax of dd.
127 void
128 jcl(char **argv)
130 struct arg *ap, tmp;
131 char *oper, *arg;
133 in.dbsz = out.dbsz = 512;
135 while ((oper = *++argv) != NULL) {
136 if ((oper = strdup(oper)) == NULL) {
137 errx(EXIT_FAILURE,
138 "unable to allocate space for the argument %s",
139 *argv);
140 /* NOTREACHED */
142 if ((arg = strchr(oper, '=')) == NULL) {
143 errx(EXIT_FAILURE, "unknown operand %s", oper);
144 /* NOTREACHED */
146 *arg++ = '\0';
147 if (!*arg) {
148 errx(EXIT_FAILURE, "no value specified for %s", oper);
149 /* NOTREACHED */
151 tmp.name = oper;
152 if (!(ap = bsearch(&tmp, args,
153 __arraycount(args), sizeof(*args), c_arg))) {
154 errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
155 /* NOTREACHED */
157 if (ddflags & ap->noset) {
158 errx(EXIT_FAILURE,
159 "%s: illegal argument combination or already set",
160 tmp.name);
161 /* NOTREACHED */
163 ddflags |= ap->set;
164 ap->f(arg);
167 /* Final sanity checks. */
169 if (ddflags & C_BS) {
171 * Bs is turned off by any conversion -- we assume the user
172 * just wanted to set both the input and output block sizes
173 * and didn't want the bs semantics, so we don't warn.
175 if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
176 C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
177 ddflags &= ~C_BS;
178 ddflags |= C_IBS|C_OBS;
181 /* Bs supersedes ibs and obs. */
182 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
183 warnx("bs supersedes ibs and obs");
187 * Ascii/ebcdic and cbs implies block/unblock.
188 * Block/unblock requires cbs and vice-versa.
190 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
191 if (!(ddflags & C_CBS)) {
192 errx(EXIT_FAILURE, "record operations require cbs");
193 /* NOTREACHED */
195 cfunc = ddflags & C_BLOCK ? block : unblock;
196 } else if (ddflags & C_CBS) {
197 if (ddflags & (C_ASCII|C_EBCDIC)) {
198 if (ddflags & C_ASCII) {
199 ddflags |= C_UNBLOCK;
200 cfunc = unblock;
201 } else {
202 ddflags |= C_BLOCK;
203 cfunc = block;
205 } else {
206 errx(EXIT_FAILURE,
207 "cbs meaningless if not doing record operations");
208 /* NOTREACHED */
210 } else
211 cfunc = def;
213 /* Read, write and seek calls take off_t as arguments.
215 * The following check is not done because an off_t is a quad
216 * for current NetBSD implementations.
218 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
219 * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
223 static int
224 c_arg(const void *a, const void *b)
227 return (strcmp(((const struct arg *)a)->name,
228 ((const struct arg *)b)->name));
231 static void
232 f_bs(char *arg)
235 in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
238 static void
239 f_cbs(char *arg)
242 cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
245 static void
246 f_count(char *arg)
249 cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
250 if (!cpy_cnt)
251 terminate(0);
254 static void
255 f_files(char *arg)
258 files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
259 if (!files_cnt)
260 terminate(0);
263 static void
264 f_ibs(char *arg)
267 if (!(ddflags & C_BS))
268 in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
271 static void
272 f_if(char *arg)
275 in.name = arg;
278 #ifdef NO_MSGFMT
279 /* Build a small version (i.e. for a ramdisk root) */
280 static void
281 f_msgfmt(char *arg)
284 errx(EXIT_FAILURE, "msgfmt option disabled");
285 /* NOTREACHED */
287 #else /* NO_MSGFMT */
288 static void
289 f_msgfmt(char *arg)
293 * If the format string is not valid, dd_write_msg() will print
294 * an error and exit.
296 dd_write_msg(arg, 0);
298 msgfmt = arg;
300 #endif /* NO_MSGFMT */
302 static void
303 f_obs(char *arg)
306 if (!(ddflags & C_BS))
307 out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
310 static void
311 f_of(char *arg)
314 out.name = arg;
317 static void
318 f_seek(char *arg)
321 out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
324 static void
325 f_skip(char *arg)
328 in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
331 static void
332 f_progress(char *arg)
335 progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
338 #ifdef NO_CONV
339 /* Build a small version (i.e. for a ramdisk root) */
340 static void
341 f_conv(char *arg)
344 errx(EXIT_FAILURE, "conv option disabled");
345 /* NOTREACHED */
347 #else /* NO_CONV */
349 static const struct conv {
350 const char *name;
351 u_int set, noset;
352 const u_char *ctab;
353 } clist[] = {
354 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
355 { "block", C_BLOCK, C_UNBLOCK, NULL },
356 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
357 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
358 { "lcase", C_LCASE, C_UCASE, NULL },
359 { "noerror", C_NOERROR, 0, NULL },
360 { "notrunc", C_NOTRUNC, 0, NULL },
361 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
362 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
363 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
364 { "osync", C_OSYNC, C_BS, NULL },
365 { "sparse", C_SPARSE, 0, NULL },
366 { "swab", C_SWAB, 0, NULL },
367 { "sync", C_SYNC, 0, NULL },
368 { "ucase", C_UCASE, C_LCASE, NULL },
369 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
370 /* If you add items to this table, be sure to add the
371 * conversions to the C_BS check in the jcl routine above.
375 static void
376 f_conv(char *arg)
378 struct conv *cp, tmp;
380 while (arg != NULL) {
381 tmp.name = strsep(&arg, ",");
382 if (!(cp = bsearch(&tmp, clist,
383 __arraycount(clist), sizeof(*clist), c_conv))) {
384 errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
385 /* NOTREACHED */
387 if (ddflags & cp->noset) {
388 errx(EXIT_FAILURE,
389 "%s: illegal conversion combination", tmp.name);
390 /* NOTREACHED */
392 ddflags |= cp->set;
393 if (cp->ctab)
394 ctab = cp->ctab;
398 static int
399 c_conv(const void *a, const void *b)
402 return (strcmp(((const struct conv *)a)->name,
403 ((const struct conv *)b)->name));
406 #endif /* NO_CONV */
408 static void
409 f_iflag(char *arg)
411 /* Build a small version (i.e. for a ramdisk root) */
412 #ifdef NO_IOFLAG
413 errx(EXIT_FAILURE, "iflag option disabled");
414 /* NOTREACHED */
415 #else
416 iflag = f_ioflag(arg, C_IFLAG);
417 return;
418 #endif
421 static void
422 f_oflag(char *arg)
424 /* Build a small version (i.e. for a ramdisk root) */
425 #ifdef NO_IOFLAG
426 errx(EXIT_FAILURE, "oflag option disabled");
427 /* NOTREACHED */
428 #else
429 oflag = f_ioflag(arg, C_OFLAG);
430 return;
431 #endif
434 #ifndef NO_IOFLAG
435 static const struct ioflag {
436 const char *name;
437 u_int set;
438 u_int allowed;
439 } olist[] = {
440 /* the array needs to be sorted by the first column so
441 bsearch() can be used to find commands quickly */
442 { "alt_io", O_ALT_IO, C_IFLAG|C_OFLAG },
443 { "append", O_APPEND, C_OFLAG },
444 { "async", O_ASYNC, C_IFLAG|C_OFLAG },
445 { "cloexec", O_CLOEXEC, C_IFLAG|C_OFLAG },
446 { "creat", O_CREAT, C_OFLAG },
447 { "direct", O_DIRECT, C_IFLAG|C_OFLAG },
448 { "directory", O_DIRECTORY, C_NONE },
449 { "dsync", O_DSYNC, C_OFLAG },
450 { "excl", O_EXCL, C_IFLAG|C_OFLAG },
451 { "exlock", O_EXLOCK, C_IFLAG|C_OFLAG },
452 { "noctty", O_NOCTTY, C_IFLAG|C_OFLAG },
453 { "nofollow", O_NOFOLLOW, C_IFLAG|C_OFLAG },
454 { "nonblock", O_NONBLOCK, C_IFLAG|C_OFLAG },
455 { "nosigpipe", O_NOSIGPIPE, C_IFLAG|C_OFLAG },
456 { "rdonly", O_RDONLY, C_IFLAG },
457 { "rdwr", O_RDWR, C_IFLAG },
458 { "rsync", O_RSYNC, C_IFLAG },
459 { "search", O_SEARCH, C_IFLAG|C_OFLAG },
460 { "shlock", O_SHLOCK, C_IFLAG|C_OFLAG },
461 { "sync", O_SYNC, C_IFLAG|C_OFLAG },
462 { "trunc", O_TRUNC, C_IFLAG|C_OFLAG },
463 { "wronly", O_WRONLY, C_NONE },
466 static u_int
467 f_ioflag(char *arg, u_int flagtype)
469 u_int ioflag = 0;
470 struct ioflag *cp, tmp;
471 const char *flagstr = (flagtype == C_IFLAG) ? "iflag" : "oflag";
473 while (arg != NULL) {
474 tmp.name = strsep(&arg, ",");
475 if (!(cp = bsearch(&tmp, olist,
476 __arraycount(olist), sizeof(*olist), c_ioflag))) {
477 errx(EXIT_FAILURE, "unknown %s %s", flagstr, tmp.name);
478 /* NOTREACHED */
481 if ((cp->set & O_ACCMODE) && (flagtype == C_OFLAG)) {
482 warnx("rdonly, rdwr and wronly are ignored for oflag");
483 continue;
486 if ((cp->allowed & flagtype) == 0) {
487 warnx("%s set for %s but makes no sense",
488 cp->name, flagstr);
491 ioflag |= cp->set;
495 return ioflag;
498 static int
499 c_ioflag(const void *a, const void *b)
502 return (strcmp(((const struct ioflag *)a)->name,
503 ((const struct ioflag *)b)->name));
505 #endif /* NO_IOFLAG */