summaryrefslogtreecommitdiff
path: root/src/cat.c
blob: 7901a897717f3d99c5b82044bd3db000034e8836 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * cat.c
 * Implementation of SUSv3 XCU cat utility
 * Copyright © 2007 Rich Felker
 * Licensed under the terms of the GNU General Public License, v2 or later
 */

#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>

static int my_write(int fd, const char *s, size_t l)
{
	while (l) {
		ssize_t n = write(fd, s, l);
		if (n<0) return -1;
		s += n; l -= n;
	}
	return 0;
}

static void my_perror(char *prog, char *msg)
{
	char *err = strerror(errno);
	write(2, prog, strlen(prog));
	write(2, ": ", 2);
	write(2, msg, strlen(msg));
	write(2, ": ", 2);
	write(2, err, strlen(err));
	write(2, "\n", 1);
}

int main(int argc, char *argv[])
{
	char buf[4096];
	char **v = argv+1;
	int n = argc-1;
	ssize_t i, k, l;
	int err = 0;
	int fd;

	signal(SIGPIPE, SIG_IGN);

	for (; n && v[0][0] == '-' && v[0][1]; v++, n--) {
		if (v[0][1]=='-' && !v[0][2]) { /* -- */
			v++; n--;
			break;
		}
		if (v[0][1]!='u' || v[0][2]) { /* not -u */
			errno = EINVAL;
			my_perror(argv[0], v[0]);
			exit(1);
		}
	}
	if (!n) {
		n = 1;
		v = (char *[]){ "-", NULL };
	}
	for (; n; v++, n--) {
		if (v[0][0] == '-' && !v[0][1]) fd = 0;
		else if ((fd = open(v[0], O_RDONLY)) < 0) {
			my_perror(argv[0], v[0]);
			err = 1;
			continue;
		}
		while ((l = read(fd, buf, sizeof buf)) > 0) {
			if (my_write(1, buf, l) < 0) {
				my_perror(argv[0], "write error");
				exit(1);
			}
		}
		if (l) {
			my_perror(argv[0], v[0]);
			err = 1;
		}
		if (fd) close(fd);
	}
	return err;
}