mknod.c revision 37422
11558Srgrimes/*
21558Srgrimes * Copyright (c) 1989, 1993
31558Srgrimes *	The Regents of the University of California.  All rights reserved.
41558Srgrimes *
51558Srgrimes * This code is derived from software contributed to Berkeley by
61558Srgrimes * Kevin Fall.
71558Srgrimes *
81558Srgrimes * Redistribution and use in source and binary forms, with or without
91558Srgrimes * modification, are permitted provided that the following conditions
101558Srgrimes * are met:
111558Srgrimes * 1. Redistributions of source code must retain the above copyright
121558Srgrimes *    notice, this list of conditions and the following disclaimer.
131558Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141558Srgrimes *    notice, this list of conditions and the following disclaimer in the
151558Srgrimes *    documentation and/or other materials provided with the distribution.
161558Srgrimes * 3. All advertising materials mentioning features or use of this software
171558Srgrimes *    must display the following acknowledgement:
181558Srgrimes *	This product includes software developed by the University of
191558Srgrimes *	California, Berkeley and its contributors.
201558Srgrimes * 4. Neither the name of the University nor the names of its contributors
211558Srgrimes *    may be used to endorse or promote products derived from this software
221558Srgrimes *    without specific prior written permission.
231558Srgrimes *
241558Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251558Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261558Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271558Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281558Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291558Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301558Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311558Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321558Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331558Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341558Srgrimes * SUCH DAMAGE.
351558Srgrimes */
361558Srgrimes
371558Srgrimes#ifndef lint
3817347Sbdestatic const char copyright[] =
391558Srgrimes"@(#) Copyright (c) 1989, 1993\n\
401558Srgrimes	The Regents of the University of California.  All rights reserved.\n";
411558Srgrimes#endif /* not lint */
421558Srgrimes
431558Srgrimes#ifndef lint
4417347Sbde#if 0
451558Srgrimesstatic char sccsid[] = "@(#)mknod.c	8.1 (Berkeley) 6/5/93";
4637422Scharnier#endif
4717347Sbdestatic const char rcsid[] =
4837422Scharnier	"$Id$";
491558Srgrimes#endif /* not lint */
501558Srgrimes
511558Srgrimes#include <sys/types.h>
521558Srgrimes#include <sys/stat.h>
5317347Sbde
5437422Scharnier#include <err.h>
5517347Sbde#include <errno.h>
561558Srgrimes#include <stdio.h>
5717347Sbde#include <stdlib.h>
5817347Sbde#include <unistd.h>
591558Srgrimes
6037422Scharnierstatic void
6137422Scharnierusage()
6237422Scharnier{
6337422Scharnier	(void)fprintf(stderr, "usage: mknod name [b | c] major minor\n");
6437422Scharnier	exit(1);
6537422Scharnier}
6637422Scharnier
6717347Sbdeint
681558Srgrimesmain(argc, argv)
691558Srgrimes	int argc;
701558Srgrimes	char **argv;
711558Srgrimes{
7217347Sbde	dev_t dev;
7317302Sjoerg	char *endp;
7417347Sbde	long major, minor;
7517347Sbde	mode_t mode;
7617347Sbde	int range_error;
771558Srgrimes
7837422Scharnier	if (argc != 5)
7937422Scharnier		usage();
801558Srgrimes
811558Srgrimes	mode = 0666;
821558Srgrimes	if (argv[2][0] == 'c')
831558Srgrimes		mode |= S_IFCHR;
841558Srgrimes	else if (argv[2][0] == 'b')
851558Srgrimes		mode |= S_IFBLK;
8626674Scharnier	else
8726674Scharnier		errx(1, "node must be type 'b' or 'c'");
881558Srgrimes
8917347Sbde	errno = 0;
9017347Sbde	major = (long)strtoul(argv[3], &endp, 0);
9126674Scharnier	if (endp == argv[3] || *endp != '\0')
9226674Scharnier		errx(1, "%s: non-numeric major number", argv[3]);
9317347Sbde	range_error = errno;
9417347Sbde	errno = 0;
9517347Sbde	minor = (long)strtoul(argv[4], &endp, 0);
9626674Scharnier	if (endp == argv[4] || *endp != '\0')
9726674Scharnier		errx(1, "%s: non-numeric minor number", argv[4]);
9817347Sbde	range_error |= errno;
9917347Sbde	dev = makedev(major, minor);
10026674Scharnier	if (range_error || major(dev) != major || minor(dev) != minor)
10126674Scharnier		errx(1, "major or minor number too large");
10217302Sjoerg
10326674Scharnier	if (mknod(argv[1], mode, dev) != 0)
10426674Scharnier		err(1, "%s", argv[1]);
1051558Srgrimes	exit(0);
1061558Srgrimes}
107