1/***********************************************************************
2*                                                                      *
3*               This software is part of the ast package               *
4*          Copyright (c) 1992-2012 AT&T Intellectual Property          *
5*                      and is licensed under the                       *
6*                 Eclipse Public License, Version 1.0                  *
7*                    by AT&T Intellectual Property                     *
8*                                                                      *
9*                A copy of the License is available at                 *
10*          http://www.eclipse.org/org/documents/epl-v10.html           *
11*         (with md5 checksum b35adb5213ca9657e911e9befb180842)         *
12*                                                                      *
13*              Information and Software Systems Research               *
14*                            AT&T Research                             *
15*                           Florham Park NJ                            *
16*                                                                      *
17*                 Glenn Fowler <gsf@research.att.com>                  *
18*                  David Korn <dgk@research.att.com>                   *
19*                                                                      *
20***********************************************************************/
21#pragma prototyped
22/*
23 * David Korn
24 * AT&T Bell Laboratories
25 *
26 * mkfifo
27 */
28
29static const char usage[] =
30"[-?\n@(#)$Id: mkfifo (AT&T Research) 2009-01-02 $\n]"
31USAGE_LICENSE
32"[+NAME?mkfifo - make FIFOs (named pipes)]"
33"[+DESCRIPTION?\bmkfifo\b creates one or more FIFO's.  By "
34	"default, the mode of created FIFO is \ba=rw\b minus the "
35	"bits set in the \bumask\b(1).]"
36"[m:mode]:[mode?Set the mode of created FIFO to \amode\a.  "
37	"\amode\a is symbolic or octal mode as in \bchmod\b(1).  Relative "
38	"modes assume an initial mode of \ba=rw\b.]"
39"\n"
40"\nfile ...\n"
41"\n"
42"[+EXIT STATUS?]{"
43        "[+0?All FIFO's created successfully.]"
44        "[+>0?One or more FIFO's could not be created.]"
45"}"
46"[+SEE ALSO?\bchmod\b(1), \bumask\b(1)]"
47;
48
49#include <cmd.h>
50#include <ls.h>
51
52int
53b_mkfifo(int argc, char** argv, Shbltin_t* context)
54{
55	register char*	arg;
56	register mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
57	register mode_t	mask = 0;
58	register int	mflag = 0;
59
60	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
61	for (;;)
62	{
63		switch (optget(argv, usage))
64		{
65		case 'm':
66			mflag = 1;
67			mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
68			if (*opt_info.arg)
69				error(ERROR_exit(0), "%s: invalid mode", arg);
70			continue;
71		case ':':
72			error(2, "%s", opt_info.arg);
73			break;
74		case '?':
75			error(ERROR_usage(2), "%s", opt_info.arg);
76			break;
77		}
78		break;
79	}
80	argv += opt_info.index;
81	if (error_info.errors || !*argv)
82		error(ERROR_usage(2), "%s", optusage(NiL));
83	mask = umask(0);
84	if (!mflag)
85	{
86		mode &= ~mask;
87		umask(mask);
88		mask = 0;
89	}
90	while (arg = *argv++)
91		if (mkfifo(arg, mode) < 0)
92			error(ERROR_system(0), "%s:", arg);
93	if (mask)
94		umask(mask);
95	return error_info.errors != 0;
96}
97