aboutsummaryrefslogtreecommitdiff
path: root/coreutils/ln.c
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2005-04-29 22:13:04 +0000
committerRob Landley <rob@landley.net>2005-04-29 22:13:04 +0000
commit3071e2fda3d9a609a00f31418a97fe8174f8b7fc (patch)
tree0291a3cc89f763fbf64253e30f08ebf382efc712 /coreutils/ln.c
parentfdc4c203e54227a2d79fe0a4936ce4bc020e1eff (diff)
downloadbusybox-w32-3071e2fda3d9a609a00f31418a97fe8174f8b7fc.tar.gz
busybox-w32-3071e2fda3d9a609a00f31418a97fe8174f8b7fc.tar.bz2
busybox-w32-3071e2fda3d9a609a00f31418a97fe8174f8b7fc.zip
Patch from Matthew S. Wood:
> The following patch adds support for the -S and -b flags to `ln'. These > flags [especially -b] are used extensively in Debian pre and post > installation scripts. Comments from Vladimir Oleynik influenced the final patch, and I also ripped out the in-file changelog since it belongs here. At the time, it said: /* Apr 15, 2004 Matthew S. Wood (mwood@realmsys.com) * * Implement '-b' (backup) flag. * Implement '-S' (backup suffix) flag. * * * Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) * * Fixed bug involving -n option. Essentially, -n was always in effect. */
Diffstat (limited to 'coreutils/ln.c')
-rw-r--r--coreutils/ln.c32
1 files changed, 24 insertions, 8 deletions
diff --git a/coreutils/ln.c b/coreutils/ln.c
index 885ba61db..274e0d06c 100644
--- a/coreutils/ln.c
+++ b/coreutils/ln.c
@@ -21,21 +21,20 @@
21 */ 21 */
22 22
23/* BB_AUDIT SUSv3 compliant */ 23/* BB_AUDIT SUSv3 compliant */
24/* BB_AUDIT GNU options missing: -b, -d, -F, -i, -S, and -v. */ 24/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
25/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ 25/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
26 26
27/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) 27#include <stdio.h>
28 *
29 * Fixed bug involving -n option. Essentially, -n was always in effect.
30 */
31
32#include <stdlib.h> 28#include <stdlib.h>
33#include <unistd.h> 29#include <unistd.h>
30#include <errno.h>
34#include "busybox.h" 31#include "busybox.h"
35 32
36#define LN_SYMLINK 1 33#define LN_SYMLINK 1
37#define LN_FORCE 2 34#define LN_FORCE 2
38#define LN_NODEREFERENCE 4 35#define LN_NODEREFERENCE 4
36#define LN_BACKUP 8
37#define LN_SUFFIX 16
39 38
40extern int ln_main(int argc, char **argv) 39extern int ln_main(int argc, char **argv)
41{ 40{
@@ -44,10 +43,11 @@ extern int ln_main(int argc, char **argv)
44 char *last; 43 char *last;
45 char *src_name; 44 char *src_name;
46 char *src; 45 char *src;
46 char *suffix = "~";
47 struct stat statbuf; 47 struct stat statbuf;
48 int (*link_func)(const char *, const char *); 48 int (*link_func)(const char *, const char *);
49 49
50 flag = bb_getopt_ulflags(argc, argv, "sfn"); 50 flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix);
51 51
52 if (argc == optind) { 52 if (argc == optind) {
53 bb_show_usage(); 53 bb_show_usage();
@@ -80,7 +80,23 @@ extern int ln_main(int argc, char **argv)
80 continue; 80 continue;
81 } 81 }
82 82
83 if (flag & LN_FORCE) { 83 if (flag & LN_BACKUP) {
84 char *backup = NULL;
85 bb_xasprintf(&backup, "%s%s", src, suffix);
86 if (rename(src, backup) < 0 && errno != ENOENT) {
87 bb_perror_msg(src);
88 status = EXIT_FAILURE;
89 free(backup);
90 continue;
91 }
92 free(backup);
93 /*
94 * When the source and dest are both hard links to the same
95 * inode, a rename may succeed even though nothing happened.
96 * Therefore, always unlink().
97 */
98 unlink(src);
99 } else if (flag & LN_FORCE) {
84 unlink(src); 100 unlink(src);
85 } 101 }
86 102