Changeset 2725 in MondoRescue for branches/2.2.9/mindi-busybox/miscutils/makedevs.c
- Timestamp:
- Feb 25, 2011, 9:26:54 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.9/mindi-busybox/miscutils/makedevs.c
r1765 r2725 11 11 12 12 #if ENABLE_FEATURE_MAKEDEVS_LEAF 13 int makedevs_main(int argc, char **argv); 13 /* 14 makedevs NAME TYPE MAJOR MINOR FIRST LAST [s] 15 TYPEs: 16 b Block device 17 c Character device 18 f FIFO 19 20 FIRST..LAST specify numbers appended to NAME. 21 If 's' is the last argument, the base device is created as well. 22 Examples: 23 makedevs /dev/ttyS c 4 66 2 63 -> ttyS2-ttyS63 24 makedevs /dev/hda b 3 0 0 8 s -> hda,hda1-hda8 25 */ 26 int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 14 27 int makedevs_main(int argc, char **argv) 15 28 { 16 29 mode_t mode; 17 char *basedev, *type, *nodname, buf[255];30 char *basedev, *type, *nodname, *buf; 18 31 int Smajor, Sminor, S, E; 19 32 20 if (argc < 7 || *argv[1]=='-')33 if (argc < 7 || argv[1][0] == '-') 21 34 bb_show_usage(); 22 35 23 36 basedev = argv[1]; 37 buf = xasprintf("%s%u", argv[1], (unsigned)-1); 24 38 type = argv[2]; 25 Smajor = xatoi_ u(argv[3]);26 Sminor = xatoi_ u(argv[4]);27 S = xatoi_ u(argv[5]);28 E = xatoi_ u(argv[6]);29 nodname = arg c == 8? basedev : buf;39 Smajor = xatoi_positive(argv[3]); 40 Sminor = xatoi_positive(argv[4]); 41 S = xatoi_positive(argv[5]); 42 E = xatoi_positive(argv[6]); 43 nodname = argv[7] ? basedev : buf; 30 44 31 45 mode = 0660; 32 33 46 switch (type[0]) { 34 47 case 'c': … … 46 59 47 60 while (S <= E) { 48 int sz; 49 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); 53 54 /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */ 55 61 sprintf(buf, "%s%u", basedev, S); 62 63 /* if mode != S_IFCHR and != S_IFBLK, 64 * third param in mknod() ignored */ 56 65 if (mknod(nodname, mode, makedev(Smajor, Sminor))) 57 bb_ error_msg("failed to create: %s", nodname);58 59 if (nodname == basedev)/* ex. /dev/hda - to /dev/hda1 ... */66 bb_perror_msg("can't create '%s'", nodname); 67 68 /*if (nodname == basedev)*/ /* ex. /dev/hda - to /dev/hda1 ... */ 60 69 nodname = buf; 61 70 S++; … … 68 77 #elif ENABLE_FEATURE_MAKEDEVS_TABLE 69 78 70 /* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */71 72 int makedevs_main(int argc, char **argv) ;73 int makedevs_main(int argc , char **argv)79 /* Licensed under GPLv2 or later, see file LICENSE in this source tree. */ 80 81 int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 82 int makedevs_main(int argc UNUSED_PARAM, char **argv) 74 83 { 75 FILE *table = stdin; 76 char *rootdir = NULL; 77 char *line = NULL; 78 int linenum = 0; 84 parser_t *parser; 85 char *line = (char *)"-"; 79 86 int ret = EXIT_SUCCESS; 80 87 88 opt_complementary = "=1"; /* exactly one param */ 81 89 getopt32(argv, "d:", &line); 82 if (line) 83 table = xfopen(line, "r"); 84 85 if (optind >= argc || (rootdir=argv[optind])==NULL) { 86 bb_error_msg_and_die("root directory not specified"); 87 } 88 89 xchdir(rootdir); 90 argv += optind; 91 92 xchdir(*argv); /* ensure root dir exists */ 90 93 91 94 umask(0); 92 95 93 printf("rootdir=%s\n ", rootdir);94 if ( line) {95 printf(" table='%s'\n", line);96 printf("rootdir=%s\ntable=", *argv); 97 if (NOT_LONE_DASH(line)) { 98 printf("'%s'\n", line); 96 99 } else { 97 printf("table=<stdin>\n"); 98 } 99 100 while ((line = xmalloc_getline(table))) { 100 puts("<stdin>"); 101 } 102 103 parser = config_open(line); 104 while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) { 105 int linenum; 101 106 char type; 102 unsigned intmode = 0755;103 unsigned intmajor = 0;104 unsigned intminor = 0;105 unsigned intcount = 0;106 unsigned in t increment = 0;107 unsigned intstart = 0;107 unsigned mode = 0755; 108 unsigned major = 0; 109 unsigned minor = 0; 110 unsigned count = 0; 111 unsigned increment = 0; 112 unsigned start = 0; 108 113 char name[41]; 109 114 char user[41]; 110 115 char group[41]; 111 char *full_name ;116 char *full_name = name; 112 117 uid_t uid; 113 118 gid_t gid; 114 119 115 linenum++; 116 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)) 121 { 122 if (*line=='\0' || *line=='#' || isspace(*line)) 123 continue; 124 bb_error_msg("line %d invalid: '%s'", linenum, line); 120 linenum = parser->lineno; 121 122 if ((2 > sscanf(line, "%40s %c %o %40s %40s %u %u %u %u %u", 123 name, &type, &mode, user, group, 124 &major, &minor, &start, &increment, &count)) 125 || ((unsigned)(major | minor | start | count | increment) > 255) 126 ) { 127 bb_error_msg("invalid line %d: '%s'", linenum, line); 125 128 ret = EXIT_FAILURE; 126 129 continue; 127 130 } 128 if (name[0] == '#') {129 continue;130 }131 131 132 132 gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid(); 133 133 uid = (*user) ? get_ug_id(user, xuname2uid) : getuid(); 134 full_name = concat_path_file(rootdir, name); 134 /* We are already in the right root dir, 135 * so make absolute paths relative */ 136 if ('/' == *full_name) 137 full_name++; 135 138 136 139 if (type == 'd') { 137 140 bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); 138 141 if (chown(full_name, uid, gid) == -1) { 139 bb_perror_msg("line %d: chown failed for %s", linenum, full_name); 140 ret = EXIT_FAILURE; 141 goto loop; 142 } 143 if ((mode != -1) && (chmod(full_name, mode) < 0)){ 144 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); 145 ret = EXIT_FAILURE; 146 goto loop; 142 chown_fail: 143 bb_perror_msg("line %d: can't chown %s", linenum, full_name); 144 ret = EXIT_FAILURE; 145 continue; 146 } 147 if (chmod(full_name, mode) < 0) { 148 chmod_fail: 149 bb_perror_msg("line %d: can't chmod %s", linenum, full_name); 150 ret = EXIT_FAILURE; 151 continue; 147 152 } 148 153 } else if (type == 'f') { … … 151 156 bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name); 152 157 ret = EXIT_FAILURE; 153 goto loop; 154 } 155 if (chown(full_name, uid, gid) == -1) { 156 bb_perror_msg("line %d: chown failed for %s", linenum, full_name); 157 ret = EXIT_FAILURE; 158 goto loop; 159 } 160 if ((mode != -1) && (chmod(full_name, mode) < 0)){ 161 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); 162 ret = EXIT_FAILURE; 163 goto loop; 164 } 158 continue; 159 } 160 if (chown(full_name, uid, gid) < 0) 161 goto chown_fail; 162 if (chmod(full_name, mode) < 0) 163 goto chmod_fail; 165 164 } else { 166 165 dev_t rdev; 166 unsigned i; 167 char *full_name_inc; 167 168 168 169 if (type == 'p') { 169 170 mode |= S_IFIFO; 170 } 171 else if (type == 'c') { 171 } else if (type == 'c') { 172 172 mode |= S_IFCHR; 173 } 174 else if (type == 'b') { 173 } else if (type == 'b') { 175 174 mode |= S_IFBLK; 176 175 } else { 177 176 bb_error_msg("line %d: unsupported file type %c", linenum, type); 178 177 ret = EXIT_FAILURE; 179 goto loop; 180 } 181 182 if (count > 0) { 183 int i; 184 char *full_name_inc; 185 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); 192 ret = EXIT_FAILURE; 193 } 194 else if (chown(full_name_inc, uid, gid) == -1) { 195 bb_perror_msg("line %d: chown failed for %s", linenum, full_name_inc); 196 ret = EXIT_FAILURE; 197 } 198 if ((mode != -1) && (chmod(full_name_inc, mode) < 0)){ 199 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name_inc); 200 ret = EXIT_FAILURE; 201 } 202 } 203 free(full_name_inc); 204 } else { 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); 178 continue; 179 } 180 181 full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2); 182 if (count) 183 count--; 184 for (i = start; i <= start + count; i++) { 185 sprintf(full_name_inc, count ? "%s%u" : "%s", full_name, i); 186 rdev = makedev(major, minor + (i - start) * increment); 187 if (mknod(full_name_inc, mode, rdev) < 0) { 188 bb_perror_msg("line %d: can't create node %s", linenum, full_name_inc); 189 ret = EXIT_FAILURE; 190 } else if (chown(full_name_inc, uid, gid) < 0) { 191 bb_perror_msg("line %d: can't chown %s", linenum, full_name_inc); 192 ret = EXIT_FAILURE; 193 } else if (chmod(full_name_inc, mode) < 0) { 194 bb_perror_msg("line %d: can't chmod %s", linenum, full_name_inc); 208 195 ret = EXIT_FAILURE; 209 196 } 210 else if (chown(full_name, uid, gid) == -1) { 211 bb_perror_msg("line %d: chown failed for %s", linenum, full_name); 212 ret = EXIT_FAILURE; 213 } 214 if ((mode != -1) && (chmod(full_name, mode) < 0)){ 215 bb_perror_msg("line %d: chmod failed for %s", linenum, full_name); 216 ret = EXIT_FAILURE; 217 } 218 } 197 } 198 free(full_name_inc); 219 199 } 220 loop: 221 free(line); 222 free(full_name); 223 } 224 fclose(table); 200 } 201 if (ENABLE_FEATURE_CLEAN_UP) 202 config_close(parser); 225 203 226 204 return ret;
Note:
See TracChangeset
for help on using the changeset viewer.