From d7fdad4baaadb796547f330e9a668a1576c30db4 Mon Sep 17 00:00:00 2001 From: sheldonh Date: Mon, 14 Aug 2000 08:48:55 +0000 Subject: 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 --- bin/ln/ln.1 | 20 +++++++++++++++++--- bin/ln/ln.c | 34 +++++++++++++++++++++++++++++----- 2 files changed, 46 insertions(+), 8 deletions(-) (limited to 'bin/ln') 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 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); } -- cgit v1.1