[Midnightbsd-cvs] src [11355] trunk/bin/cp: sync with freebsd
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Fri Jul 6 08:06:17 EDT 2018
Revision: 11355
http://svnweb.midnightbsd.org/src/?rev=11355
Author: laffer1
Date: 2018-07-06 08:06:16 -0400 (Fri, 06 Jul 2018)
Log Message:
-----------
sync with freebsd
Modified Paths:
--------------
trunk/bin/cp/Makefile
trunk/bin/cp/cp.1
trunk/bin/cp/cp.c
trunk/bin/cp/extern.h
trunk/bin/cp/utils.c
Property Changed:
----------------
trunk/bin/cp/cp.1
Modified: trunk/bin/cp/Makefile
===================================================================
--- trunk/bin/cp/Makefile 2018-07-06 12:05:26 UTC (rev 11354)
+++ trunk/bin/cp/Makefile 2018-07-06 12:06:16 UTC (rev 11355)
@@ -1,6 +1,6 @@
+# $MidnightBSD$
# @(#)Makefile 8.1 (Berkeley) 5/31/93
-# $FreeBSD: src/bin/cp/Makefile,v 1.11 2002/07/15 12:08:21 sheldonh Exp $
-# $MidnightBSD: src/bin/cp/Makefile,v 1.2 2006/07/07 13:46:33 laffer1 Exp $
+# $FreeBSD: stable/10/bin/cp/Makefile 192586 2009-05-22 15:56:43Z trasz $
PROG= cp
SRCS= cp.c utils.c
Modified: trunk/bin/cp/cp.1
===================================================================
--- trunk/bin/cp/cp.1 2018-07-06 12:05:26 UTC (rev 11354)
+++ trunk/bin/cp/cp.1 2018-07-06 12:06:16 UTC (rev 11355)
@@ -1,3 +1,4 @@
+.\" $MidnightBSD$
.\"-
.\" Copyright (c) 1989, 1990, 1993, 1994
.\" The Regents of the University of California. All rights reserved.
@@ -30,10 +31,9 @@
.\" SUCH DAMAGE.
.\"
.\" @(#)cp.1 8.3 (Berkeley) 4/18/94
-.\" $FreeBSD: src/bin/cp/cp.1,v 1.33 2005/02/25 00:40:46 trhodes Exp $
-.\" $MidnightBSD: src/bin/cp/cp.1,v 1.7 2008/06/30 02:34:58 laffer1 Exp $
+.\" $FreeBSD: stable/10/bin/cp/cp.1 291774 2015-12-04 17:36:35Z bdrewery $
.\"
-.Dd September 4, 2012
+.Dd June 6, 2015
.Dt CP 1
.Os
.Sh NAME
@@ -46,7 +46,7 @@
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl f | i | n
-.Op Fl alpvx
+.Op Fl alpsvx
.Ar source_file target_file
.Nm
.Oo
@@ -54,7 +54,7 @@
.Op Fl H | Fl L | Fl P
.Oc
.Op Fl f | i | n
-.Op Fl alpvx
+.Op Fl alpsvx
.Ar source_file ... target_directory
.Sh DESCRIPTION
In the first synopsis form, the
@@ -180,6 +180,8 @@
and either the user ID or group ID cannot be preserved, neither
the set-user-ID nor set-group-ID bits are preserved in the copy's
permissions.
+.It Fl s
+Create symbolic links to regular files in a hierarchy instead of copying.
.It Fl v
Cause
.Nm
@@ -252,6 +254,27 @@
will be written to the standard output.
.Sh EXIT STATUS
.Ex -std
+.Sh EXAMPLES
+Make a copy of file
+.Pa foo
+named
+.Pa bar :
+.Pp
+.Dl $ cp foo bar
+.Pp
+Copy a group of files to the
+.Pa /tmp
+directory:
+.Pp
+.Dl $ cp *.txt /tmp
+.Pp
+Copy the directory
+.Pa junk
+and all of its contents (including any subdirectories) to the
+.Pa /tmp
+directory:
+.Pp
+.Dl $ cp -R junk /tmp
.Sh COMPATIBILITY
Historic versions of the
.Nm
@@ -266,7 +289,7 @@
is strongly discouraged as the behavior is
implementation-dependent.
In
-.Mx ,
+.Fx ,
.Fl r
is a synonym for
.Fl RL
@@ -278,12 +301,14 @@
files while recreating a hierarchy.
.Pp
The
-.Fl v
+.Fl l,
+.Fl s,
+.Fl v,
+.Fl x
and
.Fl n
options are non-standard and their use in scripts is not recommended.
.Sh SEE ALSO
-.Xr cpdup 1 ,
.Xr mv 1 ,
.Xr rcp 1 ,
.Xr umask 2 ,
Property changes on: trunk/bin/cp/cp.1
___________________________________________________________________
Added: svn:keywords
## -0,0 +1 ##
+MidnightBSD=%H
\ No newline at end of property
Modified: trunk/bin/cp/cp.c
===================================================================
--- trunk/bin/cp/cp.c 2018-07-06 12:05:26 UTC (rev 11354)
+++ trunk/bin/cp/cp.c 2018-07-06 12:06:16 UTC (rev 11355)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*-
* Copyright (c) 1988, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -30,8 +31,6 @@
* SUCH DAMAGE.
*/
-/* $FreeBSD: src/bin/cp/cp.c,v 1.51.2.1 2005/11/12 21:21:45 csjp Exp $" */
-
#if 0
#ifndef lint
static char const copyright[] =
@@ -44,7 +43,7 @@
#endif /* not lint */
#endif
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD$");
+__FBSDID("$FreeBSD: stable/10/bin/cp/cp.c 291774 2015-12-04 17:36:35Z bdrewery $");
/*
* Cp copies source files to target files.
@@ -77,8 +76,8 @@
#include "extern.h"
#define STRIP_TRAILING_SLASH(p) { \
- while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
- *--(p).p_end = 0; \
+ while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \
+ *--(p).p_end = 0; \
}
static char emptystring[] = "";
@@ -85,7 +84,7 @@
PATH_T to = { to.p_path, emptystring, "" };
-int fflag, iflag, lflag, nflag, pflag, vflag;
+int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
static int Rflag, rflag;
volatile sig_atomic_t info;
@@ -92,7 +91,6 @@
enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE };
static int copy(char *[], enum op, int);
-static int mastercmp(const FTSENT * const *, const FTSENT * const *);
static void siginfo(int __unused);
int
@@ -105,7 +103,7 @@
fts_options = FTS_NOCHDIR | FTS_PHYSICAL;
Hflag = Lflag = 0;
- while ((ch = getopt(argc, argv, "HLPRafilnprvx")) != -1)
+ while ((ch = getopt(argc, argv, "HLPRafilnprsvx")) != -1)
switch (ch) {
case 'H':
Hflag = 1;
@@ -148,6 +146,9 @@
rflag = Lflag = 1;
Hflag = 0;
break;
+ case 's':
+ sflag = 1;
+ break;
case 'v':
vflag = 1;
break;
@@ -166,6 +167,8 @@
if (Rflag && rflag)
errx(1, "the -R and -r options may not be specified together");
+ if (lflag && sflag)
+ errx(1, "the -l and -s options may not be specified together");
if (rflag)
Rflag = 1;
if (Rflag) {
@@ -186,7 +189,7 @@
if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path))
errx(1, "%s: name too long", target);
to.p_end = to.p_path + strlen(to.p_path);
- if (to.p_path == to.p_end) {
+ if (to.p_path == to.p_end) {
*to.p_end++ = '.';
*to.p_end = 0;
}
@@ -243,10 +246,10 @@
type = FILE_TO_FILE;
if (have_trailing_slash && type == FILE_TO_FILE) {
- if (r == -1)
+ if (r == -1) {
errx(1, "directory %s does not exist",
- to.p_path);
- else
+ to.p_path);
+ } else
errx(1, "%s is not a directory", to.p_path);
}
} else
@@ -276,7 +279,7 @@
mask = ~umask(0777);
umask(~mask);
- if ((ftsp = fts_open(argv, fts_options, mastercmp)) == NULL)
+ if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL)
err(1, "fts_open");
for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) {
switch (curr->fts_info) {
@@ -297,8 +300,8 @@
/*
* If we are in case (2) or (3) above, we need to append the
- * source name to the target name.
- */
+ * source name to the target name.
+ */
if (type != FILE_TO_FILE) {
/*
* Need to remember the roots of traversals to create
@@ -377,7 +380,8 @@
mode = curr->fts_statp->st_mode;
if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) ||
((mode | S_IRWXU) & mask) != (mode & mask))
- if (chmod(to.p_path, mode & mask) != 0){
+ if (chmod(to.p_path, mode & mask) !=
+ 0) {
warn("chmod: %s", to.p_path);
rval = 1;
}
@@ -385,7 +389,7 @@
continue;
}
- /* Not an error but need to remember it happened */
+ /* Not an error but need to remember it happened. */
if (stat(to.p_path, &to_stat) == -1)
dne = 1;
else {
@@ -411,7 +415,7 @@
switch (curr->fts_statp->st_mode & S_IFMT) {
case S_IFLNK:
- /* Catch special case of a non-dangling symlink */
+ /* Catch special case of a non-dangling symlink. */
if ((fts_options & FTS_LOGICAL) ||
((fts_options & FTS_COMFOLLOW) &&
curr->fts_level == 0)) {
@@ -436,7 +440,7 @@
* modified by the umask. Trade-off between being
* able to write the directory (if from directory is
* 555) and not causing a permissions race. If the
- * umask blocks owner writes, we fail..
+ * umask blocks owner writes, we fail.
*/
if (dne) {
if (mkdir(to.p_path,
@@ -455,7 +459,7 @@
break;
case S_IFBLK:
case S_IFCHR:
- if (Rflag) {
+ if (Rflag && !sflag) {
if (copy_special(curr->fts_statp, !dne))
badcp = rval = 1;
} else {
@@ -465,10 +469,10 @@
break;
case S_IFSOCK:
warnx("%s is a socket (not copied).",
- curr->fts_path);
+ curr->fts_path);
break;
case S_IFIFO:
- if (Rflag) {
+ if (Rflag && !sflag) {
if (copy_fifo(curr->fts_statp, !dne))
badcp = rval = 1;
} else {
@@ -490,32 +494,6 @@
return (rval);
}
-/*
- * mastercmp --
- * The comparison function for the copy order. The order is to copy
- * non-directory files before directory files. The reason for this
- * is because files tend to be in the same cylinder group as their
- * parent directory, whereas directories tend not to be. Copying the
- * files first reduces seeking.
- */
-static int
-mastercmp(const FTSENT * const *a, const FTSENT * const *b)
-{
- int a_info, b_info;
-
- a_info = (*a)->fts_info;
- if (a_info == FTS_ERR || a_info == FTS_NS || a_info == FTS_DNR)
- return (0);
- b_info = (*b)->fts_info;
- if (b_info == FTS_ERR || b_info == FTS_NS || b_info == FTS_DNR)
- return (0);
- if (a_info == FTS_D)
- return (-1);
- if (b_info == FTS_D)
- return (1);
- return (0);
-}
-
static void
siginfo(int sig __unused)
{
Modified: trunk/bin/cp/extern.h
===================================================================
--- trunk/bin/cp/extern.h 2018-07-06 12:05:26 UTC (rev 11354)
+++ trunk/bin/cp/extern.h 2018-07-06 12:06:16 UTC (rev 11355)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -27,9 +28,8 @@
* SUCH DAMAGE.
*
* @(#)extern.h 8.2 (Berkeley) 4/1/94
- * $FreeBSD: src/bin/cp/extern.h,v 1.19.8.1 2005/11/12 21:21:45 csjp Exp $
- * $MidnightBSD: src/bin/cp/extern.h,v 1.2 2006/07/07 13:46:33 laffer1 Exp $
-*/
+ * $FreeBSD: stable/10/bin/cp/extern.h 291774 2015-12-04 17:36:35Z bdrewery $
+ */
typedef struct {
char *p_end; /* pointer to NULL at end of path */
@@ -38,7 +38,7 @@
} PATH_T;
extern PATH_T to;
-extern int fflag, iflag, lflag, nflag, pflag, vflag;
+extern int fflag, iflag, lflag, nflag, pflag, sflag, vflag;
extern volatile sig_atomic_t info;
__BEGIN_DECLS
Modified: trunk/bin/cp/utils.c
===================================================================
--- trunk/bin/cp/utils.c 2018-07-06 12:05:26 UTC (rev 11354)
+++ trunk/bin/cp/utils.c 2018-07-06 12:06:16 UTC (rev 11355)
@@ -1,3 +1,4 @@
+/* $MidnightBSD$ */
/*-
* Copyright (c) 1991, 1993, 1994
* The Regents of the University of California. All rights reserved.
@@ -27,8 +28,6 @@
* SUCH DAMAGE.
*/
-/* $FreeBSD: src/bin/cp/utils.c,v 1.45.2.1 2005/11/12 21:21:45 csjp Exp $ */
-
#ifndef lint
#if 0
static char sccsid[] = "@(#)utils.c 8.3 (Berkeley) 4/1/94";
@@ -35,7 +34,7 @@
#endif
#endif /* not lint */
#include <sys/cdefs.h>
-__MBSDID("$MidnightBSD: src/bin/cp/utils.c,v 1.4 2008/06/30 02:20:01 laffer1 Exp $");
+__FBSDID("$FreeBSD: stable/10/bin/cp/utils.c 297782 2016-04-10 15:02:29Z jilles $");
#include <sys/types.h>
#include <sys/acl.h>
@@ -59,15 +58,19 @@
#define cp_pct(x, y) ((y == 0) ? 0 : (int)(100.0 * (x) / (y)))
-/* Memory strategy threshold, in pages: if physmem is larger then this, use a
- * large buffer */
+/*
+ * Memory strategy threshold, in pages: if physmem is larger then this, use a
+ * large buffer.
+ */
#define PHYSPAGES_THRESHOLD (32*1024)
-/* Maximum buffer size in bytes - do not allow it to grow larger than this */
+/* Maximum buffer size in bytes - do not allow it to grow larger than this. */
#define BUFSIZE_MAX (2*1024*1024)
-/* Small (default) buffer size in bytes. It's inefficient for this to be
- * smaller than MAXPHYS */
+/*
+ * Small (default) buffer size in bytes. It's inefficient for this to be
+ * smaller than MAXPHYS.
+ */
#define BUFSIZE_SMALL (MAXPHYS)
int
@@ -79,13 +82,15 @@
ssize_t wcount;
size_t wresid;
off_t wtotal;
- int ch, checkch, from_fd = 0, rcount, rval, to_fd = 0;
+ int ch, checkch, from_fd, rcount, rval, to_fd;
char *bufp;
#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
char *p;
#endif
- if ((from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
+ from_fd = to_fd = -1;
+ if (!lflag && !sflag &&
+ (from_fd = open(entp->fts_path, O_RDONLY, 0)) == -1) {
warn("%s", entp->fts_path);
return (1);
}
@@ -105,63 +110,65 @@
if (nflag) {
if (vflag)
printf("%s not overwritten\n", to.p_path);
- (void)close(from_fd);
- return (0);
+ rval = 1;
+ goto done;
} else if (iflag) {
(void)fprintf(stderr, "overwrite %s? %s",
- to.p_path, YESNO);
+ to.p_path, YESNO);
checkch = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
if (checkch != 'y' && checkch != 'Y') {
- (void)close(from_fd);
(void)fprintf(stderr, "not overwritten\n");
- return (1);
+ rval = 1;
+ goto done;
}
}
-
+
if (fflag) {
- /* remove existing destination file name,
- * create a new file */
- (void)unlink(to.p_path);
- if (!lflag)
- to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
- fs->st_mode & ~(S_ISUID | S_ISGID));
- } else {
- if (!lflag)
- /* overwrite existing destination file name */
- to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
+ /*
+ * Remove existing destination file name create a new
+ * file.
+ */
+ (void)unlink(to.p_path);
+ if (!lflag && !sflag) {
+ to_fd = open(to.p_path,
+ O_WRONLY | O_TRUNC | O_CREAT,
+ fs->st_mode & ~(S_ISUID | S_ISGID));
+ }
+ } else if (!lflag && !sflag) {
+ /* Overwrite existing destination file name. */
+ to_fd = open(to.p_path, O_WRONLY | O_TRUNC, 0);
}
- } else {
- if (!lflag)
- to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
- fs->st_mode & ~(S_ISUID | S_ISGID));
+ } else if (!lflag && !sflag) {
+ to_fd = open(to.p_path, O_WRONLY | O_TRUNC | O_CREAT,
+ fs->st_mode & ~(S_ISUID | S_ISGID));
}
-
- if (to_fd == -1) {
+
+ if (!lflag && !sflag && to_fd == -1) {
warn("%s", to.p_path);
- (void)close(from_fd);
- return (1);
+ rval = 1;
+ goto done;
}
rval = 0;
- if (!lflag) {
+ if (!lflag && !sflag) {
/*
- * Mmap and write if less than 8M (the limit is so we don't totally
- * trash memory on big files. This is really a minor hack, but it
- * wins some CPU back.
+ * Mmap and write if less than 8M (the limit is so we don't
+ * totally trash memory on big files. This is really a minor
+ * hack, but it wins some CPU back.
* Some filesystems, such as smbnetfs, don't support mmap,
* so this is a best-effort attempt.
*/
#ifdef VM_AND_BUFFER_CACHE_SYNCHRONIZED
if (S_ISREG(fs->st_mode) && fs->st_size > 0 &&
- fs->st_size <= 8 * 1024 * 1024 &&
+ fs->st_size <= 8 * 1024 * 1024 &&
(p = mmap(NULL, (size_t)fs->st_size, PROT_READ,
MAP_SHARED, from_fd, (off_t)0)) != MAP_FAILED) {
wtotal = 0;
for (bufp = p, wresid = fs->st_size; ;
- bufp += wcount, wresid -= (size_t)wcount) {
+ bufp += wcount, wresid -= (size_t)wcount) {
wcount = write(to_fd, bufp, wresid);
if (wcount <= 0)
break;
@@ -206,7 +213,7 @@
wtotal = 0;
while ((rcount = read(from_fd, buf, bufsize)) > 0) {
for (bufp = buf, wresid = rcount; ;
- bufp += wcount, wresid -= wcount) {
+ bufp += wcount, wresid -= wcount) {
wcount = write(to_fd, bufp, wresid);
if (wcount <= 0)
break;
@@ -232,13 +239,18 @@
rval = 1;
}
}
- } else {
+ } else if (lflag) {
if (link(entp->fts_path, to.p_path)) {
warn("%s", to.p_path);
rval = 1;
}
+ } else if (sflag) {
+ if (symlink(entp->fts_path, to.p_path)) {
+ warn("%s", to.p_path);
+ rval = 1;
+ }
}
-
+
/*
* Don't remove the target even after an error. The target might
* not be a regular file, or its attributes might be important,
@@ -246,7 +258,7 @@
* to remove it if we created it and its length is 0.
*/
- if (!lflag) {
+ if (!lflag && !sflag) {
if (pflag && setfile(fs, to_fd))
rval = 1;
if (pflag && preserve_fd_acls(from_fd, to_fd) != 0)
@@ -257,8 +269,9 @@
}
}
- (void)close(from_fd);
-
+done:
+ if (from_fd != -1)
+ (void)close(from_fd);
return (rval);
}
@@ -268,6 +281,11 @@
int len;
char llink[PATH_MAX];
+ if (exists && nflag) {
+ if (vflag)
+ printf("%s not overwritten\n", to.p_path);
+ return (1);
+ }
if ((len = readlink(p->fts_path, llink, sizeof(llink) - 1)) == -1) {
warn("readlink: %s", p->fts_path);
return (1);
@@ -287,6 +305,12 @@
int
copy_fifo(struct stat *from_stat, int exists)
{
+
+ if (exists && nflag) {
+ if (vflag)
+ printf("%s not overwritten\n", to.p_path);
+ return (1);
+ }
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
@@ -301,6 +325,12 @@
int
copy_special(struct stat *from_stat, int exists)
{
+
+ if (exists && nflag) {
+ if (vflag)
+ printf("%s not overwritten\n", to.p_path);
+ return (1);
+ }
if (exists && unlink(to.p_path)) {
warn("unlink: %s", to.p_path);
return (1);
@@ -315,7 +345,7 @@
int
setfile(struct stat *fs, int fd)
{
- static struct timeval tv[2];
+ static struct timespec tspec[2];
struct stat ts;
int rval, gotstat, islink, fdval;
@@ -323,12 +353,13 @@
fdval = fd != -1;
islink = !fdval && S_ISLNK(fs->st_mode);
fs->st_mode &= S_ISUID | S_ISGID | S_ISVTX |
- S_IRWXU | S_IRWXG | S_IRWXO;
+ S_IRWXU | S_IRWXG | S_IRWXO;
- TIMESPEC_TO_TIMEVAL(&tv[0], &fs->st_atim);
- TIMESPEC_TO_TIMEVAL(&tv[1], &fs->st_mtim);
- if (islink ? lutimes(to.p_path, tv) : utimes(to.p_path, tv)) {
- warn("%sutimes: %s", islink ? "l" : "", to.p_path);
+ tspec[0] = fs->st_atim;
+ tspec[1] = fs->st_mtim;
+ if (utimensat(AT_FDCWD, to.p_path, tspec,
+ islink ? AT_SYMLINK_NOFOLLOW : 0)) {
+ warn("utimensat: %s", to.p_path);
rval = 1;
}
if (fdval ? fstat(fd, &ts) :
@@ -337,7 +368,7 @@
else {
gotstat = 1;
ts.st_mode &= S_ISUID | S_ISGID | S_ISVTX |
- S_IRWXU | S_IRWXG | S_IRWXO;
+ S_IRWXU | S_IRWXG | S_IRWXO;
}
/*
* Changing the ownership probably won't succeed, unless we're root
@@ -461,7 +492,7 @@
return (0);
/*
- * If the file is a link we will not follow it
+ * If the file is a link we will not follow it.
*/
if (S_ISLNK(fs->st_mode)) {
aclgetf = acl_get_link_np;
@@ -520,8 +551,10 @@
{
(void)fprintf(stderr, "%s\n%s\n",
-"usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpvx] source_file target_file",
-" cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpvx] source_file ... "
-"target_directory");
+ "usage: cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
+ "source_file target_file",
+ " cp [-R [-H | -L | -P]] [-f | -i | -n] [-alpsvx] "
+ "source_file ... "
+ "target_directory");
exit(EX_USAGE);
}
More information about the Midnightbsd-cvs
mailing list