libbb: introduce and use xmalloc_ttyname (-32 in bss).
authorDenis Vlasenko <vda.linux@googlemail.com>
Tue, 23 Dec 2008 23:36:47 +0000 (23:36 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Tue, 23 Dec 2008 23:36:47 +0000 (23:36 -0000)
ash: small code shrink

   text    data     bss     dec     hex filename
 793669     504    7524  801697   c3ba1 busybox_old
 793659     504    7492  801655   c3b77 busybox_unstripped

TODO
coreutils/tty.c
include/libbb.h
init/mesg.c
libbb/xfuncs.c
loginutils/login.c
loginutils/su.c
selinux/sestatus.c
shell/ash.c

diff --git a/TODO b/TODO
index f579211..6d6b7e9 100644 (file)
--- a/TODO
+++ b/TODO
@@ -23,7 +23,7 @@ Rob Landley suggested these:
     lash is phased out. hush can be configured down to be nearly as small,
     but less buggy :)
   init
-    General cleanup (should use ENABLE_FEATURE_INIT_SYSLOG and ENABLE_FEATURE_INIT_DEBUG).
+    General cleanup (should use ENABLE_FEATURE_INIT_SYSLOG).
   Do a SUSv3 audit
     Look at the full Single Unix Specification version 3 (available online at
     "http://www.opengroup.org/onlinepubs/009695399/nfindex.html") and
index e832894..d49fb50 100644 (file)
@@ -30,7 +30,7 @@ int tty_main(int argc, char **argv SKIP_INCLUDE_SUSv2(UNUSED_PARAM))
 
        retval = 0;
 
-       s = ttyname(0);
+       s = xmalloc_ttyname(0);
        if (s == NULL) {
        /* According to SUSv3, ttyname can fail with EBADF or ENOTTY.
         * We know the file descriptor is good, so failure means not a tty. */
index e6767e3..e0541a7 100644 (file)
@@ -1144,6 +1144,7 @@ const char *nth_string(const char *strings, int n) FAST_FUNC;
 extern void print_login_issue(const char *issue_file, const char *tty) FAST_FUNC;
 extern void print_login_prompt(void) FAST_FUNC;
 
+char *xmalloc_ttyname(int fd) FAST_FUNC;
 /* NB: typically you want to pass fd 0, not 1. Think 'applet | grep something' */
 int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC;
 
index cfb517f..ca230f3 100644 (file)
@@ -25,7 +25,7 @@ int mesg_main(int argc, char **argv)
        if (--argc == 0
         || (argc == 1 && ((c = **++argv) == 'y' || c == 'n'))
        ) {
-               tty = ttyname(STDERR_FILENO);
+               tty = xmalloc_ttyname(STDERR_FILENO);
                if (tty == NULL) {
                        tty = "ttyname";
                } else if (stat(tty, &sb) == 0) {
index f3c3c53..a86efc2 100644 (file)
@@ -268,6 +268,17 @@ off_t FAST_FUNC fdlength(int fd)
 }
 #endif
 
+char* FAST_FUNC xmalloc_ttyname(int fd)
+{
+       char *buf = xzalloc(128);
+       int r = ttyname_r(fd, buf, 127);
+       if (r) {
+               free(buf);
+               buf = NULL;
+       }
+       return buf;
+}
+
 /* It is perfectly ok to pass in a NULL for either width or for
  * height, in which case that value will not be set.  */
 int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)
index a18b4d5..af87143 100644 (file)
@@ -275,7 +275,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
        if (!isatty(0) || !isatty(1) || !isatty(2))
                return EXIT_FAILURE;            /* Must be a terminal */
        safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
-       tmp = ttyname(0);
+       tmp = xmalloc_ttyname(STDIN_FILENO);
        if (tmp) {
                safe_strncpy(full_tty, tmp, sizeof(full_tty));
                if (strncmp(full_tty, "/dev/", 5) == 0)
@@ -285,12 +285,12 @@ int login_main(int argc UNUSED_PARAM, char **argv)
        read_or_build_utent(&utent, run_by_root);
 
        if (opt & LOGIN_OPT_h) {
-               USE_FEATURE_UTMP(
+               if (ENABLE_FEATURE_UTMP)
                        safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
-               )
                fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
-       } else
+       } else {
                fromhost = xasprintf(" on '%s'", short_tty);
+       }
 
        /* Was breaking "login <username>" from shell command line: */
        /*bb_setpgrp();*/
index e7e0001..de8c18d 100644 (file)
@@ -44,7 +44,7 @@ int su_main(int argc UNUSED_PARAM, char **argv)
                But getlogin can fail -- usually due to lack of utmp entry.
                in this case resort to getpwuid.  */
                old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : "");
-               tty = ttyname(2) ? : "none";
+               tty = xmalloc_ttyname(2) ? : "none";
                openlog(applet_name, 0, LOG_AUTH);
        }
 
index 2166296..1a02a6b 100644 (file)
@@ -114,7 +114,8 @@ static void display_verbose(void)
        /* files contexts */
        puts("\nFile contexts:");
 
-       cterm = ttyname(0);
+       cterm = xmalloc_ttyname(0);
+//FIXME: if cterm == NULL, we segfault!??
        puts(cterm);
        if (cterm && lgetfilecon(cterm, &con) >= 0) {
                printf(COL_FMT "%s\n", "Controlling term:", con);
@@ -122,7 +123,7 @@ static void display_verbose(void)
                        freecon(con);
        }
 
-       for (i=0; fc[i] != NULL; i++) {
+       for (i = 0; fc[i] != NULL; i++) {
                struct stat stbuf;
 
                if (lgetfilecon(fc[i], &con) < 0)
index 2fc903b..492ccd7 100644 (file)
@@ -13691,7 +13691,7 @@ int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int ash_main(int argc UNUSED_PARAM, char **argv)
 {
        char *shinit;
-       volatile int state;
+       volatile smallint state;
        struct jmploc jmploc;
        struct stackmark smark;
 
@@ -13714,7 +13714,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
        state = 0;
        if (setjmp(jmploc.loc)) {
                int e;
-               int s;
+               smallint s;
 
                reset();
 
@@ -13769,7 +13769,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
                }
        }
 #endif
-       if (argv[0] && argv[0][0] == '-')
+       if (/* argv[0] && */ argv[0][0] == '-')
                isloginsh = 1;
        if (isloginsh) {
                state = 1;