use %m printf specifier where appropriate

function                                             old     new   delta
out                                                   85      75     -10
udhcpd_main                                         1472    1461     -11
open_stdio_to_tty                                     98      85     -13
init_exec                                            245     232     -13
udhcpc_main                                         2763    2749     -14
do_cmd                                              4771    4755     -16
status_line_bold_errno                                32      14     -18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/7 up/down: 0/-95)             Total: -95 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2017-09-29 18:17:25 +02:00
parent f625836e60
commit 6f97b30a90
9 changed files with 25 additions and 19 deletions

View File

@ -1038,7 +1038,7 @@ static void colon(char *buf)
} }
if (cnt < 0) { if (cnt < 0) {
if (cnt == -1) if (cnt == -1)
status_line_bold("Write error: %s", strerror(errno)); status_line_bold("Write error: "STRERROR_FMT STRERROR_ERRNO);
} else { } else {
modified_count = 0; modified_count = 0;
last_modified_count = -1; last_modified_count = -1;
@ -3131,7 +3131,7 @@ static void status_line_bold(const char *format, ...)
static void status_line_bold_errno(const char *fn) static void status_line_bold_errno(const char *fn)
{ {
status_line_bold("'%s' %s", fn, strerror(errno)); status_line_bold("'%s' "STRERROR_FMT, fn STRERROR_ERRNO);
} }
// format status buffer // format status buffer
@ -4066,7 +4066,7 @@ static void do_cmd(int c)
cnt = file_write(current_filename, text, end - 1); cnt = file_write(current_filename, text, end - 1);
if (cnt < 0) { if (cnt < 0) {
if (cnt == -1) if (cnt == -1)
status_line_bold("Write error: %s", strerror(errno)); status_line_bold("Write error: "STRERROR_FMT STRERROR_ERRNO);
} else if (cnt == (end - 1 - text + 1)) { } else if (cnt == (end - 1 - text + 1)) {
editing = 0; editing = 0;
} }

View File

@ -162,6 +162,17 @@
#ifndef HAVE_XTABS #ifndef HAVE_XTABS
# define XTABS TAB3 # define XTABS TAB3
#endif #endif
/*
* Use '%m' to append error string on platforms that support it,
* '%s' and strerror() on those that don't.
*/
#ifdef HAVE_PRINTF_PERCENTM
# define STRERROR_FMT "%m"
# define STRERROR_ERRNO /*nothing*/
#else
# define STRERROR_FMT "%s"
# define STRERROR_ERRNO ,strerror(errno)
#endif
/* Some libc's forget to declare these, do it ourself */ /* Some libc's forget to declare these, do it ourself */

View File

@ -391,8 +391,10 @@ static int open_stdio_to_tty(const char* tty_name)
/* fd can be only < 0 or 0: */ /* fd can be only < 0 or 0: */
fd = device_open(tty_name, O_RDWR); fd = device_open(tty_name, O_RDWR);
if (fd) { if (fd) {
message(L_LOG | L_CONSOLE, "can't open %s: %s", message(L_LOG | L_CONSOLE, "can't open %s: "STRERROR_FMT,
tty_name, strerror(errno)); tty_name
STRERROR_ERRNO
);
return 0; /* failure */ return 0; /* failure */
} }
dup2(STDIN_FILENO, STDOUT_FILENO); dup2(STDIN_FILENO, STDOUT_FILENO);
@ -469,7 +471,7 @@ static void init_exec(const char *command)
} }
/* Here command never contains the dash, cmd[0] might */ /* Here command never contains the dash, cmd[0] might */
BB_EXECVP(command, cmd); BB_EXECVP(command, cmd);
message(L_LOG | L_CONSOLE, "can't run '%s': %s", command, strerror(errno)); message(L_LOG | L_CONSOLE, "can't run '%s': "STRERROR_FMT, command STRERROR_ERRNO);
/* returns if execvp fails */ /* returns if execvp fails */
} }

View File

@ -1432,7 +1432,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv)
len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd); len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd);
if (len == -1) { if (len == -1) {
/* Error is severe, reopen socket */ /* Error is severe, reopen socket */
bb_error_msg("read error: %s, reopening socket", strerror(errno)); bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
sleep(discover_timeout); /* 3 seconds by default */ sleep(discover_timeout); /* 3 seconds by default */
change_listen_mode(listen_mode); /* just close and reopen */ change_listen_mode(listen_mode); /* just close and reopen */
} }

View File

@ -1631,7 +1631,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv)
len = udhcp_recv_raw_packet(&packet, sockfd); len = udhcp_recv_raw_packet(&packet, sockfd);
if (len == -1) { if (len == -1) {
/* Error is severe, reopen socket */ /* Error is severe, reopen socket */
bb_error_msg("read error: %s, reopening socket", strerror(errno)); bb_error_msg("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
sleep(discover_timeout); /* 3 seconds by default */ sleep(discover_timeout); /* 3 seconds by default */
change_listen_mode(listen_mode); /* just close and reopen */ change_listen_mode(listen_mode); /* just close and reopen */
} }

View File

@ -948,7 +948,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv)
if (bytes < 0) { if (bytes < 0) {
/* bytes can also be -2 ("bad packet data") */ /* bytes can also be -2 ("bad packet data") */
if (bytes == -1 && errno != EINTR) { if (bytes == -1 && errno != EINTR) {
log1("read error: %s, reopening socket", strerror(errno)); log1("read error: "STRERROR_FMT", reopening socket" STRERROR_ERRNO);
close(server_socket); close(server_socket);
server_socket = -1; server_socket = -1;
} }

View File

@ -224,7 +224,7 @@ static void out(const char *p, const char *m1)
{ {
printf("%s%s%s: %s", p, *service, islog ? "/log" : "", m1); printf("%s%s%s: %s", p, *service, islog ? "/log" : "", m1);
if (errno) { if (errno) {
printf(": %s", strerror(errno)); printf(": "STRERROR_FMT STRERROR_ERRNO);
} }
bb_putchar('\n'); /* will also flush the output */ bb_putchar('\n'); /* will also flush the output */
} }

View File

@ -104,7 +104,7 @@ int matchpathcon_main(int argc UNUSED_PARAM, char **argv)
freecon(con); freecon(con);
continue; continue;
} }
printf("actual context unknown: %s, should be ", strerror(errno)); printf("actual context unknown: "STRERROR_FMT", should be " STRERROR_ERRNO);
error += print_matchpathcon(path, 1); error += print_matchpathcon(path, 1);
} }
matchpathcon_fini(); matchpathcon_fini();

View File

@ -1312,16 +1312,9 @@ ash_msg_and_raise_error(const char *msg, ...)
} }
/* /*
* Use '%m' to append error string on platforms that support it, '%s' and
* strerror() on those that don't.
*
* 'fmt' must be a string literal. * 'fmt' must be a string literal.
*/ */
#ifdef HAVE_PRINTF_PERCENTM #define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": "STRERROR_FMT, ##__VA_ARGS__ STRERROR_ERRNO)
#define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": %m", ##__VA_ARGS__)
#else
#define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": %s", ##__VA_ARGS__, strerror(errno))
#endif
static void raise_error_syntax(const char *) NORETURN; static void raise_error_syntax(const char *) NORETURN;
static void static void