aboutsummaryrefslogtreecommitdiff
path: root/miscutils
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2004-06-05 07:54:52 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2004-06-05 07:54:52 +0000
commit84b6264670619c8051f0c81a1ed7bb58e161df21 (patch)
tree80a5447e076ed386b5d7ae7c92540ee87b39f008 /miscutils
parent58b118ae33e70bdd37872a2e68f09234c890eeac (diff)
downloadbusybox-w32-84b6264670619c8051f0c81a1ed7bb58e161df21.tar.gz
busybox-w32-84b6264670619c8051f0c81a1ed7bb58e161df21.tar.bz2
busybox-w32-84b6264670619c8051f0c81a1ed7bb58e161df21.zip
Device table support for makedevs, the previous behaviour can been
selected at configure time.
Diffstat (limited to 'miscutils')
-rw-r--r--miscutils/Config.in30
-rw-r--r--miscutils/makedevs.c192
2 files changed, 190 insertions, 32 deletions
diff --git a/miscutils/Config.in b/miscutils/Config.in
index 7e18c16a0..320f4c2b4 100644
--- a/miscutils/Config.in
+++ b/miscutils/Config.in
@@ -143,10 +143,32 @@ config CONFIG_MAKEDEVS
143 bool "makedevs" 143 bool "makedevs"
144 default n 144 default n
145 help 145 help
146 'makedevs' is a utility used and created by the Linux Router Project. 146 'makedevs' is a utility used to create a batch of devices with
147 It creates a large number of device special files (/dev devices) 147 one command.
148 rather quickly, and can be considerably faster then running mknod a 148 .
149 zillion times. 149 There are two choices for command line behaviour, the interface
150 as used by LEAF/Linux Router Project, or a device table file.
151 .
152 'leaf' is traditionally what busybox follows, it allows multiple
153 devices of a particluar type to be created per command.
154 e.g. /dev/hda[0-9]
155 Device properties are passed as command line arguments.
156 .
157 'table' reads device properties from a file or stdin, allowing
158 a batch of unrelated devices to be makde with one command.
159 User/group names are allowed as an alternative to uid/gid.
160
161choice
162 prompt "Choose makedevs behaviour"
163 default CONFIG_FEATURE_MAKDEVS_TABLE
164
165config CONFIG_FEATURE_MAKEDEVS_LEAF
166 bool "leaf"
167
168config CONFIG_FEATURE_MAKEDEVS_TABLE
169 bool "table"
170
171endchoice
150 172
151config CONFIG_MT 173config CONFIG_MT
152 bool "mt" 174 bool "mt"
diff --git a/miscutils/makedevs.c b/miscutils/makedevs.c
index 45498bb1d..57b2d6c70 100644
--- a/miscutils/makedevs.c
+++ b/miscutils/makedevs.c
@@ -1,20 +1,27 @@
1/* vi: set sw=4 ts=4: */ 1/* vi: set sw=4 ts=4: */
2/*
3 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
4 *
5 * makedevs
6 * Make ranges of device files quickly.
7 * known bugs: can't deal with alpha ranges
8 */
9 2
3#include <sys/types.h>
4
5#include <fcntl.h>
6#include <getopt.h>
10#include <stdio.h> 7#include <stdio.h>
11#include <stdlib.h> 8#include <stdlib.h>
12#include <string.h> 9#include <string.h>
13#include <fcntl.h> 10#include <time.h>
14#include <unistd.h> 11#include <unistd.h>
15#include <sys/types.h> 12
16#include "busybox.h" 13#include "busybox.h"
17 14
15#ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
16
17/*
18 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
19 *
20 * makedevs
21 * Make ranges of device files quickly.
22 * known bugs: can't deal with alpha ranges
23 */
24
18int makedevs_main(int argc, char **argv) 25int makedevs_main(int argc, char **argv)
19{ 26{
20 mode_t mode; 27 mode_t mode;
@@ -69,24 +76,153 @@ int makedevs_main(int argc, char **argv)
69 return 0; 76 return 0;
70} 77}
71 78
79#elif defined CONFIG_FEATURE_MAKEDEVS_TABLE
80
72/* 81/*
73And this is what this program replaces. The shell is too slow! 82 * This program is free software; you can redistribute it and/or modify
74 83 * it under the terms of the GNU General Public License version 2 as
75makedev () { 84 * published by the Free Software Foundation.
76local basedev=$1; local S=$2; local E=$3 85 *
77local major=$4; local Sminor=$5; local type=$6 86 * This program is distributed in the hope that it will be useful,
78local sbase=$7 87 * but WITHOUT ANY WARRANTY; without even the implied warranty of
79 88 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 if [ ! "$sbase" = "" ]; then 89 * GNU Library General Public License for more details.
81 mknod "$basedev" $type $major $Sminor 90 *
82 S=`expr $S + 1` 91 * You should have received a copy of the GNU General Public License
83 Sminor=`expr $Sminor + 1` 92 * along with this program; if not, write to the Free Software
84 fi 93 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
85 94 *
86 while [ $S -le $E ]; do 95 */
87 mknod "$basedev$S" $type $major $Sminor 96
88 S=`expr $S + 1` 97static const struct option makedevs_long_options[] = {
89 Sminor=`expr $Sminor + 1` 98 {"root", 1, NULL, 'r'},
90 done 99 {0, 0, 0, 0}
100};
101
102extern int makedevs_main(int argc, char **argv)
103{
104 FILE *table;
105 int opt;
106 char *rootdir = "./";
107 char *line;
108 int ret = EXIT_SUCCESS;
109
110 bb_opt_complementaly = "d~r";
111 bb_applet_long_options = makedevs_long_options;
112 opt = bb_getopt_ulflags(argc, argv, "d:r:", &rootdir, &rootdir);
113
114 if (optind + 1 == argc) {
115 table = bb_xfopen(argv[optind], "r");
116 } else {
117 table = stdin;
118 }
119
120 if (chdir(rootdir) == -1) {
121 bb_perror_msg_and_die("Couldnt chdor to %s", rootdir);
122 }
123
124 umask(0);
125
126 while ((line = bb_get_chomped_line_from_file(table))) {
127 char type;
128 unsigned int mode = 0755;
129 unsigned int major = 0;
130 unsigned int minor = 0;
131 unsigned int count = 0;
132 unsigned int increment = 0;
133 unsigned int start = 0;
134 char name[41];
135 char user[41];
136 char group[41];
137 char *full_name;
138 uid_t uid;
139 gid_t gid;
140
141 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
142 &type, &mode, user, group, &major,
143 &minor, &start, &increment, &count)) ||
144 ((major | minor | start | count | increment) > 255)) {
145 bb_error_msg("Ignoring invalid line\n%s\n", line);
146 ret = EXIT_FAILURE;
147 continue;
148 }
149 if (name[0] == '#') {
150 continue;
151 }
152 if (group) {
153 gid = get_ug_id(group, my_getgrnam);
154 } else {
155 gid = getgid();
156 }
157 if (user) {
158 uid = get_ug_id(user, my_getpwnam);
159 } else {
160 uid = getuid();
161 }
162 full_name = concat_path_file(rootdir, name);
163
164 if (type == 'd') {
165 bb_make_directory(full_name, mode | S_IFDIR, 0);
166 if (chown(full_name, uid, gid) == -1) {
167 bb_perror_msg("chown failed for %s", full_name);
168 ret = EXIT_FAILURE;
169 goto loop;
170 }
171 } else {
172 dev_t rdev;
173
174 if (type == 'p') {
175 mode |= S_IFIFO;
176 }
177 else if (type == 'c') {
178 mode |= S_IFCHR;
179 }
180 else if (type == 'b') {
181 mode |= S_IFBLK;
182 } else {
183 bb_error_msg("Unsupported file type %c", type);
184 ret = EXIT_FAILURE;
185 goto loop;
186 }
187
188 if (count > 0) {
189 int i;
190 char *full_name_inc;
191
192 full_name_inc = xmalloc(strlen(full_name) + 4);
193 for (i = start; i < count; i++) {
194 sprintf(full_name_inc, "%s%d", full_name, i);
195 rdev = (major << 8) + minor + (i * increment - start);
196 if (mknod(full_name_inc, mode, rdev) == -1) {
197 bb_perror_msg("Couldnt create node %s", full_name_inc);
198 ret = EXIT_FAILURE;
199 }
200 else if (chown(full_name_inc, uid, gid) == -1) {
201 bb_perror_msg("chown failed for %s", full_name_inc);
202 ret = EXIT_FAILURE;
203 }
204 }
205 free(full_name_inc);
206 } else {
207 rdev = (major << 8) + minor;
208 if (mknod(full_name, mode, rdev) == -1) {
209 bb_perror_msg("Couldnt create node %s", full_name);
210 ret = EXIT_FAILURE;
211 }
212 else if (chown(full_name, uid, gid) == -1) {
213 bb_perror_msg("chown failed for %s", full_name);
214 ret = EXIT_FAILURE;
215 }
216 }
217 }
218loop:
219 free(line);
220 free(full_name);
221 }
222 fclose(table);
223
224 return 0;
91} 225}
92*/ 226#else
227# error makdedevs configuration error, either leaf or table must be selected
228#endif