Don't include closeout.h or version-etc.h explicitly. Now, they're included via sys2.h.
[platform/upstream/coreutils.git] / src / mknod.c
1 /* mknod -- make special files
2    Copyright (C) 90, 91, 1995-1999 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Usage: mknod [-m mode] [--mode=mode] path {bcu} major minor
19                 make a block or character device node
20           mknod [-m mode] [--mode=mode] path p
21                 make a FIFO (named pipe)
22
23    Options:
24    -m, --mode=mode      Set the mode of created nodes to MODE, which is
25                         symbolic as in chmod and uses the umask as a point of
26                         departure.
27
28    David MacKenzie <djm@ai.mit.edu>  */
29
30 #include <config.h>
31 #include <stdio.h>
32 #include <getopt.h>
33 #include <sys/types.h>
34
35 #include "system.h"
36 #include "error.h"
37 #include "modechange.h"
38 #include "xstrtol.h"
39
40 /* The official name of this program (e.g., no `g' prefix).  */
41 #define PROGRAM_NAME "mknod"
42
43 #define AUTHORS "David MacKenzie"
44
45 /* The name this program was run with. */
46 char *program_name;
47
48 static struct option const longopts[] =
49 {
50   {"mode", required_argument, NULL, 'm'},
51   {GETOPT_HELP_OPTION_DECL},
52   {GETOPT_VERSION_OPTION_DECL},
53   {NULL, 0, NULL, 0}
54 };
55
56 void
57 usage (int status)
58 {
59   if (status != 0)
60     fprintf (stderr, _("Try `%s --help' for more information.\n"),
61              program_name);
62   else
63     {
64       printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
65       printf (_("\
66 Create the special file NAME of the given TYPE.\n\
67 \n\
68   -m, --mode=MODE   set permission mode (as in chmod), not 0666 - umask\n\
69       --help        display this help and exit\n\
70       --version     output version information and exit\n\
71 \n\
72 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise.  TYPE may be:\n\
73 \n\
74   b      create a block (buffered) special file\n\
75   c, u   create a character (unbuffered) special file\n\
76   p      create a FIFO\n\
77 "));
78       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
79       close_stdout ();
80     }
81   exit (status);
82 }
83
84 int
85 main (int argc, char **argv)
86 {
87   unsigned short newmode;
88   struct mode_change *change;
89   char *symbolic_mode;
90   int optc;
91   int i_major, i_minor;
92   long int tmp_major, tmp_minor;
93   char *s;
94
95   program_name = argv[0];
96   setlocale (LC_ALL, "");
97   bindtextdomain (PACKAGE, LOCALEDIR);
98   textdomain (PACKAGE);
99
100   symbolic_mode = NULL;
101
102   while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
103     {
104       switch (optc)
105         {
106         case 0:
107           break;
108         case 'm':
109           symbolic_mode = optarg;
110           break;
111         case_GETOPT_HELP_CHAR;
112         case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
113         default:
114           usage (1);
115         }
116     }
117
118   newmode = 0666 & ~umask (0);
119   if (symbolic_mode)
120     {
121       change = mode_compile (symbolic_mode, 0);
122       if (change == MODE_INVALID)
123         error (1, 0, _("invalid mode"));
124       else if (change == MODE_MEMORY_EXHAUSTED)
125         error (1, 0, _("virtual memory exhausted"));
126       newmode = mode_adjust (newmode, change);
127     }
128
129   if (argc - optind != 2 && argc - optind != 4)
130     {
131       const char *msg;
132       if (argc - optind < 2)
133         msg = _("too few arguments");
134       else if (argc - optind > 4)
135         msg = _("too many arguments");
136       else
137         msg = _("wrong number of arguments");
138       error (0, 0, msg);
139       usage (1);
140     }
141
142   /* Only check the first character, to allow mnemonic usage like
143      `mknod /dev/rst0 character 18 0'. */
144
145   switch (argv[optind + 1][0])
146     {
147     case 'b':                   /* `block' or `buffered' */
148 #ifndef S_IFBLK
149       error (4, 0, _("block special files not supported"));
150 #else
151       if (argc - optind != 4)
152         {
153           error (0, 0, _("\
154 when creating block special files, major and minor device\n\
155 numbers must be specified"));
156           usage (1);
157         }
158
159       s = argv[optind + 2];
160       if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
161         error (1, 0, _("invalid major device number `%s'"), s);
162
163       s = argv[optind + 3];
164       if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
165         error (1, 0, _("invalid minor device number `%s'"), s);
166
167       i_major = (int) tmp_major;
168       i_minor = (int) tmp_minor;
169
170       if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
171         error (1, errno, "%s", argv[optind]);
172 #endif
173       break;
174
175     case 'c':                   /* `character' */
176     case 'u':                   /* `unbuffered' */
177 #ifndef S_IFCHR
178       error (4, 0, _("character special files not supported"));
179 #else
180       if (argc - optind != 4)
181         {
182           error (0, 0, _("\
183 when creating character special files, major and minor device\n\
184 numbers must be specified"));
185           usage (1);
186         }
187
188       s = argv[optind + 2];
189       if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
190         error (1, 0, _("invalid major device number `%s'"), s);
191
192       s = argv[optind + 3];
193       if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
194         error (1, 0, _("invalid minor device number `%s'"), s);
195
196       i_major = (int) tmp_major;
197       i_minor = (int) tmp_minor;
198
199       if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
200         error (1, errno, "%s", argv[optind]);
201 #endif
202       break;
203
204     case 'p':                   /* `pipe' */
205 #ifndef S_ISFIFO
206       error (4, 0, _("fifo files not supported"));
207 #else
208       if (argc - optind != 2)
209         {
210           error (0, 0, _("\
211 major and minor device numbers may not be specified for fifo files"));
212           usage (1);
213         }
214       if (mkfifo (argv[optind], newmode))
215         error (1, errno, "%s", argv[optind]);
216 #endif
217       break;
218
219     default:
220       usage (1);
221     }
222
223   exit (0);
224 }