1 /* vi: set sw=4 ts=4: */
3 * public domain -- Dave 'Kill a Cop' Cinege <dcinege@psychosis.com>
6 * Make ranges of device files quickly.
7 * known bugs: can't deal with alpha ranges
12 #if ENABLE_FEATURE_MAKEDEVS_LEAF
13 int makedevs_main(int argc, char **argv);
14 int makedevs_main(int argc, char **argv)
17 char *basedev, *type, *nodname, buf[255];
18 int Smajor, Sminor, S, E;
20 if (argc < 7 || *argv[1]=='-')
25 Smajor = xatoi_u(argv[3]);
26 Sminor = xatoi_u(argv[4]);
29 nodname = argc == 8 ? basedev : buf;
50 sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
51 if (sz < 0 || sz >= sizeof(buf)) /* libc different */
52 bb_error_msg_and_die("%s too large", basedev);
54 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
56 if (mknod(nodname, mode, makedev(Smajor, Sminor)))
57 bb_error_msg("failed to create: %s", nodname);
59 if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
68 #elif ENABLE_FEATURE_MAKEDEVS_TABLE
70 /* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */
72 int makedevs_main(int argc, char **argv);
73 int makedevs_main(int argc, char **argv)
79 int ret = EXIT_SUCCESS;
81 getopt32(argv, "d:", &line);
83 table = xfopen(line, "r");
85 if (optind >= argc || (rootdir=argv[optind])==NULL) {
86 bb_error_msg_and_die("root directory not specified");
93 printf("rootdir=%s\n", rootdir);
95 printf("table='%s'\n", line);
97 printf("table=<stdin>\n");
100 while ((line = xmalloc_getline(table))) {
102 unsigned int mode = 0755;
103 unsigned int major = 0;
104 unsigned int minor = 0;
105 unsigned int count = 0;
106 unsigned int increment = 0;
107 unsigned int start = 0;
117 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
118 &type, &mode, user, group, &major,
119 &minor, &start, &increment, &count)) ||
120 ((major | minor | start | count | increment) > 255))
122 if (*line=='\0' || *line=='#' || isspace(*line))
124 bb_error_msg("line %d invalid: '%s'", linenum, line);
128 if (name[0] == '#') {
132 gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
133 uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
134 full_name = concat_path_file(rootdir, name);
137 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
138 if (chown(full_name, uid, gid) == -1) {
139 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
143 if ((mode != -1) && (chmod(full_name, mode) < 0)){
144 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
148 } else if (type == 'f') {
150 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
151 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
155 if (chown(full_name, uid, gid) == -1) {
156 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
160 if ((mode != -1) && (chmod(full_name, mode) < 0)){
161 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
171 else if (type == 'c') {
174 else if (type == 'b') {
177 bb_error_msg("line %d: unsupported file type %c", linenum, type);
186 full_name_inc = xmalloc(strlen(full_name) + 4);
187 for (i = start; i < count; i++) {
188 sprintf(full_name_inc, "%s%d", full_name, i);
189 rdev = makedev(major, minor + (i * increment - start));
190 if (mknod(full_name_inc, mode, rdev) == -1) {
191 bb_perror_msg("line %d: cannot create node %s", linenum, full_name_inc);
194 else if (chown(full_name_inc, uid, gid) == -1) {
195 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
198 if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
199 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc);
205 rdev = makedev(major, minor);
206 if (mknod(full_name, mode, rdev) == -1) {
207 bb_perror_msg("line %d: cannot create node %s", linenum, full_name);
210 else if (chown(full_name, uid, gid) == -1) {
211 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
214 if ((mode != -1) && (chmod(full_name, mode) < 0)){
215 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
230 # error makedevs configuration error, either leaf or table must be selected