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
This commit is contained in:
Denis Vlasenko 2008-12-23 23:36:47 +00:00
parent b3523b9cd3
commit 4e12b1a2a9
9 changed files with 26 additions and 13 deletions

2
TODO
View File

@ -23,7 +23,7 @@ Rob Landley suggested these:
lash is phased out. hush can be configured down to be nearly as small, lash is phased out. hush can be configured down to be nearly as small,
but less buggy :) but less buggy :)
init 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 Do a SUSv3 audit
Look at the full Single Unix Specification version 3 (available online at Look at the full Single Unix Specification version 3 (available online at
"http://www.opengroup.org/onlinepubs/009695399/nfindex.html") and "http://www.opengroup.org/onlinepubs/009695399/nfindex.html") and

View File

@ -30,7 +30,7 @@ int tty_main(int argc, char **argv SKIP_INCLUDE_SUSv2(UNUSED_PARAM))
retval = 0; retval = 0;
s = ttyname(0); s = xmalloc_ttyname(0);
if (s == NULL) { if (s == NULL) {
/* According to SUSv3, ttyname can fail with EBADF or ENOTTY. /* According to SUSv3, ttyname can fail with EBADF or ENOTTY.
* We know the file descriptor is good, so failure means not a tty. */ * We know the file descriptor is good, so failure means not a tty. */

View 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_issue(const char *issue_file, const char *tty) FAST_FUNC;
extern void print_login_prompt(void) 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' */ /* 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; int get_terminal_width_height(int fd, unsigned *width, unsigned *height) FAST_FUNC;

View File

@ -25,7 +25,7 @@ int mesg_main(int argc, char **argv)
if (--argc == 0 if (--argc == 0
|| (argc == 1 && ((c = **++argv) == 'y' || c == 'n')) || (argc == 1 && ((c = **++argv) == 'y' || c == 'n'))
) { ) {
tty = ttyname(STDERR_FILENO); tty = xmalloc_ttyname(STDERR_FILENO);
if (tty == NULL) { if (tty == NULL) {
tty = "ttyname"; tty = "ttyname";
} else if (stat(tty, &sb) == 0) { } else if (stat(tty, &sb) == 0) {

View File

@ -268,6 +268,17 @@ off_t FAST_FUNC fdlength(int fd)
} }
#endif #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 /* It is perfectly ok to pass in a NULL for either width or for
* height, in which case that value will not be set. */ * height, in which case that value will not be set. */
int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height) int FAST_FUNC get_terminal_width_height(int fd, unsigned *width, unsigned *height)

View File

@ -275,7 +275,7 @@ int login_main(int argc UNUSED_PARAM, char **argv)
if (!isatty(0) || !isatty(1) || !isatty(2)) if (!isatty(0) || !isatty(1) || !isatty(2))
return EXIT_FAILURE; /* Must be a terminal */ return EXIT_FAILURE; /* Must be a terminal */
safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty)); safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
tmp = ttyname(0); tmp = xmalloc_ttyname(STDIN_FILENO);
if (tmp) { if (tmp) {
safe_strncpy(full_tty, tmp, sizeof(full_tty)); safe_strncpy(full_tty, tmp, sizeof(full_tty));
if (strncmp(full_tty, "/dev/", 5) == 0) 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); read_or_build_utent(&utent, run_by_root);
if (opt & LOGIN_OPT_h) { if (opt & LOGIN_OPT_h) {
USE_FEATURE_UTMP( if (ENABLE_FEATURE_UTMP)
safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host)); safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));
)
fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host); fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
} else } else {
fromhost = xasprintf(" on '%s'", short_tty); fromhost = xasprintf(" on '%s'", short_tty);
}
/* Was breaking "login <username>" from shell command line: */ /* Was breaking "login <username>" from shell command line: */
/*bb_setpgrp();*/ /*bb_setpgrp();*/

View 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. But getlogin can fail -- usually due to lack of utmp entry.
in this case resort to getpwuid. */ in this case resort to getpwuid. */
old_user = xstrdup(USE_FEATURE_UTMP(getlogin() ? : ) (pw = getpwuid(cur_uid)) ? pw->pw_name : ""); 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); openlog(applet_name, 0, LOG_AUTH);
} }

View File

@ -114,7 +114,8 @@ static void display_verbose(void)
/* files contexts */ /* files contexts */
puts("\nFile contexts:"); puts("\nFile contexts:");
cterm = ttyname(0); cterm = xmalloc_ttyname(0);
//FIXME: if cterm == NULL, we segfault!??
puts(cterm); puts(cterm);
if (cterm && lgetfilecon(cterm, &con) >= 0) { if (cterm && lgetfilecon(cterm, &con) >= 0) {
printf(COL_FMT "%s\n", "Controlling term:", con); printf(COL_FMT "%s\n", "Controlling term:", con);
@ -122,7 +123,7 @@ static void display_verbose(void)
freecon(con); freecon(con);
} }
for (i=0; fc[i] != NULL; i++) { for (i = 0; fc[i] != NULL; i++) {
struct stat stbuf; struct stat stbuf;
if (lgetfilecon(fc[i], &con) < 0) if (lgetfilecon(fc[i], &con) < 0)

View File

@ -13691,7 +13691,7 @@ int ash_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int ash_main(int argc UNUSED_PARAM, char **argv) int ash_main(int argc UNUSED_PARAM, char **argv)
{ {
char *shinit; char *shinit;
volatile int state; volatile smallint state;
struct jmploc jmploc; struct jmploc jmploc;
struct stackmark smark; struct stackmark smark;
@ -13714,7 +13714,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
state = 0; state = 0;
if (setjmp(jmploc.loc)) { if (setjmp(jmploc.loc)) {
int e; int e;
int s; smallint s;
reset(); reset();
@ -13769,7 +13769,7 @@ int ash_main(int argc UNUSED_PARAM, char **argv)
} }
} }
#endif #endif
if (argv[0] && argv[0][0] == '-') if (/* argv[0] && */ argv[0][0] == '-')
isloginsh = 1; isloginsh = 1;
if (isloginsh) { if (isloginsh) {
state = 1; state = 1;