Mercurial > hg > toybox
annotate toys/cat.c @ 313:d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
author  Rob Landley <rob@landley.net> 

date  Fri, 15 Aug 2008 14:16:53 0500 
parents  1c160cadf5b9 
children 
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 * 
313
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

3 * cat.c  copy inputs to stdout. 
279
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. 
313
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

17 Filename "" is a synonym for stdin. 
279
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

18 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

19 u Copy one byte at a time (slow). 
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 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

22 #include "toys.h" 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

23 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

24 static void do_cat(int fd, char *name) 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

25 { 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

26 int len, size=toys.optflags ? 1 : sizeof(toybuf); 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

27 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

28 for (;;) { 
313
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

29 len = read(fd, toybuf, size); 
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

30 if (len<0) { 
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

31 perror_msg("%s",name); 
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

32 toys.exitval = EXIT_FAILURE; 
d1ae502ee190
An error from an input file isn't fatal, keep reading remaining input files.
Rob Landley <rob@landley.net>
parents:
281
diff
changeset

33 } 
279
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

34 if (len<1) break; 
281  35 xwrite(1, toybuf, len); 
279
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 } 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

38 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

39 void cat_main(void) 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

40 { 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

41 loopfiles(toys.optargs, do_cat); 
0ca2fcee572b
Spent the five minutes to implement "cat".
Rob Landley <rob@landley.net>
parents:
diff
changeset

42 } 