commit: 3beb5bd08df7a57d37cfbc4b541e9d8f34a4e40a
parent fe8dc8259541bc2815bd553b87da11c3fc729f68
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Thu, 4 Jul 2024 13:13:18 +0200
cmd/install: Add support for -T option
Diffstat:
3 files changed, 45 insertions(+), 8 deletions(-)
diff --git a/cmd/install.1 b/cmd/install.1
@@ -9,7 +9,7 @@
.Nd install binairies
.Sh SYNOPSIS
.Nm
-.Op Fl cDp
+.Op Fl cDpT
.Op Fl g Ar group
.Op Fl m Ar mode
.Op Fl o Ar owner
@@ -66,6 +66,13 @@ The default mode is set to
Sets user ownership.
.It Fl p
Preserve file access and modification times.
+.It Fl T
+Treat
+.Ar destination
+as a normal file.
+Which asserts a single
+.Ar source
+operand.
.El
.Sh STANDARDS
None known.
diff --git a/cmd/install.c b/cmd/install.c
@@ -187,7 +187,7 @@ static void
usage()
{
fprintf(stderr, "\
-Usage: install [-cDp] [-g group] [-m mode] [-o owner] source... destination\n\
+Usage: install [-cDpT] [-g group] [-m mode] [-o owner] source... destination\n\
install -d [-c] [-g group] [-m mode] [-o owner] directory...\n\
");
}
@@ -197,12 +197,13 @@ main(int argc, char *argv[])
{
const char *errstr = NULL;
bool create_parents = false;
+ bool opt_T = false;
mkdir_parents_filemask = umask(0);
umask(mkdir_parents_filemask);
int c = -1;
- while((c = getopt(argc, argv, ":cDdpg:m:o:")) != -1)
+ while((c = getopt(argc, argv, ":cDdpTg:m:o:")) != -1)
{
switch(c)
{
@@ -224,6 +225,9 @@ main(int argc, char *argv[])
case 'o':
if(parse_group(optarg, &user) != 0) return 1;
break;
+ case 'T':
+ opt_T = true;
+ break;
case 'm':
mode = new_mode(optarg, 0755, &errstr);
if(errstr != NULL)
@@ -273,15 +277,25 @@ main(int argc, char *argv[])
return 0;
}
- if(argc < 2)
+ char *dest = argv[argc - 1];
+ bool multi_src = argc > 2;
+
+ if(opt_T)
{
- fprintf(stderr, "%s: Expected at least 2 arguments, got %d\n", argv0, argc);
+ if(argc != 2)
+ {
+ fprintf(stderr, "%s: Option -T passed, expected exactly 2 operands, got %d\n", argv0, argc);
+ return 1;
+ }
+
+ assert(!multi_src);
+ }
+ else if(argc < 2)
+ {
+ fprintf(stderr, "%s: Expected at least 2 operands, got %d\n", argv0, argc);
return 1;
}
- char *dest = argv[argc - 1];
- bool multi_src = argc > 2;
-
if(create_parents)
{
char *destdir = dest;
diff --git a/test-cmd/install.t b/test-cmd/install.t
@@ -69,6 +69,22 @@ install -D, multi src
$ test -f gran/dad/bro
$ rm -r sis bro gran
+install -T
+ $ test ! -e src_T
+ $ touch src_T
+ $ test -e src_T
+ $ install -T src_T dest_T
+ $ test -e src_T
+ $ test -e dest_T
+ $ rm dest_T
+ $ install -T src_T src2_T bogus_T
+ install: Option -T passed, expected exactly 2 operands, got 3
+ [1]
+ $ test -e src_T
+ $ test ! -e src2_T
+ $ test ! -e bogus_T
+ $ rm src_T
+
No files should be left
$ find .
.