xref: /freebsd/sbin/mknod/mknod.c (revision 86eaffaf5152edfad5ea0ff3168ace1f25c4096c)
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 #else
477c9e694fSBruce Evans static const char rcsid[] =
4886eaffafSPhilippe Charnier     "$Id: mknod.c,v 1.6 1997/03/12 19:03:40 bde Exp $";
497c9e694fSBruce Evans #endif
508fae3551SRodney W. Grimes #endif /* not lint */
518fae3551SRodney W. Grimes 
528fae3551SRodney W. Grimes #include <sys/types.h>
538fae3551SRodney W. Grimes #include <sys/stat.h>
548fae3551SRodney W. Grimes 
557c9e694fSBruce Evans #include <errno.h>
567c9e694fSBruce Evans #include <stdio.h>
577c9e694fSBruce Evans #include <stdlib.h>
587c9e694fSBruce Evans #include <string.h>
597c9e694fSBruce Evans #include <unistd.h>
6086eaffafSPhilippe Charnier #include <err.h>
617c9e694fSBruce Evans 
627c9e694fSBruce Evans int
638fae3551SRodney W. Grimes main(argc, argv)
648fae3551SRodney W. Grimes 	int argc;
658fae3551SRodney W. Grimes 	char **argv;
668fae3551SRodney W. Grimes {
677c9e694fSBruce Evans 	dev_t dev;
68ce6bb537SJoerg Wunsch 	char *endp;
697c9e694fSBruce Evans 	long major, minor;
707c9e694fSBruce Evans 	mode_t mode;
717c9e694fSBruce Evans 	int range_error;
728fae3551SRodney W. Grimes 
738fae3551SRodney W. Grimes 	if (argc != 5) {
748fae3551SRodney W. Grimes 		(void)fprintf(stderr,
758fae3551SRodney W. Grimes 		    "usage: mknod name [b | c] major minor\n");
768fae3551SRodney W. Grimes 		exit(1);
778fae3551SRodney W. Grimes 	}
788fae3551SRodney W. Grimes 
798fae3551SRodney W. Grimes 	mode = 0666;
808fae3551SRodney W. Grimes 	if (argv[2][0] == 'c')
818fae3551SRodney W. Grimes 		mode |= S_IFCHR;
828fae3551SRodney W. Grimes 	else if (argv[2][0] == 'b')
838fae3551SRodney W. Grimes 		mode |= S_IFBLK;
8486eaffafSPhilippe Charnier 	else
8586eaffafSPhilippe Charnier 		errx(1, "node must be type 'b' or 'c'");
868fae3551SRodney W. Grimes 
877c9e694fSBruce Evans 	errno = 0;
887c9e694fSBruce Evans 	major = (long)strtoul(argv[3], &endp, 0);
8986eaffafSPhilippe Charnier 	if (endp == argv[3] || *endp != '\0')
9086eaffafSPhilippe Charnier 		errx(1, "%s: non-numeric major number", argv[3]);
917c9e694fSBruce Evans 	range_error = errno;
927c9e694fSBruce Evans 	errno = 0;
937c9e694fSBruce Evans 	minor = (long)strtoul(argv[4], &endp, 0);
9486eaffafSPhilippe Charnier 	if (endp == argv[4] || *endp != '\0')
9586eaffafSPhilippe Charnier 		errx(1, "%s: non-numeric minor number", argv[4]);
967c9e694fSBruce Evans 	range_error |= errno;
977c9e694fSBruce Evans 	dev = makedev(major, minor);
9886eaffafSPhilippe Charnier 	if (range_error || major(dev) != major || minor(dev) != minor)
9986eaffafSPhilippe Charnier 		errx(1, "major or minor number too large");
100ce6bb537SJoerg Wunsch 
10186eaffafSPhilippe Charnier 	if (mknod(argv[1], mode, dev) != 0)
10286eaffafSPhilippe Charnier 		err(1, "%s", argv[1]);
1038fae3551SRodney W. Grimes 	exit(0);
1048fae3551SRodney W. Grimes }
105