*: remove "Options:" string from help texts
[oweals/busybox.git] / coreutils / ln.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ln implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  */
9
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 */
13
14 //usage:#define ln_trivial_usage
15 //usage:       "[OPTIONS] TARGET... LINK|DIR"
16 //usage:#define ln_full_usage "\n\n"
17 //usage:       "Create a link LINK or DIR/TARGET to the specified TARGET(s)\n"
18 //usage:     "\n        -s      Make symlinks instead of hardlinks"
19 //usage:     "\n        -f      Remove existing destinations"
20 //usage:     "\n        -n      Don't dereference symlinks - treat like normal file"
21 //usage:     "\n        -b      Make a backup of the target (if exists) before link operation"
22 //usage:     "\n        -S suf  Use suffix instead of ~ when making backup files"
23 //usage:
24 //usage:#define ln_example_usage
25 //usage:       "$ ln -s BusyBox /tmp/ls\n"
26 //usage:       "$ ls -l /tmp/ls\n"
27 //usage:       "lrwxrwxrwx    1 root     root            7 Apr 12 18:39 ls -> BusyBox*\n"
28
29 #include "libbb.h"
30
31 /* This is a NOEXEC applet. Be very careful! */
32
33
34 #define LN_SYMLINK          1
35 #define LN_FORCE            2
36 #define LN_NODEREFERENCE    4
37 #define LN_BACKUP           8
38 #define LN_SUFFIX           16
39
40 int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
41 int ln_main(int argc, char **argv)
42 {
43         int status = EXIT_SUCCESS;
44         int opts;
45         char *last;
46         char *src_name;
47         char *src;
48         char *suffix = (char*)"~";
49         struct stat statbuf;
50         int (*link_func)(const char *, const char *);
51
52         opt_complementary = "-1"; /* min one arg */
53         opts = getopt32(argv, "sfnbS:", &suffix);
54
55         last = argv[argc - 1];
56         argv += optind;
57
58         if (!argv[1]) {
59                 /* "ln PATH/TO/FILE" -> "ln PATH/TO/FILE FILE" */
60                 *--argv = last;
61                 /* xstrdup is needed: "ln -s PATH/TO/FILE/" is equivalent to
62                  * "ln -s PATH/TO/FILE/ FILE", not "ln -s PATH/TO/FILE FILE"
63                  */
64                 last = bb_get_last_path_component_strip(xstrdup(last));
65         }
66
67         do {
68                 src_name = NULL;
69                 src = last;
70
71                 if (is_directory(src,
72                                 (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
73                                 NULL)
74                 ) {
75                         src_name = xstrdup(*argv);
76                         src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
77                         free(src_name);
78                         src_name = src;
79                 }
80                 if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
81                         // coreutils: "ln dangling_symlink new_hardlink" works
82                         if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
83                                 bb_simple_perror_msg(*argv);
84                                 status = EXIT_FAILURE;
85                                 free(src_name);
86                                 continue;
87                         }
88                 }
89
90                 if (opts & LN_BACKUP) {
91                         char *backup;
92                         backup = xasprintf("%s%s", src, suffix);
93                         if (rename(src, backup) < 0 && errno != ENOENT) {
94                                 bb_simple_perror_msg(src);
95                                 status = EXIT_FAILURE;
96                                 free(backup);
97                                 continue;
98                         }
99                         free(backup);
100                         /*
101                          * When the source and dest are both hard links to the same
102                          * inode, a rename may succeed even though nothing happened.
103                          * Therefore, always unlink().
104                          */
105                         unlink(src);
106                 } else if (opts & LN_FORCE) {
107                         unlink(src);
108                 }
109
110                 link_func = link;
111                 if (opts & LN_SYMLINK) {
112                         link_func = symlink;
113                 }
114
115                 if (link_func(*argv, src) != 0) {
116                         bb_simple_perror_msg(src);
117                         status = EXIT_FAILURE;
118                 }
119
120                 free(src_name);
121
122         } while ((++argv)[1]);
123
124         return status;
125 }