1 /* $NetBSD: args.c,v 1.25 2004/01/17 20:48:57 dbj 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.25 2004/01/17 20:48:57 dbj Exp $");
45 #include <sys/types.h>
58 static int c_arg(const void *, const void *);
60 static int c_conv(const void *, const void *);
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
{
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.
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
);
114 errx(EXIT_FAILURE
, "no value specified for %s", oper
);
118 if (!(ap
= (struct arg
*)bsearch(&tmp
, args
,
119 sizeof(args
)/sizeof(struct arg
), sizeof(struct arg
),
121 errx(EXIT_FAILURE
, "unknown operand %s", tmp
.name
);
124 if (ddflags
& ap
->noset
) {
126 "%s: illegal argument combination or already set",
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
)) {
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");
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
;
174 "cbs meaningless if not doing record operations");
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);
191 c_arg(const void *a
, const void *b
)
194 return (strcmp(((const struct arg
*)a
)->name
,
195 ((const struct arg
*)b
)->name
));
202 in
.dbsz
= out
.dbsz
= strsuftoll("block size", arg
, 1, UINT_MAX
);
209 cbsz
= strsuftoll("conversion record size", arg
, 1, UINT_MAX
);
216 cpy_cnt
= strsuftoll("block count", arg
, 0, LLONG_MAX
);
225 files_cnt
= (u_int
)strsuftoll("file count", arg
, 0, UINT_MAX
);
234 if (!(ddflags
& C_BS
))
235 in
.dbsz
= strsuftoll("input block size", arg
, 1, UINT_MAX
);
249 if (!(ddflags
& C_BS
))
250 out
.dbsz
= strsuftoll("output block size", arg
, 1, UINT_MAX
);
264 out
.offset
= strsuftoll("seek blocks", arg
, 0, LLONG_MAX
);
271 in
.offset
= strsuftoll("skip blocks", arg
, 0, LLONG_MAX
);
275 f_progress(char *arg
)
278 progress
= strsuftoll("progress blocks", arg
, 0, LLONG_MAX
);
282 /* Build a small version (i.e. for a ramdisk root) */
287 errx(EXIT_FAILURE
, "conv option disabled");
292 static const struct conv
{
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.
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
),
328 errx(EXIT_FAILURE
, "unknown conversion %s", tmp
.name
);
331 if (ddflags
& cp
->noset
) {
332 errx(EXIT_FAILURE
, "%s: illegal conversion combination", tmp
.name
);
342 c_conv(const void *a
, const void *b
)
345 return (strcmp(((const struct conv
*)a
)->name
,
346 ((const struct conv
*)b
)->name
));