[hackers] [sbase] cp: add symlink deref flags -H and -L for cp and mv || Tai Chi Minh Ralph Eastwood
commit af8be7f92c6b5ac5686ff75ea42a634f6eab5ece
Author: Tai Chi Minh Ralph Eastwood <tcmreastwood_AT_gmail.com>
Date: Mon Feb 9 20:56:23 2015 +0000
cp: add symlink deref flags -H and -L for cp and mv
diff --git a/cp.c b/cp.c
index f3b91f1..25fc07e 100644
--- a/cp.c
+++ b/cp.c
_AT_@ -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;
_AT_@ -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;
_AT_@ -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;
}
diff --git a/fs.h b/fs.h
index c6535d7..d9faeac 100644
--- a/fs.h
+++ b/fs.h
_AT_@ -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 *);
diff --git a/libutil/cp.c b/libutil/cp.c
index d252e52..7e0cd3a 100644
--- a/libutil/cp.c
+++ b/libutil/cp.c
_AT_@ -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;
_AT_@ -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;
}
_AT_@ -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);
diff --git a/libutil/enmasse.c b/libutil/enmasse.c
index 7a2fa2a..6281b79 100644
--- a/libutil/enmasse.c
+++ b/libutil/enmasse.c
_AT_@ -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;
_AT_@ -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];
_AT_@ -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);
}
diff --git a/libutil/fnck.c b/libutil/fnck.c
index 3085ab7..8afc4b1 100644
--- a/libutil/fnck.c
+++ b/libutil/fnck.c
_AT_@ -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;
_AT_@ -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);
}
diff --git a/mv.c b/mv.c
index ff29a8a..605e9d8 100644
--- a/mv.c
+++ b/mv.c
_AT_@ -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);
}
_AT_@ -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;
}
diff --git a/util.h b/util.h
index 6716ae5..f9175b2 100644
--- a/util.h
+++ b/util.h
_AT_@ -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);
Received on Tue Mar 24 2015 - 23:53:40 CET
This archive was generated by hypermail 2.3.0
: Wed Mar 25 2015 - 00:09:37 CET