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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 /* BB_AUDIT SUSv3 compliant */
24 /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
25 /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
35 #define LN_NODEREFERENCE 4
39 extern int ln_main(int argc, char **argv)
41 int status = EXIT_SUCCESS;
48 int (*link_func)(const char *, const char *);
50 flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix);
56 last = argv[argc - 1];
59 if (argc == optind + 1) {
61 last = bb_get_last_path_component(bb_xstrdup(last));
69 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
71 src_name = bb_xstrdup(*argv);
72 src = concat_path_file(src, bb_get_last_path_component(src_name));
76 if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
78 status = EXIT_FAILURE;
83 if (flag & LN_BACKUP) {
85 bb_xasprintf(&backup, "%s%s", src, suffix);
86 if (rename(src, backup) < 0 && errno != ENOENT) {
88 status = EXIT_FAILURE;
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().
99 } else if (flag & LN_FORCE) {
104 if (flag & LN_SYMLINK) {
108 if (link_func(*argv, src) != 0) {
110 status = EXIT_FAILURE;
115 } while ((++argv)[1]);