merge with 3.8.3b
[coreutils.git] / src / mknod.c
blob7024351867c82a38d1b8673dab1d55bfe2dd055e
1 /* mknod -- make special files
2 Copyright (C) 1990, 1991 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 2, or (at your option)
7 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, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
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)
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.
28 David MacKenzie <djm@ai.mit.edu> */
30 #ifdef HAVE_CONFIG_H
31 #if defined (CONFIG_BROKETS)
32 /* We use <config.h> instead of "config.h" so that a compilation
33 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
34 (which it would do because it found this file in $srcdir). */
35 #include <config.h>
36 #else
37 #include "config.h"
38 #endif
39 #endif
41 #include <stdio.h>
42 #include <getopt.h>
43 #include <sys/types.h>
44 #include "system.h"
45 #include "modechange.h"
46 #include "version.h"
48 void error ();
50 static void usage ();
52 /* The name this program was run with. */
53 char *program_name;
55 /* If non-zero, display usage information and exit. */
56 static int show_help;
58 /* If non-zero, print the version on standard output and exit. */
59 static int show_version;
61 static struct option const longopts[] =
63 {"mode", required_argument, NULL, 'm'},
64 {"help", no_argument, &show_help, 1},
65 {"version", no_argument, &show_version, 1},
66 {NULL, 0, NULL, 0}
69 void
70 main (argc, argv)
71 int argc;
72 char **argv;
74 unsigned short newmode;
75 struct mode_change *change;
76 char *symbolic_mode;
77 int optc;
79 program_name = argv[0];
80 symbolic_mode = NULL;
82 while ((optc = getopt_long (argc, argv, "m:", longopts, (int *) 0)) != EOF)
84 switch (optc)
86 case 0:
87 break;
88 case 'm':
89 symbolic_mode = optarg;
90 break;
91 default:
92 usage ();
96 if (show_version)
98 printf ("%s\n", version_string);
99 exit (0);
102 if (show_help)
103 usage ();
105 newmode = 0666 & ~umask (0);
106 if (symbolic_mode)
108 change = mode_compile (symbolic_mode, 0);
109 if (change == MODE_INVALID)
110 error (1, 0, "invalid mode");
111 else if (change == MODE_MEMORY_EXHAUSTED)
112 error (1, 0, "virtual memory exhausted");
113 newmode = mode_adjust (newmode, change);
116 if (argc - optind != 2 && argc - optind != 4)
117 usage ();
119 /* Only check the first character, to allow mnemonic usage like
120 `mknod /dev/rst0 character 18 0'. */
122 switch (argv[optind + 1][0])
124 case 'b': /* `block' or `buffered' */
125 #ifndef S_IFBLK
126 error (4, 0, "block special files not supported");
127 #else
128 if (argc - optind != 4)
129 usage ();
130 if (mknod (argv[optind], newmode | S_IFBLK,
131 makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
132 error (1, errno, "%s", argv[optind]);
133 #endif
134 break;
136 case 'c': /* `character' */
137 case 'u': /* `unbuffered' */
138 #ifndef S_IFCHR
139 error (4, 0, "character special files not supported");
140 #else
141 if (argc - optind != 4)
142 usage ();
143 if (mknod (argv[optind], newmode | S_IFCHR,
144 makedev (atoi (argv[optind + 2]), atoi (argv[optind + 3]))))
145 error (1, errno, "%s", argv[optind]);
146 #endif
147 break;
149 case 'p': /* `pipe' */
150 #ifndef S_ISFIFO
151 error (4, 0, "fifo files not supported");
152 #else
153 if (argc - optind != 2)
154 usage ();
155 if (mkfifo (argv[optind], newmode))
156 error (1, errno, "%s", argv[optind]);
157 #endif
158 break;
160 default:
161 usage ();
164 exit (0);
167 static void
168 usage ()
170 fprintf (stderr, "\
171 Usage: %s [options] path {bcu} major minor\n\
172 %s [options] path p\n\
173 Options:\n\
174 [-m mode] [--mode=mode] [--help] [--version]\n",
175 program_name, program_name);
176 exit (1);