#include "gbos.h" /* mv SRC DST: there is no rename syscall, so this is cp + rm. The copy has to * succeed before the source is removed - a half-copied DST plus a deleted SRC * loses the file. */ 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: mv SRC DST"); nl(); sexit(2); } if (streq(argv[0], argv[1])) sexit(0); /* nothing to do */ in = open(argv[0], O_READ); if (in == EISDIR) { puts("mv: SRC is a directory"); nl(); sexit(2); } if (in == NOFD) { puts("mv: no such file: "); puts(argv[0]); nl(); sexit(2); } out = open(argv[1], O_WRITE); if (out > 3) { puts("mv: cannot create "); puts(argv[1]); nl(); close(in); sexit(2); } while ((ch = fgetc(in)) >= 0) fputc(out, (char)ch); close(in); close(out); fremove(argv[0]); /* only now is it safe */ }