catsys.c

#include <unistd.h>
#include <fcntl.h>
#include <string.h>

int cat(int fd)
{
	char x;
	int rv;

	while((rv = read(fd, &x, 1)) == 1){
		if (write(1, &x, 1) != 1){
			(void) write(2, "Could not write to screen\n", 26);
			return(-1);
		}
	}
	if (rv != 0){
		(void) write(2, "Error reading\n", 13);
		return(-1);
	}
	return(0);
}

int main(int argc, char *argv[])
{
	int i;
	int fd;
	int rv;

	for(i = 1; i < argc; i++){
		if ((fd = open(argv[i], O_RDONLY)) < 0){
			write(2, "Could not read ", 15);
			write(2, argv[i], (size_t) strlen(argv[i]));
			write(2, "\n", 1);
		}
		else{
			rv -= cat(fd);
			(void) close(fd);
		}
	}

	return(rv);
}




UC Davis sigil
Matt Bishop
Office: 2209 Watershed Sciences
Phone: +1 (530) 752-8060
Email: mabishop@ucdavis.edu
ECS 36A, Programming & Problem Solving
Version of April 2, 2024 at 12:13PM

You can get the raw source code here.

Valid HTML 4.01 Transitional Built with BBEdit Built on a Macintosh