sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

mv.c (1076B)


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