From e565522068410c57707592a3f1586381865ab69c Mon Sep 17 00:00:00 2001 From: Connor Lane Smith Date: Sat, 4 Jun 2011 02:56:18 +0100 Subject: [PATCH] ln -f --- ln.1 | 7 +++++-- ln.c | 23 ++++++++++++++++++++--- uname.c | 2 +- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/ln.1 b/ln.1 index beacd1e..4205ea7 100644 --- a/ln.1 +++ b/ln.1 @@ -3,12 +3,12 @@ ln \- make links between files .SH SYNOPSIS .B ln -.RB [ \-s ] +.RB [ \-fs ] .I file .RI [ name ] .P .B ln -.RB [ \-s ] +.RB [ \-fs ] .RI [ file ...] .RI [ directory ] .SH DESCRIPTION @@ -18,6 +18,9 @@ it is linked into the current directory. If multiple files are listed they will be linked into the given directory. .SH OPTIONS .TP +.B \-f +remove existing destinations. +.TP .B \-s create a symlink. .SH SEE ALSO diff --git a/ln.c b/ln.c index e61bb7c..8016084 100644 --- a/ln.c +++ b/ln.c @@ -1,23 +1,40 @@ /* See LICENSE file for copyright and license details. */ +#include #include +#include #include #include #include "util.h" +static int ln(const char *, const char *); + +static bool sflag = false; +static bool fflag = false; + int main(int argc, char *argv[]) { - bool sflag = false; char c; - while((c = getopt(argc, argv, "s")) != -1) + while((c = getopt(argc, argv, "fs")) != -1) switch(c) { + case 'f': + fflag = true; + break; case 's': sflag = true; break; default: exit(EXIT_FAILURE); } - enmasse(argc - optind, &argv[optind], sflag ? symlink : link); + enmasse(argc - optind, &argv[optind], ln); return EXIT_SUCCESS; } + +int +ln(const char *s1, const char *s2) +{ + if(fflag && remove(s2) != 0 && errno != ENOENT) + eprintf("remove %s:", s2); + return (sflag ? symlink : link)(s1, s2); +} diff --git a/uname.c b/uname.c index 75ff52a..49fbda5 100644 --- a/uname.c +++ b/uname.c @@ -40,7 +40,7 @@ main(int argc, char *argv[]) default: exit(EXIT_FAILURE); } - if(uname(&u) == -1) + if(uname(&u) != 0) eprintf("uname:"); if(sflag || !(nflag || rflag || vflag || mflag))