dd: clarify meaning of multiplication factors; put xM in order
[platform/upstream/coreutils.git] / src / mkfifo.c
1 /* mkfifo -- make fifo's (named pipes)
2    Copyright (C) 90, 91, 1995-2008 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* David MacKenzie <djm@ai.mit.edu>  */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
24
25 #include "system.h"
26 #include "error.h"
27 #include "modechange.h"
28 #include "quote.h"
29
30 /* The official name of this program (e.g., no `g' prefix).  */
31 #define PROGRAM_NAME "mkfifo"
32
33 #define AUTHORS proper_name ("David MacKenzie")
34
35 static struct option const longopts[] =
36 {
37   {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
38   {"mode", required_argument, NULL, 'm'},
39   {GETOPT_HELP_OPTION_DECL},
40   {GETOPT_VERSION_OPTION_DECL},
41   {NULL, 0, NULL, 0}
42 };
43
44 void
45 usage (int status)
46 {
47   if (status != EXIT_SUCCESS)
48     fprintf (stderr, _("Try `%s --help' for more information.\n"),
49              program_name);
50   else
51     {
52       printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
53       fputs (_("\
54 Create named pipes (FIFOs) with the given NAMEs.\n\
55 \n\
56 "), stdout);
57       fputs (_("\
58 Mandatory arguments to long options are mandatory for short options too.\n\
59 "), stdout);
60       fputs (_("\
61   -Z, --context=CTX  set the SELinux security context of each NAME to CTX\n\
62 "), stdout);
63       fputs (_("\
64   -m, --mode=MODE   set file permission bits to MODE, not a=rw - umask\n\
65 "), stdout);
66       fputs (HELP_OPTION_DESCRIPTION, stdout);
67       fputs (VERSION_OPTION_DESCRIPTION, stdout);
68       emit_bug_reporting_address ();
69     }
70   exit (status);
71 }
72
73 int
74 main (int argc, char **argv)
75 {
76   mode_t newmode;
77   char const *specified_mode = NULL;
78   int exit_status = EXIT_SUCCESS;
79   int optc;
80   security_context_t scontext = NULL;
81
82   initialize_main (&argc, &argv);
83   set_program_name (argv[0]);
84   setlocale (LC_ALL, "");
85   bindtextdomain (PACKAGE, LOCALEDIR);
86   textdomain (PACKAGE);
87
88   atexit (close_stdout);
89
90   while ((optc = getopt_long (argc, argv, "m:Z:", longopts, NULL)) != -1)
91     {
92       switch (optc)
93         {
94         case 'm':
95           specified_mode = optarg;
96           break;
97         case 'Z':
98           scontext = optarg;
99           break;
100         case_GETOPT_HELP_CHAR;
101         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
102         default:
103           usage (EXIT_FAILURE);
104         }
105     }
106
107   if (optind == argc)
108     {
109       error (0, 0, _("missing operand"));
110       usage (EXIT_FAILURE);
111     }
112
113   if (scontext && setfscreatecon (scontext) < 0)
114     error (EXIT_FAILURE, errno,
115            _("failed to set default file creation context to %s"),
116            quote (scontext));
117
118   newmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
119   if (specified_mode)
120     {
121       struct mode_change *change = mode_compile (specified_mode);
122       if (!change)
123         error (EXIT_FAILURE, 0, _("invalid mode"));
124       newmode = mode_adjust (newmode, false, umask (0), change, NULL);
125       free (change);
126       if (newmode & ~S_IRWXUGO)
127         error (EXIT_FAILURE, 0,
128                _("mode must specify only file permission bits"));
129     }
130
131   for (; optind < argc; ++optind)
132     if (mkfifo (argv[optind], newmode) != 0)
133       {
134         error (0, errno, _("cannot create fifo %s"), quote (argv[optind]));
135         exit_status = EXIT_FAILURE;
136       }
137
138   exit (exit_status);
139 }