aboutsummaryrefslogtreecommitdiff
path: root/ch8/8-01_cat-remastered.c
blob: 5e11ca8df737286c1544a06af2a40596b05a2d10 (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h> /* printf and friends */
#include <fcntl.h> /* open, creat, etc */
#include <unistd.h> /* read, close */
#include <stdarg.h> /* va_* */
#include <stdlib.h> /* exit */

/* The C Programming Language: 2nd Edition
 *
 * Exercise 8-1: Rewrite the program `cat` from Chapter 7 using read(),
 * write(), open(), and close() instead of their standard library
 * equivalents. Perferm experiments to determine the relative speeds of
 * the two versions.
 *
 * Notes: The `cat` program can be found on pp 162-163 in the book.
 */

#define DEBUG 0

char buf;
/* Let's have fewer magic numbers */
enum {
	STDIN,
	STDOUT,
	STDERR
};

#if DEBUG == 1
/* d_printf - output a debug message
 * This was shamelessly taken from the book
 */
void d_printf(char *fmt, ...) {
	va_list args;
	va_start(args, fmt);
	fprintf(stderr, "debug: ");
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
	va_end(args);
}
#endif

void in2out() {
	int status;
	while ((status = read(STDIN, &buf, 1))) {
		if (status != -1) {
			status = write(STDOUT, &buf, 1);
			if (status == -1) {
				fprintf(stderr, "error: Could not write to stdout; something is very wrong.\n");
				exit(1);
			}
		} else {
			fprintf(stderr, "error: Could not read stdin; something is very wrong.\n");
		}
	}
}

int main(int argc, char **argv) {
	int fd;
	/* Remember the name we were invoked as, for messages */
	ssize_t fstatus;
	argv++;
#if DEBUG == 1
	char *prog = *argv;
	d_printf("Starting %s", prog);
#endif
	if (argc == 1) {
#if DEBUG == 1
		d_printf("No arguments specified; copying stdin to stdout");
#endif
		in2out();
		argv++;
		exit(0);
	}
	/* The trick to understanding this is "x--" will run one more time than
	 * expected, because it doesn't decrement until after it's been evaluated
	 * for use in the expression. You can also look at it as "we've already
	 * pushed argv ahead by one".
	 */
	while (argc-- > 1) {
#if DEBUG == 1
		d_printf("Trying to open %s", *argv);
#endif
		fd = open(*argv, O_RDONLY, 0);
		if (fd != -1) {
#if DEBUG == 1
			d_printf("It was successfully opened. Reading.");
#endif
			while ((fstatus = read(fd, &buf, 1))) {
				if (fstatus == (-1)) {
					/* Fail early, fail often */
					fprintf(stderr, "error: Cannot read from '%s'. \n", *argv);
					close(fd);
					return 1;
				}
				if ((fstatus = write(STDOUT, &buf, 1)) == (-1)) {
					fprintf(stderr, "error: Cannot write to stdout.\n");
					close(fd);
					return 1;
				}
			}
#if DEBUG == 1
			d_printf("%s read complete.", *argv);
#endif
		} else {
			fprintf(stderr, "error: Could not open file '%s' for reading.\n", *argv);
			close(fd);
			return 1;
		}
#if DEBUG == 1
		d_printf("Closing file %s.", *argv);
#endif
		close(fd);
		argv++;
	}
	exit(0);
}