ifconfig: code shrink
[oweals/busybox.git] / miscutils / makedevs.c
index 24d07ebe0f3f253fd8b35395aee346bf306dda9e..1f88f34288437c1e5daf4cba2193cf308d886776 100644 (file)
@@ -7,18 +7,10 @@
  * known bugs: can't deal with alpha ranges
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <fcntl.h>
-#include <time.h>
-#include <ctype.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/sysmacros.h>     /* major() and minor() */
-#include "busybox.h"
-
-#ifdef CONFIG_FEATURE_MAKEDEVS_LEAF
+#include "libbb.h"
+
+#if ENABLE_FEATURE_MAKEDEVS_LEAF
+int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int makedevs_main(int argc, char **argv)
 {
        mode_t mode;
@@ -30,10 +22,10 @@ int makedevs_main(int argc, char **argv)
 
        basedev = argv[1];
        type = argv[2];
-       Smajor = atoi(argv[3]);
-       Sminor = atoi(argv[4]);
-       S = atoi(argv[5]);
-       E = atoi(argv[6]);
+       Smajor = xatoi_u(argv[3]);
+       Sminor = xatoi_u(argv[4]);
+       S = xatoi_u(argv[5]);
+       E = xatoi_u(argv[6]);
        nodname = argc == 8 ? basedev : buf;
 
        mode = 0660;
@@ -56,13 +48,13 @@ int makedevs_main(int argc, char **argv)
                int sz;
 
                sz = snprintf(buf, sizeof(buf), "%s%d", basedev, S);
-               if(sz<0 || sz>=sizeof(buf))  /* libc different */
+               if (sz < 0 || sz >= sizeof(buf))  /* libc different */
                        bb_error_msg_and_die("%s too large", basedev);
 
        /* if mode != S_IFCHR and != S_IFBLK third param in mknod() ignored */
 
                if (mknod(nodname, mode, makedev(Smajor, Sminor)))
-                       bb_error_msg("Failed to create: %s", nodname);
+                       bb_error_msg("failed to create: %s", nodname);
 
                if (nodname == basedev) /* ex. /dev/hda - to /dev/hda1 ... */
                        nodname = buf;
@@ -73,10 +65,11 @@ int makedevs_main(int argc, char **argv)
        return 0;
 }
 
-#elif defined CONFIG_FEATURE_MAKEDEVS_TABLE
+#elif ENABLE_FEATURE_MAKEDEVS_TABLE
 
 /* Licensed under the GPL v2 or later, see the file LICENSE in this tarball. */
 
+int makedevs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int makedevs_main(int argc, char **argv)
 {
        FILE *table = stdin;
@@ -85,16 +78,15 @@ int makedevs_main(int argc, char **argv)
        int linenum = 0;
        int ret = EXIT_SUCCESS;
 
-       unsigned long flags;
-       flags = bb_getopt_ulflags(argc, argv, "d:", &line);
+       getopt32(argv, "d:", &line);
        if (line)
-               table = bb_xfopen(line, "r");
+               table = xfopen(line, "r");
 
        if (optind >= argc || (rootdir=argv[optind])==NULL) {
                bb_error_msg_and_die("root directory not specified");
        }
 
-       bb_xchdir(rootdir);
+       xchdir(rootdir);
 
        umask(0);
 
@@ -105,7 +97,7 @@ int makedevs_main(int argc, char **argv)
                printf("table=<stdin>\n");
        }
 
-       while ((line = bb_get_chomped_line_from_file(table))) {
+       while ((line = xmalloc_getline(table))) {
                char type;
                unsigned int mode = 0755;
                unsigned int major = 0;
@@ -129,7 +121,7 @@ int makedevs_main(int argc, char **argv)
                {
                        if (*line=='\0' || *line=='#' || isspace(*line))
                                continue;
-                       bb_error_msg("line %d invalid: '%s'\n", linenum, line);
+                       bb_error_msg("line %d invalid: '%s'", linenum, line);
                        ret = EXIT_FAILURE;
                        continue;
                }
@@ -137,8 +129,8 @@ int makedevs_main(int argc, char **argv)
                        continue;
                }
 
-               gid = (*group) ? get_ug_id(group, bb_xgetgrnam) : getgid();
-               uid = (*user) ? get_ug_id(user, bb_xgetpwnam) : getuid();
+               gid = (*group) ? get_ug_id(group, xgroup2gid) : getgid();
+               uid = (*user) ? get_ug_id(user, xuname2uid) : getuid();
                full_name = concat_path_file(rootdir, name);
 
                if (type == 'd') {
@@ -170,8 +162,7 @@ int makedevs_main(int argc, char **argv)
                                ret = EXIT_FAILURE;
                                goto loop;
                        }
-               } else
-               {
+               } else {
                        dev_t rdev;
 
                        if (type == 'p') {
@@ -195,9 +186,9 @@ int makedevs_main(int argc, char **argv)
                                full_name_inc = xmalloc(strlen(full_name) + 4);
                                for (i = start; i < count; i++) {
                                        sprintf(full_name_inc, "%s%d", full_name, i);
-                                       rdev = (major << 8) + minor + (i * increment - start);
+                                       rdev = makedev(major, minor + (i * increment - start));
                                        if (mknod(full_name_inc, mode, rdev) == -1) {
-                                               bb_perror_msg("line %d: could not create node %s", linenum, full_name_inc);
+                                               bb_perror_msg("line %d: cannot create node %s", linenum, full_name_inc);
                                                ret = EXIT_FAILURE;
                                        }
                                        else if (chown(full_name_inc, uid, gid) == -1) {
@@ -211,9 +202,9 @@ int makedevs_main(int argc, char **argv)
                                }
                                free(full_name_inc);
                        } else {
-                               rdev = (major << 8) + minor;
+                               rdev = makedev(major, minor);
                                if (mknod(full_name, mode, rdev) == -1) {
-                                       bb_perror_msg("line %d: could not create node %s", linenum, full_name);
+                                       bb_perror_msg("line %d: cannot create node %s", linenum, full_name);
                                        ret = EXIT_FAILURE;
                                }
                                else if (chown(full_name, uid, gid) == -1) {
@@ -232,7 +223,7 @@ loop:
        }
        fclose(table);
 
-       return 0;
+       return ret;
 }
 
 #else