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