1*0a6a1f1dSLionel Sambuc /* $NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $ */
2c19d619dSJacob Adams
3c19d619dSJacob Adams /*-
4c19d619dSJacob Adams * Copyright (c) 1991, 1993, 1994
5c19d619dSJacob Adams * The Regents of the University of California. All rights reserved.
6c19d619dSJacob Adams *
7c19d619dSJacob Adams * This code is derived from software contributed to Berkeley by
8c19d619dSJacob Adams * Keith Muller of the University of California, San Diego and Lance
9c19d619dSJacob Adams * Visser of Convex Computer Corporation.
10c19d619dSJacob Adams *
11c19d619dSJacob Adams * Redistribution and use in source and binary forms, with or without
12c19d619dSJacob Adams * modification, are permitted provided that the following conditions
13c19d619dSJacob Adams * are met:
14c19d619dSJacob Adams * 1. Redistributions of source code must retain the above copyright
15c19d619dSJacob Adams * notice, this list of conditions and the following disclaimer.
16c19d619dSJacob Adams * 2. Redistributions in binary form must reproduce the above copyright
17c19d619dSJacob Adams * notice, this list of conditions and the following disclaimer in the
18c19d619dSJacob Adams * documentation and/or other materials provided with the distribution.
19c19d619dSJacob Adams * 3. Neither the name of the University nor the names of its contributors
20c19d619dSJacob Adams * may be used to endorse or promote products derived from this software
21c19d619dSJacob Adams * without specific prior written permission.
22c19d619dSJacob Adams *
23c19d619dSJacob Adams * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24c19d619dSJacob Adams * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25c19d619dSJacob Adams * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26c19d619dSJacob Adams * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27c19d619dSJacob Adams * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28c19d619dSJacob Adams * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29c19d619dSJacob Adams * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30c19d619dSJacob Adams * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31c19d619dSJacob Adams * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32c19d619dSJacob Adams * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33c19d619dSJacob Adams * SUCH DAMAGE.
34c19d619dSJacob Adams */
35c19d619dSJacob Adams
36c19d619dSJacob Adams #include <sys/cdefs.h>
37c19d619dSJacob Adams #ifndef lint
38c19d619dSJacob Adams #if 0
39c19d619dSJacob Adams static char sccsid[] = "@(#)args.c 8.3 (Berkeley) 4/2/94";
40c19d619dSJacob Adams #else
41*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: args.c,v 1.39 2015/03/18 13:23:49 manu Exp $");
42c19d619dSJacob Adams #endif
43c19d619dSJacob Adams #endif /* not lint */
44c19d619dSJacob Adams
45c19d619dSJacob Adams #include <sys/types.h>
46c19d619dSJacob Adams #include <sys/time.h>
47c19d619dSJacob Adams
48*0a6a1f1dSLionel Sambuc #ifndef NO_IOFLAG
49*0a6a1f1dSLionel Sambuc #include <fcntl.h>
50*0a6a1f1dSLionel Sambuc #endif /* NO_IOFLAG */
51c19d619dSJacob Adams #include <err.h>
52c19d619dSJacob Adams #include <errno.h>
53c19d619dSJacob Adams #include <limits.h>
54c19d619dSJacob Adams #include <stdio.h>
55c19d619dSJacob Adams #include <stdlib.h>
56c19d619dSJacob Adams #include <string.h>
57c19d619dSJacob Adams
58c19d619dSJacob Adams #include "dd.h"
59c19d619dSJacob Adams #include "extern.h"
60c19d619dSJacob Adams
61c19d619dSJacob Adams static int c_arg(const void *, const void *);
62c19d619dSJacob Adams
63c19d619dSJacob Adams #ifdef NO_MSGFMT
64c19d619dSJacob Adams static void f_msgfmt(char *) __dead;
65c19d619dSJacob Adams #else
66c19d619dSJacob Adams static void f_msgfmt(char *);
67c19d619dSJacob Adams #endif /* NO_MSGFMT */
68c19d619dSJacob Adams
69c19d619dSJacob Adams #ifdef NO_CONV
70c19d619dSJacob Adams static void f_conv(char *) __dead;
71c19d619dSJacob Adams #else
72c19d619dSJacob Adams static void f_conv(char *);
73c19d619dSJacob Adams static int c_conv(const void *, const void *);
74c19d619dSJacob Adams #endif /* NO_CONV */
75c19d619dSJacob Adams
76*0a6a1f1dSLionel Sambuc #ifdef NO_IOFLAG
77*0a6a1f1dSLionel Sambuc static void f_iflag(char *) __dead;
78*0a6a1f1dSLionel Sambuc static void f_oflag(char *) __dead;
79*0a6a1f1dSLionel Sambuc #else
80*0a6a1f1dSLionel Sambuc static void f_iflag(char *);
81*0a6a1f1dSLionel Sambuc static void f_oflag(char *);
82*0a6a1f1dSLionel Sambuc static u_int f_ioflag(char *, u_int);
83*0a6a1f1dSLionel Sambuc static int c_ioflag(const void *, const void *);
84*0a6a1f1dSLionel Sambuc #endif /* NO_IOFLAG */
85*0a6a1f1dSLionel Sambuc
86c19d619dSJacob Adams static void f_bs(char *);
87c19d619dSJacob Adams static void f_cbs(char *);
88c19d619dSJacob Adams static void f_count(char *);
89c19d619dSJacob Adams static void f_files(char *);
90c19d619dSJacob Adams static void f_ibs(char *);
91c19d619dSJacob Adams static void f_if(char *);
92c19d619dSJacob Adams static void f_obs(char *);
93c19d619dSJacob Adams static void f_of(char *);
94c19d619dSJacob Adams static void f_seek(char *);
95c19d619dSJacob Adams static void f_skip(char *);
96c19d619dSJacob Adams static void f_progress(char *);
97c19d619dSJacob Adams
98c19d619dSJacob Adams static const struct arg {
99c19d619dSJacob Adams const char *name;
100c19d619dSJacob Adams void (*f)(char *);
101c19d619dSJacob Adams u_int set, noset;
102c19d619dSJacob Adams } args[] = {
103c19d619dSJacob Adams /* the array needs to be sorted by the first column so
104c19d619dSJacob Adams bsearch() can be used to find commands quickly */
105c19d619dSJacob Adams { "bs", f_bs, C_BS, C_BS|C_IBS|C_OBS|C_OSYNC },
106c19d619dSJacob Adams { "cbs", f_cbs, C_CBS, C_CBS },
107c19d619dSJacob Adams { "conv", f_conv, 0, 0 },
108c19d619dSJacob Adams { "count", f_count, C_COUNT, C_COUNT },
109c19d619dSJacob Adams { "files", f_files, C_FILES, C_FILES },
110c19d619dSJacob Adams { "ibs", f_ibs, C_IBS, C_BS|C_IBS },
111c19d619dSJacob Adams { "if", f_if, C_IF, C_IF },
112*0a6a1f1dSLionel Sambuc { "iflag", f_iflag, C_IFLAG, C_IFLAG },
113c19d619dSJacob Adams { "iseek", f_skip, C_SKIP, C_SKIP },
114c19d619dSJacob Adams { "msgfmt", f_msgfmt, 0, 0 },
115c19d619dSJacob Adams { "obs", f_obs, C_OBS, C_BS|C_OBS },
116c19d619dSJacob Adams { "of", f_of, C_OF, C_OF },
117*0a6a1f1dSLionel Sambuc { "oflag", f_oflag, C_OFLAG, C_OFLAG },
118c19d619dSJacob Adams { "oseek", f_seek, C_SEEK, C_SEEK },
119c19d619dSJacob Adams { "progress", f_progress, 0, 0 },
120c19d619dSJacob Adams { "seek", f_seek, C_SEEK, C_SEEK },
121c19d619dSJacob Adams { "skip", f_skip, C_SKIP, C_SKIP },
122c19d619dSJacob Adams };
123c19d619dSJacob Adams
124c19d619dSJacob Adams /*
125c19d619dSJacob Adams * args -- parse JCL syntax of dd.
126c19d619dSJacob Adams */
127c19d619dSJacob Adams void
jcl(char ** argv)128c19d619dSJacob Adams jcl(char **argv)
129c19d619dSJacob Adams {
130c19d619dSJacob Adams struct arg *ap, tmp;
131c19d619dSJacob Adams char *oper, *arg;
132c19d619dSJacob Adams
133c19d619dSJacob Adams in.dbsz = out.dbsz = 512;
134c19d619dSJacob Adams
135c19d619dSJacob Adams while ((oper = *++argv) != NULL) {
136c19d619dSJacob Adams if ((oper = strdup(oper)) == NULL) {
137c19d619dSJacob Adams errx(EXIT_FAILURE,
138c19d619dSJacob Adams "unable to allocate space for the argument %s",
139c19d619dSJacob Adams *argv);
140c19d619dSJacob Adams /* NOTREACHED */
141c19d619dSJacob Adams }
142c19d619dSJacob Adams if ((arg = strchr(oper, '=')) == NULL) {
143c19d619dSJacob Adams errx(EXIT_FAILURE, "unknown operand %s", oper);
144c19d619dSJacob Adams /* NOTREACHED */
145c19d619dSJacob Adams }
146c19d619dSJacob Adams *arg++ = '\0';
147c19d619dSJacob Adams if (!*arg) {
148c19d619dSJacob Adams errx(EXIT_FAILURE, "no value specified for %s", oper);
149c19d619dSJacob Adams /* NOTREACHED */
150c19d619dSJacob Adams }
151c19d619dSJacob Adams tmp.name = oper;
152c19d619dSJacob Adams if (!(ap = bsearch(&tmp, args,
153c19d619dSJacob Adams __arraycount(args), sizeof(*args), c_arg))) {
154c19d619dSJacob Adams errx(EXIT_FAILURE, "unknown operand %s", tmp.name);
155c19d619dSJacob Adams /* NOTREACHED */
156c19d619dSJacob Adams }
157c19d619dSJacob Adams if (ddflags & ap->noset) {
158c19d619dSJacob Adams errx(EXIT_FAILURE,
159c19d619dSJacob Adams "%s: illegal argument combination or already set",
160c19d619dSJacob Adams tmp.name);
161c19d619dSJacob Adams /* NOTREACHED */
162c19d619dSJacob Adams }
163c19d619dSJacob Adams ddflags |= ap->set;
164c19d619dSJacob Adams ap->f(arg);
165c19d619dSJacob Adams }
166c19d619dSJacob Adams
167c19d619dSJacob Adams /* Final sanity checks. */
168c19d619dSJacob Adams
169c19d619dSJacob Adams if (ddflags & C_BS) {
170c19d619dSJacob Adams /*
171c19d619dSJacob Adams * Bs is turned off by any conversion -- we assume the user
172c19d619dSJacob Adams * just wanted to set both the input and output block sizes
173c19d619dSJacob Adams * and didn't want the bs semantics, so we don't warn.
174c19d619dSJacob Adams */
175c19d619dSJacob Adams if (ddflags & (C_BLOCK | C_LCASE | C_SWAB | C_UCASE |
176c19d619dSJacob Adams C_UNBLOCK | C_OSYNC | C_ASCII | C_EBCDIC | C_SPARSE)) {
177c19d619dSJacob Adams ddflags &= ~C_BS;
178c19d619dSJacob Adams ddflags |= C_IBS|C_OBS;
179c19d619dSJacob Adams }
180c19d619dSJacob Adams
181c19d619dSJacob Adams /* Bs supersedes ibs and obs. */
182c19d619dSJacob Adams if (ddflags & C_BS && ddflags & (C_IBS|C_OBS))
183c19d619dSJacob Adams warnx("bs supersedes ibs and obs");
184c19d619dSJacob Adams }
185c19d619dSJacob Adams
186c19d619dSJacob Adams /*
187c19d619dSJacob Adams * Ascii/ebcdic and cbs implies block/unblock.
188c19d619dSJacob Adams * Block/unblock requires cbs and vice-versa.
189c19d619dSJacob Adams */
190c19d619dSJacob Adams if (ddflags & (C_BLOCK|C_UNBLOCK)) {
191c19d619dSJacob Adams if (!(ddflags & C_CBS)) {
192c19d619dSJacob Adams errx(EXIT_FAILURE, "record operations require cbs");
193c19d619dSJacob Adams /* NOTREACHED */
194c19d619dSJacob Adams }
195c19d619dSJacob Adams cfunc = ddflags & C_BLOCK ? block : unblock;
196c19d619dSJacob Adams } else if (ddflags & C_CBS) {
197c19d619dSJacob Adams if (ddflags & (C_ASCII|C_EBCDIC)) {
198c19d619dSJacob Adams if (ddflags & C_ASCII) {
199c19d619dSJacob Adams ddflags |= C_UNBLOCK;
200c19d619dSJacob Adams cfunc = unblock;
201c19d619dSJacob Adams } else {
202c19d619dSJacob Adams ddflags |= C_BLOCK;
203c19d619dSJacob Adams cfunc = block;
204c19d619dSJacob Adams }
205c19d619dSJacob Adams } else {
206c19d619dSJacob Adams errx(EXIT_FAILURE,
207c19d619dSJacob Adams "cbs meaningless if not doing record operations");
208c19d619dSJacob Adams /* NOTREACHED */
209c19d619dSJacob Adams }
210c19d619dSJacob Adams } else
211c19d619dSJacob Adams cfunc = def;
212c19d619dSJacob Adams
213c19d619dSJacob Adams /* Read, write and seek calls take off_t as arguments.
214c19d619dSJacob Adams *
215c19d619dSJacob Adams * The following check is not done because an off_t is a quad
216c19d619dSJacob Adams * for current NetBSD implementations.
217c19d619dSJacob Adams *
218c19d619dSJacob Adams * if (in.offset > INT_MAX/in.dbsz || out.offset > INT_MAX/out.dbsz)
219c19d619dSJacob Adams * errx(1, "seek offsets cannot be larger than %d", INT_MAX);
220c19d619dSJacob Adams */
221c19d619dSJacob Adams }
222c19d619dSJacob Adams
223c19d619dSJacob Adams static int
c_arg(const void * a,const void * b)224c19d619dSJacob Adams c_arg(const void *a, const void *b)
225c19d619dSJacob Adams {
226c19d619dSJacob Adams
227c19d619dSJacob Adams return (strcmp(((const struct arg *)a)->name,
228c19d619dSJacob Adams ((const struct arg *)b)->name));
229c19d619dSJacob Adams }
230c19d619dSJacob Adams
231c19d619dSJacob Adams static void
f_bs(char * arg)232c19d619dSJacob Adams f_bs(char *arg)
233c19d619dSJacob Adams {
234c19d619dSJacob Adams
235c19d619dSJacob Adams in.dbsz = out.dbsz = strsuftoll("block size", arg, 1, UINT_MAX);
236c19d619dSJacob Adams }
237c19d619dSJacob Adams
238c19d619dSJacob Adams static void
f_cbs(char * arg)239c19d619dSJacob Adams f_cbs(char *arg)
240c19d619dSJacob Adams {
241c19d619dSJacob Adams
242c19d619dSJacob Adams cbsz = strsuftoll("conversion record size", arg, 1, UINT_MAX);
243c19d619dSJacob Adams }
244c19d619dSJacob Adams
245c19d619dSJacob Adams static void
f_count(char * arg)246c19d619dSJacob Adams f_count(char *arg)
247c19d619dSJacob Adams {
248c19d619dSJacob Adams
249c19d619dSJacob Adams cpy_cnt = strsuftoll("block count", arg, 0, LLONG_MAX);
250c19d619dSJacob Adams if (!cpy_cnt)
251c19d619dSJacob Adams terminate(0);
252c19d619dSJacob Adams }
253c19d619dSJacob Adams
254c19d619dSJacob Adams static void
f_files(char * arg)255c19d619dSJacob Adams f_files(char *arg)
256c19d619dSJacob Adams {
257c19d619dSJacob Adams
258c19d619dSJacob Adams files_cnt = (u_int)strsuftoll("file count", arg, 0, UINT_MAX);
259c19d619dSJacob Adams if (!files_cnt)
260c19d619dSJacob Adams terminate(0);
261c19d619dSJacob Adams }
262c19d619dSJacob Adams
263c19d619dSJacob Adams static void
f_ibs(char * arg)264c19d619dSJacob Adams f_ibs(char *arg)
265c19d619dSJacob Adams {
266c19d619dSJacob Adams
267c19d619dSJacob Adams if (!(ddflags & C_BS))
268c19d619dSJacob Adams in.dbsz = strsuftoll("input block size", arg, 1, UINT_MAX);
269c19d619dSJacob Adams }
270c19d619dSJacob Adams
271c19d619dSJacob Adams static void
f_if(char * arg)272c19d619dSJacob Adams f_if(char *arg)
273c19d619dSJacob Adams {
274c19d619dSJacob Adams
275c19d619dSJacob Adams in.name = arg;
276c19d619dSJacob Adams }
277c19d619dSJacob Adams
278c19d619dSJacob Adams #ifdef NO_MSGFMT
279c19d619dSJacob Adams /* Build a small version (i.e. for a ramdisk root) */
280c19d619dSJacob Adams static void
f_msgfmt(char * arg)281c19d619dSJacob Adams f_msgfmt(char *arg)
282c19d619dSJacob Adams {
283c19d619dSJacob Adams
284c19d619dSJacob Adams errx(EXIT_FAILURE, "msgfmt option disabled");
285c19d619dSJacob Adams /* NOTREACHED */
286c19d619dSJacob Adams }
287c19d619dSJacob Adams #else /* NO_MSGFMT */
288c19d619dSJacob Adams static void
f_msgfmt(char * arg)289c19d619dSJacob Adams f_msgfmt(char *arg)
290c19d619dSJacob Adams {
291c19d619dSJacob Adams
292c19d619dSJacob Adams /*
293c19d619dSJacob Adams * If the format string is not valid, dd_write_msg() will print
294c19d619dSJacob Adams * an error and exit.
295c19d619dSJacob Adams */
296c19d619dSJacob Adams dd_write_msg(arg, 0);
297c19d619dSJacob Adams
298c19d619dSJacob Adams msgfmt = arg;
299c19d619dSJacob Adams }
300c19d619dSJacob Adams #endif /* NO_MSGFMT */
301c19d619dSJacob Adams
302c19d619dSJacob Adams static void
f_obs(char * arg)303c19d619dSJacob Adams f_obs(char *arg)
304c19d619dSJacob Adams {
305c19d619dSJacob Adams
306c19d619dSJacob Adams if (!(ddflags & C_BS))
307c19d619dSJacob Adams out.dbsz = strsuftoll("output block size", arg, 1, UINT_MAX);
308c19d619dSJacob Adams }
309c19d619dSJacob Adams
310c19d619dSJacob Adams static void
f_of(char * arg)311c19d619dSJacob Adams f_of(char *arg)
312c19d619dSJacob Adams {
313c19d619dSJacob Adams
314c19d619dSJacob Adams out.name = arg;
315c19d619dSJacob Adams }
316c19d619dSJacob Adams
317c19d619dSJacob Adams static void
f_seek(char * arg)318c19d619dSJacob Adams f_seek(char *arg)
319c19d619dSJacob Adams {
320c19d619dSJacob Adams
321c19d619dSJacob Adams out.offset = strsuftoll("seek blocks", arg, 0, LLONG_MAX);
322c19d619dSJacob Adams }
323c19d619dSJacob Adams
324c19d619dSJacob Adams static void
f_skip(char * arg)325c19d619dSJacob Adams f_skip(char *arg)
326c19d619dSJacob Adams {
327c19d619dSJacob Adams
328c19d619dSJacob Adams in.offset = strsuftoll("skip blocks", arg, 0, LLONG_MAX);
329c19d619dSJacob Adams }
330c19d619dSJacob Adams
331c19d619dSJacob Adams static void
f_progress(char * arg)332c19d619dSJacob Adams f_progress(char *arg)
333c19d619dSJacob Adams {
334c19d619dSJacob Adams
335c19d619dSJacob Adams progress = strsuftoll("progress blocks", arg, 0, LLONG_MAX);
336c19d619dSJacob Adams }
337c19d619dSJacob Adams
338c19d619dSJacob Adams #ifdef NO_CONV
339c19d619dSJacob Adams /* Build a small version (i.e. for a ramdisk root) */
340c19d619dSJacob Adams static void
f_conv(char * arg)341c19d619dSJacob Adams f_conv(char *arg)
342c19d619dSJacob Adams {
343c19d619dSJacob Adams
344c19d619dSJacob Adams errx(EXIT_FAILURE, "conv option disabled");
345c19d619dSJacob Adams /* NOTREACHED */
346c19d619dSJacob Adams }
347c19d619dSJacob Adams #else /* NO_CONV */
348c19d619dSJacob Adams
349c19d619dSJacob Adams static const struct conv {
350c19d619dSJacob Adams const char *name;
351c19d619dSJacob Adams u_int set, noset;
352c19d619dSJacob Adams const u_char *ctab;
353c19d619dSJacob Adams } clist[] = {
354c19d619dSJacob Adams { "ascii", C_ASCII, C_EBCDIC, e2a_POSIX },
355c19d619dSJacob Adams { "block", C_BLOCK, C_UNBLOCK, NULL },
356c19d619dSJacob Adams { "ebcdic", C_EBCDIC, C_ASCII, a2e_POSIX },
357c19d619dSJacob Adams { "ibm", C_EBCDIC, C_ASCII, a2ibm_POSIX },
358c19d619dSJacob Adams { "lcase", C_LCASE, C_UCASE, NULL },
359c19d619dSJacob Adams { "noerror", C_NOERROR, 0, NULL },
360c19d619dSJacob Adams { "notrunc", C_NOTRUNC, 0, NULL },
361c19d619dSJacob Adams { "oldascii", C_ASCII, C_EBCDIC, e2a_32V },
362c19d619dSJacob Adams { "oldebcdic", C_EBCDIC, C_ASCII, a2e_32V },
363c19d619dSJacob Adams { "oldibm", C_EBCDIC, C_ASCII, a2ibm_32V },
364c19d619dSJacob Adams { "osync", C_OSYNC, C_BS, NULL },
365c19d619dSJacob Adams { "sparse", C_SPARSE, 0, NULL },
366c19d619dSJacob Adams { "swab", C_SWAB, 0, NULL },
367c19d619dSJacob Adams { "sync", C_SYNC, 0, NULL },
368c19d619dSJacob Adams { "ucase", C_UCASE, C_LCASE, NULL },
369c19d619dSJacob Adams { "unblock", C_UNBLOCK, C_BLOCK, NULL },
370c19d619dSJacob Adams /* If you add items to this table, be sure to add the
371c19d619dSJacob Adams * conversions to the C_BS check in the jcl routine above.
372c19d619dSJacob Adams */
373c19d619dSJacob Adams };
374c19d619dSJacob Adams
375c19d619dSJacob Adams static void
f_conv(char * arg)376c19d619dSJacob Adams f_conv(char *arg)
377c19d619dSJacob Adams {
378c19d619dSJacob Adams struct conv *cp, tmp;
379c19d619dSJacob Adams
380c19d619dSJacob Adams while (arg != NULL) {
381c19d619dSJacob Adams tmp.name = strsep(&arg, ",");
382c19d619dSJacob Adams if (!(cp = bsearch(&tmp, clist,
383c19d619dSJacob Adams __arraycount(clist), sizeof(*clist), c_conv))) {
384c19d619dSJacob Adams errx(EXIT_FAILURE, "unknown conversion %s", tmp.name);
385c19d619dSJacob Adams /* NOTREACHED */
386c19d619dSJacob Adams }
387c19d619dSJacob Adams if (ddflags & cp->noset) {
388c19d619dSJacob Adams errx(EXIT_FAILURE,
389c19d619dSJacob Adams "%s: illegal conversion combination", tmp.name);
390c19d619dSJacob Adams /* NOTREACHED */
391c19d619dSJacob Adams }
392c19d619dSJacob Adams ddflags |= cp->set;
393c19d619dSJacob Adams if (cp->ctab)
394c19d619dSJacob Adams ctab = cp->ctab;
395c19d619dSJacob Adams }
396c19d619dSJacob Adams }
397c19d619dSJacob Adams
398c19d619dSJacob Adams static int
c_conv(const void * a,const void * b)399c19d619dSJacob Adams c_conv(const void *a, const void *b)
400c19d619dSJacob Adams {
401c19d619dSJacob Adams
402c19d619dSJacob Adams return (strcmp(((const struct conv *)a)->name,
403c19d619dSJacob Adams ((const struct conv *)b)->name));
404c19d619dSJacob Adams }
405c19d619dSJacob Adams
406c19d619dSJacob Adams #endif /* NO_CONV */
407*0a6a1f1dSLionel Sambuc
408*0a6a1f1dSLionel Sambuc static void
f_iflag(char * arg)409*0a6a1f1dSLionel Sambuc f_iflag(char *arg)
410*0a6a1f1dSLionel Sambuc {
411*0a6a1f1dSLionel Sambuc /* Build a small version (i.e. for a ramdisk root) */
412*0a6a1f1dSLionel Sambuc #ifdef NO_IOFLAG
413*0a6a1f1dSLionel Sambuc errx(EXIT_FAILURE, "iflag option disabled");
414*0a6a1f1dSLionel Sambuc /* NOTREACHED */
415*0a6a1f1dSLionel Sambuc #else
416*0a6a1f1dSLionel Sambuc iflag = f_ioflag(arg, C_IFLAG);
417*0a6a1f1dSLionel Sambuc return;
418*0a6a1f1dSLionel Sambuc #endif
419*0a6a1f1dSLionel Sambuc }
420*0a6a1f1dSLionel Sambuc
421*0a6a1f1dSLionel Sambuc static void
f_oflag(char * arg)422*0a6a1f1dSLionel Sambuc f_oflag(char *arg)
423*0a6a1f1dSLionel Sambuc {
424*0a6a1f1dSLionel Sambuc /* Build a small version (i.e. for a ramdisk root) */
425*0a6a1f1dSLionel Sambuc #ifdef NO_IOFLAG
426*0a6a1f1dSLionel Sambuc errx(EXIT_FAILURE, "oflag option disabled");
427*0a6a1f1dSLionel Sambuc /* NOTREACHED */
428*0a6a1f1dSLionel Sambuc #else
429*0a6a1f1dSLionel Sambuc oflag = f_ioflag(arg, C_OFLAG);
430*0a6a1f1dSLionel Sambuc return;
431*0a6a1f1dSLionel Sambuc #endif
432*0a6a1f1dSLionel Sambuc }
433*0a6a1f1dSLionel Sambuc
434*0a6a1f1dSLionel Sambuc #ifndef NO_IOFLAG
435*0a6a1f1dSLionel Sambuc static const struct ioflag {
436*0a6a1f1dSLionel Sambuc const char *name;
437*0a6a1f1dSLionel Sambuc u_int set;
438*0a6a1f1dSLionel Sambuc u_int allowed;
439*0a6a1f1dSLionel Sambuc } olist[] = {
440*0a6a1f1dSLionel Sambuc /* the array needs to be sorted by the first column so
441*0a6a1f1dSLionel Sambuc bsearch() can be used to find commands quickly */
442*0a6a1f1dSLionel Sambuc { "alt_io", O_ALT_IO, C_IFLAG|C_OFLAG },
443*0a6a1f1dSLionel Sambuc { "append", O_APPEND, C_OFLAG },
444*0a6a1f1dSLionel Sambuc { "async", O_ASYNC, C_IFLAG|C_OFLAG },
445*0a6a1f1dSLionel Sambuc { "cloexec", O_CLOEXEC, C_IFLAG|C_OFLAG },
446*0a6a1f1dSLionel Sambuc { "creat", O_CREAT, C_OFLAG },
447*0a6a1f1dSLionel Sambuc { "direct", O_DIRECT, C_IFLAG|C_OFLAG },
448*0a6a1f1dSLionel Sambuc { "directory", O_DIRECTORY, C_NONE },
449*0a6a1f1dSLionel Sambuc { "dsync", O_DSYNC, C_OFLAG },
450*0a6a1f1dSLionel Sambuc { "excl", O_EXCL, C_IFLAG|C_OFLAG },
451*0a6a1f1dSLionel Sambuc { "exlock", O_EXLOCK, C_IFLAG|C_OFLAG },
452*0a6a1f1dSLionel Sambuc { "noctty", O_NOCTTY, C_IFLAG|C_OFLAG },
453*0a6a1f1dSLionel Sambuc { "nofollow", O_NOFOLLOW, C_IFLAG|C_OFLAG },
454*0a6a1f1dSLionel Sambuc { "nonblock", O_NONBLOCK, C_IFLAG|C_OFLAG },
455*0a6a1f1dSLionel Sambuc { "nosigpipe", O_NOSIGPIPE, C_IFLAG|C_OFLAG },
456*0a6a1f1dSLionel Sambuc { "rdonly", O_RDONLY, C_IFLAG },
457*0a6a1f1dSLionel Sambuc { "rdwr", O_RDWR, C_IFLAG },
458*0a6a1f1dSLionel Sambuc { "rsync", O_RSYNC, C_IFLAG },
459*0a6a1f1dSLionel Sambuc { "search", O_SEARCH, C_IFLAG|C_OFLAG },
460*0a6a1f1dSLionel Sambuc { "shlock", O_SHLOCK, C_IFLAG|C_OFLAG },
461*0a6a1f1dSLionel Sambuc { "sync", O_SYNC, C_IFLAG|C_OFLAG },
462*0a6a1f1dSLionel Sambuc { "trunc", O_TRUNC, C_IFLAG|C_OFLAG },
463*0a6a1f1dSLionel Sambuc { "wronly", O_WRONLY, C_NONE },
464*0a6a1f1dSLionel Sambuc };
465*0a6a1f1dSLionel Sambuc
466*0a6a1f1dSLionel Sambuc static u_int
f_ioflag(char * arg,u_int flagtype)467*0a6a1f1dSLionel Sambuc f_ioflag(char *arg, u_int flagtype)
468*0a6a1f1dSLionel Sambuc {
469*0a6a1f1dSLionel Sambuc u_int ioflag = 0;
470*0a6a1f1dSLionel Sambuc struct ioflag *cp, tmp;
471*0a6a1f1dSLionel Sambuc const char *flagstr = (flagtype == C_IFLAG) ? "iflag" : "oflag";
472*0a6a1f1dSLionel Sambuc
473*0a6a1f1dSLionel Sambuc while (arg != NULL) {
474*0a6a1f1dSLionel Sambuc tmp.name = strsep(&arg, ",");
475*0a6a1f1dSLionel Sambuc if (!(cp = bsearch(&tmp, olist,
476*0a6a1f1dSLionel Sambuc __arraycount(olist), sizeof(*olist), c_ioflag))) {
477*0a6a1f1dSLionel Sambuc errx(EXIT_FAILURE, "unknown %s %s", flagstr, tmp.name);
478*0a6a1f1dSLionel Sambuc /* NOTREACHED */
479*0a6a1f1dSLionel Sambuc }
480*0a6a1f1dSLionel Sambuc
481*0a6a1f1dSLionel Sambuc if ((cp->set & O_ACCMODE) && (flagtype == C_OFLAG)) {
482*0a6a1f1dSLionel Sambuc warnx("rdonly, rdwr and wronly are ignored for oflag");
483*0a6a1f1dSLionel Sambuc continue;
484*0a6a1f1dSLionel Sambuc }
485*0a6a1f1dSLionel Sambuc
486*0a6a1f1dSLionel Sambuc if ((cp->allowed & flagtype) == 0) {
487*0a6a1f1dSLionel Sambuc warnx("%s set for %s but makes no sense",
488*0a6a1f1dSLionel Sambuc cp->name, flagstr);
489*0a6a1f1dSLionel Sambuc }
490*0a6a1f1dSLionel Sambuc
491*0a6a1f1dSLionel Sambuc ioflag |= cp->set;
492*0a6a1f1dSLionel Sambuc }
493*0a6a1f1dSLionel Sambuc
494*0a6a1f1dSLionel Sambuc
495*0a6a1f1dSLionel Sambuc return ioflag;
496*0a6a1f1dSLionel Sambuc }
497*0a6a1f1dSLionel Sambuc
498*0a6a1f1dSLionel Sambuc static int
c_ioflag(const void * a,const void * b)499*0a6a1f1dSLionel Sambuc c_ioflag(const void *a, const void *b)
500*0a6a1f1dSLionel Sambuc {
501*0a6a1f1dSLionel Sambuc
502*0a6a1f1dSLionel Sambuc return (strcmp(((const struct ioflag *)a)->name,
503*0a6a1f1dSLionel Sambuc ((const struct ioflag *)b)->name));
504*0a6a1f1dSLionel Sambuc }
505*0a6a1f1dSLionel Sambuc #endif /* NO_IOFLAG */
506