1 /* yes.c - Repeatedly output a string.
2 *
3 * Copyright 2007 Rob Landley <[email protected]>
4
5 USE_YES(NEWTOY(yes, 0, TOYFLAG_USR|TOYFLAG_BIN))
6
7 config YES
8 bool "yes"
9 default y
10 help
11 usage: yes [args...]
12
13 Repeatedly output line until killed. If no args, output 'y'.
14 */
15
16 #include "toys.h"
17
yes_main(void)18 void yes_main(void)
19 {
20 struct iovec *iov = (void *)toybuf;
21 char *out, *ss;
22 long len, ll, i, j;
23
24 // Collate command line arguments into one string, or repeated "y\n".
25 for (len = i = 0; toys.optargs[i]; i++) len += strlen(toys.optargs[i]) + 1;
26 ss = out = xmalloc(len ? : 128);
27 if (!i) for (i = 0; i<64; i++) {
28 *ss++ = 'y';
29 *ss++ = '\n';
30 } else {
31 for (i = 0; toys.optargs[i]; i++)
32 ss += sprintf(ss, " %s"+!i, toys.optargs[i]);
33 *ss++ = '\n';
34 }
35
36 // Populate a redundant iovec[] outputting the same string many times
37 for (i = ll = 0; i<sizeof(toybuf)/sizeof(*iov); i++) {
38 iov[i].iov_base = out;
39 ll += (iov[i].iov_len = ss-out);
40 }
41
42 // Writev the output until stdout stops accepting it
43 for (;;) for (len = 0; len<ll; len += j)
44 if (0>(j = writev(1, iov, i))) perror_exit(0);
45 }
46