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

4
fs.h
View File

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

View File

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

View File

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

View File

@ -4,7 +4,8 @@
#include "../util.h" #include "../util.h"
void 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; 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); eprintf("%s -> %s: same file\n", a, b);
} }
if (fn(a, b) < 0) if (fn(a, b, ff) < 0)
eprintf("%s -> %s:", a, b); eprintf("%s -> %s:", a, b);
} }

8
mv.c
View File

@ -11,14 +11,14 @@
static int mv_status = 0; static int mv_status = 0;
static int static int
mv(const char *s1, const char *s2) mv(const char *s1, const char *s2, char ff)
{ {
if (rename(s1, s2) == 0) if (rename(s1, s2) == 0)
return (mv_status = 0); return (mv_status = 0);
if (errno == EXDEV) { if (errno == EXDEV) {
cp_rflag = 1; cp_aflag = cp_rflag = cp_pflag = 1;
rm_rflag = 1; rm_rflag = 1;
cp(s1, s2); cp(s1, s2, ff);
rm(s1); rm(s1);
return (mv_status = cp_status || rm_status); 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))) if (argc > 3 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
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, 'P');
return mv_status; 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); int eregcomp(regex_t *, const char *, int);
/* misc */ /* misc */
void enmasse(int, 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 *)); void fnck(const char *, const char *, int (*)(const char *, const char *, char), char);
mode_t getumask(void); mode_t getumask(void);
char *humansize(double); char *humansize(double);
mode_t parsemode(const char *, mode_t, mode_t); mode_t parsemode(const char *, mode_t, mode_t);