BB: manually writes echo's buffer
[akaros.git] / tools / apps / busybox / akaros-patches / 1.17.3 / 0007-echo-buffering.patch
1 diff -ru busybox-1.17.3-orig/coreutils/echo.c busybox-1.17.3-akaros/coreutils/echo.c
2 --- busybox-1.17.3-orig/coreutils/echo.c        2015-03-06 12:06:11.732280338 -0500
3 +++ busybox-1.17.3-akaros/coreutils/echo.c      2015-03-06 12:34:12.607118758 -0500
4 @@ -33,6 +33,12 @@
5  {
6         int ret;
7         const char *arg;
8 +       /* In lieu of using fputs/fflush, Akaros works better with raw writes.  The
9 +        * main issue is that we want to have the entire echo sent as one write.
10 +        * using setvbuf for line buffered mode seems to work, but fflush won't
11 +        * propagate errors from the kernel. */
12 +       char write_buf[4096];
13 +       size_t buf_idx = 0;
14  #if !ENABLE_FEATURE_FANCY_ECHO
15         enum {
16                 eflag = '\\',
17 @@ -100,12 +106,7 @@
18                 /* arg is already == *argv and isn't NULL */
19                 int c;
20  
21 -               if (!eflag) {
22 -                       /* optimization for very common case */
23 -                       ret = fputs(arg, stdout);
24 -                       if (ret == EOF)
25 -                               perror("echo failed");
26 -               } else while ((c = *arg++)) {
27 +               while ((c = *arg++)) {
28                         if (c == eflag) {       /* Check for escape seq. */
29                                 if (*arg == 'c') {
30                                         /* '\c' means cancel newline and
31 @@ -130,25 +131,27 @@
32                                         c = bb_process_escape_sequence(&arg);
33                                 }
34                         }
35 -                       bb_putchar(c);
36 +                       write_buf[buf_idx++] = c;
37                 }
38  
39                 arg = *++argv;
40                 if (!arg)
41                         break;
42 -               bb_putchar(' ');
43 +               write_buf[buf_idx++] = ' ';
44         }
45  
46   newline_ret:
47         if (nflag) {
48 -               bb_putchar('\n');
49 +               write_buf[buf_idx++] = '\n';
50         }
51   ret:
52 -       errno = 0;
53 -       ret = fflush_all();
54 -       if (errno)
55 +       ret = write(1, write_buf, buf_idx);
56 +       if (ret == -1) {
57                 perror("echo failed");
58 -       return ret;
59 +               return EOF;
60 +       } else {
61 +               return 0;
62 +       }
63  }
64  
65  /*-