xref: /netbsd/bin/dd/args.c (revision 638015db)
1 /*	$NetBSD: args.c,v 1.42 2022/01/14 23:55:16 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
10  *
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.
22  *
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.
34  */
35 
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.42 2022/01/14 23:55:16 christos Exp $");
42 #endif
43 #endif /* not lint */
44 
45 #include <sys/types.h>
46 #include <sys/time.h>
47 
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>
57 
58 #include "dd.h"
59 #include "extern.h"
60 
61 static int	c_arg(const void *, const void *);
62 
63 #ifdef NO_MSGFMT
64 static void	f_msgfmt(char *) __dead;
65 #else
66 static void	f_msgfmt(char *);
67 #endif /* NO_MSGFMT */
68 
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 */
75 
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 */
85 
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 *);
97 
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 },
122 };
123 
124 /*
125  * args -- parse JCL syntax of dd.
126  */
127 void
jcl(char ** argv)128 jcl(char **argv)
129 {
130 	const struct arg *ap;
131 	struct arg tmp;
132 	char *oper, *arg;
133 
134 	in.dbsz = out.dbsz = 512;
135 
136 	while ((oper = *++argv) != NULL) {
137 		if ((oper = strdup(oper)) == NULL) {
138 			errx(EXIT_FAILURE,
139 			    "unable to allocate space for the argument %s",
140 			    *argv);
141 			/* NOTREACHED */
142 		}
143 		if ((arg = strchr(oper, '=')) == NULL) {
144 			errx(EXIT_FAILURE, "unknown operand %s", oper);
145 			/* NOTREACHED */
146 		}
147 		*arg++ = '\0';
148 		if (!*arg) {
149 			errx(EXIT_FAILURE, "no value specified for %s", oper);
150 			/* NOTREACHED */
151 		}
152 		tmp.name = oper;
153 		if (!(ap = bsearch(&tmp, args,
154 		    __arraycount(args), sizeof(*args), c_arg))) {
155 			errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
156 			/* NOTREACHED */
157 		}
158 		if (ddflags & ap->noset) {
159 			errx(EXIT_FAILURE,
160 			    "%s: illegal argument combination or already set",
161 			    tmp.name);
162 			/* NOTREACHED */
163 		}
164 		ddflags |= ap->set;
165 		ap->f(arg);
166 	}
167 
168 	/* Final sanity checks. */
169 
170 	if (ddflags & C_BS) {
171 		/*
172 		 * Bs is turned off by any conversion -- we assume the user
173 		 * just wanted to set both the input and output block sizes
174 		 * and didn't want the bs semantics, so we don't warn.
175 		 */
176 		if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
177 		    C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
178 			ddflags &= ~C_BS;
179 			ddflags |= C_IBS|C_OBS;
180 		}
181 
182 		/* Bs supersedes ibs and obs. */
183 		if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
184 			warnx("bs supersedes ibs and obs");
185 	}
186 
187 	/*
188 	 * Ascii/ebcdic and cbs implies block/unblock.
189 	 * Block/unblock requires cbs and vice-versa.
190 	 */
191 	if (ddflags & (C_BLOCK|C_UNBLOCK)) {
192 		if (!(ddflags & C_CBS)) {
193 			errx(EXIT_FAILURE, "record operations require cbs");
194 			/* NOTREACHED */
195 		}
196 		cfunc = ddflags & C_BLOCK ? block : unblock;
197 	} else if (ddflags & C_CBS) {
198 		if (ddflags & (C_ASCII|C_EBCDIC)) {
199 			if (ddflags & C_ASCII) {
200 				ddflags |= C_UNBLOCK;
201 				cfunc = unblock;
202 			} else {
203 				ddflags |= C_BLOCK;
204 				cfunc = block;
205 			}
206 		} else {
207 			errx(EXIT_FAILURE,
208 			    "cbs meaningless if not doing record operations");
209 			/* NOTREACHED */
210 		}
211 	} else
212 		cfunc = def;
213 
214 	/* Read, write and seek calls take off_t as arguments.
215 	 *
216 	 * The following check is not done because an off_t is a quad
217 	 *  for current NetBSD implementations.
218 	 *
219 	 * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
220 	 *	errx(1, "seek offsets cannot be larger than %d", INT_MAX);
221 	 */
222 }
223 
224 static int
c_arg(const void * a,const void * b)225 c_arg(const void *a, const void *b)
226 {
227 
228 	return (strcmp(((const struct arg *)a)->name,
229 	    ((const struct arg *)b)->name));
230 }
231 
232 static void
f_bs(char * arg)233 f_bs(char *arg)
234 {
235 
236 	in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
237 }
238 
239 static void
f_cbs(char * arg)240 f_cbs(char *arg)
241 {
242 
243 	cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
244 }
245 
246 static void
f_count(char * arg)247 f_count(char *arg)
248 {
249 
250 	cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
251 	if (!cpy_cnt)
252 		terminate(0);
253 }
254 
255 static void
f_files(char * arg)256 f_files(char *arg)
257 {
258 
259 	files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
260 	if (!files_cnt)
261 		terminate(0);
262 }
263 
264 static void
f_ibs(char * arg)265 f_ibs(char *arg)
266 {
267 
268 	if (!(ddflags & C_BS))
269 		in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
270 }
271 
272 static void
f_if(char * arg)273 f_if(char *arg)
274 {
275 
276 	in.name = arg;
277 }
278 
279 #ifdef NO_MSGFMT
280 /* Build a small version (i.e. for a ramdisk root) */
281 static void
f_msgfmt(char * arg)282 f_msgfmt(char *arg)
283 {
284 
285 	errx(EXIT_FAILURE, "msgfmt option disabled");
286 	/* NOTREACHED */
287 }
288 #else	/* NO_MSGFMT */
289 static void
f_msgfmt(char * arg)290 f_msgfmt(char *arg)
291 {
292 
293 	/*
294 	 * If the format string is not valid, dd_write_msg() will print
295 	 * an error and exit.
296 	 */
297 	dd_write_msg(arg, 0);
298 
299 	msgfmt = arg;
300 }
301 #endif	/* NO_MSGFMT */
302 
303 static void
f_obs(char * arg)304 f_obs(char *arg)
305 {
306 
307 	if (!(ddflags & C_BS))
308 		out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
309 }
310 
311 static void
f_of(char * arg)312 f_of(char *arg)
313 {
314 
315 	out.name = arg;
316 }
317 
318 static void
f_seek(char * arg)319 f_seek(char *arg)
320 {
321 
322 	out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
323 }
324 
325 static void
f_skip(char * arg)326 f_skip(char *arg)
327 {
328 
329 	in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
330 }
331 
332 static void
f_progress(char * arg)333 f_progress(char *arg)
334 {
335 
336 	progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
337 }
338 
339 #ifdef	NO_CONV
340 /* Build a small version (i.e. for a ramdisk root) */
341 static void
f_conv(char * arg)342 f_conv(char *arg)
343 {
344 
345 	errx(EXIT_FAILURE, "conv option disabled");
346 	/* NOTREACHED */
347 }
348 #else	/* NO_CONV */
349 
350 static const struct conv {
351 	const char *name;
352 	u_int set, noset;
353 	const u_char *ctab;
354 } clist[] = {
355 	{ "ascii",	C_ASCII,	C_EBCDIC,	e2a_POSIX },
356 	{ "block",	C_BLOCK,	C_UNBLOCK,	NULL },
357 	{ "ebcdic",	C_EBCDIC,	C_ASCII,	a2e_POSIX },
358 	{ "ibm",	C_EBCDIC,	C_ASCII,	a2ibm },
359 	{ "lcase",	C_LCASE,	C_UCASE,	NULL },
360 	{ "noerror",	C_NOERROR,	0,		NULL },
361 	{ "notrunc",	C_NOTRUNC,	0,		NULL },
362 	{ "oldascii",	C_ASCII,	C_EBCDIC,	e2a_32V },
363 	{ "oldebcdic",	C_EBCDIC,	C_ASCII,	a2e_32V },
364 	{ "oldibm",	C_EBCDIC,	C_ASCII,	a2ibm },
365 	{ "osync",	C_OSYNC,	C_BS,		NULL },
366 	{ "sparse",	C_SPARSE,	0,		NULL },
367 	{ "swab",	C_SWAB,		0,		NULL },
368 	{ "sync",	C_SYNC,		0,		NULL },
369 	{ "ucase",	C_UCASE,	C_LCASE,	NULL },
370 	{ "unblock",	C_UNBLOCK,	C_BLOCK,	NULL },
371 	/* If you add items to this table, be sure to add the
372 	 * conversions to the C_BS check in the jcl routine above.
373 	 */
374 };
375 
376 static void
f_conv(char * arg)377 f_conv(char *arg)
378 {
379 	const struct conv *cp;
380 	struct conv tmp;
381 
382 	while (arg != NULL) {
383 		tmp.name = strsep(&arg, ",");
384 		if (!(cp = bsearch(&tmp, clist,
385 		    __arraycount(clist), sizeof(*clist), c_conv))) {
386 			errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
387 			/* NOTREACHED */
388 		}
389 		if (ddflags & cp->noset) {
390 			errx(EXIT_FAILURE,
391 			    "%s: illegal conversion combination", tmp.name);
392 			/* NOTREACHED */
393 		}
394 		ddflags |= cp->set;
395 		if (cp->ctab)
396 			ctab = cp->ctab;
397 	}
398 }
399 
400 static int
c_conv(const void * a,const void * b)401 c_conv(const void *a, const void *b)
402 {
403 
404 	return (strcmp(((const struct conv *)a)->name,
405 	    ((const struct conv *)b)->name));
406 }
407 
408 #endif	/* NO_CONV */
409 
410 static void
f_iflag(char * arg)411 f_iflag(char *arg)
412 {
413 /* Build a small version (i.e. for a ramdisk root) */
414 #ifdef	NO_IOFLAG
415 	errx(EXIT_FAILURE, "iflag option disabled");
416 	/* NOTREACHED */
417 #else
418 	iflag = f_ioflag(arg, C_IFLAG);
419 	return;
420 #endif
421 }
422 
423 static void
f_oflag(char * arg)424 f_oflag(char *arg)
425 {
426 /* Build a small version (i.e. for a ramdisk root) */
427 #ifdef	NO_IOFLAG
428 	errx(EXIT_FAILURE, "oflag option disabled");
429 	/* NOTREACHED */
430 #else
431 	oflag = f_ioflag(arg, C_OFLAG);
432 	return;
433 #endif
434 }
435 
436 #ifndef	NO_IOFLAG
437 static const struct ioflag {
438 	const char *name;
439 	u_int set;
440 	u_int allowed;
441 } olist[] = {
442      /* the array needs to be sorted by the first column so
443 	bsearch() can be used to find commands quickly */
444 	{ "alt_io",	O_ALT_IO,	C_IFLAG|C_OFLAG	},
445 	{ "append",	O_APPEND,	C_OFLAG		},
446 	{ "async",	O_ASYNC,	C_IFLAG|C_OFLAG	},
447 	{ "cloexec",	O_CLOEXEC,	C_IFLAG|C_OFLAG	},
448 	{ "creat",	O_CREAT,	C_OFLAG		},
449 	{ "direct",	O_DIRECT,	C_IFLAG|C_OFLAG	},
450 	{ "directory",	O_DIRECTORY,	C_NONE		},
451 	{ "dsync",	O_DSYNC,	C_OFLAG		},
452 	{ "excl",	O_EXCL,		C_OFLAG		},
453 	{ "exlock",	O_EXLOCK,	C_IFLAG|C_OFLAG	},
454 	{ "noctty",	O_NOCTTY,	C_IFLAG|C_OFLAG	},
455 	{ "nofollow",	O_NOFOLLOW,	C_IFLAG|C_OFLAG	},
456 	{ "nonblock",	O_NONBLOCK,	C_IFLAG|C_OFLAG	},
457 	{ "nosigpipe",	O_NOSIGPIPE,	C_IFLAG|C_OFLAG	},
458 	{ "rdonly",	O_RDONLY,	C_IFLAG		},
459 	{ "rdwr",	O_RDWR,		C_IFLAG		},
460 	{ "rsync",	O_RSYNC,	C_IFLAG		},
461 	{ "shlock",	O_SHLOCK,	C_IFLAG|C_OFLAG	},
462 	{ "sync",	O_SYNC,		C_IFLAG|C_OFLAG	},
463 	{ "trunc",	O_TRUNC,	C_OFLAG		},
464 	{ "wronly",	O_WRONLY,	C_OFLAG		},
465 };
466 
467 static u_int
f_ioflag(char * arg,u_int flagtype)468 f_ioflag(char *arg, u_int flagtype)
469 {
470 	u_int ioflag = 0;
471 	const struct ioflag *cp;
472 	struct ioflag tmp;
473 	const char *flagstr = (flagtype == C_IFLAG) ? "iflag" : "oflag";
474 
475 	while (arg != NULL) {
476 		tmp.name = strsep(&arg, ",");
477 		if (!(cp = bsearch(&tmp, olist,
478 		    __arraycount(olist), sizeof(*olist), c_ioflag))) {
479 			errx(EXIT_FAILURE, "unknown %s %s", flagstr, tmp.name);
480 			/* NOTREACHED */
481 		}
482 
483 		if ((cp->set & O_ACCMODE) && (flagtype == C_OFLAG)) {
484 			warnx("rdonly, rdwr and wronly are ignored for oflag");
485 			continue;
486 		}
487 
488 		if ((cp->allowed & flagtype) == 0) {
489 			warnx("%s set for %s but makes no sense",
490 			      cp->name, flagstr);
491 		}
492 
493 		ioflag |= cp->set;
494 	}
495 
496 
497 	return ioflag;
498 }
499 
500 static int
c_ioflag(const void * a,const void * b)501 c_ioflag(const void *a, const void *b)
502 {
503 
504 	return (strcmp(((const struct ioflag *)a)->name,
505 	    ((const struct ioflag *)b)->name));
506 }
507 #endif	/* NO_IOFLAG */
508