makedevs: shrink by Vladimir

function                                             old     new   delta
makedevs_main                                       1071    1049     -22
packed_usage                                       24744   24708     -36
This commit is contained in:
Denis Vlasenko 2008-08-03 22:14:02 +00:00
parent 7ce47e698e
commit b2dc913527

View File

@ -15,7 +15,7 @@ makedevs NAME TYPE MAJOR MINOR FIRST LAST [s]
TYPEs: TYPEs:
b Block device b Block device
c Character device c Character device
p FIFO f FIFO
FIRST..LAST specify numbers appended to NAME. FIRST..LAST specify numbers appended to NAME.
If 's' is the last argument, the base device is created as well. If 's' is the last argument, the base device is created as well.
@ -82,28 +82,27 @@ int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int makedevs_main(int argc UNUSED_PARAM, char **argv) int makedevs_main(int argc UNUSED_PARAM, char **argv)
{ {
parser_t *parser; parser_t *parser;
char *rootdir = NULL;
char *line = (char *)"-"; char *line = (char *)"-";
int linenum;
int ret = EXIT_SUCCESS; int ret = EXIT_SUCCESS;
opt_complementary = "=1"; /* exactly one param */ opt_complementary = "=1"; /* exactly one param */
getopt32(argv, "d:", &line); getopt32(argv, "d:", &line);
rootdir = argv[optind]; argv += optind;
parser = config_open(line);
xchdir(rootdir); xchdir(*argv); /* ensure root dir exists */
umask(0); umask(0);
printf("rootdir=%s\n", rootdir); printf("rootdir=%s\ntable=", *argv);
if (NOT_LONE_DASH(line)) { if (NOT_LONE_DASH(line)) {
printf("table='%s'\n", line); printf("'%s'\n", line);
} else { } else {
printf("table=<stdin>\n"); puts("<stdin>");
} }
parser = config_open(line);
while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) { while (config_read(parser, &line, 1, 1, "# \t", PARSE_NORMAL)) {
int linenum;
char type; char type;
unsigned mode = 0755; unsigned mode = 0755;
unsigned major = 0; unsigned major = 0;
@ -114,7 +113,7 @@ int makedevs_main(int argc UNUSED_PARAM, char **argv)
char name[41]; char name[41];
char user[41]; char user[41];
char group[41]; char group[41];
char *full_name; char *full_name = name;
uid_t uid; uid_t uid;
gid_t gid; gid_t gid;
@ -132,7 +131,10 @@ int makedevs_main(int argc UNUSED_PARAM, char **argv)
gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid(); gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
uid = (*user) ? get_ug_id(user, xuname2uid) : getuid(); uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
full_name = concat_path_file(rootdir, name); /* We are already in the right root dir,
* so make absolute paths relative */
if ('/' == *full_name)
full_name++;
if (type == 'd') { if (type == 'd') {
bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR); bb_make_directory(full_name, mode | S_IFDIR, FILEUTILS_RECUR);
@ -140,20 +142,20 @@ int makedevs_main(int argc UNUSED_PARAM, char **argv)
chown_fail: chown_fail:
bb_perror_msg("line %d: can't chown %s", linenum, full_name); bb_perror_msg("line %d: can't chown %s", linenum, full_name);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
goto loop; continue;
} }
if (chmod(full_name, mode) < 0) { if (chmod(full_name, mode) < 0) {
chmod_fail: chmod_fail:
bb_perror_msg("line %d: can't chmod %s", linenum, full_name); bb_perror_msg("line %d: can't chmod %s", linenum, full_name);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
goto loop; continue;
} }
} else if (type == 'f') { } else if (type == 'f') {
struct stat st; struct stat st;
if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) { if ((stat(full_name, &st) < 0 || !S_ISREG(st.st_mode))) {
bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name); bb_perror_msg("line %d: regular file '%s' does not exist", linenum, full_name);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
goto loop; continue;
} }
if (chown(full_name, uid, gid) < 0) if (chown(full_name, uid, gid) < 0)
goto chown_fail; goto chown_fail;
@ -173,7 +175,7 @@ int makedevs_main(int argc UNUSED_PARAM, char **argv)
} else { } else {
bb_error_msg("line %d: unsupported file type %c", linenum, type); bb_error_msg("line %d: unsupported file type %c", linenum, type);
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
goto loop; continue;
} }
full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2); full_name_inc = xmalloc(strlen(full_name) + sizeof(int)*3 + 2);
@ -195,8 +197,6 @@ int makedevs_main(int argc UNUSED_PARAM, char **argv)
} }
free(full_name_inc); free(full_name_inc);
} }
loop:
free(full_name);
} }
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
config_close(parser); config_close(parser);