diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2011-02-07 02:03:51 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2011-02-07 02:03:51 +0100 |
commit | 8ee2adab21328761b80e0cbc513eda7eaa880b24 (patch) | |
tree | c771696ab3b1be735e831b1721ab751c874e1cd3 /coreutils/echo.c | |
parent | b8ab4b038803df195eee9844c3597dd640c00393 (diff) | |
download | busybox-w32-8ee2adab21328761b80e0cbc513eda7eaa880b24.tar.gz busybox-w32-8ee2adab21328761b80e0cbc513eda7eaa880b24.tar.bz2 busybox-w32-8ee2adab21328761b80e0cbc513eda7eaa880b24.zip |
echo: do not retry on write errors
function old new delta
echo_main 297 336 +39
stpcpy - 22 +22
run_pipe 1561 1566 +5
pseudo_exec_argv 187 192 +5
hush_exit 75 80 +5
------------------------------------------------------------------------------
(add/remove: 3/0 grow/shrink: 4/0 up/down: 98/0) Total: 76 bytes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'coreutils/echo.c')
-rw-r--r-- | coreutils/echo.c | 87 |
1 files changed, 48 insertions, 39 deletions
diff --git a/coreutils/echo.c b/coreutils/echo.c index 3821e594e..5fa3d10c5 100644 --- a/coreutils/echo.c +++ b/coreutils/echo.c | |||
@@ -29,46 +29,42 @@ | |||
29 | 29 | ||
30 | /* NB: can be used by shell even if not enabled as applet */ | 30 | /* NB: can be used by shell even if not enabled as applet */ |
31 | 31 | ||
32 | /* | ||
33 | * NB2: we don't use stdio, we need better error handing. | ||
34 | * Examples include writing into non-opened stdout and error on write. | ||
35 | * | ||
36 | * With stdio, output gets shoveled into stdout buffer, and even | ||
37 | * fflush cannot clear it out. It seems that even if libc receives | ||
38 | * EBADF on write attempts, it feels determined to output data no matter what. | ||
39 | * If echo is called by shell, it will try writing again later, and possibly | ||
40 | * will clobber future output. Not good. | ||
41 | * | ||
42 | * Solaris has fpurge which discards buffered input. glibc has __fpurge. | ||
43 | * But this function is not standard. | ||
44 | */ | ||
45 | |||
32 | int echo_main(int argc UNUSED_PARAM, char **argv) | 46 | int echo_main(int argc UNUSED_PARAM, char **argv) |
33 | { | 47 | { |
48 | char **pp; | ||
34 | const char *arg; | 49 | const char *arg; |
50 | char *out; | ||
51 | char *buffer; | ||
52 | unsigned buflen; | ||
53 | int r; | ||
35 | #if !ENABLE_FEATURE_FANCY_ECHO | 54 | #if !ENABLE_FEATURE_FANCY_ECHO |
36 | enum { | 55 | enum { |
37 | eflag = '\\', | 56 | eflag = '\\', |
38 | nflag = 1, /* 1 -- print '\n' */ | 57 | nflag = 1, /* 1 -- print '\n' */ |
39 | }; | 58 | }; |
40 | 59 | ||
41 | /* We must check that stdout is not closed. | 60 | argv++; |
42 | * The reason for this is highly non-obvious. | ||
43 | * echo_main is used from shell. Shell must correctly handle "echo foo" | ||
44 | * if stdout is closed. With stdio, output gets shoveled into | ||
45 | * stdout buffer, and even fflush cannot clear it out. It seems that | ||
46 | * even if libc receives EBADF on write attempts, it feels determined | ||
47 | * to output data no matter what. So it will try later, | ||
48 | * and possibly will clobber future output. Not good. */ | ||
49 | // TODO: check fcntl() & O_ACCMODE == O_WRONLY or O_RDWR? | ||
50 | if (fcntl(1, F_GETFL) == -1) | ||
51 | return 1; /* match coreutils 6.10 (sans error msg to stderr) */ | ||
52 | //if (dup2(1, 1) != 1) - old way | ||
53 | // return 1; | ||
54 | |||
55 | arg = *++argv; | ||
56 | if (!arg) | ||
57 | goto newline_ret; | ||
58 | #else | 61 | #else |
59 | const char *p; | 62 | const char *p; |
60 | char nflag = 1; | 63 | char nflag = 1; |
61 | char eflag = 0; | 64 | char eflag = 0; |
62 | 65 | ||
63 | /* We must check that stdout is not closed. */ | 66 | while ((arg = *++argv) != NULL) { |
64 | if (fcntl(1, F_GETFL) == -1) | 67 | if (!arg || arg[0] != '-') |
65 | return 1; | ||
66 | |||
67 | while (1) { | ||
68 | arg = *++argv; | ||
69 | if (!arg) | ||
70 | goto newline_ret; | ||
71 | if (*arg != '-') | ||
72 | break; | 68 | break; |
73 | 69 | ||
74 | /* If it appears that we are handling options, then make sure | 70 | /* If it appears that we are handling options, then make sure |
@@ -77,7 +73,7 @@ int echo_main(int argc UNUSED_PARAM, char **argv) | |||
77 | */ | 73 | */ |
78 | p = arg + 1; | 74 | p = arg + 1; |
79 | if (!*p) /* A single '-', so echo it. */ | 75 | if (!*p) /* A single '-', so echo it. */ |
80 | goto just_echo; | 76 | break; |
81 | 77 | ||
82 | do { | 78 | do { |
83 | if (!strrchr("neE", *p)) | 79 | if (!strrchr("neE", *p)) |
@@ -95,19 +91,27 @@ int echo_main(int argc UNUSED_PARAM, char **argv) | |||
95 | } | 91 | } |
96 | just_echo: | 92 | just_echo: |
97 | #endif | 93 | #endif |
98 | while (1) { | 94 | |
99 | /* arg is already == *argv and isn't NULL */ | 95 | buflen = 1; |
96 | pp = argv; | ||
97 | while ((arg = *pp) != NULL) { | ||
98 | buflen += strlen(arg) + 1; | ||
99 | pp++; | ||
100 | } | ||
101 | out = buffer = xmalloc(buflen); | ||
102 | |||
103 | while ((arg = *argv) != NULL) { | ||
100 | int c; | 104 | int c; |
101 | 105 | ||
102 | if (!eflag) { | 106 | if (!eflag) { |
103 | /* optimization for very common case */ | 107 | /* optimization for very common case */ |
104 | fputs(arg, stdout); | 108 | out = stpcpy(out, arg); |
105 | } else while ((c = *arg++)) { | 109 | } else while ((c = *arg++)) { |
106 | if (c == eflag) { /* Check for escape seq. */ | 110 | if (c == eflag) { /* Check for escape seq. */ |
107 | if (*arg == 'c') { | 111 | if (*arg == 'c') { |
108 | /* '\c' means cancel newline and | 112 | /* '\c' means cancel newline and |
109 | * ignore all subsequent chars. */ | 113 | * ignore all subsequent chars. */ |
110 | goto ret; | 114 | goto do_write; |
111 | } | 115 | } |
112 | #if !ENABLE_FEATURE_FANCY_ECHO | 116 | #if !ENABLE_FEATURE_FANCY_ECHO |
113 | /* SUSv3 specifies that octal escapes must begin with '0'. */ | 117 | /* SUSv3 specifies that octal escapes must begin with '0'. */ |
@@ -127,21 +131,26 @@ int echo_main(int argc UNUSED_PARAM, char **argv) | |||
127 | c = bb_process_escape_sequence(&arg); | 131 | c = bb_process_escape_sequence(&arg); |
128 | } | 132 | } |
129 | } | 133 | } |
130 | bb_putchar(c); | 134 | *out++ = c; |
131 | } | 135 | } |
132 | 136 | ||
133 | arg = *++argv; | 137 | if (!*++argv) |
134 | if (!arg) | ||
135 | break; | 138 | break; |
136 | bb_putchar(' '); | 139 | *out++ = ' '; |
137 | } | 140 | } |
138 | 141 | ||
139 | newline_ret: | ||
140 | if (nflag) { | 142 | if (nflag) { |
141 | bb_putchar('\n'); | 143 | *out++ = '\n'; |
142 | } | 144 | } |
143 | ret: | 145 | |
144 | return fflush_all(); | 146 | do_write: |
147 | r = full_write(STDOUT_FILENO, buffer, out - buffer); | ||
148 | free(buffer); | ||
149 | if (r < 0) { | ||
150 | bb_perror_msg(bb_msg_write_error); | ||
151 | return 1; | ||
152 | } | ||
153 | return 0; | ||
145 | } | 154 | } |
146 | 155 | ||
147 | /*- | 156 | /*- |