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 */
18 #define LN_NODEREFERENCE 4
22 int ln_main(int argc, char **argv)
24 int status = EXIT_SUCCESS;
31 int (*link_func)(const char *, const char *);
33 flag = getopt32(argc, argv, "sfnbS:", &suffix);
39 last = argv[argc - 1];
42 if (argc == optind + 1) {
44 last = bb_get_last_path_component(xstrdup(last));
52 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
54 src_name = xstrdup(*argv);
55 src = concat_path_file(src, bb_get_last_path_component(src_name));
59 if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
60 // coreutils: "ln dangling_symlink new_hardlink" works
61 if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
62 bb_perror_msg("%s", *argv);
63 status = EXIT_FAILURE;
69 if (flag & LN_BACKUP) {
71 backup = xasprintf("%s%s", src, suffix);
72 if (rename(src, backup) < 0 && errno != ENOENT) {
73 bb_perror_msg("%s", src);
74 status = EXIT_FAILURE;
80 * When the source and dest are both hard links to the same
81 * inode, a rename may succeed even though nothing happened.
82 * Therefore, always unlink().
85 } else if (flag & LN_FORCE) {
90 if (flag & LN_SYMLINK) {
94 if (link_func(*argv, src) != 0) {
95 bb_perror_msg("%s", src);
96 status = EXIT_FAILURE;
101 } while ((++argv)[1]);