shred: increase I/O block size for periodic pattern case
[coreutils.git] / src / mkfifo.c
blob4c6dac468772b3589b959bfefa95816bff233947
1 /* mkfifo -- make fifo's (named pipes)
2 Copyright (C) 1990-2013 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* David MacKenzie <djm@ai.mit.edu> */
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <sys/types.h>
23 #include <selinux/selinux.h>
25 #include "system.h"
26 #include "error.h"
27 #include "modechange.h"
28 #include "quote.h"
29 #include "smack.h"
31 /* The official name of this program (e.g., no 'g' prefix). */
32 #define PROGRAM_NAME "mkfifo"
34 #define AUTHORS proper_name ("David MacKenzie")
36 static struct option const longopts[] =
38 {GETOPT_SELINUX_CONTEXT_OPTION_DECL},
39 {"mode", required_argument, NULL, 'm'},
40 {GETOPT_HELP_OPTION_DECL},
41 {GETOPT_VERSION_OPTION_DECL},
42 {NULL, 0, NULL, 0}
45 void
46 usage (int status)
48 if (status != EXIT_SUCCESS)
49 emit_try_help ();
50 else
52 printf (_("Usage: %s [OPTION]... NAME...\n"), program_name);
53 fputs (_("\
54 Create named pipes (FIFOs) with the given NAMEs.\n\
55 "), stdout);
57 emit_mandatory_arg_note ();
59 fputs (_("\
60 -m, --mode=MODE set file permission bits to MODE, not a=rw - umask\n\
61 "), stdout);
62 fputs (_("\
63 -Z, --context=CTX set the SELinux security context of each NAME to CTX\n\
64 "), stdout);
65 fputs (HELP_OPTION_DESCRIPTION, stdout);
66 fputs (VERSION_OPTION_DESCRIPTION, stdout);
67 emit_ancillary_info ();
69 exit (status);
72 int
73 main (int argc, char **argv)
75 mode_t newmode;
76 char const *specified_mode = NULL;
77 int exit_status = EXIT_SUCCESS;
78 int optc;
79 security_context_t scontext = NULL;
81 initialize_main (&argc, &argv);
82 set_program_name (argv[0]);
83 setlocale (LC_ALL, "");
84 bindtextdomain (PACKAGE, LOCALEDIR);
85 textdomain (PACKAGE);
87 atexit (close_stdout);
89 while ((optc = getopt_long (argc, argv, "m:Z:", longopts, NULL)) != -1)
91 switch (optc)
93 case 'm':
94 specified_mode = optarg;
95 break;
96 case 'Z':
97 scontext = optarg;
98 break;
99 case_GETOPT_HELP_CHAR;
100 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
101 default:
102 usage (EXIT_FAILURE);
106 if (optind == argc)
108 error (0, 0, _("missing operand"));
109 usage (EXIT_FAILURE);
112 if (scontext)
114 int ret = 0;
115 if (is_smack_enabled ())
116 ret = smack_set_label_for_self (scontext);
117 else
118 ret = setfscreatecon (scontext);
120 if (ret < 0)
121 error (EXIT_FAILURE, errno,
122 _("failed to set default file creation context to %s"),
123 quote (scontext));
126 newmode = MODE_RW_UGO;
127 if (specified_mode)
129 mode_t umask_value;
130 struct mode_change *change = mode_compile (specified_mode);
131 if (!change)
132 error (EXIT_FAILURE, 0, _("invalid mode"));
133 umask_value = umask (0);
134 umask (umask_value);
135 newmode = mode_adjust (newmode, false, umask_value, change, NULL);
136 free (change);
137 if (newmode & ~S_IRWXUGO)
138 error (EXIT_FAILURE, 0,
139 _("mode must specify only file permission bits"));
142 for (; optind < argc; ++optind)
143 if (mkfifo (argv[optind], newmode) != 0)
145 error (0, errno, _("cannot create fifo %s"), quote (argv[optind]));
146 exit_status = EXIT_FAILURE;
148 else if (specified_mode && lchmod (argv[optind], newmode) != 0)
150 error (0, errno, _("cannot set permissions of `%s'"),
151 quote (argv[optind]));
152 exit_status = EXIT_FAILURE;
155 exit (exit_status);