cp: add -a, -d, -p
This commit is contained in:
parent
8e8d8ff242
commit
8b3a9c1971
12
cp.1
12
cp.1
|
@ -8,7 +8,7 @@ cp \- copy files and directories
|
|||
.RI [ name ]
|
||||
.P
|
||||
.B cp
|
||||
.RB [ \-Rr ]
|
||||
.RB [ \-adpRr ]
|
||||
.RI [ file ...]
|
||||
.RI [ directory ]
|
||||
.SH DESCRIPTION
|
||||
|
@ -17,6 +17,16 @@ copies a given file, naming it the given name. If multiple files are listed
|
|||
they will be copied into the given directory.
|
||||
.SH OPTIONS
|
||||
.TP
|
||||
.B \-a
|
||||
preserve mode, timestamp, links and permissions.
|
||||
Implies \-d, \-p, \-r.
|
||||
.TP
|
||||
.B \-d
|
||||
don't dereference links. preserve links.
|
||||
.TP
|
||||
.B \-p
|
||||
preserve mode, timestamp, links and permissions.
|
||||
.TP
|
||||
.B \-f
|
||||
if an existing destination file cannot be opened, remove it and try again.
|
||||
.TP
|
||||
|
|
14
cp.c
14
cp.c
|
@ -8,7 +8,7 @@
|
|||
static void
|
||||
usage(void)
|
||||
{
|
||||
eprintf("usage: %s [-fRr] source... dest\n", argv0);
|
||||
eprintf("usage: %s [-adfpRr] source... dest\n", argv0);
|
||||
}
|
||||
|
||||
int
|
||||
|
@ -17,6 +17,18 @@ main(int argc, char *argv[])
|
|||
struct stat st;
|
||||
|
||||
ARGBEGIN {
|
||||
case 'a':
|
||||
cp_aflag = true; /* implies -dpr */
|
||||
cp_dflag = true;
|
||||
cp_pflag = true;
|
||||
cp_rflag = true;
|
||||
break;
|
||||
case 'd':
|
||||
cp_dflag = true;
|
||||
break;
|
||||
case 'p':
|
||||
cp_pflag = true;
|
||||
break;
|
||||
case 'f':
|
||||
cp_fflag = true;
|
||||
break;
|
||||
|
|
4
fs.h
4
fs.h
|
@ -1,8 +1,12 @@
|
|||
/* See LICENSE file for copyright and license details. */
|
||||
#include <stdbool.h>
|
||||
|
||||
extern bool cp_aflag;
|
||||
extern bool cp_dflag;
|
||||
extern bool cp_fflag;
|
||||
extern bool cp_pflag;
|
||||
extern bool cp_rflag;
|
||||
|
||||
extern bool rm_fflag;
|
||||
extern bool rm_rflag;
|
||||
|
||||
|
|
43
util/cp.c
43
util/cp.c
|
@ -7,12 +7,18 @@
|
|||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <limits.h>
|
||||
#include <utime.h>
|
||||
|
||||
#include "../fs.h"
|
||||
#include "../text.h"
|
||||
#include "../util.h"
|
||||
|
||||
bool cp_aflag = false;
|
||||
bool cp_dflag = false;
|
||||
bool cp_fflag = false;
|
||||
bool cp_pflag = false;
|
||||
bool cp_rflag = false;
|
||||
|
||||
int
|
||||
|
@ -23,10 +29,31 @@ cp(const char *s1, const char *s2)
|
|||
long size1, size2;
|
||||
struct dirent *d;
|
||||
struct stat st;
|
||||
struct utimbuf ut;
|
||||
char buf[PATH_MAX];
|
||||
DIR *dp;
|
||||
int r;
|
||||
|
||||
if (stat(s1, &st) == 0 && S_ISDIR(st.st_mode)) {
|
||||
if(cp_dflag == true)
|
||||
r = lstat(s1, &st);
|
||||
else
|
||||
r = stat(s1, &st);
|
||||
|
||||
if(r == 0) {
|
||||
if(cp_dflag == true && S_ISLNK(st.st_mode)) {
|
||||
if(cp_fflag == true)
|
||||
remove(s2);
|
||||
if(readlink(s1, buf, sizeof(buf) - 1) >= 0)
|
||||
symlink(buf, s2);
|
||||
|
||||
/* preserve owner ? */
|
||||
if(cp_aflag == true || cp_pflag == true) {
|
||||
if(lchown(s2, st.st_uid, st.st_gid) == -1)
|
||||
weprintf("cp: can't preserve ownership of '%s':", s2);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
if(S_ISDIR(st.st_mode)) {
|
||||
if (!cp_rflag)
|
||||
eprintf("%s: is a directory\n", s1);
|
||||
|
||||
|
@ -58,8 +85,10 @@ cp(const char *s1, const char *s2)
|
|||
closedir(dp);
|
||||
free(ns1);
|
||||
free(ns2);
|
||||
goto preserve;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
if(!(f1 = fopen(s1, "r")))
|
||||
eprintf("fopen %s:", s1);
|
||||
|
||||
|
@ -78,5 +107,17 @@ cp(const char *s1, const char *s2)
|
|||
fclose(f2);
|
||||
fclose(f1);
|
||||
|
||||
preserve:
|
||||
if(cp_aflag == true || cp_pflag == true) {
|
||||
/* timestamp */
|
||||
ut.actime = st.st_atime;
|
||||
ut.modtime = st.st_mtime;
|
||||
utime(s2, &ut);
|
||||
|
||||
/* preserve owner ? */
|
||||
if(chown(s2, st.st_uid, st.st_gid) == -1)
|
||||
weprintf("cp: can't preserve ownership of '%s':", s2);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user