mirror of
https://github.com/SmallPond/MIT6.828_OS.git
synced 2026-04-29 05:11:07 +08:00
my solution to lab5
This commit is contained in:
36
lab/user/cat.c
Normal file
36
lab/user/cat.c
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <inc/lib.h>
|
||||
|
||||
char buf[8192];
|
||||
|
||||
void
|
||||
cat(int f, char *s)
|
||||
{
|
||||
long n;
|
||||
int r;
|
||||
|
||||
while ((n = read(f, buf, (long)sizeof(buf))) > 0)
|
||||
if ((r = write(1, buf, n)) != n)
|
||||
panic("write error copying %s: %e", s, r);
|
||||
if (n < 0)
|
||||
panic("error reading %s: %e", s, n);
|
||||
}
|
||||
|
||||
void
|
||||
umain(int argc, char **argv)
|
||||
{
|
||||
int f, i;
|
||||
|
||||
binaryname = "cat";
|
||||
if (argc == 1)
|
||||
cat(0, "<stdin>");
|
||||
else
|
||||
for (i = 1; i < argc; i++) {
|
||||
f = open(argv[i], O_RDONLY);
|
||||
if (f < 0)
|
||||
printf("can't open %s: %e\n", argv[i], f);
|
||||
else {
|
||||
cat(f, argv[i]);
|
||||
close(f);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user