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