sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

cat.c (824B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <fcntl.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 
      6 #include "util.h"
      7 
      8 static void
      9 usage(void)
     10 {
     11 	eprintf("usage: %s [-u] [file ...]\n", argv0);
     12 }
     13 
     14 int
     15 main(int argc, char *argv[])
     16 {
     17 	int fd, ret = 0;
     18 
     19 	ARGBEGIN {
     20 	case 'u':
     21 		break;
     22 	default:
     23 		usage();
     24 	} ARGEND
     25 
     26 	if (!argc) {
     27 		if (concat(0, "<stdin>", 1, "<stdout>") < 0)
     28 			ret = 1;
     29 	} else {
     30 		for (; *argv; argc--, argv++) {
     31 			if (!strcmp(*argv, "-")) {
     32 				*argv = "<stdin>";
     33 				fd = 0;
     34 			} else if ((fd = open(*argv, O_RDONLY)) < 0) {
     35 				weprintf("open %s:", *argv);
     36 				ret = 1;
     37 				continue;
     38 			}
     39 			switch (concat(fd, *argv, 1, "<stdout>")) {
     40 			case -1:
     41 				ret = 1;
     42 				break;
     43 			case -2:
     44 				return 1;  /* exit on write error */
     45 			}
     46 			if (fd != 0)
     47 				close(fd);
     48 		}
     49 	}
     50 
     51 	return ret;
     52 }