Sync usage with man page.
[netbsd-mini2440.git] / bin / dd / args.c
blobc7f17dfbdcc4a4ff619ee85ee2ade71201bd5d28
1 /* $NetBSD: args.c,v 1.25 2004/01/17 20:48:57 dbj 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.25 2004/01/17 20:48:57 dbj Exp $");
42 #endif
43 #endif /* not lint */
45 #include <sys/types.h>
46 #include <sys/time.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
55 #include "dd.h"
56 #include "extern.h"
58 static int c_arg(const void *, const void *);
59 #ifndef NO_CONV
60 static int c_conv(const void *, const void *);
61 #endif
62 static void f_bs(char *);
63 static void f_cbs(char *);
64 static void f_conv(char *);
65 static void f_count(char *);
66 static void f_files(char *);
67 static void f_ibs(char *);
68 static void f_if(char *);
69 static void f_obs(char *);
70 static void f_of(char *);
71 static void f_seek(char *);
72 static void f_skip(char *);
73 static void f_progress(char *);
75 static const struct arg {
76 const char *name;
77 void (*f)(char *);
78 u_int set, noset;
79 } args[] = {
80 /* the array needs to be sorted by the first column so
81 bsearch() can be used to find commands quickly */
82 { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
83 { "cbs", f_cbs, C_CBS, C_CBS },
84 { "conv", f_conv, 0, 0 },
85 { "count", f_count, C_COUNT, C_COUNT },
86 { "files", f_files, C_FILES, C_FILES },
87 { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
88 { "if", f_if, C_IF, C_IF },
89 { "obs", f_obs, C_OBS, C_BS|C_OBS },
90 { "of", f_of, C_OF, C_OF },
91 { "progress", f_progress, 0, 0 },
92 { "seek", f_seek, C_SEEK, C_SEEK },
93 { "skip", f_skip, C_SKIP, C_SKIP },
97 * args -- parse JCL syntax of dd.
99 void
100 jcl(char **argv)
102 struct arg *ap, tmp;
103 char *oper, *arg;
105 in.dbsz = out.dbsz = 512;
107 while ((oper = *++argv) != NULL) {
108 if ((arg = strchr(oper, '=')) == NULL) {
109 errx(EXIT_FAILURE, "unknown operand %s", oper);
110 /* NOTREACHED */
112 *arg++ = '\0';
113 if (!*arg) {
114 errx(EXIT_FAILURE, "no value specified for %s", oper);
115 /* NOTREACHED */
117 tmp.name = oper;
118 if (!(ap = (struct arg *)bsearch(&tmp, args,
119 sizeof(args)/sizeof(struct arg), sizeof(struct arg),
120 c_arg))) {
121 errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
122 /* NOTREACHED */
124 if (ddflags & ap->noset) {
125 errx(EXIT_FAILURE,
126 "%s: illegal argument combination or already set",
127 tmp.name);
128 /* NOTREACHED */
130 ddflags |= ap->set;
131 ap->f(arg);
134 /* Final sanity checks. */
136 if (ddflags & C_BS) {
138 * Bs is turned off by any conversion -- we assume the user
139 * just wanted to set both the input and output block sizes
140 * and didn't want the bs semantics, so we don't warn.
142 if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
143 C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
144 ddflags &= ~C_BS;
145 ddflags |= C_IBS|C_OBS;
148 /* Bs supersedes ibs and obs. */
149 if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
150 warnx("bs supersedes ibs and obs");
154 * Ascii/ebcdic and cbs implies block/unblock.
155 * Block/unblock requires cbs and vice-versa.
157 if (ddflags & (C_BLOCK|C_UNBLOCK)) {
158 if (!(ddflags & C_CBS)) {
159 errx(EXIT_FAILURE, "record operations require cbs");
160 /* NOTREACHED */
162 cfunc = ddflags & C_BLOCK ? block : unblock;
163 } else if (ddflags & C_CBS) {
164 if (ddflags & (C_ASCII|C_EBCDIC)) {
165 if (ddflags & C_ASCII) {
166 ddflags |= C_UNBLOCK;
167 cfunc = unblock;
168 } else {
169 ddflags |= C_BLOCK;
170 cfunc = block;
172 } else {
173 errx(EXIT_FAILURE,
174 "cbs meaningless if not doing record operations");
175 /* NOTREACHED */
177 } else
178 cfunc = def;
180 /* Read, write and seek calls take off_t as arguments.
182 * The following check is not done because an off_t is a quad
183 * for current NetBSD implementations.
185 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
186 * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
190 static int
191 c_arg(const void *a, const void *b)
194 return (strcmp(((const struct arg *)a)->name,
195 ((const struct arg *)b)->name));
198 static void
199 f_bs(char *arg)
202 in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
205 static void
206 f_cbs(char *arg)
209 cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
212 static void
213 f_count(char *arg)
216 cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
217 if (!cpy_cnt)
218 terminate(0);
221 static void
222 f_files(char *arg)
225 files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
226 if (!files_cnt)
227 terminate(0);
230 static void
231 f_ibs(char *arg)
234 if (!(ddflags & C_BS))
235 in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
238 static void
239 f_if(char *arg)
242 in.name = arg;
245 static void
246 f_obs(char *arg)
249 if (!(ddflags & C_BS))
250 out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
253 static void
254 f_of(char *arg)
257 out.name = arg;
260 static void
261 f_seek(char *arg)
264 out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
267 static void
268 f_skip(char *arg)
271 in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
274 static void
275 f_progress(char *arg)
278 progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
281 #ifdef NO_CONV
282 /* Build a small version (i.e. for a ramdisk root) */
283 static void
284 f_conv(char *arg)
287 errx(EXIT_FAILURE, "conv option disabled");
288 /* NOTREACHED */
290 #else /* NO_CONV */
292 static const struct conv {
293 const char *name;
294 u_int set, noset;
295 const u_char *ctab;
296 } clist[] = {
297 { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
298 { "block", C_BLOCK, C_UNBLOCK, NULL },
299 { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
300 { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
301 { "lcase", C_LCASE, C_UCASE, NULL },
302 { "noerror", C_NOERROR, 0, NULL },
303 { "notrunc", C_NOTRUNC, 0, NULL },
304 { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
305 { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
306 { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
307 { "osync", C_OSYNC, C_BS, NULL },
308 { "sparse", C_SPARSE, 0, NULL },
309 { "swab", C_SWAB, 0, NULL },
310 { "sync", C_SYNC, 0, NULL },
311 { "ucase", C_UCASE, C_LCASE, NULL },
312 { "unblock", C_UNBLOCK, C_BLOCK, NULL },
313 /* If you add items to this table, be sure to add the
314 * conversions to the C_BS check in the jcl routine above.
318 static void
319 f_conv(char *arg)
321 struct conv *cp, tmp;
323 while (arg != NULL) {
324 tmp.name = strsep(&arg, ",");
325 if (!(cp = (struct conv *)bsearch(&tmp, clist,
326 sizeof(clist)/sizeof(struct conv), sizeof(struct conv),
327 c_conv))) {
328 errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
329 /* NOTREACHED */
331 if (ddflags & cp->noset) {
332 errx(EXIT_FAILURE, "%s: illegal conversion combination", tmp.name);
333 /* NOTREACHED */
335 ddflags |= cp->set;
336 if (cp->ctab)
337 ctab = cp->ctab;
341 static int
342 c_conv(const void *a, const void *b)
345 return (strcmp(((const struct conv *)a)->name,
346 ((const struct conv *)b)->name));
349 #endif /* NO_CONV */