Browse Source

Catch obvious mistakes in mv

K. Lange 2 years ago
parent
commit
544a7b16f4
1 changed files with 11 additions and 0 deletions
  1. 11 0
      apps/mv.c

+ 11 - 0
apps/mv.c

@@ -7,7 +7,9 @@
  *
  * TODO: Actually implement the plumbing for mv!
  */
+#include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <unistd.h>
 #include <sys/wait.h>
 
@@ -24,6 +26,15 @@ static int call(char * args[]) {
 }
 
 int main(int argc, char * argv[]) {
+	if (argc < 3) {
+		fprintf(stderr, "%s: missing operand\n", argv[0]);
+		return 1;
+	}
+	if (!strcmp(argv[1], argv[2])) {
+		fprintf(stderr, "%s: %s and %s are the same file\n", argv[0], argv[1], argv[2]);
+		return 1;
+	}
+	/* TODO stat magic for other ways to reference the same file */
 	if (call((char *[]){"/bin/cp",argv[1],argv[2],NULL})) return 1;
 	if (call((char *[]){"/bin/rm",argv[1],NULL})) return 1;
 	return 0;