xref: /titanic_52/usr/src/cmd/mkfifo/mkfifo.c (revision ba4e3c84e6b9390bbf7df80b5f1d11dec34cc525)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #include	<stdlib.h>
34 #include	<sys/types.h>
35 #include	<sys/stat.h>
36 #include	<stdio.h>
37 #include	<errno.h>
38 #include	<string.h>
39 #include	<errno.h>
40 #include	<locale.h>
41 #include	<stdarg.h>
42 
43 #define	ALLRW	(S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)
44 
45 static void
46 usage();
47 
48 void
49 errmsg(int severity, int code, char *format, ...);
50 
51 /* from chmod:common.c: */
52 
53 extern mode_t
54 newmode(char *modestr, mode_t basemode, mode_t umask, char *file, char *path);
55 
56 int
57 main(int argc, char *argv[])
58 {
59 	char *path;
60 	int exitval = 0;
61 	int c, i;
62 	int mflag = 0;
63 	int errflg = 0;
64 	mode_t umsk = umask(0);
65 	mode_t mode = ALLRW & (~umsk);
66 
67 	(void) setlocale(LC_ALL, "");
68 #if	!defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
69 #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
70 #endif
71 	(void) textdomain(TEXT_DOMAIN);
72 
73 	if (argc < 2) {
74 		usage();
75 		exit(1);
76 	}
77 
78 	while ((c = getopt(argc, argv, "m:")) != EOF) {
79 		switch (c) {
80 		case 'm':
81 			mflag++;
82 			mode = newmode(optarg, ALLRW, umsk, "", "");
83 			break;
84 
85 		default:
86 			errflg++;
87 			break;
88 		}
89 	}
90 
91 	if (argc < optind || errflg) {
92 		usage();
93 		exit(1);
94 	}
95 
96 	for (i = optind; i < argc; i++) {
97 		path = argv[i];
98 		if (mkfifo(path, mode) < 0) {
99 			perror("mkfifo");
100 			exitval = 1;
101 		}
102 	}
103 
104 	return (exitval);
105 
106 }
107 
108 static void
109 usage()
110 {
111 	(void) fprintf(stderr,
112 	    gettext("usage: mkfifo [-m mode] file ...\n"));
113 }
114 
115 /*
116  *  errmsg - This is an interface required by the code common to mkfifo and
117  *	     chmod.  The severity parameter is ignored here, but is meaningful
118  *	     to chmod.
119  */
120 
121 /* ARGSUSED */
122 /* PRINTFLIKE3 */
123 void
124 errmsg(int severity, int code, char *format, ...)
125 {
126 	va_list ap;
127 
128 	va_start(ap, format);
129 
130 	(void) fprintf(stderr, "mkfifo: ");
131 	(void) vfprintf(stderr, format, ap);
132 
133 	va_end(ap);
134 
135 	if (code > 0) {
136 		exit(code);
137 	}
138 }
139