|
CS471/571 - Operating Systems
| Displaying exercises/e1/solution/from.c
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define K 1024
/**
* This program should open a file (file-name given on the command line,) and
* output it to the standard output. You may only use the kernel system calls
* for I/O, you may not use any C standard library I/O calls. An error should be
* displayed if the file cannot be opened and the program should return an
* non-successful return value.
*
* This program should do I/O in blocks of at least 1K at a time, it is not
* correct to read and write only 1 byte at a time.
*/
int main(int argc, char *argv[])
{
if (argc < 2) {
printf("Usage: from <filename>\n");
return 1;
}
int r, fd = open(argv[1], O_RDONLY);
char buf[K];
if (fd < 0) {
perror("File open");
return 1;
}
while( (r = read(fd, buf, K)) > 0) {
write(STDOUT_FILENO, buf, r);
}
close(fd);
return 0;
}
|