From 6f97b30a904407c1f1701d937d9258ca9175c6ab Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Fri, 29 Sep 2017 18:17:25 +0200 Subject: 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 --- editors/vi.c | 6 +++--- include/libbb.h | 11 +++++++++++ init/init.c | 8 +++++--- networking/udhcp/d6_dhcpc.c | 2 +- networking/udhcp/dhcpc.c | 2 +- networking/udhcp/dhcpd.c | 2 +- runit/sv.c | 2 +- selinux/matchpathcon.c | 2 +- shell/ash.c | 9 +-------- 9 files changed, 25 insertions(+), 19 deletions(-) diff --git a/editors/vi.c b/editors/vi.c index 60c73c0eb..cdfb27cc5 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -1038,7 +1038,7 @@ static void colon(char *buf) } if (cnt < 0) { if (cnt == -1) - status_line_bold("Write error: %s", strerror(errno)); + status_line_bold("Write error: "STRERROR_FMT STRERROR_ERRNO); } else { modified_count = 0; 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) { - status_line_bold("'%s' %s", fn, strerror(errno)); + status_line_bold("'%s' "STRERROR_FMT, fn STRERROR_ERRNO); } // format status buffer @@ -4066,7 +4066,7 @@ static void do_cmd(int c) cnt = file_write(current_filename, text, end - 1); if (cnt < 0) { 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)) { editing = 0; } diff --git a/include/libbb.h b/include/libbb.h index aff2825ac..daccf154a 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -162,6 +162,17 @@ #ifndef HAVE_XTABS # define XTABS TAB3 #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 */ diff --git a/init/init.c b/init/init.c index 64e5c3e0b..5304e5c2c 100644 --- a/init/init.c +++ b/init/init.c @@ -391,8 +391,10 @@ static int open_stdio_to_tty(const char* tty_name) /* fd can be only < 0 or 0: */ fd = device_open(tty_name, O_RDWR); if (fd) { - message(L_LOG | L_CONSOLE, "can't open %s: %s", - tty_name, strerror(errno)); + message(L_LOG | L_CONSOLE, "can't open %s: "STRERROR_FMT, + tty_name + STRERROR_ERRNO + ); return 0; /* failure */ } 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 */ 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 */ } diff --git a/networking/udhcp/d6_dhcpc.c b/networking/udhcp/d6_dhcpc.c index c13f23505..37ffd064d 100644 --- a/networking/udhcp/d6_dhcpc.c +++ b/networking/udhcp/d6_dhcpc.c @@ -1432,7 +1432,7 @@ int udhcpc6_main(int argc UNUSED_PARAM, char **argv) len = d6_recv_raw_packet(&srv6_buf, &packet, sockfd); if (len == -1) { /* 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 */ change_listen_mode(listen_mode); /* just close and reopen */ } diff --git a/networking/udhcp/dhcpc.c b/networking/udhcp/dhcpc.c index 4548d4ac7..0013f4aa7 100644 --- a/networking/udhcp/dhcpc.c +++ b/networking/udhcp/dhcpc.c @@ -1631,7 +1631,7 @@ int udhcpc_main(int argc UNUSED_PARAM, char **argv) len = udhcp_recv_raw_packet(&packet, sockfd); if (len == -1) { /* 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 */ change_listen_mode(listen_mode); /* just close and reopen */ } diff --git a/networking/udhcp/dhcpd.c b/networking/udhcp/dhcpd.c index 57d8b36c5..238542bb0 100644 --- a/networking/udhcp/dhcpd.c +++ b/networking/udhcp/dhcpd.c @@ -948,7 +948,7 @@ int udhcpd_main(int argc UNUSED_PARAM, char **argv) if (bytes < 0) { /* bytes can also be -2 ("bad packet data") */ 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); server_socket = -1; } diff --git a/runit/sv.c b/runit/sv.c index 1d0809be8..dc5dcceb3 100644 --- a/runit/sv.c +++ b/runit/sv.c @@ -224,7 +224,7 @@ static void out(const char *p, const char *m1) { printf("%s%s%s: %s", p, *service, islog ? "/log" : "", m1); if (errno) { - printf(": %s", strerror(errno)); + printf(": "STRERROR_FMT STRERROR_ERRNO); } bb_putchar('\n'); /* will also flush the output */ } diff --git a/selinux/matchpathcon.c b/selinux/matchpathcon.c index e57120d3b..6945a0295 100644 --- a/selinux/matchpathcon.c +++ b/selinux/matchpathcon.c @@ -104,7 +104,7 @@ int matchpathcon_main(int argc UNUSED_PARAM, char **argv) freecon(con); 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); } matchpathcon_fini(); diff --git a/shell/ash.c b/shell/ash.c index 3fa7f4d9f..39705a350 100644 --- a/shell/ash.c +++ b/shell/ash.c @@ -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. */ -#ifdef HAVE_PRINTF_PERCENTM -#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 +#define ash_msg_and_raise_perror(fmt, ...) ash_msg_and_raise_error(fmt ": "STRERROR_FMT, ##__VA_ARGS__ STRERROR_ERRNO) static void raise_error_syntax(const char *) NORETURN; static void -- cgit v1.2.3