From 808d93c0eca49e0b22056e23d965f0d967433fbb Mon Sep 17 00:00:00 2001 From: Bernd Kuhls Date: Sun, 10 Jan 2021 13:20:44 +0100 Subject: update_passwd: fix context variable Commit https://git.busybox.net/busybox/commit/libbb/update_passwd.c?id=2496616b0a8d1c80cd1416b73a4847b59b9f969a changed the variable used from context to seuser but forgot this change resulting in build errors detected by buildroot autobuilders: http://autobuild.buildroot.net/results/b89/b89b7d0f0601bb706e76cea31cf4e43326e5540c//build-end.log libbb/update_passwd.c:51:11: error: 'context' undeclared (first use in this function); did you mean 'ucontext'? freecon(context); Signed-off-by: Bernd Kuhls Signed-off-by: Denys Vlasenko --- libbb/update_passwd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libbb') diff --git a/libbb/update_passwd.c b/libbb/update_passwd.c index 7b67f30cd..a228075cc 100644 --- a/libbb/update_passwd.c +++ b/libbb/update_passwd.c @@ -48,7 +48,7 @@ static void check_selinux_update_passwd(const char *username) bb_simple_error_msg_and_die("SELinux: access denied"); } if (ENABLE_FEATURE_CLEAN_UP) - freecon(context); + freecon(seuser); } #else # define check_selinux_update_passwd(username) ((void)0) -- cgit v1.2.3-55-g6feb From 59120c330330467c9e6aaec6d4ca8295baa653af Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 29 Jan 2021 13:22:48 +0000 Subject: libbb: code shrink and speed up find_applet_by_name() find_applet_by_name() determines the appropriate range of applet indices to check for the given name and performs a linear search in applet_names[]. Revise the code so the index of the upper bound of the range, 'max', isn't calculated. Instead check the value of the first non-matching character to see if we've reached the end of the range. This new code speeds up the time to find a valid applet name by 6% and halves the time to detect that a given name is invalid. The average time to detect an invalid name is now the same as for a valid one. function old new delta find_applet_by_name 155 133 -22 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-22) Total: -22 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/appletlib.c | 96 ++++++++++--------------------------------------------- 1 file changed, 17 insertions(+), 79 deletions(-) (limited to 'libbb') diff --git a/libbb/appletlib.c b/libbb/appletlib.c index 5f59f1273..542211255 100644 --- a/libbb/appletlib.c +++ b/libbb/appletlib.c @@ -176,7 +176,7 @@ void FAST_FUNC bb_show_usage(void) int FAST_FUNC find_applet_by_name(const char *name) { - unsigned i, max; + unsigned i; int j; const char *p; @@ -200,105 +200,43 @@ int FAST_FUNC find_applet_by_name(const char *name) #endif p = applet_names; - i = 0; #if KNOWN_APPNAME_OFFSETS <= 0 - max = NUM_APPLETS; + i = 0; #else - max = NUM_APPLETS * KNOWN_APPNAME_OFFSETS; + i = NUM_APPLETS * (KNOWN_APPNAME_OFFSETS - 1); for (j = ARRAY_SIZE(applet_nameofs)-1; j >= 0; j--) { const char *pp = applet_names + applet_nameofs[j]; if (strcmp(name, pp) >= 0) { //bb_error_msg("name:'%s' >= pp:'%s'", name, pp); p = pp; - i = max - NUM_APPLETS; break; } - max -= NUM_APPLETS; + i -= NUM_APPLETS; } - max /= (unsigned)KNOWN_APPNAME_OFFSETS; i /= (unsigned)KNOWN_APPNAME_OFFSETS; - //bb_error_msg("name:'%s' starting from:'%s' i:%u max:%u", name, p, i, max); + //bb_error_msg("name:'%s' starting from:'%s' i:%u", name, p, i); #endif /* Open-coded linear search without strcmp/strlen calls for speed */ - -#if 0 /*BB_UNALIGNED_MEMACCESS_OK && BB_LITTLE_ENDIAN*/ - /* skip "[\0" name, it's surely not it */ - if (ENABLE_TEST && LONE_CHAR(p, '[')) - i++, p += 2; - /* All remaining applet names in p[] are at least 2 chars long */ - /* name[] is also at least 2 chars long */ - - n32 = (name[0] << 0) | (name[1] << 8) | (name[2] << 16); - while (i < max) { - uint32_t p32; - char ch; - - /* Quickly check match of the first 3 bytes */ - move_from_unaligned32(p32, p); - p += 3; - if ((p32 & 0x00ffffff) != n32) { - /* Most likely case: 3 first bytes do not match */ - i++; - if ((p32 & 0x00ff0000) == '\0') - continue; // p[2] was NUL - p++; - if ((p32 & 0xff000000) == '\0') - continue; // p[3] was NUL - /* p[0..3] aren't matching and none is NUL, check the rest */ - while (*p++ != '\0') - continue; - continue; - } - - /* Unlikely branch: first 3 bytes ([0..2]) match */ - if ((p32 & 0x00ff0000) == '\0') { - /* name is 2-byte long, it is full match */ - //bb_error_msg("found:'%s' i:%u", name, i); - return i; - } - /* Check remaining bytes [3..NUL] */ - ch = (p32 >> 24); - j = 3; - while (ch == name[j]) { - if (ch == '\0') { - //bb_error_msg("found:'%s' i:%u", name, i); - return i; - } - ch = *++p; - j++; - } - /* Not a match. Skip it, including NUL */ - while (ch != '\0') - ch = *++p; - p++; - i++; - } - return -1; -#else - while (i < max) { - char ch; - j = 0; - /* Do we see "name\0" in applet_names[p] position? */ - while ((ch = *p) == name[j]) { - if (ch == '\0') { + while (*p) { + /* Do we see "name\0" at current position in applet_names? */ + for (j = 0; *p == name[j]; ++j) { + if (*p++ == '\0') { //bb_error_msg("found:'%s' i:%u", name, i); return i; /* yes */ } - p++; - j++; } - /* No. - * p => 1st non-matching char in applet_names[], - * skip to and including NUL. - */ - while (ch != '\0') - ch = *++p; - p++; + /* No. Have we gone too far, alphabetically? */ + if (*p > name[j]) { + //bb_error_msg("break:'%s' i:%u", name, i); + break; + } + /* No. Move to the start of the next applet name. */ + while (*p++ != '\0') + continue; i++; } return -1; -#endif } -- cgit v1.2.3-55-g6feb From bcf91d276047910538c5de0a45f3a4645e3ff471 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Fri, 29 Jan 2021 13:23:27 +0000 Subject: libbb: code shrink and speed up index_in_strings() Rewrite index_in_strings() to replace calls to strcmp()/strlen(). With this change searching for valid names in the applet_names array (for example) is 40% faster. The code has to assume the strings aren't sorted, so will always scan the entire array when presented with an invalid name. function old new delta index_in_strings 63 56 -7 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/1 up/down: 0/-7) Total: -7 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/compare_string_array.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'libbb') diff --git a/libbb/compare_string_array.c b/libbb/compare_string_array.c index a06e57d3d..d8cd033a3 100644 --- a/libbb/compare_string_array.c +++ b/libbb/compare_string_array.c @@ -63,13 +63,19 @@ int FAST_FUNC index_in_str_array(const char *const string_array[], const char *k int FAST_FUNC index_in_strings(const char *strings, const char *key) { - int idx = 0; + int j, idx = 0; while (*strings) { - if (strcmp(strings, key) == 0) { - return idx; + /* Do we see "key\0" at current position in strings? */ + for (j = 0; *strings == key[j]; ++j) { + if (*strings++ == '\0') { + //bb_error_msg("found:'%s' i:%u", key, idx); + return idx; /* yes */ + } } - strings += strlen(strings) + 1; /* skip NUL */ + /* No. Move to the start of the next string. */ + while (*strings++ != '\0') + continue; idx++; } return -1; -- cgit v1.2.3-55-g6feb From c849e72c0b216578f96b763529c5e58056d0662c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Thu, 21 Jan 2021 08:40:54 +0000 Subject: nl: ensure '-b n' option displays file content The command 'nl -b n' should output no line numbers, just some spaces as a placeholder followed by the actual file content. Add tests for line numbering by cat and nl. The correct results were obtained from coreutils. function old new delta print_numbered_lines 152 157 +5 .rodata 182456 182453 -3 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/1 up/down: 5/-3) Total: 2 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- coreutils/cat.c | 2 +- coreutils/nl.c | 2 +- libbb/print_numbered_lines.c | 3 ++- testsuite/cat.tests | 24 ++++++++++++++++++++++++ testsuite/nl.tests | 39 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100755 testsuite/nl.tests (limited to 'libbb') diff --git a/coreutils/cat.c b/coreutils/cat.c index 65f0648f9..dae6089bd 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c @@ -201,7 +201,7 @@ int cat_main(int argc UNUSED_PARAM, char **argv) ns.start = 1; ns.inc = 1; ns.sep = "\t"; - ns.empty_str = "\n"; + ns.empty_str = NULL; ns.all = !(opts & CAT_OPT_b); /* -n without -b */ ns.nonempty = (opts & CAT_OPT_b); /* -b (with or without -n) */ exitcode = EXIT_SUCCESS; diff --git a/coreutils/nl.c b/coreutils/nl.c index 800b73c26..d06673881 100644 --- a/coreutils/nl.c +++ b/coreutils/nl.c @@ -68,7 +68,7 @@ int nl_main(int argc UNUSED_PARAM, char **argv) &ns.width, &ns.sep, &ns.start, &ns.inc, &opt_b); ns.all = (opt_b[0] == 'a'); ns.nonempty = (opt_b[0] == 't'); - ns.empty_str = xasprintf("%*s\n", ns.width + (int)strlen(ns.sep), ""); + ns.empty_str = xasprintf("%*s", ns.width + (int)strlen(ns.sep), ""); argv += optind; if (!*argv) diff --git a/libbb/print_numbered_lines.c b/libbb/print_numbered_lines.c index d6459d7c3..4758068a4 100644 --- a/libbb/print_numbered_lines.c +++ b/libbb/print_numbered_lines.c @@ -22,10 +22,11 @@ int FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename if (ns->all || (ns->nonempty && line[0]) ) { - printf("%*u%s%s\n", ns->width, N, ns->sep, line); + printf("%*u%s", ns->width, N, ns->sep); N += ns->inc; } else if (ns->empty_str) fputs(ns->empty_str, stdout); + puts(line); free(line); } ns->start = N; diff --git a/testsuite/cat.tests b/testsuite/cat.tests index 10970dc90..cf924ab5b 100755 --- a/testsuite/cat.tests +++ b/testsuite/cat.tests @@ -22,4 +22,28 @@ testing 'cat -v' \ 'foo\n' SKIP= +optional FEATURE_CATN +testing 'cat -n' \ + 'cat -n' \ +"\ + 1 line 1 + 2 + 3 line 3 +" \ + '' \ + 'line 1\n\nline 3\n' +SKIP= + +optional FEATURE_CATN +testing 'cat -b' \ + 'cat -b' \ +"\ + 1 line 1 + + 2 line 3 +" \ + '' \ + 'line 1\n\nline 3\n' +SKIP= + exit $FAILCOUNT diff --git a/testsuite/nl.tests b/testsuite/nl.tests new file mode 100755 index 000000000..95e7abb58 --- /dev/null +++ b/testsuite/nl.tests @@ -0,0 +1,39 @@ +#!/bin/sh +# Copyright 2021 by Ron Yorston +# Licensed under GPLv2, see file LICENSE in this source tree. + +. ./testing.sh + +# testing "test name" "commands" "expected result" "file input" "stdin" + +testing "nl numbers all lines" \ + "nl -b a input" \ +"\ + 1 line 1 + 2 + 3 line 3 +" \ + "line 1\n\nline 3\n" \ + "" + +testing "nl numbers non-empty lines" \ + "nl -b t input" \ +"\ + 1 line 1 + + 2 line 3 +" \ + "line 1\n\nline 3\n" \ + "" + +testing "nl numbers no lines" \ + "nl -b n input" \ +"\ + line 1 + + line 3 +" \ + "line 1\n\nline 3\n" \ + "" + +exit $FAILCOUNT -- cgit v1.2.3-55-g6feb From 6ad38d66d08a2646c6423b967a790656ae026c95 Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Sat, 23 Jan 2021 13:22:33 +0000 Subject: libbb: code shrink fgets_str Use a NULL value of maxsz_p to indicate to xmalloc_fgets_internal() that the caller doesn't care about the maximum size of the buffer. This allows the default maximum size to be set once in xmalloc_fgets_internal() instead of separately in each caller. function old new delta xmalloc_fgets_internal 273 287 +14 xmalloc_fgets_str 30 9 -21 xmalloc_fgetline_str 33 12 -21 xmalloc_fgets_str_len 38 10 -28 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 1/3 up/down: 14/-70) Total: -56 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- libbb/fgets_str.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'libbb') diff --git a/libbb/fgets_str.c b/libbb/fgets_str.c index 1a7f2e9e0..c884ef8af 100644 --- a/libbb/fgets_str.c +++ b/libbb/fgets_str.c @@ -17,7 +17,7 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, int linebufsz = 0; int idx = 0; int ch; - size_t maxsz = *maxsz_p; + size_t maxsz = maxsz_p ? *maxsz_p : INT_MAX - 4095; while (1) { ch = fgetc(file); @@ -53,7 +53,8 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, /* Grow/shrink *first*, then store NUL */ linebuf = xrealloc(linebuf, idx + 1); linebuf[idx] = '\0'; - *maxsz_p = idx; + if (maxsz_p) + *maxsz_p = idx; return linebuf; } @@ -63,23 +64,15 @@ static char *xmalloc_fgets_internal(FILE *file, const char *terminating_string, * Return NULL if EOF is reached immediately. */ char* FAST_FUNC xmalloc_fgets_str(FILE *file, const char *terminating_string) { - size_t maxsz = INT_MAX - 4095; - return xmalloc_fgets_internal(file, terminating_string, 0, &maxsz); + return xmalloc_fgets_internal(file, terminating_string, 0, NULL); } char* FAST_FUNC xmalloc_fgets_str_len(FILE *file, const char *terminating_string, size_t *maxsz_p) { - size_t maxsz; - - if (!maxsz_p) { - maxsz = INT_MAX - 4095; - maxsz_p = &maxsz; - } return xmalloc_fgets_internal(file, terminating_string, 0, maxsz_p); } char* FAST_FUNC xmalloc_fgetline_str(FILE *file, const char *terminating_string) { - size_t maxsz = INT_MAX - 4095; - return xmalloc_fgets_internal(file, terminating_string, 1, &maxsz); + return xmalloc_fgets_internal(file, terminating_string, 1, NULL); } -- cgit v1.2.3-55-g6feb From cad3fc743aa7c7744e4fcf044371f0fda50fa51f Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 3 Feb 2021 20:47:14 +0100 Subject: libbb: introduce and use fputs_stdout function old new delta fputs_stdout - 12 +12 zxc_vm_process 7237 7230 -7 yes_main 85 78 -7 write_block 380 373 -7 wrapf 305 298 -7 strings_main 437 430 -7 show_bridge 353 346 -7 rev_main 384 377 -7 put_prompt_custom 58 51 -7 put_cur_glyph_and_inc_cursor 168 161 -7 print_numbered_lines 152 145 -7 print_named_ascii 130 123 -7 print_name 135 128 -7 print_login_issue 386 379 -7 print_ascii 208 201 -7 powertop_main 1249 1242 -7 od_main 1789 1782 -7 logread_main 518 511 -7 head_main 804 797 -7 display_process_list 1319 1312 -7 cut_main 1002 995 -7 bb_dump_dump 1550 1543 -7 bb_ask_noecho 393 386 -7 baseNUM_main 702 695 -7 expand_main 755 745 -10 dumpleases_main 497 487 -10 write1 12 - -12 putcsi 37 23 -14 print_login_prompt 55 41 -14 paste_main 525 511 -14 cat_main 440 426 -14 print_it 245 230 -15 print_addrinfo 1188 1171 -17 print_rule 770 750 -20 print_linkinfo 842 822 -20 httpd_main 791 771 -20 ------------------------------------------------------------------------------ (add/remove: 1/1 grow/shrink: 0/34 up/down: 12/-341) Total: -329 bytes Signed-off-by: Ron Yorston Signed-off-by: Denys Vlasenko --- applets/individual.c | 2 +- coreutils/cat.c | 2 +- coreutils/cut.c | 2 +- coreutils/expand.c | 2 +- coreutils/head.c | 2 +- coreutils/id.c | 2 +- coreutils/ls.c | 2 +- coreutils/od_bloaty.c | 26 +++++++++++++------------- coreutils/paste.c | 4 ++-- coreutils/stat.c | 2 +- coreutils/stty.c | 2 +- coreutils/uudecode.c | 2 +- coreutils/yes.c | 2 +- editors/ed.c | 2 +- editors/vi.c | 2 +- include/libbb.h | 1 + libbb/bb_askpass.c | 2 +- libbb/dump.c | 2 +- libbb/lineedit.c | 8 ++++---- libbb/login.c | 6 +++--- libbb/print_numbered_lines.c | 2 +- libbb/xfuncs_printf.c | 5 +++++ miscutils/bc.c | 2 +- miscutils/conspy.c | 4 ++-- miscutils/strings.c | 2 +- networking/brctl.c | 2 +- networking/httpd.c | 4 ++-- networking/libiproute/ipaddress.c | 14 ++++++-------- networking/libiproute/iprule.c | 7 +++---- networking/udhcp/dumpleases.c | 2 +- procps/powertop.c | 2 +- procps/top.c | 2 +- shell/hush.c | 2 +- sysklogd/logread.c | 4 ++-- util-linux/fdisk_gpt.c | 4 ++-- util-linux/rev.c | 2 +- 36 files changed, 69 insertions(+), 66 deletions(-) (limited to 'libbb') diff --git a/applets/individual.c b/applets/individual.c index 4c468df06..e94f26c93 100644 --- a/applets/individual.c +++ b/applets/individual.c @@ -19,6 +19,6 @@ int main(int argc, char **argv) void bb_show_usage(void) { - fputs(APPLET_full_usage "\n", stdout); + fputs_stdout(APPLET_full_usage "\n"); exit(EXIT_FAILURE); } diff --git a/coreutils/cat.c b/coreutils/cat.c index dae6089bd..4b3414941 100644 --- a/coreutils/cat.c +++ b/coreutils/cat.c @@ -152,7 +152,7 @@ static int catv(unsigned opts, char **argv) eol_seen = (c == eol_char); #endif visible(c, buf, opts); - fputs(buf, stdout); + fputs_stdout(buf); } } if (ENABLE_FEATURE_CLEAN_UP && fd) diff --git a/coreutils/cut.c b/coreutils/cut.c index 1acbb513e..16418ff33 100644 --- a/coreutils/cut.c +++ b/coreutils/cut.c @@ -167,7 +167,7 @@ static void cut_file(FILE *file, char delim, const struct cut_list *cut_lists, u * printed */ if (nfields_printed > 0) putchar(delim); - fputs(field, stdout); + fputs_stdout(field); printed[ndelim] = 'X'; nfields_printed++; /* shouldn't overflow.. */ } diff --git a/coreutils/expand.c b/coreutils/expand.c index 5f5993921..c5e1de6f5 100644 --- a/coreutils/expand.c +++ b/coreutils/expand.c @@ -123,7 +123,7 @@ static void expand(FILE *file, unsigned tab_size, unsigned opt) } ptr++; } - fputs(ptr_strbeg, stdout); + fputs_stdout(ptr_strbeg); free(line); } } diff --git a/coreutils/head.c b/coreutils/head.c index 1219dfe8b..b6efabbe0 100644 --- a/coreutils/head.c +++ b/coreutils/head.c @@ -117,7 +117,7 @@ print_except_N_last_lines(FILE *fp, unsigned count) char *c; if (head == count) head = 0; - fputs(circle[head], stdout); + fputs_stdout(circle[head]); c = xmalloc_fgets(fp); if (!c) goto ret; diff --git a/coreutils/id.c b/coreutils/id.c index 78d5f2a50..f453a87ae 100644 --- a/coreutils/id.c +++ b/coreutils/id.c @@ -258,7 +258,7 @@ int id_main(int argc UNUSED_PARAM, char **argv) bb_error_msg_and_die("can't get process context%s", username ? " for a different user" : ""); } - fputs(scontext, stdout); + fputs_stdout(scontext); } /* freecon(NULL) seems to be harmless */ if (ENABLE_FEATURE_CLEAN_UP) diff --git a/coreutils/ls.c b/coreutils/ls.c index e5375a61a..80ef92079 100644 --- a/coreutils/ls.c +++ b/coreutils/ls.c @@ -453,7 +453,7 @@ static unsigned print_name(const char *name) name = printable_string2(&uni_stat, name); if (!(option_mask32 & OPT_Q)) { - fputs(name, stdout); + fputs_stdout(name); return uni_stat.unicode_width; } diff --git a/coreutils/od_bloaty.c b/coreutils/od_bloaty.c index d5bd7bfe8..5b5e56a21 100644 --- a/coreutils/od_bloaty.c +++ b/coreutils/od_bloaty.c @@ -422,19 +422,19 @@ print_named_ascii(size_t n_bytes, const char *block, masked_c &= 0x7f; if (masked_c == 0x7f) { - fputs(" del", stdout); + fputs_stdout(" del"); continue; } if (masked_c > ' ') { buf[3] = masked_c; - fputs(buf, stdout); + fputs_stdout(buf); continue; } /* Why? Because printf(" %3.3s") is much slower... */ buf[6] = charname[masked_c][0]; buf[7] = charname[masked_c][1]; buf[8] = charname[masked_c][2]; - fputs(buf+5, stdout); + fputs_stdout(buf+5); } } @@ -451,7 +451,7 @@ print_ascii(size_t n_bytes, const char *block, if (ISPRINT(c)) { buf[3] = c; - fputs(buf, stdout); + fputs_stdout(buf); continue; } switch (c) { @@ -485,7 +485,7 @@ print_ascii(size_t n_bytes, const char *block, buf[8] = (c & 7) + '0'; s = buf + 5; } - fputs(s, stdout); + fputs_stdout(s); } } @@ -881,7 +881,7 @@ format_address_label(off_t address, char c) static void dump_hexl_mode_trailer(size_t n_bytes, const char *block) { - fputs(" >", stdout); + fputs_stdout(" >"); while (n_bytes--) { unsigned c = *(unsigned char *) block++; c = (ISPRINT(c) ? c : '.'); @@ -1121,13 +1121,13 @@ dump_strings(off_t address, off_t end_offset) for (i = 0; (c = buf[i]); i++) { switch (c) { - case '\007': fputs("\\a", stdout); break; - case '\b': fputs("\\b", stdout); break; - case '\f': fputs("\\f", stdout); break; - case '\n': fputs("\\n", stdout); break; - case '\r': fputs("\\r", stdout); break; - case '\t': fputs("\\t", stdout); break; - case '\v': fputs("\\v", stdout); break; + case '\007': fputs_stdout("\\a"); break; + case '\b': fputs_stdout("\\b"); break; + case '\f': fputs_stdout("\\f"); break; + case '\n': fputs_stdout("\\n"); break; + case '\r': fputs_stdout("\\r"); break; + case '\t': fputs_stdout("\\t"); break; + case '\v': fputs_stdout("\\v"); break; default: putchar(c); } } diff --git a/coreutils/paste.c b/coreutils/paste.c index fd2aa5027..7def4de11 100644 --- a/coreutils/paste.c +++ b/coreutils/paste.c @@ -53,7 +53,7 @@ static void paste_files(FILE** files, int file_cnt, char* delims, int del_cnt) --active_files; continue; } - fputs(line, stdout); + fputs_stdout(line); free(line); delim = '\n'; if (i != file_cnt - 1) { @@ -79,7 +79,7 @@ static void paste_files_separate(FILE** files, char* delims, int del_cnt) line = NULL; while ((next_line = xmalloc_fgetline(files[i])) != NULL) { if (line) { - fputs(line, stdout); + fputs_stdout(line); free(line); delim = delims[del_idx++]; if (del_idx == del_cnt) diff --git a/coreutils/stat.c b/coreutils/stat.c index 073b2c67b..a8393468e 100644 --- a/coreutils/stat.c +++ b/coreutils/stat.c @@ -439,7 +439,7 @@ static void print_it(const char *masterformat, /* print preceding string */ *p = '\0'; - fputs(b, stdout); + fputs_stdout(b); p += len; b = p + 1; diff --git a/coreutils/stty.c b/coreutils/stty.c index 19253964c..ba2b78317 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c @@ -855,7 +855,7 @@ static void wrapf(const char *message, ...) } } } - fputs(buf, stdout); + fputs_stdout(buf); G.current_col += buflen; if (buf[buflen-1] == '\n') G.current_col = 0; diff --git a/coreutils/uudecode.c b/coreutils/uudecode.c index 164b208ea..02b037276 100644 --- a/coreutils/uudecode.c +++ b/coreutils/uudecode.c @@ -319,7 +319,7 @@ int baseNUM_main(int argc UNUSED_PARAM, char **argv) } if (col == 0) { - fputs(dst_buf, stdout); + fputs_stdout(dst_buf); } else { char *result = dst_buf; if (rem == 0) diff --git a/coreutils/yes.c b/coreutils/yes.c index 9a435a761..0ad25926f 100644 --- a/coreutils/yes.c +++ b/coreutils/yes.c @@ -43,7 +43,7 @@ int yes_main(int argc UNUSED_PARAM, char **argv) do { pp = argv; while (1) { - fputs(*pp, stdout); + fputs_stdout(*pp); if (!*++pp) break; putchar(' '); diff --git a/editors/ed.c b/editors/ed.c index d3ae8da92..c50faeefa 100644 --- a/editors/ed.c +++ b/editors/ed.c @@ -553,7 +553,7 @@ static int printLines(int num1, int num2, int expandFlag) fputc_printable(ch | PRINTABLE_META, stdout); } - fputs("$\n", stdout); + fputs_stdout("$\n"); setCurNum(num1++); lp = lp->next; diff --git a/editors/vi.c b/editors/vi.c index adfb2b87c..458ca6293 100644 --- a/editors/vi.c +++ b/editors/vi.c @@ -522,7 +522,7 @@ static void show_help(void) static void write1(const char *out) { - fputs(out, stdout); + fputs_stdout(out); } #if ENABLE_FEATURE_VI_WIN_RESIZE diff --git a/include/libbb.h b/include/libbb.h index cb6336474..135d9111d 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -874,6 +874,7 @@ char *xmalloc_substitute_string(const char *src, int count, const char *sub, con int bb_putchar(int ch) FAST_FUNC; /* Note: does not use stdio, writes to fd 2 directly */ int bb_putchar_stderr(char ch) FAST_FUNC; +int fputs_stdout(const char *s) FAST_FUNC; char *xasprintf(const char *format, ...) __attribute__ ((format(printf, 1, 2))) FAST_FUNC RETURNS_MALLOC; char *auto_string(char *str) FAST_FUNC; // gcc-4.1.1 still isn't good enough at optimizing it diff --git a/libbb/bb_askpass.c b/libbb/bb_askpass.c index 2dcead35a..66d6a479e 100644 --- a/libbb/bb_askpass.c +++ b/libbb/bb_askpass.c @@ -25,7 +25,7 @@ char* FAST_FUNC bb_ask_noecho(int fd, int timeout, const char *prompt) /* Was buggy: was printing prompt *before* flushing input, * which was upsetting "expect" based scripts of some users. */ - fputs(prompt, stdout); + fputs_stdout(prompt); fflush_all(); tcgetattr(fd, &oldtio); diff --git a/libbb/dump.c b/libbb/dump.c index 1ba1132b3..fb7849e7d 100644 --- a/libbb/dump.c +++ b/libbb/dump.c @@ -560,7 +560,7 @@ static void display(priv_dumper_t* dumper) ) { if (dumper->pub.eofstring) { /* xxd support: requested to not pad incomplete blocks */ - fputs(dumper->pub.eofstring, stdout); + fputs_stdout(dumper->pub.eofstring); return; } if (!(pr->flags & (F_TEXT | F_BPAD))) diff --git a/libbb/lineedit.c b/libbb/lineedit.c index 75de88e77..367396b91 100644 --- a/libbb/lineedit.c +++ b/libbb/lineedit.c @@ -312,7 +312,7 @@ static void BB_PUTCHAR(wchar_t c) ssize_t len = wcrtomb(buf, c, &mbst); if (len > 0) { buf[len] = '\0'; - fputs(buf, stdout); + fputs_stdout(buf); } } else { /* In this case, c is always one byte */ @@ -460,7 +460,7 @@ static void beep(void) */ static void put_prompt_custom(bool is_full) { - fputs((is_full ? cmdedit_prompt : prompt_last_line), stdout); + fputs_stdout((is_full ? cmdedit_prompt : prompt_last_line)); cursor = 0; cmdedit_y = cmdedit_prmt_len / cmdedit_termw; /* new quasireal y */ cmdedit_x = cmdedit_prmt_len % cmdedit_termw; @@ -1851,7 +1851,7 @@ static void ask_terminal(void) pfd.events = POLLIN; if (safe_poll(&pfd, 1, 0) == 0) { S.sent_ESC_br6n = 1; - fputs(ESC"[6n", stdout); + fputs_stdout(ESC"[6n"); fflush_all(); /* make terminal see it ASAP! */ } } @@ -2957,7 +2957,7 @@ int FAST_FUNC read_line_input(line_input_t *st, const char *prompt, char *comman #undef read_line_input int FAST_FUNC read_line_input(const char* prompt, char* command, int maxsize) { - fputs(prompt, stdout); + fputs_stdout(prompt); fflush_all(); if (!fgets(command, maxsize, stdin)) return -1; diff --git a/libbb/login.c b/libbb/login.c index 7f593d80d..af860c277 100644 --- a/libbb/login.c +++ b/libbb/login.c @@ -120,7 +120,7 @@ void FAST_FUNC print_login_issue(const char *issue_file, const char *tty) buf[0] = c; } } - fputs(outbuf, stdout); + fputs_stdout(outbuf); } fclose(fp); fflush_all(); @@ -130,8 +130,8 @@ void FAST_FUNC print_login_prompt(void) { char *hostname = safe_gethostname(); - fputs(hostname, stdout); - fputs(LOGIN, stdout); + fputs_stdout(hostname); + fputs_stdout(LOGIN); fflush_all(); free(hostname); } diff --git a/libbb/print_numbered_lines.c b/libbb/print_numbered_lines.c index 4758068a4..b64f85597 100644 --- a/libbb/print_numbered_lines.c +++ b/libbb/print_numbered_lines.c @@ -25,7 +25,7 @@ int FAST_FUNC print_numbered_lines(struct number_state *ns, const char *filename printf("%*u%s", ns->width, N, ns->sep); N += ns->inc; } else if (ns->empty_str) - fputs(ns->empty_str, stdout); + fputs_stdout(ns->empty_str); puts(line); free(line); } diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 99596b9d0..f0399ca45 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -318,6 +318,11 @@ int FAST_FUNC bb_putchar(int ch) return putchar(ch); } +int FAST_FUNC fputs_stdout(const char *s) +{ + return fputs(s, stdout); +} + /* Die with an error message if we can't copy an entire FILE* to stdout, * then close that file. */ void FAST_FUNC xprint_and_close_file(FILE *file) diff --git a/miscutils/bc.c b/miscutils/bc.c index 53eb5c799..02a61ac49 100644 --- a/miscutils/bc.c +++ b/miscutils/bc.c @@ -6448,7 +6448,7 @@ static BC_STATUS zdc_program_printStream(void) char *str; idx = (r->t == XC_RESULT_STR) ? r->d.id.idx : n->rdx; str = *xc_program_str(idx); - fputs(str, stdout); + fputs_stdout(str); } RETURN_STATUS(s); diff --git a/miscutils/conspy.c b/miscutils/conspy.c index fac11d339..21a498d0f 100644 --- a/miscutils/conspy.c +++ b/miscutils/conspy.c @@ -107,8 +107,8 @@ enum { static void putcsi(const char *s) { - fputs(ESC"[", stdout); - fputs(s, stdout); + fputs_stdout(ESC"["); + fputs_stdout(s); } static void clrscr(void) diff --git a/miscutils/strings.c b/miscutils/strings.c index 51412f401..e4a68227e 100644 --- a/miscutils/strings.c +++ b/miscutils/strings.c @@ -92,7 +92,7 @@ int strings_main(int argc UNUSED_PARAM, char **argv) if (option_mask32 & (PRINT_OFFSET | PRINT_RADIX)) { printf(radix_fmt, offset - n); } - fputs(string, stdout); + fputs_stdout(string); } count++; } diff --git a/networking/brctl.c b/networking/brctl.c index f057f9b60..e1f3e6445 100644 --- a/networking/brctl.c +++ b/networking/brctl.c @@ -156,7 +156,7 @@ static int show_bridge(const char *name, int need_hdr) else if (LONE_CHAR(filedata, '1')) strcpy(filedata, "yes"); - fputs(filedata, stdout); + fputs_stdout(filedata); /* sfx points past "BR/bridge/", turn it into "BR/brif": */ sfx[-4] = 'f'; sfx[-3] = '\0'; diff --git a/networking/httpd.c b/networking/httpd.c index 3cad28921..e6757d943 100644 --- a/networking/httpd.c +++ b/networking/httpd.c @@ -2788,12 +2788,12 @@ int httpd_main(int argc UNUSED_PARAM, char **argv) , &verbose ); if (opt & OPT_DECODE_URL) { - fputs(percent_decode_in_place(url_for_decode, /*strict:*/ 0), stdout); + fputs_stdout(percent_decode_in_place(url_for_decode, /*strict:*/ 0)); return 0; } #if ENABLE_FEATURE_HTTPD_ENCODE_URL_STR if (opt & OPT_ENCODE_URL) { - fputs(encodeString(url_for_encode), stdout); + fputs_stdout(encodeString(url_for_encode)); return 0; } #endif diff --git a/networking/libiproute/ipaddress.c b/networking/libiproute/ipaddress.c index dfd84c000..17a838411 100644 --- a/networking/libiproute/ipaddress.c +++ b/networking/libiproute/ipaddress.c @@ -192,20 +192,20 @@ static NOINLINE int print_linkinfo(const struct nlmsghdr *n) printf("%c link/%s ", _SL_, ll_type_n2a(ifi->ifi_type, b1)); if (tb[IFLA_ADDRESS]) { - fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), + fputs_stdout(ll_addr_n2a(RTA_DATA(tb[IFLA_ADDRESS]), RTA_PAYLOAD(tb[IFLA_ADDRESS]), ifi->ifi_type, - b1, sizeof(b1)), stdout); + b1, sizeof(b1))); } if (tb[IFLA_BROADCAST]) { if (ifi->ifi_flags & IFF_POINTOPOINT) printf(" peer "); else printf(" brd "); - fputs(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), + fputs_stdout(ll_addr_n2a(RTA_DATA(tb[IFLA_BROADCAST]), RTA_PAYLOAD(tb[IFLA_BROADCAST]), ifi->ifi_type, - b1, sizeof(b1)), stdout); + b1, sizeof(b1))); } } bb_putchar('\n'); @@ -307,9 +307,7 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM, printf(" family %d ", ifa->ifa_family); if (rta_tb[IFA_LOCAL]) { - fputs(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL])), - stdout - ); + fputs_stdout(rt_addr_n2a(ifa->ifa_family, RTA_DATA(rta_tb[IFA_LOCAL]))); if (rta_tb[IFA_ADDRESS] == NULL || memcmp(RTA_DATA(rta_tb[IFA_ADDRESS]), RTA_DATA(rta_tb[IFA_LOCAL]), 4) == 0 @@ -363,7 +361,7 @@ static int FAST_FUNC print_addrinfo(const struct sockaddr_nl *who UNUSED_PARAM, if (ifa_flags) printf("flags %02x ", ifa_flags); if (rta_tb[IFA_LABEL]) - fputs((char*)RTA_DATA(rta_tb[IFA_LABEL]), stdout); + fputs_stdout((char*)RTA_DATA(rta_tb[IFA_LABEL])); if (rta_tb[IFA_CACHEINFO]) { struct ifa_cacheinfo *ci = RTA_DATA(rta_tb[IFA_CACHEINFO]); char buf[128]; diff --git a/networking/libiproute/iprule.c b/networking/libiproute/iprule.c index 50acfe4e7..a23a00d87 100644 --- a/networking/libiproute/iprule.c +++ b/networking/libiproute/iprule.c @@ -88,10 +88,9 @@ static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM, r->rtm_src_len ); } else { - fputs(format_host(r->rtm_family, + fputs_stdout(format_host(r->rtm_family, RTA_PAYLOAD(tb[RTA_SRC]), - RTA_DATA(tb[RTA_SRC])), - stdout + RTA_DATA(tb[RTA_SRC])) ); } } else if (r->rtm_src_len) { @@ -178,7 +177,7 @@ static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM, } else printf("masquerade"); } else if (r->rtm_type != RTN_UNICAST) - fputs(rtnl_rtntype_n2a(r->rtm_type), stdout); + fputs_stdout(rtnl_rtntype_n2a(r->rtm_type)); bb_putchar('\n'); /*fflush_all();*/ diff --git a/networking/udhcp/dumpleases.c b/networking/udhcp/dumpleases.c index 1e9405205..19606c58e 100644 --- a/networking/udhcp/dumpleases.c +++ b/networking/udhcp/dumpleases.c @@ -112,7 +112,7 @@ int dumpleases_main(int argc UNUSED_PARAM, char **argv) printf("%02u:%02u:%02u\n", h, m, (unsigned)expires); } else { /* -a */ time_t t = expires_abs; - fputs(ctime(&t), stdout); + fputs_stdout(ctime(&t)); } } /* close(fd); */ diff --git a/procps/powertop.c b/procps/powertop.c index d508b5f78..fc6018b7a 100644 --- a/procps/powertop.c +++ b/procps/powertop.c @@ -818,7 +818,7 @@ int powertop_main(int argc UNUSED_PARAM, char UNUSED_PARAM **argv) for (i = 0; i < MAX_CSTATE_COUNT + 2; i++) if (cstate_lines[i][0]) - fputs(cstate_lines[i], stdout); + fputs_stdout(cstate_lines[i]); i = process_timer_stats(); #if ENABLE_FEATURE_POWERTOP_PROCIRQ diff --git a/procps/top.c b/procps/top.c index 8d39526ff..cadc4ecec 100644 --- a/procps/top.c +++ b/procps/top.c @@ -712,7 +712,7 @@ static NOINLINE void display_process_list(int lines_rem, int scr_width) ); if ((int)(scr_width - col) > 1) read_cmdline(line_buf + col, scr_width - col, s->pid, s->comm); - fputs(line_buf, stdout); + fputs_stdout(line_buf); /* printf(" %d/%d %lld/%lld", s->pcpu, total_pcpu, cur_jif.busy - prev_jif.busy, cur_jif.total - prev_jif.total); */ s++; diff --git a/shell/hush.c b/shell/hush.c index 77f90f82f..5eb6fa396 100644 --- a/shell/hush.c +++ b/shell/hush.c @@ -2692,7 +2692,7 @@ static int get_user_input(struct in_str *i) * Without check_and_run_traps, handler never runs. */ check_and_run_traps(); - fputs(prompt_str, stdout); + fputs_stdout(prompt_str); fflush_all(); } r = hfgetc(i->file); diff --git a/sysklogd/logread.c b/sysklogd/logread.c index b52dc9cac..d5f8ca0a2 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -205,7 +205,7 @@ int logread_main(int argc UNUSED_PARAM, char **argv) cur = shbuf_tail; #else while (cur != shbuf_tail) { - fputs(shbuf_data + cur, stdout); + fputs_stdout(shbuf_data + cur); cur += strlen(shbuf_data + cur) + 1; if (cur >= shbuf_size) cur = 0; @@ -217,7 +217,7 @@ int logread_main(int argc UNUSED_PARAM, char **argv) #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING for (i = 0; i < len_total; i += strlen(copy + i) + 1) { - fputs(copy + i, stdout); + fputs_stdout(copy + i); } free(copy); #endif diff --git a/util-linux/fdisk_gpt.c b/util-linux/fdisk_gpt.c index e884e3dc1..4c30f31f8 100644 --- a/util-linux/fdisk_gpt.c +++ b/util-linux/fdisk_gpt.c @@ -87,7 +87,7 @@ gpt_print_wide36(uint16_t *s) } wc[i] = 0; if (wcstombs(buf, wc, sizeof(buf)) <= sizeof(buf)-1) - fputs(printable_string(buf), stdout); + fputs_stdout(printable_string(buf)); #else char buf[37]; int i = 0; @@ -98,7 +98,7 @@ gpt_print_wide36(uint16_t *s) i++; } buf[i] = '\0'; - fputs(buf, stdout); + fputs_stdout(buf); #endif } diff --git a/util-linux/rev.c b/util-linux/rev.c index ede4fbc9c..d439b4da8 100644 --- a/util-linux/rev.c +++ b/util-linux/rev.c @@ -108,7 +108,7 @@ int rev_main(int argc UNUSED_PARAM, char **argv) #else strrev(buf, strlen(buf)); #endif - fputs(buf, stdout); + fputs_stdout(buf); } fclose(fp); } while (*argv); -- cgit v1.2.3-55-g6feb