1 /* vi: set sw=4 ts=4: */
3 * Mini ln implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* BB_AUDIT SUSv3 compliant */
11 /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
16 /* This is a NOEXEC applet. Be very careful! */
21 #define LN_NODEREFERENCE 4
25 int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
26 int ln_main(int argc, char **argv)
28 int status = EXIT_SUCCESS;
33 char *suffix = (char*)"~";
35 int (*link_func)(const char *, const char *);
37 opt_complementary = "-1"; /* min one arg */
38 opts = getopt32(argv, "sfnbS:", &suffix);
40 last = argv[argc - 1];
43 if (argc == optind + 1) {
45 last = bb_get_last_path_component_strip(xstrdup(last));
53 (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
56 src_name = xstrdup(*argv);
57 src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
61 if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
62 // coreutils: "ln dangling_symlink new_hardlink" works
63 if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
64 bb_simple_perror_msg(*argv);
65 status = EXIT_FAILURE;
71 if (opts & LN_BACKUP) {
73 backup = xasprintf("%s%s", src, suffix);
74 if (rename(src, backup) < 0 && errno != ENOENT) {
75 bb_simple_perror_msg(src);
76 status = EXIT_FAILURE;
82 * When the source and dest are both hard links to the same
83 * inode, a rename may succeed even though nothing happened.
84 * Therefore, always unlink().
87 } else if (opts & LN_FORCE) {
92 if (opts & LN_SYMLINK) {
96 if (link_func(*argv, src) != 0) {
97 bb_simple_perror_msg(src);
98 status = EXIT_FAILURE;
103 } while ((++argv)[1]);