1 /* vi: set sw=4 ts=4: */
11 #include <sys/types.h>
12 #include <sys/sysmacros.h> /* major() and minor() */
15 #ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
17 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
20 * Make ranges of device files quickly.
21 * known bugs: can't deal with alpha ranges
23 int makedevs_main(int argc, char **argv)
26 char *basedev, *type, *nodname, buf[255];
27 int Smajor, Sminor, S, E;
29 if (argc < 7 || *argv[1]=='-')
34 Smajor = atoi(argv[3]);
35 Sminor = atoi(argv[4]);
38 nodname = argc == 8 ? basedev : buf;
59 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
60 if(sz<0 || sz>=sizeof(buf)) /* libc different */
61 bb_error_msg_and_die("%s too large", basedev);
63 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
65 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
66 bb_error_msg("Failed to create: %s", nodname);
68 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
77 #elif defined CONFIG_FEATURE_MAKEDEVS_TABLE
80 * This program is free software; you can redistribute it and/or modify
81 * it under the terms of the GNU General Public License version 2 as
82 * published by the Free Software Foundation.
84 * This program is distributed in the hope that it will be useful,
85 * but WITHOUT ANY WARRANTY; without even the implied warranty of
86 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
87 * GNU Library General Public License for more details.
89 * You should have received a copy of the GNU General Public License
90 * along with this program; if not, write to the Free Software
91 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
95 extern int makedevs_main(int argc, char **argv)
102 int ret = EXIT_SUCCESS;
104 while ((opt = getopt(argc, argv, "d:")) != -1) {
107 table = bb_xfopen((line=optarg), "r");
114 if (optind >= argc || (rootdir=argv[optind])==NULL) {
115 bb_error_msg_and_die("root directory not specified");
118 if (chdir(rootdir) != 0) {
119 bb_perror_msg_and_die("could not chdir to %s", rootdir);
124 printf("rootdir=%s\n", rootdir);
126 printf("table='%s'\n", line);
128 printf("table=<stdin>\n");
131 while ((line = bb_get_chomped_line_from_file(table))) {
133 unsigned int mode = 0755;
134 unsigned int major = 0;
135 unsigned int minor = 0;
136 unsigned int count = 0;
137 unsigned int increment = 0;
138 unsigned int start = 0;
148 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
149 &type, &mode, user, group, &major,
150 &minor, &start, &increment, &count)) ||
151 ((major | minor | start | count | increment) > 255))
153 if (*line=='\0' || *line=='#' || isspace(*line))
155 bb_error_msg("line %d invalid: '%s'\n", linenum, line);
159 if (name[0] == '#') {
163 gid = get_ug_id(group, my_getgrnam);
168 uid = get_ug_id(user, my_getpwnam);
172 full_name = concat_path_file(rootdir, name);
175 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
176 if (chown(full_name, uid, gid) == -1) {
177 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
187 else if (type == 'c') {
190 else if (type == 'b') {
193 bb_error_msg("line %d: unsupported file type %c", linenum, type);
202 full_name_inc = xmalloc(strlen(full_name) + 4);
203 for (i = start; i < count; i++) {
204 sprintf(full_name_inc, "%s%d", full_name, i);
205 rdev = (major << 8) + minor + (i * increment - start);
206 if (mknod(full_name_inc, mode, rdev) == -1) {
207 bb_perror_msg("line %d: could not create node %s", linenum, full_name_inc);
210 else if (chown(full_name_inc, uid, gid) == -1) {
211 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
217 rdev = (major << 8) + minor;
218 if (mknod(full_name, mode, rdev) == -1) {
219 bb_perror_msg("line %d: could not create node %s", linenum, full_name);
222 else if (chown(full_name, uid, gid) == -1) {
223 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
238 # error makdedevs configuration error, either leaf or table must be selected