summaryrefslogtreecommitdiffstats
path: root/bin/ln
diff options
context:
space:
mode:
authorsheldonh <sheldonh@FreeBSD.org>2000-08-14 08:48:55 +0000
committersheldonh <sheldonh@FreeBSD.org>2000-08-14 08:48:55 +0000
commitd7fdad4baaadb796547f330e9a668a1576c30db4 (patch)
treecca5a7e6e070de4bbd81a18a11baf37d335dbb7c /bin/ln
parentb6a0e38086413b5b8471128a7d3c0f63e18ee841 (diff)
downloadFreeBSD-src-d7fdad4baaadb796547f330e9a668a1576c30db4.zip
FreeBSD-src-d7fdad4baaadb796547f330e9a668a1576c30db4.tar.gz
Add the -i option, as found in rm(1), which provides an interactive
mode in which the user is prompted for confirmation before an existing file is replaced. Submitted by: alex
Diffstat (limited to 'bin/ln')
-rw-r--r--bin/ln/ln.120
-rw-r--r--bin/ln/ln.c34
2 files changed, 46 insertions, 8 deletions
diff --git a/bin/ln/ln.1 b/bin/ln/ln.1
index 89f4e5f..6120ae57 100644
--- a/bin/ln/ln.1
+++ b/bin/ln/ln.1
@@ -44,11 +44,11 @@
.Nd make links
.Sh SYNOPSIS
.Nm ln
-.Op Fl fsv
+.Op Fl fisv
.Ar source_file
.Op target_file
.Nm ln
-.Op Fl fsv
+.Op Fl fisv
.Ar source_file ...
.Op target_dir
.Nm link
@@ -73,6 +73,18 @@ The options are as follows:
.Bl -tag -width flag
.It Fl f
Unlink any already existing file, permitting the link to occur.
+The
+.Fl f
+option overrides any previous
+.Fl i
+options.
+.It Fl i
+Request confirmation before attempting to replace already existing file.
+The
+.Fl i
+option overrides any previous
+.Fl f
+options.
.It Fl s
Create a symbolic link.
.It Fl v
@@ -144,7 +156,9 @@ operation using the two passed arguments.
.Sh COMPATIBILITY
The
.Fl v
-option is non-standard and its use in scripts is not recommended.
+and
+.Fl i
+options are non-standard and their use in scripts is not recommended.
.Sh HISTORY
An
.Nm
diff --git a/bin/ln/ln.c b/bin/ln/ln.c
index dc2db0f..9ec1efc 100644
--- a/bin/ln/ln.c
+++ b/bin/ln/ln.c
@@ -56,6 +56,7 @@ static const char rcsid[] =
#include <unistd.h>
int fflag; /* Unlink existing files. */
+int iflag; /* Interactive mode. */
int sflag; /* Symbolic, not hard, link. */
int vflag; /* Verbose output. */
/* System link call. */
@@ -92,10 +93,17 @@ main(argc, argv)
usage();
}
- while ((ch = getopt(argc, argv, "fsv")) != -1)
+ fflag = iflag = sflag = vflag = 0;
+
+ while ((ch = getopt(argc, argv, "fisv")) != -1)
switch (ch) {
case 'f':
fflag = 1;
+ iflag = 0; /* -f overrides iflag */
+ break;
+ case 'i':
+ iflag = 1;
+ fflag = 0; /* -i overrides fflag */
break;
case 's':
sflag = 1;
@@ -139,7 +147,7 @@ linkit(target, source, isdir)
int isdir;
{
struct stat sb;
- int exists;
+ int exists, ch, first;
char *p, path[MAXPATHLEN];
if (!sflag) {
@@ -172,7 +180,23 @@ linkit(target, source, isdir)
* If the file exists, and -f was specified, unlink it.
* Attempt the link.
*/
- if ((fflag && exists && unlink(source)) || (*linkf)(target, source)) {
+ if (fflag && exists && unlink(source)) {
+ warn("%s", source);
+ return (1);
+ } else if (iflag && exists) {
+ fprintf(stderr, "replace %s? ", source);
+ fflush(stderr);
+
+ first = ch = getchar();
+ while(ch != '\n' && ch != EOF)
+ ch = getchar();
+
+ if ((first == 'y' || first == 'Y') && unlink(source)) {
+ warn("%s", source);
+ return (1);
+ }
+ }
+ if ((*linkf)(target, source)) {
warn("%s", source);
return (1);
}
@@ -185,8 +209,8 @@ void
usage()
{
(void)fprintf(stderr, "%s\n%s\n%s\n",
- "usage: ln [-fsv] file1 file2",
- " ln [-fsv] file ... directory",
+ "usage: ln [-fisv] file1 file2",
+ " ln [-fisv] file ... directory",
" link file1 file2");
exit(1);
}
OpenPOWER on IntegriCloud