mkfifo.c revision 99112
1249259Sdim/*
2249259Sdim * Copyright (c) 1990, 1993
3249259Sdim *	The Regents of the University of California.  All rights reserved.
4249259Sdim *
5249259Sdim * Redistribution and use in source and binary forms, with or without
6249259Sdim * modification, are permitted provided that the following conditions
7249259Sdim * are met:
8249259Sdim * 1. Redistributions of source code must retain the above copyright
9249259Sdim *    notice, this list of conditions and the following disclaimer.
10249259Sdim * 2. Redistributions in binary form must reproduce the above copyright
11249259Sdim *    notice, this list of conditions and the following disclaimer in the
12249259Sdim *    documentation and/or other materials provided with the distribution.
13249259Sdim * 3. All advertising materials mentioning features or use of this software
14249259Sdim *    must display the following acknowledgement:
15249259Sdim *	This product includes software developed by the University of
16249259Sdim *	California, Berkeley and its contributors.
17249259Sdim * 4. Neither the name of the University nor the names of its contributors
18249259Sdim *    may be used to endorse or promote products derived from this software
19249259Sdim *    without specific prior written permission.
20249259Sdim *
21249259Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22249259Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23249259Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24249259Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25249259Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26249259Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27249259Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28249259Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29249259Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30249259Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31249259Sdim * SUCH DAMAGE.
32249259Sdim */
33249259Sdim
34249259Sdim#ifndef lint
35249259Sdimstatic const char copyright[] =
36249259Sdim"@(#) Copyright (c) 1990, 1993\n\
37249259Sdim	The Regents of the University of California.  All rights reserved.\n";
38249259Sdim#endif /* not lint */
39249259Sdim
40249259Sdim#ifndef lint
41249259Sdim#if 0
42249259Sdimstatic char sccsid[] = "@(#)mkfifo.c	8.2 (Berkeley) 1/5/94";
43249259Sdim#endif
44249259Sdim#endif /* not lint */
45249259Sdim#include <sys/cdefs.h>
46249259Sdim__FBSDID("$FreeBSD: head/usr.bin/mkfifo/mkfifo.c 99112 2002-06-30 05:25:07Z obrien $");
47249259Sdim
48249259Sdim#include <sys/types.h>
49249259Sdim#include <sys/stat.h>
50249259Sdim
51249259Sdim#include <err.h>
52249259Sdim#include <errno.h>
53249259Sdim#include <stdio.h>
54249259Sdim#include <stdlib.h>
55249259Sdim#include <string.h>
56249259Sdim#include <unistd.h>
57249259Sdim
58249259Sdim#define	BASEMODE	S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | \
59249259Sdim			S_IROTH | S_IWOTH
60249259Sdim
61249259Sdimstatic void usage(void);
62249259Sdim
63249259Sdimstatic int f_mode;
64249259Sdim
65249259Sdimint
66249259Sdimmain(argc, argv)
67249259Sdim	int argc;
68249259Sdim	char *argv[];
69249259Sdim{
70249259Sdim	char *modestr;
71249259Sdim	void *modep;
72249259Sdim	mode_t fifomode;
73249259Sdim	int ch, exitval;
74249259Sdim
75249259Sdim	while ((ch = getopt(argc, argv, "m:")) != -1)
76249259Sdim		switch(ch) {
77249259Sdim		case 'm':
78249259Sdim			f_mode = 1;
79249259Sdim			modestr = optarg;
80249259Sdim			break;
81249259Sdim		case '?':
82249259Sdim		default:
83249259Sdim			usage();
84249259Sdim		}
85249259Sdim	argc -= optind;
86249259Sdim	argv += optind;
87249259Sdim	if (argv[0] == NULL)
88249259Sdim		usage();
89249259Sdim
90249259Sdim	if (f_mode) {
91249259Sdim		umask(0);
92249259Sdim		errno = 0;
93249259Sdim		if ((modep = setmode(modestr)) == NULL) {
94249259Sdim			if (errno)
95249259Sdim				err(1, "setmode");
96249259Sdim			errx(1, "invalid file mode: %s", modestr);
97249259Sdim		}
98249259Sdim		fifomode = getmode(modep, BASEMODE);
99249259Sdim	} else {
100249259Sdim		fifomode = BASEMODE;
101249259Sdim	}
102249259Sdim
103249259Sdim	for (exitval = 0; *argv != NULL; ++argv)
104249259Sdim		if (mkfifo(*argv, fifomode) < 0) {
105249259Sdim			warn("%s", *argv);
106249259Sdim			exitval = 1;
107249259Sdim		}
108249259Sdim	exit(exitval);
109249259Sdim}
110249259Sdim
111249259Sdimstatic void
112249259Sdimusage()
113249259Sdim{
114249259Sdim	(void)fprintf(stderr, "usage: mkfifo [-m mode] fifo_name ...\n");
115249259Sdim	exit(1);
116249259Sdim}
117249259Sdim