Fix return values in rm(1) and mv(1)

by setting rm_status to 1 if removing 1 file in the list fails.
Extend this to mv_status in mv(1).
This commit is contained in:
FRIGN 2015-01-30 12:45:54 +01:00
parent a9c7d16cde
commit e60885699c
4 changed files with 26 additions and 20 deletions

1
fs.h
View File

@ -9,6 +9,7 @@ extern int cp_status;
extern int rm_fflag; extern int rm_fflag;
extern int rm_rflag; extern int rm_rflag;
extern int rm_status;
int cp(const char *, const char *); int cp(const char *, const char *);
void rm(const char *); void rm(const char *);

View File

@ -6,12 +6,16 @@
int rm_fflag = 0; int rm_fflag = 0;
int rm_rflag = 0; int rm_rflag = 0;
int rm_status = 0;
void void
rm(const char *path) rm(const char *path)
{ {
if (rm_rflag) if (rm_rflag)
recurse(path, rm); recurse(path, rm);
if (remove(path) < 0 && !rm_fflag) if (remove(path) < 0) {
eprintf("remove %s:", path); if (!rm_fflag)
weprintf("remove %s:", path);
rm_status = 1;
}
} }

35
mv.c
View File

@ -8,7 +8,23 @@
#include "fs.h" #include "fs.h"
#include "util.h" #include "util.h"
static int mv(const char *, const char *); int mv_status = 0;
static int
mv(const char *s1, const char *s2)
{
if (rename(s1, s2) == 0)
return (mv_status = 0);
if (errno == EXDEV) {
cp_rflag = 1;
rm_rflag = 1;
cp(s1, s2);
rm(s1);
return (mv_status = cp_status || rm_status);
}
mv_status = 1;
return -1;
}
static void static void
usage(void) usage(void)
@ -36,20 +52,5 @@ main(int argc, char *argv[])
eprintf("%s: not a directory\n", argv[argc-1]); eprintf("%s: not a directory\n", argv[argc-1]);
enmasse(argc, &argv[0], mv); enmasse(argc, &argv[0], mv);
return 0; return mv_status;
}
static int
mv(const char *s1, const char *s2)
{
if (rename(s1, s2) == 0)
return 0;
if (errno == EXDEV) {
cp_rflag = 1;
rm_rflag = 1;
cp(s1, s2);
rm(s1);
return 0;
}
return -1;
} }

2
rm.c
View File

@ -41,5 +41,5 @@ main(int argc, char *argv[])
for (; argc > 0; argc--, argv++) for (; argc > 0; argc--, argv++)
rm(argv[0]); rm(argv[0]);
return 0; return rm_status;
} }