morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

mv.c (826B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <sys/stat.h>
      6 #include <unistd.h>
      7 
      8 #include "fs.h"
      9 #include "util.h"
     10 
     11 static int mv(const char *, const char *);
     12 
     13 static void
     14 usage(void)
     15 {
     16 	eprintf("usage: %s [-f] source... dest\n", argv0);
     17 }
     18 
     19 int
     20 main(int argc, char *argv[])
     21 {
     22 	struct stat st;
     23 
     24 	ARGBEGIN {
     25 	case 'f':
     26 		break;
     27 	default:
     28 		usage();
     29 	} ARGEND;
     30 
     31 	if (argc < 2)
     32 		usage();
     33 
     34 	if (argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
     35 		eprintf("%s: not a directory\n", argv[argc-1]);
     36 	enmasse(argc, &argv[0], mv);
     37 
     38 	return 0;
     39 }
     40 
     41 static int
     42 mv(const char *s1, const char *s2)
     43 {
     44 	if (rename(s1, s2) == 0)
     45 		return 0;
     46 	if (errno == EXDEV) {
     47 		cp_rflag = 1;
     48 		rm_rflag = 1;
     49 		cp(s1, s2);
     50 		rm(s1);
     51 		return 0;
     52 	}
     53 	return -1;
     54 }