Include long-options.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 "closeout.h"
37 #include "error.h"
38 #include "long-options.h"
39 #include "modechange.h"
40 #include "xstrtol.h"
41
42 /* The name this program was run with. */
43 char *program_name;
44
45 static struct option const longopts[] =
46 {
47   {"mode", required_argument, NULL, 'm'},
48   {NULL, 0, NULL, 0}
49 };
50
51 void
52 usage (int status)
53 {
54   if (status != 0)
55     fprintf (stderr, _("Try `%s --help' for more information.\n"),
56              program_name);
57   else
58     {
59       printf (_("Usage: %s [OPTION]... NAME TYPE [MAJOR MINOR]\n"), program_name);
60       printf (_("\
61 Create the special file NAME of the given TYPE.\n\
62 \n\
63   -m, --mode=MODE   set permission mode (as in chmod), not 0666 - umask\n\
64       --help        display this help and exit\n\
65       --version     output version information and exit\n\
66 \n\
67 MAJOR MINOR are forbidden for TYPE p, mandatory otherwise.  TYPE may be:\n\
68 \n\
69   b      create a block (buffered) special file\n\
70   c, u   create a character (unbuffered) special file\n\
71   p      create a FIFO\n\
72 "));
73       puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
74       close_stdout ();
75     }
76   exit (status);
77 }
78
79 int
80 main (int argc, char **argv)
81 {
82   unsigned short newmode;
83   struct mode_change *change;
84   char *symbolic_mode;
85   int optc;
86   int i_major, i_minor;
87   long int tmp_major, tmp_minor;
88   char *s;
89
90   program_name = argv[0];
91   setlocale (LC_ALL, "");
92   bindtextdomain (PACKAGE, LOCALEDIR);
93   textdomain (PACKAGE);
94
95   parse_long_options (argc, argv, "mknod", GNU_PACKAGE, VERSION,
96                       "David MacKenzie", usage);
97
98   symbolic_mode = NULL;
99
100   while ((optc = getopt_long (argc, argv, "m:", longopts, NULL)) != -1)
101     {
102       switch (optc)
103         {
104         case 0:
105           break;
106         case 'm':
107           symbolic_mode = optarg;
108           break;
109         default:
110           usage (1);
111         }
112     }
113
114   newmode = 0666 & ~umask (0);
115   if (symbolic_mode)
116     {
117       change = mode_compile (symbolic_mode, 0);
118       if (change == MODE_INVALID)
119         error (1, 0, _("invalid mode"));
120       else if (change == MODE_MEMORY_EXHAUSTED)
121         error (1, 0, _("virtual memory exhausted"));
122       newmode = mode_adjust (newmode, change);
123     }
124
125   if (argc - optind != 2 && argc - optind != 4)
126     {
127       const char *msg;
128       if (argc - optind < 2)
129         msg = _("too few arguments");
130       else if (argc - optind > 4)
131         msg = _("too many arguments");
132       else
133         msg = _("wrong number of arguments");
134       error (0, 0, msg);
135       usage (1);
136     }
137
138   /* Only check the first character, to allow mnemonic usage like
139      `mknod /dev/rst0 character 18 0'. */
140
141   switch (argv[optind + 1][0])
142     {
143     case 'b':                   /* `block' or `buffered' */
144 #ifndef S_IFBLK
145       error (4, 0, _("block special files not supported"));
146 #else
147       if (argc - optind != 4)
148         {
149           error (0, 0, _("\
150 when creating block special files, major and minor device\n\
151 numbers must be specified"));
152           usage (1);
153         }
154
155       s = argv[optind + 2];
156       if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
157         error (1, 0, _("invalid major device number `%s'"), s);
158
159       s = argv[optind + 3];
160       if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
161         error (1, 0, _("invalid minor device number `%s'"), s);
162
163       i_major = (int) tmp_major;
164       i_minor = (int) tmp_minor;
165
166       if (mknod (argv[optind], newmode | S_IFBLK, makedev (i_major, i_minor)))
167         error (1, errno, "%s", argv[optind]);
168 #endif
169       break;
170
171     case 'c':                   /* `character' */
172     case 'u':                   /* `unbuffered' */
173 #ifndef S_IFCHR
174       error (4, 0, _("character special files not supported"));
175 #else
176       if (argc - optind != 4)
177         {
178           error (0, 0, _("\
179 when creating character special files, major and minor device\n\
180 numbers must be specified"));
181           usage (1);
182         }
183
184       s = argv[optind + 2];
185       if (xstrtol (s, NULL, 0, &tmp_major, NULL) != LONGINT_OK)
186         error (1, 0, _("invalid major device number `%s'"), s);
187
188       s = argv[optind + 3];
189       if (xstrtol (s, NULL, 0, &tmp_minor, NULL) != LONGINT_OK)
190         error (1, 0, _("invalid minor device number `%s'"), s);
191
192       i_major = (int) tmp_major;
193       i_minor = (int) tmp_minor;
194
195       if (mknod (argv[optind], newmode | S_IFCHR, makedev (i_major, i_minor)))
196         error (1, errno, "%s", argv[optind]);
197 #endif
198       break;
199
200     case 'p':                   /* `pipe' */
201 #ifndef S_ISFIFO
202       error (4, 0, _("fifo files not supported"));
203 #else
204       if (argc - optind != 2)
205         {
206           error (0, 0, _("\
207 major and minor device numbers may not be specified for fifo files"));
208           usage (1);
209         }
210       if (mkfifo (argv[optind], newmode))
211         error (1, errno, "%s", argv[optind]);
212 #endif
213       break;
214
215     default:
216       usage (1);
217     }
218
219   exit (0);
220 }