]>
git.saurik.com Git - apple/file_cmds.git/blob - dd/args.c
1 /* $NetBSD: args.c,v 1.13 1998/07/28 03:47:15 mycroft 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
45 __RCSID("$NetBSD: args.c,v 1.13 1998/07/28 03:47:15 mycroft Exp $");
49 #include <sys/types.h>
61 static int c_arg
__P((const void *, const void *));
62 static int c_conv
__P((const void *, const void *));
63 static void f_bs
__P((char *));
64 static void f_cbs
__P((char *));
65 static void f_conv
__P((char *));
66 static void f_count
__P((char *));
67 static void f_files
__P((char *));
68 static void f_ibs
__P((char *));
69 static void f_if
__P((char *));
70 static void f_obs
__P((char *));
71 static void f_of
__P((char *));
72 static void f_seek
__P((char *));
73 static void f_skip
__P((char *));
74 static u_long get_bsz
__P((char *));
76 static const struct arg
{
78 void (*f
) __P((char *));
81 { "bs", f_bs
, C_BS
, C_BS
|C_IBS
|C_OBS
|C_OSYNC
},
82 { "cbs", f_cbs
, C_CBS
, C_CBS
},
83 { "conv", f_conv
, 0, 0 },
84 { "count", f_count
, C_COUNT
, C_COUNT
},
85 { "files", f_files
, C_FILES
, C_FILES
},
86 { "ibs", f_ibs
, C_IBS
, C_BS
|C_IBS
},
87 { "if", f_if
, C_IF
, C_IF
},
88 { "obs", f_obs
, C_OBS
, C_BS
|C_OBS
},
89 { "of", f_of
, C_OF
, C_OF
},
90 { "seek", f_seek
, C_SEEK
, C_SEEK
},
91 { "skip", f_skip
, C_SKIP
, C_SKIP
},
97 * args -- parse JCL syntax of dd.
106 in
.dbsz
= out
.dbsz
= 512;
108 while ((oper
= *++argv
) != NULL
) {
109 if ((arg
= strchr(oper
, '=')) == NULL
)
110 errx(1, "unknown operand %s", oper
);
113 errx(1, "no value specified for %s", oper
);
115 if (!(ap
= (struct arg
*)bsearch(&tmp
, args
,
116 sizeof(args
)/sizeof(struct arg
), sizeof(struct arg
),
118 errx(1, "unknown operand %s", tmp
.name
);
119 if (ddflags
& ap
->noset
)
120 errx(1, "%s: illegal argument combination or already set",
126 /* Final sanity checks. */
128 if (ddflags
& C_BS
) {
130 * Bs is turned off by any conversion -- we assume the user
131 * just wanted to set both the input and output block sizes
132 * and didn't want the bs semantics, so we don't warn.
134 if (ddflags
& (C_BLOCK
|C_LCASE
|C_SWAB
|C_UCASE
|C_UNBLOCK
))
137 /* Bs supersedes ibs and obs. */
138 if (ddflags
& C_BS
&& ddflags
& (C_IBS
|C_OBS
))
139 warnx("bs supersedes ibs and obs");
143 * Ascii/ebcdic and cbs implies block/unblock.
144 * Block/unblock requires cbs and vice-versa.
146 if (ddflags
& (C_BLOCK
|C_UNBLOCK
)) {
147 if (!(ddflags
& C_CBS
))
148 errx(1, "record operations require cbs");
150 errx(1, "cbs cannot be zero");
151 cfunc
= ddflags
& C_BLOCK
? block
: unblock
;
152 } else if (ddflags
& C_CBS
) {
153 if (ddflags
& (C_ASCII
|C_EBCDIC
)) {
154 if (ddflags
& C_ASCII
) {
155 ddflags
|= C_UNBLOCK
;
162 errx(1, "cbs meaningless if not doing record operations");
164 errx(1, "cbs cannot be zero");
168 if (in
.dbsz
== 0 || out
.dbsz
== 0)
169 errx(1, "buffer sizes cannot be zero");
172 * Check to make sure that the buffers are not too large.
174 if (in
.dbsz
> INT_MAX
|| out
.dbsz
> INT_MAX
)
175 errx(1, "buffer sizes cannot be greater than %d", INT_MAX
);
177 /* Read, write and seek calls take off_t as arguments.
179 * The following check is not done because an off_t is a quad
180 * for current NetBSD implementations.
182 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
183 * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
192 return (strcmp(((const struct arg
*)a
)->name
,
193 ((const struct arg
*)b
)->name
));
201 in
.dbsz
= out
.dbsz
= (int)get_bsz(arg
);
209 cbsz
= (int)get_bsz(arg
);
217 cpy_cnt
= (u_int
)get_bsz(arg
);
227 files_cnt
= (int)get_bsz(arg
);
235 if (!(ddflags
& C_BS
))
236 in
.dbsz
= (int)get_bsz(arg
);
252 if (!(ddflags
& C_BS
))
253 out
.dbsz
= (int)get_bsz(arg
);
269 out
.offset
= (u_int
)get_bsz(arg
);
277 in
.offset
= (u_int
)get_bsz(arg
);
281 /* Build a small version (i.e. for a ramdisk root) */
286 errx(1, "conv option disabled");
290 static const struct conv
{
295 { "ascii", C_ASCII
, C_EBCDIC
, e2a_POSIX
},
296 { "block", C_BLOCK
, C_UNBLOCK
, NULL
},
297 { "ebcdic", C_EBCDIC
, C_ASCII
, a2e_POSIX
},
298 { "ibm", C_EBCDIC
, C_ASCII
, a2ibm_POSIX
},
299 { "lcase", C_LCASE
, C_UCASE
, NULL
},
300 { "noerror", C_NOERROR
, 0, NULL
},
301 { "notrunc", C_NOTRUNC
, 0, NULL
},
302 { "oldascii", C_ASCII
, C_EBCDIC
, e2a_32V
},
303 { "oldebcdic", C_EBCDIC
, C_ASCII
, a2e_32V
},
304 { "oldibm", C_EBCDIC
, C_ASCII
, a2ibm_32V
},
305 { "osync", C_OSYNC
, C_BS
, NULL
},
306 { "swab", C_SWAB
, 0, NULL
},
307 { "sync", C_SYNC
, 0, NULL
},
308 { "ucase", C_UCASE
, C_LCASE
, NULL
},
309 { "unblock", C_UNBLOCK
, C_BLOCK
, NULL
},
316 struct conv
*cp
, tmp
;
318 while (arg
!= NULL
) {
319 tmp
.name
= strsep(&arg
, ",");
320 if (!(cp
= (struct conv
*)bsearch(&tmp
, clist
,
321 sizeof(clist
)/sizeof(struct conv
), sizeof(struct conv
),
323 errx(1, "unknown conversion %s", tmp
.name
);
324 if (ddflags
& cp
->noset
)
325 errx(1, "%s: illegal conversion combination", tmp
.name
);
337 return (strcmp(((const struct conv
*)a
)->name
,
338 ((const struct conv
*)b
)->name
));
344 * Convert an expression of the following forms to an unsigned long.
345 * 1) A positive decimal number.
346 * 2) A positive decimal number followed by a b (mult by 512).
347 * 3) A positive decimal number followed by a k (mult by 1024).
348 * 4) A positive decimal number followed by a m (mult by 512).
349 * 5) A positive decimal number followed by a w (mult by sizeof int)
350 * 6) Two or more positive decimal numbers (with/without k,b or w).
351 * seperated by x (also * for backwards compatibility), specifying
352 * the product of the indicated values.
361 num
= strtoul(val
, &expr
, 0);
362 if (num
== ULONG_MAX
) /* Overflow. */
364 if (expr
== val
) /* No digits. */
365 errx(1, "%s: illegal numeric value", oper
);
401 case '*': /* Backward compatible. */
404 num
*= get_bsz(expr
+ 1);
406 erange
: errx(1, "%s: %s", oper
, strerror(ERANGE
));
409 errx(1, "%s: illegal numeric value", oper
);