编写代码实现cat的功能,编译成可执行文件名为pcat(pcat.c)
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(
int argc,
char *argv[]) {
int fd;
int r;
char buf[
128];
fd = open(argv[
1], O_RDONLY);
if(fd == -
1) {
perror(
"open");
return -
1;
}
while((r = read(fd, buf,
128)) >
0) {
write(STDOUT_FILENO, buf, r);
}
close(fd);
return 0;
}