2019-06-11 13:57:14 +00:00
|
|
|
#include "kernel/types.h"
|
|
|
|
#include "kernel/stat.h"
|
|
|
|
#include "user/user.h"
|
2006-08-08 19:58:06 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
char buf[512];
|
2006-08-08 19:58:06 +00:00
|
|
|
|
2006-08-23 01:09:24 +00:00
|
|
|
void
|
2007-08-28 04:13:24 +00:00
|
|
|
cat(int fd)
|
2006-08-08 19:58:06 +00:00
|
|
|
{
|
2007-08-24 20:03:40 +00:00
|
|
|
int n;
|
2006-08-08 19:58:06 +00:00
|
|
|
|
2016-09-19 11:01:30 +00:00
|
|
|
while((n = read(fd, buf, sizeof(buf))) > 0) {
|
|
|
|
if (write(1, buf, n) != n) {
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("cat: write error\n");
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2016-09-19 11:01:30 +00:00
|
|
|
}
|
|
|
|
}
|
2007-08-24 20:03:40 +00:00
|
|
|
if(n < 0){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("cat: read error\n");
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-08-08 19:58:06 +00:00
|
|
|
}
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd, i;
|
2006-08-08 19:58:06 +00:00
|
|
|
|
2007-08-28 18:32:08 +00:00
|
|
|
if(argc <= 1){
|
2007-08-28 04:13:24 +00:00
|
|
|
cat(0);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2006-08-23 01:09:24 +00:00
|
|
|
}
|
2006-08-08 19:58:06 +00:00
|
|
|
|
2007-08-24 20:03:40 +00:00
|
|
|
for(i = 1; i < argc; i++){
|
|
|
|
if((fd = open(argv[i], 0)) < 0){
|
2019-08-27 17:13:03 +00:00
|
|
|
printf("cat: cannot open %s\n", argv[i]);
|
2019-09-11 14:04:40 +00:00
|
|
|
exit(1);
|
2007-08-24 20:03:40 +00:00
|
|
|
}
|
2007-08-28 04:13:24 +00:00
|
|
|
cat(fd);
|
2007-08-24 20:03:40 +00:00
|
|
|
close(fd);
|
|
|
|
}
|
2019-09-10 16:30:10 +00:00
|
|
|
exit(0);
|
2006-08-08 19:58:06 +00:00
|
|
|
}
|