cp: add symlink deref flags -H and -L for cp and mv

This commit is contained in:
Tai Chi Minh Ralph Eastwood 2015-02-09 20:56:23 +00:00 committed by sin
parent 360a63769c
commit af8be7f92c
7 changed files with 28 additions and 24 deletions

15
cp.c
View File

@ -19,10 +19,8 @@ main(int argc, char *argv[])
ARGBEGIN {
case 'a':
/* implies -dpr */
cp_aflag = cp_Pflag = cp_pflag = cp_rflag = 1;
break;
case 'P':
cp_Pflag = 1;
cp_HLPflag = 'P';
cp_aflag = cp_pflag = cp_rflag = 1;
break;
case 'p':
cp_pflag = 1;
@ -30,10 +28,15 @@ main(int argc, char *argv[])
case 'f':
cp_fflag = 1;
break;
case 'R':
case 'r':
case 'R':
cp_rflag = 1;
break;
case 'H':
case 'L':
case 'P':
cp_HLPflag = ARGC();
break;
case 'v':
cp_vflag = 1;
break;
@ -46,6 +49,6 @@ main(int argc, char *argv[])
if (argc > 2 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
eprintf("%s: not a directory\n", argv[argc-1]);
enmasse(argc, argv, cp);
enmasse(argc, argv, cp, cp_HLPflag);
return cp_status;
}

4
fs.h
View File

@ -1,15 +1,15 @@
/* See LICENSE file for copyright and license details. */
extern int cp_aflag;
extern int cp_fflag;
extern int cp_Pflag;
extern int cp_pflag;
extern int cp_rflag;
extern int cp_vflag;
extern char cp_HLPflag;
extern int cp_status;
extern int rm_fflag;
extern int rm_rflag;
extern int rm_status;
int cp(const char *, const char *);
int cp(const char *, const char *, char);
void rm(const char *);

View File

@ -17,14 +17,14 @@
int cp_aflag = 0;
int cp_fflag = 0;
int cp_Pflag = 0;
int cp_pflag = 0;
int cp_rflag = 0;
int cp_vflag = 0;
int cp_status = 0;
char cp_HLPflag = 'L';
int
cp(const char *s1, const char *s2)
cp(const char *s1, const char *s2, char ff)
{
FILE *f1, *f2;
char *ns1, *ns2;
@ -39,9 +39,9 @@ cp(const char *s1, const char *s2)
if (cp_vflag)
printf("'%s' -> '%s'\n", s1, s2);
r = cp_Pflag ? lstat(s1, &st) : stat(s1, &st);
r = ff == 'P' ? lstat(s1, &st) : stat(s1, &st);
if (r < 0) {
weprintf("%s %s:", cp_Pflag ? "lstat" : "stat", s1);
weprintf("%s %s:", ff == 'P' ? "lstat" : "stat", s1);
cp_status = 1;
return 0;
}
@ -83,7 +83,7 @@ cp(const char *s1, const char *s2)
eprintf("%s/%s: filename too long\n",
s2, d->d_name);
}
fnck(ns1, ns2, cp);
fnck(ns1, ns2, cp, ff == 'H' ? 'P' : ff);
}
}
closedir(dp);

View File

@ -9,7 +9,7 @@
#include "../util.h"
void
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *))
enmasse(int argc, char *argv[], int (*fn)(const char *, const char *, char), char ff)
{
char *buf, *dir;
int i, len;
@ -18,7 +18,7 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *))
size_t dlen;
if (argc == 2 && !(stat(argv[1], &st) == 0 && S_ISDIR(st.st_mode))) {
fnck(argv[0], argv[1], fn);
fnck(argv[0], argv[1], fn, ff);
return;
} else {
dir = (argc == 1) ? "." : argv[--argc];
@ -35,7 +35,7 @@ enmasse(int argc, char *argv[], int (*fn)(const char *, const char *))
eprintf("%s/%s: filename too long\n", dir,
basename(argv[i]));
}
fnck(argv[i], buf, fn);
fnck(argv[i], buf, fn, ff);
}
free(buf);
}

View File

@ -4,7 +4,8 @@
#include "../util.h"
void
fnck(const char *a, const char *b, int (*fn)(const char *, const char *))
fnck(const char *a, const char *b,
int (*fn)(const char *, const char *, char), char ff)
{
struct stat sta, stb;
@ -15,6 +16,6 @@ fnck(const char *a, const char *b, int (*fn)(const char *, const char *))
eprintf("%s -> %s: same file\n", a, b);
}
if (fn(a, b) < 0)
if (fn(a, b, ff) < 0)
eprintf("%s -> %s:", a, b);
}

8
mv.c
View File

@ -11,14 +11,14 @@
static int mv_status = 0;
static int
mv(const char *s1, const char *s2)
mv(const char *s1, const char *s2, char ff)
{
if (rename(s1, s2) == 0)
return (mv_status = 0);
if (errno == EXDEV) {
cp_rflag = 1;
cp_aflag = cp_rflag = cp_pflag = 1;
rm_rflag = 1;
cp(s1, s2);
cp(s1, s2, ff);
rm(s1);
return (mv_status = cp_status || rm_status);
}
@ -50,7 +50,7 @@ main(int argc, char *argv[])
if (argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
eprintf("%s: not a directory\n", argv[argc-1]);
enmasse(argc, &argv[0], mv);
enmasse(argc, &argv[0], mv, 'P');
return mv_status;
}

4
util.h
View File

@ -48,8 +48,8 @@ int enregcomp(int, regex_t *, const char *, int);
int eregcomp(regex_t *, const char *, int);
/* misc */
void enmasse(int, char **, int (*)(const char *, const char *));
void fnck(const char *, const char *, int (*)(const char *, const char *));
void enmasse(int, char **, int (*)(const char *, const char *, char), char);
void fnck(const char *, const char *, int (*)(const char *, const char *, char), char);
mode_t getumask(void);
char *humansize(double);
mode_t parsemode(const char *, mode_t, mode_t);