[BusyBox] LEAF and ticker

Manuel Novoa III mjn3 at codepoet.org
Sun Feb 3 19:22:03 UTC 2002


Alex,

On Mon, Feb 04, 2002 at 12:07:46AM +0000, Alex Holden wrote:
> Serge Caron wrote:
> 
> >It is with tongue in cheek that I submit this busybox applet, ticker, for
> >which we have been budgeting 3Kb since the early release of the Linux 
> >Router
> >Project. It is about time this ticker got a lick.
> 
> 
> With tongue also held firmly in cheek I humbly submit a version which I 
> found to be 96 bytes smaller :)
> 
> #include <stdio.h>
> #include <unistd.h>
> 
> int main(void)
> {
> 	int i;
> 	char c[] = {'.', 'o', ':', 'O', ':', 'o'};
> 
> 	switch(fork()) {
> 		case -1:
> 			return -1;
> 		case 0:
> 			setbuf(stdout, 0);
> 			putchar(' ');
> 			while(1) {
> 				for(i = 0; i < sizeof(c); i++) {
> 					putchar('\b');
> 					putchar(c[i]);
> 					sleep(1);
> 				}
> 			}
> 	}
> 	return 0;
> }
> 
> [cue frantic attempts to beat me by another 100 bytes]
> 

Hardly frantic... but a couple of comments and my version.

1) You should _really_ declare c[] as static const.  By not doing
   so, your making the compiler build the array on the stack.
2) Not as much of an issue these days, but in some environments it
   is possible that putchar() could be defined in stdio.h as a
   masking macro.  If so, then it will expand to more than a
   simple function call.  To avoid the possibility, you could do
   either a) #undef putchar  or b)  (putchar)('\b');

Although 2)isn't an issue with glibc, 1) results (for me) in a
savings of another 20 bytes or so.

Of course, why bother with stdio if you don't want buffering?  Try
the following... (I get a size of 95 bytes on i386 with gcc -Os)

Manuel

-------------------------------------------------------------
#include <stdlib.h>
#include <unistd.h>

#ifdef TEST
#define ticker_main main
#endif

extern int ticker_main(int argc, char **argv)
{
    static const char symbol[] = ". \bo\b:\bO\b:\bo\b.";
    pid_t pid;
    const char *p;
    char i;   /* gcc often generate smaller code for char counters on i386 */

    if ((pid = fork()) == 0) {
        p = symbol;
        i = 1;
        do {
            write(1,p,i);   /* why use stdio if you don't want buffering? */
            sleep(1);
            i = 2;
            if (!*(p += 2)) {
                p = symbol + 2;
            }
        } while (1);
    }

    return (pid > 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}



More information about the busybox mailing list