toaruos/apps/mv.c

46 lines
1.1 KiB
C
Raw Normal View History

/**
* @brief Move files
*
* Poor implementation, mostly just 'cp' and 'rm'.
*
* Ideally, should figure out if it can use 'rename'... and also
* we should implement 'rename'...
*
* @copyright
2018-10-02 12:37:18 +03:00
* This file is part of ToaruOS and is released under the terms
* of the NCSA / University of Illinois License - see LICENSE.md
* Copyright (C) 2018 K. Lange
*/
2018-10-02 16:42:24 +03:00
#include <stdio.h>
2018-10-02 12:37:18 +03:00
#include <stdlib.h>
2018-10-02 16:42:24 +03:00
#include <string.h>
2018-10-02 12:37:18 +03:00
#include <unistd.h>
#include <sys/wait.h>
static int call(char * args[]) {
pid_t pid = fork();
if (!pid) {
execvp(args[0], args);
exit(1);
} else {
int status;
waitpid(pid, &status, 0);
return status;
}
}
int main(int argc, char * argv[]) {
2018-10-02 16:42:24 +03:00
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","-r",argv[1],argv[2],NULL})) return 1;
if (call((char *[]){"/bin/rm","-r",argv[1],NULL})) return 1;
2018-10-02 12:37:18 +03:00
return 0;
}