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
78 /* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */
80 int makedevs_main(int argc, char **argv)
86 int ret = EXIT_SUCCESS;
89 flags = bb_getopt_ulflags(argc, argv, "d:", &line);
91 table = bb_xfopen(line, "r");
93 if (optind >= argc || (rootdir=argv[optind])==NULL) {
94 bb_error_msg_and_die("root directory not specified");
97 if (chdir(rootdir) != 0) {
98 bb_perror_msg_and_die("could not chdir to %s", rootdir);
103 printf("rootdir=%s\n", rootdir);
105 printf("table='%s'\n", line);
107 printf("table=<stdin>\n");
110 while ((line = bb_get_chomped_line_from_file(table))) {
112 unsigned int mode = 0755;
113 unsigned int major = 0;
114 unsigned int minor = 0;
115 unsigned int count = 0;
116 unsigned int increment = 0;
117 unsigned int start = 0;
127 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", name,
128 &type, &mode, user, group, &major,
129 &minor, &start, &increment, &count)) ||
130 ((major | minor | start | count | increment) > 255))
132 if (*line=='\0' || *line=='#' || isspace(*line))
134 bb_error_msg("line %d invalid: '%s'\n", linenum, line);
138 if (name[0] == '#') {
142 gid = (*group) ? get_ug_id(group, bb_xgetgrnam) : getgid();
143 uid = (*user) ? get_ug_id(user, bb_xgetpwnam) : getuid();
144 full_name = concat_path_file(rootdir, name);
147 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
148 if (chown(full_name, uid, gid) == -1) {
149 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
153 if ((mode != -1) && (chmod(full_name, mode) < 0)){
154 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
158 } else if (type == 'f') {
160 if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
161 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
165 if (chown(full_name, uid, gid) == -1) {
166 bb_perror_msg("line %d: chown failed for %s", linenum, full_name);
170 if ((mode != -1) && (chmod(full_name, mode) < 0)){
171 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
182 else if (type == 'c') {
185 else if (type == 'b') {
188 bb_error_msg("line %d: unsupported file type %c", linenum, type);
197 full_name_inc = xmalloc(strlen(full_name) + 4);
198 for (i = start; i < count; i++) {
199 sprintf(full_name_inc, "%s%d", full_name, i);
200 rdev = (major << 8) + minor + (i * increment - start);
201 if (mknod(full_name_inc, mode, rdev) == -1) {
202 bb_perror_msg("line %d: could not create node %s", linenum, full_name_inc);
205 else if (chown(full_name_inc, uid, gid) == -1) {
206 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc);
209 if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){
210 bb_perror_msg("line %d: chmod 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: could not 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);
225 if ((mode != -1) && (chmod(full_name, mode) < 0)){
226 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name);
241 # error makdedevs configuration error, either leaf or table must be selected