xref: /freebsd/sbin/mknod/mknod.c (revision 95b9c18a7dbc89cad7545424b6c2cac64ef35fbf)
18fae3551SRodney W. Grimes /*
28fae3551SRodney W. Grimes  * Copyright (c) 1989, 1993
38fae3551SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
48fae3551SRodney W. Grimes  *
58fae3551SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
68fae3551SRodney W. Grimes  * Kevin Fall.
78fae3551SRodney W. Grimes  *
88fae3551SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
98fae3551SRodney W. Grimes  * modification, are permitted provided that the following conditions
108fae3551SRodney W. Grimes  * are met:
118fae3551SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
128fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
138fae3551SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
148fae3551SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
158fae3551SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
168fae3551SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
178fae3551SRodney W. Grimes  *    must display the following acknowledgement:
188fae3551SRodney W. Grimes  *	This product includes software developed by the University of
198fae3551SRodney W. Grimes  *	California, Berkeley and its contributors.
208fae3551SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
218fae3551SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
228fae3551SRodney W. Grimes  *    without specific prior written permission.
238fae3551SRodney W. Grimes  *
248fae3551SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
258fae3551SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
268fae3551SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
278fae3551SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
288fae3551SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
298fae3551SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
308fae3551SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
318fae3551SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
328fae3551SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
338fae3551SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
348fae3551SRodney W. Grimes  * SUCH DAMAGE.
358fae3551SRodney W. Grimes  */
368fae3551SRodney W. Grimes 
378fae3551SRodney W. Grimes #ifndef lint
387c9e694fSBruce Evans static const char copyright[] =
398fae3551SRodney W. Grimes "@(#) Copyright (c) 1989, 1993\n\
408fae3551SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
418fae3551SRodney W. Grimes #endif /* not lint */
428fae3551SRodney W. Grimes 
438fae3551SRodney W. Grimes #ifndef lint
447c9e694fSBruce Evans #if 0
458fae3551SRodney W. Grimes static char sccsid[] = "@(#)mknod.c	8.1 (Berkeley) 6/5/93";
467c9e694fSBruce Evans #endif
47fbb3447fSPhilippe Charnier static const char rcsid[] =
4895b9c18aSPoul-Henning Kamp 	"$Id: mknod.c,v 1.8 1998/07/06 07:06:15 charnier Exp $";
498fae3551SRodney W. Grimes #endif /* not lint */
508fae3551SRodney W. Grimes 
518fae3551SRodney W. Grimes #include <sys/types.h>
528fae3551SRodney W. Grimes #include <sys/stat.h>
538fae3551SRodney W. Grimes 
54fbb3447fSPhilippe Charnier #include <err.h>
557c9e694fSBruce Evans #include <errno.h>
567c9e694fSBruce Evans #include <stdio.h>
577c9e694fSBruce Evans #include <stdlib.h>
587c9e694fSBruce Evans #include <unistd.h>
59fbb3447fSPhilippe Charnier 
60fbb3447fSPhilippe Charnier static void
61fbb3447fSPhilippe Charnier usage()
62fbb3447fSPhilippe Charnier {
63fbb3447fSPhilippe Charnier 	(void)fprintf(stderr, "usage: mknod name [b | c] major minor\n");
64fbb3447fSPhilippe Charnier 	exit(1);
65fbb3447fSPhilippe Charnier }
667c9e694fSBruce Evans 
677c9e694fSBruce Evans int
688fae3551SRodney W. Grimes main(argc, argv)
698fae3551SRodney W. Grimes 	int argc;
708fae3551SRodney W. Grimes 	char **argv;
718fae3551SRodney W. Grimes {
727c9e694fSBruce Evans 	dev_t dev;
73ce6bb537SJoerg Wunsch 	char *endp;
7495b9c18aSPoul-Henning Kamp 	long mymajor, myminor;
757c9e694fSBruce Evans 	mode_t mode;
767c9e694fSBruce Evans 	int range_error;
778fae3551SRodney W. Grimes 
78fbb3447fSPhilippe Charnier 	if (argc != 5)
79fbb3447fSPhilippe Charnier 		usage();
808fae3551SRodney W. Grimes 
818fae3551SRodney W. Grimes 	mode = 0666;
828fae3551SRodney W. Grimes 	if (argv[2][0] == 'c')
838fae3551SRodney W. Grimes 		mode |= S_IFCHR;
848fae3551SRodney W. Grimes 	else if (argv[2][0] == 'b')
858fae3551SRodney W. Grimes 		mode |= S_IFBLK;
8686eaffafSPhilippe Charnier 	else
8786eaffafSPhilippe Charnier 		errx(1, "node must be type 'b' or 'c'");
888fae3551SRodney W. Grimes 
897c9e694fSBruce Evans 	errno = 0;
9095b9c18aSPoul-Henning Kamp 	mymajor = (long)strtoul(argv[3], &endp, 0);
9186eaffafSPhilippe Charnier 	if (endp == argv[3] || *endp != '\0')
9286eaffafSPhilippe Charnier 		errx(1, "%s: non-numeric major number", argv[3]);
937c9e694fSBruce Evans 	range_error = errno;
947c9e694fSBruce Evans 	errno = 0;
9595b9c18aSPoul-Henning Kamp 	myminor = (long)strtoul(argv[4], &endp, 0);
9686eaffafSPhilippe Charnier 	if (endp == argv[4] || *endp != '\0')
9786eaffafSPhilippe Charnier 		errx(1, "%s: non-numeric minor number", argv[4]);
987c9e694fSBruce Evans 	range_error |= errno;
9995b9c18aSPoul-Henning Kamp 	dev = makedev(mymajor, myminor);
10095b9c18aSPoul-Henning Kamp 	if (range_error || major(dev) != mymajor || minor(dev) != myminor)
10186eaffafSPhilippe Charnier 		errx(1, "major or minor number too large");
102ce6bb537SJoerg Wunsch 
10386eaffafSPhilippe Charnier 	if (mknod(argv[1], mode, dev) != 0)
10486eaffafSPhilippe Charnier 		err(1, "%s", argv[1]);
1058fae3551SRodney W. Grimes 	exit(0);
1068fae3551SRodney W. Grimes }
107