1 /* $NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $ */
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
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
36 #include <sys/cdefs.h>
39 static char sccsid
[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
41 __RCSID("$NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $");
45 #include <sys/types.h>
50 #endif /* NO_IOFLAG */
61 static int c_arg(const void *, const void *);
64 static void f_msgfmt(char *) __dead
;
66 static void f_msgfmt(char *);
67 #endif /* NO_MSGFMT */
70 static void f_conv(char *) __dead
;
72 static void f_conv(char *);
73 static int c_conv(const void *, const void *);
77 static void f_iflag(char *) __dead
;
78 static void f_oflag(char *) __dead
;
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
{
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.
133 in
.dbsz
= out
.dbsz
= 512;
135 while ((oper
= *++argv
) != NULL
) {
136 if ((oper
= strdup(oper
)) == NULL
) {
138 "unable to allocate space for the argument %s",
142 if ((arg
= strchr(oper
, '=')) == NULL
) {
143 errx(EXIT_FAILURE
, "unknown operand %s", oper
);
148 errx(EXIT_FAILURE
, "no value specified for %s", oper
);
152 if (!(ap
= bsearch(&tmp
, args
,
153 __arraycount(args
), sizeof(*args
), c_arg
))) {
154 errx(EXIT_FAILURE
, "unknown operand %s", tmp
.name
);
157 if (ddflags
& ap
->noset
) {
159 "%s: illegal argument combination or already set",
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
)) {
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");
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
;
207 "cbs meaningless if not doing record operations");
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);
224 c_arg(const void *a
, const void *b
)
227 return (strcmp(((const struct arg
*)a
)->name
,
228 ((const struct arg
*)b
)->name
));
235 in
.dbsz
= out
.dbsz
= strsuftoll("block size", arg
, 1, UINT_MAX
);
242 cbsz
= strsuftoll("conversion record size", arg
, 1, UINT_MAX
);
249 cpy_cnt
= strsuftoll("block count", arg
, 0, LLONG_MAX
);
258 files_cnt
= (u_int
)strsuftoll("file count", arg
, 0, UINT_MAX
);
267 if (!(ddflags
& C_BS
))
268 in
.dbsz
= strsuftoll("input block size", arg
, 1, UINT_MAX
);
279 /* Build a small version (i.e. for a ramdisk root) */
284 errx(EXIT_FAILURE
, "msgfmt option disabled");
287 #else /* NO_MSGFMT */
293 * If the format string is not valid, dd_write_msg() will print
296 dd_write_msg(arg
, 0);
300 #endif /* NO_MSGFMT */
306 if (!(ddflags
& C_BS
))
307 out
.dbsz
= strsuftoll("output block size", arg
, 1, UINT_MAX
);
321 out
.offset
= strsuftoll("seek blocks", arg
, 0, LLONG_MAX
);
328 in
.offset
= strsuftoll("skip blocks", arg
, 0, LLONG_MAX
);
332 f_progress(char *arg
)
335 progress
= strsuftoll("progress blocks", arg
, 0, LLONG_MAX
);
339 /* Build a small version (i.e. for a ramdisk root) */
344 errx(EXIT_FAILURE
, "conv option disabled");
349 static const struct conv
{
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.
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
);
387 if (ddflags
& cp
->noset
) {
389 "%s: illegal conversion combination", tmp
.name
);
399 c_conv(const void *a
, const void *b
)
402 return (strcmp(((const struct conv
*)a
)->name
,
403 ((const struct conv
*)b
)->name
));
411 /* Build a small version (i.e. for a ramdisk root) */
413 errx(EXIT_FAILURE
, "iflag option disabled");
416 iflag
= f_ioflag(arg
, C_IFLAG
);
424 /* Build a small version (i.e. for a ramdisk root) */
426 errx(EXIT_FAILURE
, "oflag option disabled");
429 oflag
= f_ioflag(arg
, C_OFLAG
);
435 static const struct ioflag
{
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
},
467 f_ioflag(char *arg
, u_int flagtype
)
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
);
481 if ((cp
->set
& O_ACCMODE
) && (flagtype
== C_OFLAG
)) {
482 warnx("rdonly, rdwr and wronly are ignored for oflag");
486 if ((cp
->allowed
& flagtype
) == 0) {
487 warnx("%s set for %s but makes no sense",
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 */