xref: /illumos-gate/usr/src/cmd/mkfifo/mkfifo.c (revision 2a8bcb4e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T		*/
28 /*	  All Rights Reserved					*/
29 /*								*/
30 
31 #include	<stdlib.h>
32 #include	<sys/types.h>
33 #include	<sys/stat.h>
34 #include	<stdio.h>
35 #include	<errno.h>
36 #include	<string.h>
37 #include	<errno.h>
38 #include	<locale.h>
39 #include	<stdarg.h>
40 
41 #define	ALLRW	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
42 
43 static void
44 usage();
45 
46 void
47 errmsg(int severity, int code, char *format, ...);
48 
49 /* from chmod:common.c: */
50 
51 extern mode_t
52 newmode(char *modestr, mode_t basemode, mode_t umask, char *file, char *path);
53 
54 int
main(int argc,char * argv[])55 main(int argc, char *argv[])
56 {
57 	char *path;
58 	int exitval = 0;
59 	int c, i;
60 	int mflag = 0;
61 	int errflg = 0;
62 	mode_t umsk = umask(0);
63 	mode_t mode = ALLRW & (~umsk);
64 
65 	(void) setlocale(LC_ALL, "");
66 #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
67 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
68 #endif
69 	(void) textdomain(TEXT_DOMAIN);
70 
71 	if (argc < 2) {
72 		usage();
73 		exit(1);
74 	}
75 
76 	while ((c = getopt(argc, argv, "m:")) != EOF) {
77 		switch (c) {
78 		case 'm':
79 			mflag++;
80 			mode = newmode(optarg, ALLRW, umsk, "", "");
81 			break;
82 
83 		default:
84 			errflg++;
85 			break;
86 		}
87 	}
88 
89 	if (argc < optind || errflg) {
90 		usage();
91 		exit(1);
92 	}
93 
94 	for (i = optind; i < argc; i++) {
95 		path = argv[i];
96 		if (mkfifo(path, mode) < 0) {
97 			perror("mkfifo");
98 			exitval = 1;
99 		}
100 	}
101 
102 	return (exitval);
103 
104 }
105 
106 static void
usage()107 usage()
108 {
109 	(void) fprintf(stderr,
110 	    gettext("usage: mkfifo [-m mode] file ...\n"));
111 }
112 
113 /*
114  *  errmsg - This is an interface required by the code common to mkfifo and
115  *	     chmod.  The severity parameter is ignored here, but is meaningful
116  *	     to chmod.
117  */
118 
119 /* ARGSUSED */
120 /* PRINTFLIKE3 */
121 void
errmsg(int severity,int code,char * format,...)122 errmsg(int severity, int code, char *format, ...)
123 {
124 	va_list ap;
125 
126 	va_start(ap, format);
127 
128 	(void) fprintf(stderr, "mkfifo: ");
129 	(void) vfprintf(stderr, format, ap);
130 
131 	va_end(ap);
132 
133 	if (code > 0) {
134 		exit(code);
135 	}
136 }
137