xref: /freebsd/sbin/mknod/mknod.c (revision 7c9e694fa7f5a0e1d55d315d0b51d91d3a56e603)
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[] =
487c9e694fSBruce Evans     "$Id$";
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>
607c9e694fSBruce Evans 
617c9e694fSBruce Evans int
628fae3551SRodney W. Grimes main(argc, argv)
638fae3551SRodney W. Grimes 	int argc;
648fae3551SRodney W. Grimes 	char **argv;
658fae3551SRodney W. Grimes {
667c9e694fSBruce Evans 	dev_t dev;
67ce6bb537SJoerg Wunsch 	char *endp;
687c9e694fSBruce Evans 	long major, minor;
697c9e694fSBruce Evans 	mode_t mode;
707c9e694fSBruce Evans 	int range_error;
718fae3551SRodney W. Grimes 
728fae3551SRodney W. Grimes 	if (argc != 5) {
738fae3551SRodney W. Grimes 		(void)fprintf(stderr,
748fae3551SRodney W. Grimes 		    "usage: mknod name [b | c] major minor\n");
758fae3551SRodney W. Grimes 		exit(1);
768fae3551SRodney W. Grimes 	}
778fae3551SRodney W. Grimes 
788fae3551SRodney W. Grimes 	mode = 0666;
798fae3551SRodney W. Grimes 	if (argv[2][0] == 'c')
808fae3551SRodney W. Grimes 		mode |= S_IFCHR;
818fae3551SRodney W. Grimes 	else if (argv[2][0] == 'b')
828fae3551SRodney W. Grimes 		mode |= S_IFBLK;
838fae3551SRodney W. Grimes 	else {
848fae3551SRodney W. Grimes 		(void)fprintf(stderr,
857c9e694fSBruce Evans 		    "mknod: node must be type 'b' or 'c'\n");
868fae3551SRodney W. Grimes 		exit(1);
878fae3551SRodney W. Grimes 	}
888fae3551SRodney W. Grimes 
897c9e694fSBruce Evans 	errno = 0;
907c9e694fSBruce Evans 	major = (long)strtoul(argv[3], &endp, 0);
917c9e694fSBruce Evans 	if (endp == argv[3] || *endp != '\0') {
92ce6bb537SJoerg Wunsch 		(void)fprintf(stderr,
937c9e694fSBruce Evans 		    "mknod: %s: non-numeric major number\n", argv[3]);
94ce6bb537SJoerg Wunsch 		exit(1);
95ce6bb537SJoerg Wunsch 	}
967c9e694fSBruce Evans 	range_error = errno;
977c9e694fSBruce Evans 	errno = 0;
987c9e694fSBruce Evans 	minor = (long)strtoul(argv[4], &endp, 0);
997c9e694fSBruce Evans 	if (endp == argv[3] || *endp != '\0') {
100ce6bb537SJoerg Wunsch 		(void)fprintf(stderr,
1017c9e694fSBruce Evans 		    "mknod: %s: non-numeric minor number\n", argv[4]);
1027c9e694fSBruce Evans 		exit(1);
1037c9e694fSBruce Evans 	}
1047c9e694fSBruce Evans 	range_error |= errno;
1057c9e694fSBruce Evans 	dev = makedev(major, minor);
1067c9e694fSBruce Evans 	if (range_error || major(dev) != major || minor(dev) != minor) {
1077c9e694fSBruce Evans 		(void)fprintf(stderr,
1087c9e694fSBruce Evans 		    "mknod: major or minor number too large\n");
109ce6bb537SJoerg Wunsch 		exit(1);
110ce6bb537SJoerg Wunsch 	}
111ce6bb537SJoerg Wunsch 
1127c9e694fSBruce Evans 	if (mknod(argv[1], mode, dev) != 0) {
1138fae3551SRodney W. Grimes 		(void)fprintf(stderr,
1148fae3551SRodney W. Grimes 		    "mknod: %s: %s\n", argv[1], strerror(errno));
1158fae3551SRodney W. Grimes 		exit(1);
1168fae3551SRodney W. Grimes 	}
1178fae3551SRodney W. Grimes 	exit(0);
1188fae3551SRodney W. Grimes }
119