xv6-riscv-kernel/user/cat.c

44 lines
619 B
C
Raw Normal View History

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"
char buf[512];
void
2007-08-28 06:13:24 +02:00
cat(int fd)
{
int n;
while((n = read(fd, buf, sizeof(buf))) > 0) {
2024-06-15 16:55:06 +02:00
if(write(1, buf, n) != n) {
2019-11-07 15:46:20 +01:00
fprintf(2, "cat: write error\n");
2019-09-11 16:04:40 +02:00
exit(1);
}
}
2024-06-15 16:55:06 +02:00
if(n < 0) {
2019-11-07 15:46:20 +01:00
fprintf(2, "cat: read error\n");
2019-09-11 16:04:40 +02:00
exit(1);
}
}
int
main(int argc, char *argv[])
{
int fd, i;
2024-06-15 16:55:06 +02:00
if(argc <= 1) {
2007-08-28 06:13:24 +02:00
cat(0);
2019-11-07 15:46:20 +01:00
exit(0);
}
2024-06-15 16:55:06 +02:00
for(i = 1; i < argc; i++) {
if((fd = open(argv[i], 0)) < 0) {
2019-11-07 15:46:20 +01:00
fprintf(2, "cat: cannot open %s\n", argv[i]);
2019-09-11 16:04:40 +02:00
exit(1);
}
2007-08-28 06:13:24 +02:00
cat(fd);
close(fd);
}
exit(0);
}