annotate toys/cat.c @ 281:1c160cadf5b9

Thinko in cat.
author Rob Landley <rob@landley.net>
date Wed, 09 Apr 2008 00:22:04 -0500
parents 0ca2fcee572b
children d1ae502ee190
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
279
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * hello.c - A hello world program.
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2006 Rob Landley <rob@landley.net>
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://www.opengroup.org/onlinepubs/009695399/utilities/cat.html
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
8
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_CAT(NEWTOY(cat, "u", TOYFLAG_BIN))
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
10
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config CAT
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "cat"
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: cat [-u] [file...]
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
16 Copy (concatenate) files to stdout. If no files listed, copy from stdin.
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
17
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
18 -u Copy one byte at a time (slow).
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
19 */
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
20
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
21 #include "toys.h"
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
22
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
23 static void do_cat(int fd, char *name)
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
24 {
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
25 int len, size=toys.optflags ? 1 : sizeof(toybuf);
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
26
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
27 for (;;) {
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
28 len = xread(fd, toybuf, size);
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
29 if (len<0) toys.exitval = EXIT_FAILURE;
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
30 if (len<1) break;
281
1c160cadf5b9 Thinko in cat.
Rob Landley <rob@landley.net>
parents: 279
diff changeset
31 xwrite(1, toybuf, len);
279
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
32 }
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
33 }
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
34
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
35 void cat_main(void)
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
36 {
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
37 loopfiles(toys.optargs, do_cat);
0ca2fcee572b Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff changeset
38 }