logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/feb01/mycat.c

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>

#define K 1024

int main (int argc, char **argv){

	if (argc < 2){
	   fprintf(stderr, "Usage %s file\n",argv[0]);
	   exit(1);
	}

	//char data[K];
	int rt;

	//open a file
	int fd = open(argv[1], O_RDONLY);

	if(fd == -1){
		write(2, "open failed\n",12);
		return -1;
	}

	int size;
	struct stat st;
	fstat(fd, &st);
	size = st.st_size;

	char buf[size+1];

	rt = read(fd, buf, size);

	//printf("Number of bytes read: %d\n", rt);

	write(1, buf, rt);

	close(fd);
	
	return 0;

}