aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2022-01-18 00:31:27 +0100
committerDenys Vlasenko <vda.linux@googlemail.com>2022-01-18 00:36:42 +0100
commit1e825acf8d715fe49af040cb02f9e96c26955832 (patch)
tree1ba246aca6ab29b154dd14e61e96fafd6bab475e
parent8ad2acf352d790d0bdd792b8e126d58a088451f3 (diff)
downloadbusybox-w32-1e825acf8d715fe49af040cb02f9e96c26955832.tar.gz
busybox-w32-1e825acf8d715fe49af040cb02f9e96c26955832.tar.bz2
busybox-w32-1e825acf8d715fe49af040cb02f9e96c26955832.zip
libbb: shrink lineedit_read_key()
function old new delta lineedit_read_key 237 231 -6 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--archival/libarchive/decompress_bunzip2.c2
-rw-r--r--coreutils/head.c6
-rw-r--r--editors/patch.c2
-rw-r--r--editors/patch_toybox.c2
-rw-r--r--include/libbb.h2
-rw-r--r--libbb/lineedit.c26
-rw-r--r--libbb/read_key.c1
7 files changed, 25 insertions, 16 deletions
diff --git a/archival/libarchive/decompress_bunzip2.c b/archival/libarchive/decompress_bunzip2.c
index 42e2b4f88..4a2b668aa 100644
--- a/archival/libarchive/decompress_bunzip2.c
+++ b/archival/libarchive/decompress_bunzip2.c
@@ -654,7 +654,7 @@ static int read_bunzip(bunzip_data *bd, char *outbuf, int len)
654 /* Subtract the 1 copy we'd output anyway to get extras */ 654 /* Subtract the 1 copy we'd output anyway to get extras */
655 --bd->writeCopies; 655 --bd->writeCopies;
656 } 656 }
657 } /* for(;;) */ 657 } /* for (;;) */
658 658
659 /* Decompression of this input block completed successfully */ 659 /* Decompression of this input block completed successfully */
660 bd->writeCRC = CRC = ~CRC; 660 bd->writeCRC = CRC = ~CRC;
diff --git a/coreutils/head.c b/coreutils/head.c
index 9586f869f..c7537a20e 100644
--- a/coreutils/head.c
+++ b/coreutils/head.c
@@ -76,7 +76,7 @@ print_except_N_last_bytes(FILE *fp, unsigned count)
76{ 76{
77 unsigned char *circle = xmalloc(++count); 77 unsigned char *circle = xmalloc(++count);
78 unsigned head = 0; 78 unsigned head = 0;
79 for(;;) { 79 for (;;) {
80 int c; 80 int c;
81 c = getc(fp); 81 c = getc(fp);
82 if (c == EOF) 82 if (c == EOF)
@@ -105,7 +105,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
105{ 105{
106 char **circle = xzalloc((++count) * sizeof(circle[0])); 106 char **circle = xzalloc((++count) * sizeof(circle[0]));
107 unsigned head = 0; 107 unsigned head = 0;
108 for(;;) { 108 for (;;) {
109 char *c; 109 char *c;
110 c = xmalloc_fgets(fp); 110 c = xmalloc_fgets(fp);
111 if (!c) 111 if (!c)
@@ -127,7 +127,7 @@ print_except_N_last_lines(FILE *fp, unsigned count)
127 } 127 }
128 ret: 128 ret:
129 head = 0; 129 head = 0;
130 for(;;) { 130 for (;;) {
131 free(circle[head++]); 131 free(circle[head++]);
132 if (head == count) 132 if (head == count)
133 break; 133 break;
diff --git a/editors/patch.c b/editors/patch.c
index 110176630..aebb5073e 100644
--- a/editors/patch.c
+++ b/editors/patch.c
@@ -418,7 +418,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
418 } 418 }
419 419
420 // Loop through the lines in the patch 420 // Loop through the lines in the patch
421 for(;;) { 421 for (;;) {
422 char *patchline; 422 char *patchline;
423 423
424 patchline = xmalloc_fgetline(stdin); 424 patchline = xmalloc_fgetline(stdin);
diff --git a/editors/patch_toybox.c b/editors/patch_toybox.c
index aebab8132..69a508b2e 100644
--- a/editors/patch_toybox.c
+++ b/editors/patch_toybox.c
@@ -441,7 +441,7 @@ int patch_main(int argc UNUSED_PARAM, char **argv)
441 TT.filein = TT.fileout = -1; 441 TT.filein = TT.fileout = -1;
442 442
443 // Loop through the lines in the patch 443 // Loop through the lines in the patch
444 for(;;) { 444 for (;;) {
445 char *patchline; 445 char *patchline;
446 446
447 patchline = get_line(TT.filepatch); 447 patchline = get_line(TT.filepatch);
diff --git a/include/libbb.h b/include/libbb.h
index b45ce91c5..8e3b7ae8e 100644
--- a/include/libbb.h
+++ b/include/libbb.h
@@ -1900,6 +1900,8 @@ enum {
1900 * (unless fd is in non-blocking mode), 1900 * (unless fd is in non-blocking mode),
1901 * subsequent reads will time out after a few milliseconds. 1901 * subsequent reads will time out after a few milliseconds.
1902 * Return of -1 means EOF or error (errno == 0 on EOF). 1902 * Return of -1 means EOF or error (errno == 0 on EOF).
1903 * Nonzero errno is not preserved across the call:
1904 * if there was no error, errno will be cleared to 0.
1903 * buffer[0] is used as a counter of buffered chars and must be 0 1905 * buffer[0] is used as a counter of buffered chars and must be 0
1904 * on first call. 1906 * on first call.
1905 * timeout: 1907 * timeout:
diff --git a/libbb/lineedit.c b/libbb/lineedit.c
index f76afd37d..82624757e 100644
--- a/libbb/lineedit.c
+++ b/libbb/lineedit.c
@@ -2155,7 +2155,7 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
2155#endif 2155#endif
2156 2156
2157 fflush_all(); 2157 fflush_all();
2158 while (1) { 2158 for (;;) {
2159 /* Wait for input. TIMEOUT = -1 makes read_key wait even 2159 /* Wait for input. TIMEOUT = -1 makes read_key wait even
2160 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't 2160 * on nonblocking stdin, TIMEOUT = 50 makes sure we won't
2161 * insist on full MB_CUR_MAX buffer to declare input like 2161 * insist on full MB_CUR_MAX buffer to declare input like
@@ -2167,24 +2167,30 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
2167 * 2167 *
2168 * Note: read_key sets errno to 0 on success. 2168 * Note: read_key sets errno to 0 on success.
2169 */ 2169 */
2170 do { 2170 for (;;) {
2171 if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) { 2171 if ((state->flags & LI_INTERRUPTIBLE) && bb_got_signal) {
2172 errno = EINTR; 2172 errno = EINTR;
2173 return -1; 2173 return -1;
2174 } 2174 }
2175//FIXME: still races here with signals, but small window to poll() inside read_key 2175//FIXME: still races here with signals, but small window to poll() inside read_key
2176 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;) 2176 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 1;)
2177 /* errno = 0; - read_key does this itself */
2177 ic = read_key(STDIN_FILENO, read_key_buffer, timeout); 2178 ic = read_key(STDIN_FILENO, read_key_buffer, timeout);
2178 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;) 2179 IF_FEATURE_EDITING_WINCH(S.ok_to_redraw = 0;)
2179 } while (!(state->flags & LI_INTERRUPTIBLE) && errno == EINTR); 2180 if (errno != EINTR)
2181 break;
2182 if (state->flags & LI_INTERRUPTIBLE) {
2183 /* LI_INTERRUPTIBLE bails out on EINTR,
2184 * but nothing really guarantees that bb_got_signal
2185 * is nonzero. Follow the least surprise principle:
2186 */
2187 if (bb_got_signal == 0)
2188 bb_got_signal = 255;
2189 goto ret;
2190 }
2191 }
2180 2192
2181 if (errno) { 2193 if (errno) {
2182 /* LI_INTERRUPTIBLE can bail out with EINTR here,
2183 * but nothing really guarantees that bb_got_signal
2184 * is nonzero. Follow the least surprise principle:
2185 */
2186 if (errno == EINTR && bb_got_signal == 0)
2187 bb_got_signal = 255; /* something nonzero */
2188#if ENABLE_UNICODE_SUPPORT 2194#if ENABLE_UNICODE_SUPPORT
2189 if (errno == EAGAIN && unicode_idx != 0) 2195 if (errno == EAGAIN && unicode_idx != 0)
2190 goto pushback; 2196 goto pushback;
@@ -2251,7 +2257,7 @@ static int lineedit_read_key(char *read_key_buffer, int timeout)
2251#endif 2257#endif
2252 break; 2258 break;
2253 } 2259 }
2254 2260 ret:
2255 return ic; 2261 return ic;
2256} 2262}
2257 2263
diff --git a/libbb/read_key.c b/libbb/read_key.c
index 829ae215c..cf8ed411e 100644
--- a/libbb/read_key.c
+++ b/libbb/read_key.c
@@ -291,6 +291,7 @@ int64_t FAST_FUNC safe_read_key(int fd, char *buffer, int timeout)
291{ 291{
292 int64_t r; 292 int64_t r;
293 do { 293 do {
294 /* errno = 0; - read_key does this itself */
294 r = read_key(fd, buffer, timeout); 295 r = read_key(fd, buffer, timeout);
295 } while (errno == EINTR); 296 } while (errno == EINTR);
296 return r; 297 return r;