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