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