diff options
author | Eric Andersen <andersen@codepoet.org> | 2005-06-09 10:16:02 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 2005-06-09 10:16:02 +0000 |
commit | 3d9256225fe96b06f6e895f693c9b17519758b6c (patch) | |
tree | e2d8a335fd223f33b787679a2cdc608dee2b3cbe /miscutils/makedevs.c | |
parent | 06813d066b379ce74801a6c125213a03104fc4e1 (diff) | |
download | busybox-w32-3d9256225fe96b06f6e895f693c9b17519758b6c.tar.gz busybox-w32-3d9256225fe96b06f6e895f693c9b17519758b6c.tar.bz2 busybox-w32-3d9256225fe96b06f6e895f693c9b17519758b6c.zip |
About time to just apply this and kill off the patches
Diffstat (limited to 'miscutils/makedevs.c')
-rw-r--r-- | miscutils/makedevs.c | 184 |
1 files changed, 158 insertions, 26 deletions
diff --git a/miscutils/makedevs.c b/miscutils/makedevs.c index 54a2e000a..2c9a0a9af 100644 --- a/miscutils/makedevs.c +++ b/miscutils/makedevs.c | |||
@@ -1,21 +1,24 @@ | |||
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 | ||
10 | #include <stdio.h> | 3 | #include <stdio.h> |
11 | #include <stdlib.h> | 4 | #include <stdlib.h> |
12 | #include <string.h> | 5 | #include <string.h> |
13 | #include <fcntl.h> | 6 | #include <fcntl.h> |
7 | #include <getopt.h> | ||
8 | #include <time.h> | ||
14 | #include <unistd.h> | 9 | #include <unistd.h> |
15 | #include <sys/types.h> | 10 | #include <sys/types.h> |
16 | #include <sys/sysmacros.h> /* major() and minor() */ | 11 | #include <sys/sysmacros.h> /* major() and minor() */ |
17 | #include "busybox.h" | 12 | #include "busybox.h" |
18 | 13 | ||
14 | #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF | ||
15 | /* | ||
16 | * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com> | ||
17 | * | ||
18 | * makedevs | ||
19 | * Make ranges of device files quickly. | ||
20 | * known bugs: can't deal with alpha ranges | ||
21 | */ | ||
19 | int makedevs_main(int argc, char **argv) | 22 | int makedevs_main(int argc, char **argv) |
20 | { | 23 | { |
21 | mode_t mode; | 24 | mode_t mode; |
@@ -70,24 +73,153 @@ int makedevs_main(int argc, char **argv) | |||
70 | return 0; | 73 | return 0; |
71 | } | 74 | } |
72 | 75 | ||
76 | #elif defined CONFIG_FEATURE_MAKEDEVS_TABLE | ||
77 | |||
73 | /* | 78 | /* |
74 | And this is what this program replaces. The shell is too slow! | 79 | * This program is free software; you can redistribute it and/or modify |
75 | 80 | * it under the terms of the GNU General Public License version 2 as | |
76 | makedev () { | 81 | * published by the Free Software Foundation. |
77 | local basedev=$1; local S=$2; local E=$3 | 82 | * |
78 | local major=$4; local Sminor=$5; local type=$6 | 83 | * This program is distributed in the hope that it will be useful, |
79 | local sbase=$7 | 84 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
80 | 85 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
81 | if [ ! "$sbase" = "" ]; then | 86 | * GNU Library General Public License for more details. |
82 | mknod "$basedev" $type $major $Sminor | 87 | * |
83 | S=`expr $S + 1` | 88 | * You should have received a copy of the GNU General Public License |
84 | Sminor=`expr $Sminor + 1` | 89 | * along with this program; if not, write to the Free Software |
85 | fi | 90 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
86 | 91 | * | |
87 | while [ $S -le $E ]; do | 92 | */ |
88 | mknod "$basedev$S" $type $major $Sminor | 93 | |
89 | S=`expr $S + 1` | 94 | static const struct option makedevs_long_options[] = { |
90 | Sminor=`expr $Sminor + 1` | 95 | {"root", 1, NULL, 'r'}, |
91 | done | 96 | {0, 0, 0, 0} |
97 | }; | ||
98 | |||
99 | extern int makedevs_main(int argc, char **argv) | ||
100 | { | ||
101 | FILE *table; | ||
102 | int opt; | ||
103 | char *rootdir = "./"; | ||
104 | char *line; | ||
105 | int ret = EXIT_SUCCESS; | ||
106 | |||
107 | bb_opt_complementaly = "d~r"; | ||
108 | bb_applet_long_options = makedevs_long_options; | ||
109 | opt = bb_getopt_ulflags(argc, argv, "d:r:", &rootdir, &rootdir); | ||
110 | |||
111 | if (optind + 1 == argc) { | ||
112 | table = bb_xfopen(argv[optind], "r"); | ||
113 | } else { | ||
114 | table = stdin; | ||
115 | } | ||
116 | |||
117 | if (chdir(rootdir) == -1) { | ||
118 | bb_perror_msg_and_die("Couldnt chdor to %s", rootdir); | ||
119 | } | ||
120 | |||
121 | umask(0); | ||
122 | |||
123 | while ((line = bb_get_chomped_line_from_file(table))) { | ||
124 | char type; | ||
125 | unsigned int mode = 0755; | ||
126 | unsigned int major = 0; | ||
127 | unsigned int minor = 0; | ||
128 | unsigned int count = 0; | ||
129 | unsigned int increment = 0; | ||
130 | unsigned int start = 0; | ||
131 | char name[41]; | ||
132 | char user[41]; | ||
133 | char group[41]; | ||
134 | char *full_name; | ||
135 | uid_t uid; | ||
136 | gid_t gid; | ||
137 | |||
138 | if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name, | ||
139 | &type, &mode, user, group, &major, | ||
140 | &minor, &start, &increment, &count)) || | ||
141 | ((major | minor | start | count | increment) > 255)) { | ||
142 | bb_error_msg("Ignoring invalid line\n%s\n", line); | ||
143 | ret = EXIT_FAILURE; | ||
144 | continue; | ||
145 | } | ||
146 | if (name[0] == '#') { | ||
147 | continue; | ||
148 | } | ||
149 | if (group) { | ||
150 | gid = get_ug_id(group, my_getgrnam); | ||
151 | } else { | ||
152 | gid = getgid(); | ||
153 | } | ||
154 | if (user) { | ||
155 | uid = get_ug_id(user, my_getpwnam); | ||
156 | } else { | ||
157 | uid = getuid(); | ||
158 | } | ||
159 | full_name = concat_path_file(rootdir, name); | ||
160 | |||
161 | if (type == 'd') { | ||
162 | bb_make_directory(full_name, mode | S_IFDIR, 0); | ||
163 | if (chown(full_name, uid, gid) == -1) { | ||
164 | bb_perror_msg("chown failed for %s", full_name); | ||
165 | ret = EXIT_FAILURE; | ||
166 | goto loop; | ||
167 | } | ||
168 | } else { | ||
169 | dev_t rdev; | ||
170 | |||
171 | if (type == 'p') { | ||
172 | mode |= S_IFIFO; | ||
173 | } | ||
174 | else if (type == 'c') { | ||
175 | mode |= S_IFCHR; | ||
176 | } | ||
177 | else if (type == 'b') { | ||
178 | mode |= S_IFBLK; | ||
179 | } else { | ||
180 | bb_error_msg("Unsupported file type %c", type); | ||
181 | ret = EXIT_FAILURE; | ||
182 | goto loop; | ||
183 | } | ||
184 | |||
185 | if (count > 0) { | ||
186 | int i; | ||
187 | char *full_name_inc; | ||
188 | |||
189 | full_name_inc = xmalloc(strlen(full_name) + 4); | ||
190 | for (i = start; i < count; i++) { | ||
191 | sprintf(full_name_inc, "%s%d", full_name, i); | ||
192 | rdev = (major << 8) + minor + (i * increment - start); | ||
193 | if (mknod(full_name_inc, mode, rdev) == -1) { | ||
194 | bb_perror_msg("Couldnt create node %s", full_name_inc); | ||
195 | ret = EXIT_FAILURE; | ||
196 | } | ||
197 | else if (chown(full_name_inc, uid, gid) == -1) { | ||
198 | bb_perror_msg("chown failed for %s", full_name_inc); | ||
199 | ret = EXIT_FAILURE; | ||
200 | } | ||
201 | } | ||
202 | free(full_name_inc); | ||
203 | } else { | ||
204 | rdev = (major << 8) + minor; | ||
205 | if (mknod(full_name, mode, rdev) == -1) { | ||
206 | bb_perror_msg("Couldnt create node %s", full_name); | ||
207 | ret = EXIT_FAILURE; | ||
208 | } | ||
209 | else if (chown(full_name, uid, gid) == -1) { | ||
210 | bb_perror_msg("chown failed for %s", full_name); | ||
211 | ret = EXIT_FAILURE; | ||
212 | } | ||
213 | } | ||
214 | } | ||
215 | loop: | ||
216 | free(line); | ||
217 | free(full_name); | ||
218 | } | ||
219 | fclose(table); | ||
220 | |||
221 | return 0; | ||
92 | } | 222 | } |
93 | */ | 223 | #else |
224 | # error makdedevs configuration error, either leaf or table must be selected | ||
225 | #endif | ||