Home | History | Annotate | Line # | Download | only in mknod
mknod.c revision 1.22
      1 /*	$NetBSD: mknod.c,v 1.22 2001/10/08 04:20:44 lukem Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1998, 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __COPYRIGHT("@(#) Copyright (c) 1998 The NetBSD Foundation, Inc.  All rights reserved.\n");
     42 __RCSID("$NetBSD: mknod.c,v 1.22 2001/10/08 04:20:44 lukem Exp $");
     43 #endif /* not lint */
     44 
     45 #include <sys/types.h>
     46 #include <sys/stat.h>
     47 
     48 #include <err.h>
     49 #include <errno.h>
     50 #include <limits.h>
     51 #include <stdio.h>
     52 #include <stdlib.h>
     53 #include <unistd.h>
     54 #include <string.h>
     55 
     56 #include "mknod.h"
     57 
     58 	int	main(int, char *[]);
     59 static	void	usage(void);
     60 
     61 
     62 #define	MAXARGS	8
     63 
     64 int
     65 main(int argc, char **argv)
     66 {
     67 	char	*name, *p;
     68 	mode_t	 mode;
     69 	dev_t	 dev;
     70 	pack_t	*pack;
     71 	u_long	 numbers[MAXARGS];
     72 	int	 n, ch, fifo, hasformat;
     73 
     74 	dev = 0;
     75 	fifo = hasformat = 0;
     76 	pack = pack_native;
     77 
     78 	while ((ch = getopt(argc, argv, "F:")) != -1) {
     79 		switch (ch) {
     80 		case 'F':
     81 			pack = find_format(optarg);
     82 			if (pack == NULL)
     83 				errx(1, "invalid format: %s", optarg);
     84 			hasformat++;
     85 			break;
     86 
     87 		default:
     88 		case '?':
     89 			usage();
     90 		}
     91 	}
     92 	argc -= optind;
     93 	argv += optind;
     94 
     95 	if (argc < 2 || argc > 10)
     96 		usage();
     97 
     98 	name = *argv;
     99 	argc--;
    100 	argv++;
    101 
    102 	mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
    103 	if (argv[0][1] != '\0')
    104 		goto badtype;
    105 	switch (*argv[0]) {
    106 	case 'c':
    107 		mode |= S_IFCHR;
    108 		break;
    109 
    110 	case 'b':
    111 		mode |= S_IFBLK;
    112 		break;
    113 
    114 	case 'p':
    115 		if (hasformat)
    116 			errx(1, "format is meaningless for fifos");
    117 		fifo = 1;
    118 		break;
    119 
    120 	default:
    121  badtype:
    122 		errx(1, "node type must be 'b', 'c' or 'p'.");
    123 	}
    124 	argc--;
    125 	argv++;
    126 
    127 	if (fifo) {
    128 		if (argc != 0)
    129 			usage();
    130 	} else {
    131 		if (argc < 1 || argc >= MAXARGS)
    132 			usage();
    133 	}
    134 
    135 	for (n = 0; n < argc; n++) {
    136 		numbers[n] = strtoul(argv[n], &p, 0);
    137 		if ((p && *p != '\0') ||
    138 		    (numbers[n] == ULONG_MAX && errno == ERANGE))
    139 			errx(1, "invalid number: %s", argv[n]);
    140 	}
    141 
    142 	switch (argc) {
    143 	case 0:
    144 		dev = 0;
    145 		break;
    146 
    147 	case 1:
    148 		dev = numbers[0];
    149 		break;
    150 
    151 	default:
    152 		dev = (*pack)(argc, numbers);
    153 		break;
    154 	}
    155 
    156 #if 0
    157 	printf("name: %s\nmode: %05o\ndev:  %08x\n", name, mode, dev);
    158 #else
    159 	if ((fifo ? mkfifo(name, mode) : mknod(name, mode, dev)) < 0)
    160 		err(1, "%s", name);
    161 #endif
    162 
    163 	return(0);
    164 }
    165 
    166 static void
    167 usage(void)
    168 {
    169 	const char *progname = getprogname();
    170 
    171 	(void)fprintf(stderr,
    172 	    "Usage: %s [-F format] name [b | c] major minor\n", progname);
    173 	(void)fprintf(stderr,
    174 	    "       %s [-F format] name [b | c] major unit subunit\n",
    175 	    progname);
    176 	(void)fprintf(stderr, "       %s name [b | c] number\n", progname);
    177 	(void)fprintf(stderr, "       %s name p\n", progname);
    178 	exit(1);
    179 }
    180