Audit cp(1)

1) Refactor the manpage, which has been a bloody mess, documenting
   fantasy-flags (-d for example) and add a STANDARDS section
2) fix usage()
3) sort ARG-block
4) Check return-value of stat() separately, so a lack of permissions
   doesn't tell the user "the directory doesn't exist", which could
   be a bit confusing.
5) Add empty line before return.
This commit is contained in:
FRIGN 2015-03-02 19:15:19 +01:00
parent eb137b9e42
commit 274e86e1aa
3 changed files with 55 additions and 34 deletions

2
README
View File

@ -20,7 +20,7 @@ The following tools are implemented ('*' == finished, '#' == UTF-8 support,
=* cmp yes none
#* cols non-posix none
=* comm yes none
= cp yes none (-i)
=*| cp yes none (-i)
=* cron non-posix none
#* cut yes none
=*| date yes none

70
cp.1
View File

@ -1,4 +1,4 @@
.Dd February 17, 2015
.Dd March 2, 2015
.Dt CP 1
.Os sbase
.Sh NAME
@ -11,45 +11,61 @@
.Fl R
.Op Fl H | L | P
.Oc
.Op Ar file ...
.Op Ar directory
.Op Ar source ...
.Op Ar dest
.Sh DESCRIPTION
.Nm
copies a given
.Ar file ,
naming it the given
.Ar name .
If multiple
.Ar files
are listed
they will be copied into the given
.Ar directory .
copies
.Ar source
to
.Ar dest .
If more than one
.Ar source
is given
.Ar dest
has to be a directory.
.Sh OPTIONS
.Bl -tag -width Ds
.It Fl a
Preserve mode, timestamp, links and permissions. Implies
.Fl d ,
.Fl p
Preserve block devices, character devices, sockets and FIFOs. Implies
.Fl p ,
.Fl P
and
.Fl r .
.Fl R .
.It Fl f
If an existing destination file cannot be opened, remove it and try again.
If an existing
.Ar dest
cannot be opened, remove it and try again.
.It Fl p
Preserve mode, timestamp and permissions.
.It Fl v
Print names of source and destination per file to stdout. In the format:
"source \-> destination".
.It Fl R
Copies directories recursively. If this flag is not specified, directories
are not copied.
Write "'source' -> 'dest'" for each
.Ar source
to stdout.
.It Fl H
Only dereference symbolic links that are passed as command line arguments when
recursively traversing directories.
Dereference
.Ar source
if it is a symbolic link.
.It Fl L
Always dereference symbolic links while recursively traversing directories
(default).
Dereference all symbolic links.
This is the default.
.It Fl P
Don't dereference symbolic links.
Preserve symbolic links.
.It Fl R
Traverse directories recursively. If this flag is not specified, directories
are not copied.
.El
.Sh SEE ALSO
.Xr mv 1
.Sh STANDARDS
The
.Nm
utility is compliant with the
.St -p1003.1-2008
specification except from the
.Op Fl i
flag.
.Pp
The flags
.Op Fl av
are an extension to that specification.

17
cp.c
View File

@ -7,7 +7,7 @@
static void
usage(void)
{
eprintf("usage: %s [-afpv] [-R [-H | -L | -P]] source... dest\n", argv0);
eprintf("usage: %s [-afpv] [-R [-H | -L | -P]] source ... dest\n", argv0);
}
int
@ -26,13 +26,13 @@ main(int argc, char *argv[])
case 'p':
cp_pflag = 1;
break;
case 'v':
cp_vflag = 1;
break;
case 'r':
case 'R':
cp_rflag = 1;
break;
case 'v':
cp_vflag = 1;
break;
case 'H':
case 'L':
case 'P':
@ -45,8 +45,13 @@ main(int argc, char *argv[])
if (argc < 2)
usage();
if (argc > 2 && !(stat(argv[argc-1], &st) == 0 && S_ISDIR(st.st_mode)))
eprintf("%s: not a directory\n", argv[argc-1]);
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, cp_HLPflag);
return cp_status;
}