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 =* cmp yes none
#* cols non-posix none #* cols non-posix none
=* comm yes none =* comm yes none
= cp yes none (-i) =*| cp yes none (-i)
=* cron non-posix none =* cron non-posix none
#* cut yes none #* cut yes none
=*| date 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 .Dt CP 1
.Os sbase .Os sbase
.Sh NAME .Sh NAME
@ -11,45 +11,61 @@
.Fl R .Fl R
.Op Fl H | L | P .Op Fl H | L | P
.Oc .Oc
.Op Ar file ... .Op Ar source ...
.Op Ar directory .Op Ar dest
.Sh DESCRIPTION .Sh DESCRIPTION
.Nm .Nm
copies a given copies
.Ar file , .Ar source
naming it the given to
.Ar name . .Ar dest .
If multiple If more than one
.Ar files .Ar source
are listed is given
they will be copied into the given .Ar dest
.Ar directory . has to be a directory.
.Sh OPTIONS .Sh OPTIONS
.Bl -tag -width Ds .Bl -tag -width Ds
.It Fl a .It Fl a
Preserve mode, timestamp, links and permissions. Implies Preserve block devices, character devices, sockets and FIFOs. Implies
.Fl d , .Fl p ,
.Fl p .Fl P
and and
.Fl r . .Fl R .
.It Fl f .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 .It Fl p
Preserve mode, timestamp and permissions. Preserve mode, timestamp and permissions.
.It Fl v .It Fl v
Print names of source and destination per file to stdout. In the format: Write "'source' -> 'dest'" for each
"source \-> destination". .Ar source
.It Fl R to stdout.
Copies directories recursively. If this flag is not specified, directories
are not copied.
.It Fl H .It Fl H
Only dereference symbolic links that are passed as command line arguments when Dereference
recursively traversing directories. .Ar source
if it is a symbolic link.
.It Fl L .It Fl L
Always dereference symbolic links while recursively traversing directories Dereference all symbolic links.
(default). This is the default.
.It Fl P .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 .El
.Sh SEE ALSO .Sh SEE ALSO
.Xr mv 1 .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.

13
cp.c
View File

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