xref: /onnv-gate/usr/src/lib/libcmd/common/mkfifo.c (revision 12068:08a39a083754)
14887Schin /***********************************************************************
24887Schin *                                                                      *
34887Schin *               This software is part of the ast package               *
4*12068SRoger.Faulkner@Oracle.COM *          Copyright (c) 1992-2010 AT&T Intellectual Property          *
54887Schin *                      and is licensed under the                       *
64887Schin *                  Common Public License, Version 1.0                  *
78462SApril.Chin@Sun.COM *                    by AT&T Intellectual Property                     *
84887Schin *                                                                      *
94887Schin *                A copy of the License is available at                 *
104887Schin *            http://www.opensource.org/licenses/cpl1.0.txt             *
114887Schin *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
124887Schin *                                                                      *
134887Schin *              Information and Software Systems Research               *
144887Schin *                            AT&T Research                             *
154887Schin *                           Florham Park NJ                            *
164887Schin *                                                                      *
174887Schin *                 Glenn Fowler <gsf@research.att.com>                  *
184887Schin *                  David Korn <dgk@research.att.com>                   *
194887Schin *                                                                      *
204887Schin ***********************************************************************/
214887Schin #pragma prototyped
224887Schin /*
234887Schin  * David Korn
244887Schin  * AT&T Bell Laboratories
254887Schin  *
264887Schin  * mkfifo
274887Schin  */
284887Schin 
294887Schin static const char usage[] =
3010898Sroland.mainz@nrubsig.org "[-?\n@(#)$Id: mkfifo (AT&T Research) 2009-01-02 $\n]"
314887Schin USAGE_LICENSE
324887Schin "[+NAME?mkfifo - make FIFOs (named pipes)]"
334887Schin "[+DESCRIPTION?\bmkfifo\b creates one or more FIFO's.  By "
344887Schin 	"default, the mode of created FIFO is \ba=rw\b minus the "
354887Schin 	"bits set in the \bumask\b(1).]"
364887Schin "[m:mode]:[mode?Set the mode of created FIFO to \amode\a.  "
374887Schin 	"\amode\a is symbolic or octal mode as in \bchmod\b(1).  Relative "
384887Schin 	"modes assume an initial mode of \ba=rw\b.]"
394887Schin "\n"
404887Schin "\nfile ...\n"
414887Schin "\n"
424887Schin "[+EXIT STATUS?]{"
434887Schin         "[+0?All FIFO's created successfully.]"
444887Schin         "[+>0?One or more FIFO's could not be created.]"
454887Schin "}"
464887Schin "[+SEE ALSO?\bchmod\b(1), \bumask\b(1)]"
474887Schin ;
484887Schin 
494887Schin #include <cmd.h>
504887Schin #include <ls.h>
514887Schin 
524887Schin int
b_mkfifo(int argc,char * argv[],void * context)534887Schin b_mkfifo(int argc, char *argv[], void* context)
544887Schin {
5510898Sroland.mainz@nrubsig.org 	register char*	arg;
5610898Sroland.mainz@nrubsig.org 	register mode_t mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
5710898Sroland.mainz@nrubsig.org 	register mode_t	mask = 0;
5810898Sroland.mainz@nrubsig.org 	register int	mflag = 0;
594887Schin 
604887Schin 	cmdinit(argc, argv, context, ERROR_CATALOG, 0);
6110898Sroland.mainz@nrubsig.org 	for (;;)
624887Schin 	{
6310898Sroland.mainz@nrubsig.org 		switch (optget(argv, usage))
6410898Sroland.mainz@nrubsig.org 		{
6510898Sroland.mainz@nrubsig.org 		case 0:
6610898Sroland.mainz@nrubsig.org 			break;
6710898Sroland.mainz@nrubsig.org 		case 'm':
6810898Sroland.mainz@nrubsig.org 			mflag = 1;
6910898Sroland.mainz@nrubsig.org 			mode = strperm(arg = opt_info.arg, &opt_info.arg, mode);
7010898Sroland.mainz@nrubsig.org 			if (*opt_info.arg)
7110898Sroland.mainz@nrubsig.org 				error(ERROR_exit(0), "%s: invalid mode", arg);
7210898Sroland.mainz@nrubsig.org 			continue;
7310898Sroland.mainz@nrubsig.org 		case ':':
7410898Sroland.mainz@nrubsig.org 			error(2, "%s", opt_info.arg);
7510898Sroland.mainz@nrubsig.org 			continue;
7610898Sroland.mainz@nrubsig.org 		case '?':
7710898Sroland.mainz@nrubsig.org 			error(ERROR_usage(2), "%s", opt_info.arg);
7810898Sroland.mainz@nrubsig.org 			continue;
7910898Sroland.mainz@nrubsig.org 		}
804887Schin 		break;
814887Schin 	}
824887Schin 	argv += opt_info.index;
8310898Sroland.mainz@nrubsig.org 	if (error_info.errors || !*argv)
8410898Sroland.mainz@nrubsig.org 		error(ERROR_usage(2), "%s", optusage(NiL));
8510898Sroland.mainz@nrubsig.org 	mask = umask(0);
8610898Sroland.mainz@nrubsig.org 	if (!mflag)
874887Schin 	{
8810898Sroland.mainz@nrubsig.org 		mode &= ~mask;
8910898Sroland.mainz@nrubsig.org 		umask(mask);
9010898Sroland.mainz@nrubsig.org 		mask = 0;
914887Schin 	}
9210898Sroland.mainz@nrubsig.org 	while (arg = *argv++)
9310898Sroland.mainz@nrubsig.org 		if (mkfifo(arg, mode) < 0)
9410898Sroland.mainz@nrubsig.org 			error(ERROR_system(0), "%s:", arg);
9510898Sroland.mainz@nrubsig.org 	if (mask)
964887Schin 		umask(mask);
9710898Sroland.mainz@nrubsig.org 	return error_info.errors != 0;
984887Schin }
99