mv.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * DUMMY mv implementation that calls cp + rm
  7. *
  8. * TODO: Actually implement the plumbing for mv!
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <sys/wait.h>
  15. static int call(char * args[]) {
  16. pid_t pid = fork();
  17. if (!pid) {
  18. execvp(args[0], args);
  19. exit(1);
  20. } else {
  21. int status;
  22. waitpid(pid, &status, 0);
  23. return status;
  24. }
  25. }
  26. int main(int argc, char * argv[]) {
  27. if (argc < 3) {
  28. fprintf(stderr, "%s: missing operand\n", argv[0]);
  29. return 1;
  30. }
  31. if (!strcmp(argv[1], argv[2])) {
  32. fprintf(stderr, "%s: %s and %s are the same file\n", argv[0], argv[1], argv[2]);
  33. return 1;
  34. }
  35. /* TODO stat magic for other ways to reference the same file */
  36. if (call((char *[]){"/bin/cp",argv[1],argv[2],NULL})) return 1;
  37. if (call((char *[]){"/bin/rm",argv[1],NULL})) return 1;
  38. return 0;
  39. }