blob: 61a1e0bcc323f841f4c80fae1e6df4c41c67e39a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#include "gbos.h"
/* cp SRC DST: copy a file. There is no rename syscall, so `mv` is this plus
* rm - and O_WRITE truncates (see fs.asm sys_open .trunc), which is exactly
* why the SRC == DST case below has to be rejected before opening DST. */
static unsigned char streq(const char *a, const char *b) {
while (*a && *a == *b) {
a++;
b++;
}
return (unsigned char)(*a == *b);
}
void main(void) {
char *argv[4];
unsigned char argc = argv_parse(argv, 4);
unsigned char in, out;
int ch;
if (argc < 2) {
puts("usage: cp SRC DST");
nl();
sexit(2);
}
if (streq(argv[0], argv[1])) {
puts("cp: SRC and DST are the same file");
nl();
sexit(2);
}
in = open(argv[0], O_READ);
if (in == EISDIR) {
puts("cp: SRC is a directory");
nl();
sexit(2);
}
if (in == NOFD) {
puts("cp: no such file: ");
puts(argv[0]);
nl();
sexit(2);
}
out = open(argv[1], O_WRITE); /* creates, or truncates an existing file */
if (out == EISDIR) {
puts("cp: DST is a directory");
nl();
close(in);
sexit(2);
}
if (out == NOFD) {
puts("cp: cannot create ");
puts(argv[1]);
nl();
close(in);
sexit(2); /* out of inodes/blocks, or a bad path */
}
while ((ch = fgetc(in)) >= 0) fputc(out, (char)ch);
close(in);
close(out); /* flushes the inode */
}
|