libbb: introduce and use xmalloc_ttyname (-32 in bss).
ash: small code shrink

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

diff --git a/TODO b/TODO
index f579211..6d6b7e9 100644
--- a/TODO
+++ b/TODO
@@ -23,7 +23,7 @@
     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
diff --git a/coreutils/tty.c b/coreutils/tty.c
index e832894..d49fb50 100644
--- a/coreutils/tty.c
+++ b/coreutils/tty.c
@@ -30,7 +30,7 @@
 
 	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. */
diff --git a/include/libbb.h b/include/libbb.h
index e6767e3..e0541a7 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1144,6 +1144,7 @@
 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;
 
diff --git a/init/mesg.c b/init/mesg.c
index cfb517f..ca230f3 100644
--- a/init/mesg.c
+++ b/init/mesg.c
@@ -25,7 +25,7 @@
 	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) {
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c
index f3c3c53..a86efc2 100644
--- a/libbb/xfuncs.c
+++ b/libbb/xfuncs.c
@@ -268,6 +268,17 @@
 }
 #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)
diff --git a/loginutils/login.c b/loginutils/login.c
index a18b4d5..af87143 100644
--- a/loginutils/login.c
+++ b/loginutils/login.c
@@ -275,7 +275,7 @@
 	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 @@
 	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();*/
diff --git a/loginutils/su.c b/loginutils/su.c
index e7e0001..de8c18d 100644
--- a/loginutils/su.c
+++ b/loginutils/su.c
@@ -44,7 +44,7 @@
 		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);
 	}
 
diff --git a/selinux/sestatus.c b/selinux/sestatus.c
index 2166296..1a02a6b 100644
--- a/selinux/sestatus.c
+++ b/selinux/sestatus.c
@@ -114,7 +114,8 @@
 	/* 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 @@
 			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)
diff --git a/shell/ash.c b/shell/ash.c
index 2fc903b..492ccd7 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -13691,7 +13691,7 @@
 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 @@
 	state = 0;
 	if (setjmp(jmploc.loc)) {
 		int e;
-		int s;
+		smallint s;
 
 		reset();
 
@@ -13769,7 +13769,7 @@
 		}
 	}
 #endif
-	if (argv[0] && argv[0][0] == '-')
+	if (/* argv[0] && */ argv[0][0] == '-')
 		isloginsh = 1;
 	if (isloginsh) {
 		state = 1;