sbase/cp.c
Michael Forney f4b9b966cf 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.
2019-12-21 21:26:19 -08:00

61 lines
950 B
C

/* See LICENSE file for copyright and license details. */
#include <sys/stat.h>
#include "fs.h"
#include "util.h"
static void
usage(void)
{
eprintf("usage: %s [-afpv] [-R [-H | -L | -P]] source ... dest\n", argv0);
}
int
main(int argc, char *argv[])
{
struct stat st;
ARGBEGIN {
case 'a':
cp_follow = 'P';
cp_aflag = cp_pflag = cp_rflag = 1;
break;
case 'f':
cp_fflag = 1;
break;
case 'p':
cp_pflag = 1;
break;
case 'r':
case 'R':
cp_rflag = 1;
break;
case 'v':
cp_vflag = 1;
break;
case 'H':
case 'L':
case 'P':
cp_follow = ARGC();
break;
default:
usage();
} ARGEND
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]);
if (!S_ISDIR(st.st_mode))
eprintf("%s: not a directory\n", argv[argc - 1]);
}
enmasse(argc, argv, cp);
return fshut(stdout, "<stdout>") || cp_status;
}