sbase

suckless unix tools
git clone git://git.suckless.org/sbase
Log | Files | Refs | README | LICENSE

commit f4b9b966cf67aecd551dc2daa2398727a76ab1c1
parent 61f5d4887aa62c67d0888992adfe0eafe834d030
Author: Michael Forney <mforney@mforney.org>
Date:   Sat, 21 Dec 2019 21:08:36 -0800

cp: Default to -P when -R is specified

POSIX only specifies the -H, -L, and -P options for use with -R, and
the default is left to the implementation. Without -R, symlinks must
be followed.

Most implementations use -P as the default with -R, which makes sense
to me as default behavior (the source and destination trees are the same).

Since we use the same code for -R and without it, and we allow -H, -L,
and -P without -R, set the default based on the presence of -R. Without
it, use -L as before, as required by POSIX, and with it, use -P to match
the behavior of other implementations.

Diffstat:
Mcp.1 | 5++++-
Mcp.c | 3+++
Mlibutil/cp.c | 2+-
3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/cp.1 b/cp.1 @@ -48,9 +48,12 @@ Dereference if it is a symbolic link. .It Fl L Dereference all symbolic links. -This is the default. +This is the default without +.Fl R . .It Fl P Preserve symbolic links. +This is the default with +.Fl R . .It Fl R Traverse directories recursively. If this flag is not specified, directories are not copied. diff --git a/cp.c b/cp.c @@ -45,6 +45,9 @@ main(int argc, char *argv[]) if (argc < 2) usage(); + if (!cp_follow) + cp_follow = cp_rflag ? 'P' : 'L'; + if (argc > 2) { if (stat(argv[argc - 1], &st) < 0) eprintf("stat %s:", argv[argc - 1]); diff --git a/libutil/cp.c b/libutil/cp.c @@ -20,7 +20,7 @@ int cp_pflag = 0; int cp_rflag = 0; int cp_vflag = 0; int cp_status = 0; -int cp_follow = 'L'; +int cp_follow; int cp(const char *s1, const char *s2, int depth)