1 /* vi: set sw=4 ts=4: */
10 #include <sys/types.h>
11 #include <sys/sysmacros.h> /* major() and minor() */
14 #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
16 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
19 * Make ranges of device files quickly.
20 * known bugs: can't deal with alpha ranges
22 int makedevs_main(int argc, char **argv)
25 char *basedev, *type, *nodname, buf[255];
26 int Smajor, Sminor, S, E;
28 if (argc < 7 || *argv[1]=='-')
33 Smajor = atoi(argv[3]);
34 Sminor = atoi(argv[4]);
37 nodname = argc == 8 ? basedev : buf;
58 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
59 if(sz<0 || sz>=sizeof(buf)) /* libc different */
60 bb_error_msg_and_die("%s too large", basedev);
62 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
64 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
65 bb_error_msg("Failed to create: %s", nodname);
67 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
76 #elif defined CONFIG_FEATURE_MAKEDEVS_TABLE
79 * This program is free software; you can redistribute it and/or modify
80 * it under the terms of the GNU General Public License version 2 as
81 * published by the Free Software Foundation.
83 * This program is distributed in the hope that it will be useful,
84 * but WITHOUT ANY WARRANTY; without even the implied warranty of
85 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86 * GNU Library General Public License for more details.
88 * You should have received a copy of the GNU General Public License
89 * along with this program; if not, write to the Free Software
90 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
94 extern int makedevs_main(int argc, char **argv)
101 int ret = EXIT_SUCCESS;
103 while ((opt = getopt(argc, argv, "d:")) != -1) {
106 table = bb_xfopen((line=optarg), "r");
113 if (optind >= argc || (rootdir=argv[optind])==NULL) {
114 bb_error_msg_and_die("root directory not speficied");
117 if (chdir(rootdir) != 0) {
118 bb_perror_msg_and_die("Couldnt chdir to %s", rootdir);
123 printf("rootdir=%s\n", rootdir);
125 printf("table='%s'\n", line);
127 printf("table=<stdin>\n");
130 while ((line = bb_get_chomped_line_from_file(table))) {
132 unsigned int mode = 0755;
133 unsigned int major = 0;
134 unsigned int minor = 0;
135 unsigned int count = 0;
136 unsigned int increment = 0;
137 unsigned int start = 0;
147 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
148 &type, &mode, user, group, &major,
149 &minor, &start, &increment, &count)) ||
150 ((major | minor | start | count | increment) > 255))
152 if (*line=='\0' || *line=='#' || isspace(*line))
154 bb_error_msg("line %d invalid: '%s'\n", linenum, line);
158 if (name[0] == '#') {
162 gid = get_ug_id(group, my_getgrnam);
167 uid = get_ug_id(user, my_getpwnam);
171 full_name = concat_path_file(rootdir, name);
174 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
175 if (chown(full_name, uid, gid) == -1) {
176 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
186 else if (type == 'c') {
189 else if (type == 'b') {
192 bb_error_msg("line %d: Unsupported file type %c", linenum, type);
201 full_name_inc = xmalloc(strlen(full_name) + 4);
202 for (i = start; i < count; i++) {
203 sprintf(full_name_inc, "%s%d", full_name, i);
204 rdev = (major << 8) + minor + (i * increment - start);
205 if (mknod(full_name_inc, mode, rdev) == -1) {
206 bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name_inc);
209 else if (chown(full_name_inc, uid, gid) == -1) {
210 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
216 rdev = (major << 8) + minor;
217 if (mknod(full_name, mode, rdev) == -1) {
218 bb_perror_msg("line %d: Couldnt create node %s", linenum, full_name);
221 else if (chown(full_name, uid, gid) == -1) {
222 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
237 # error makdedevs configuration error, either leaf or table must be selected