Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini ln implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 6 | * |
"Robert P. J. Day" | 801ab14 | 2006-07-12 07:56:04 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* BB_AUDIT SUSv3 compliant */ |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 11 | /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ |
| 13 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 14 | #include "libbb.h" |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 16 | /* This is a NOEXEC applet. Be very careful! */ |
| 17 | |
| 18 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | #define LN_SYMLINK 1 |
| 20 | #define LN_FORCE 2 |
| 21 | #define LN_NODEREFERENCE 4 |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 22 | #define LN_BACKUP 8 |
| 23 | #define LN_SUFFIX 16 |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 24 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 25 | int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 26 | int ln_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | { |
Eric Andersen | 13241df | 2000-10-04 16:02:53 +0000 | [diff] [blame] | 28 | int status = EXIT_SUCCESS; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 29 | int opts; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | char *last; |
| 31 | char *src_name; |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 32 | char *src; |
Denis Vlasenko | a41fdf3 | 2007-01-29 22:51:00 +0000 | [diff] [blame] | 33 | char *suffix = (char*)"~"; |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 34 | struct stat statbuf; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | int (*link_func)(const char *, const char *); |
| 36 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 37 | opt_complementary = "-1"; /* min one arg */ |
| 38 | opts = getopt32(argv, "sfnbS:", &suffix); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 39 | |
| 40 | last = argv[argc - 1]; |
| 41 | argv += optind; |
| 42 | |
| 43 | if (argc == optind + 1) { |
| 44 | *--argv = last; |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 45 | last = bb_get_last_path_component_strip(xstrdup(last)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | do { |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 49 | src_name = NULL; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | src = last; |
| 51 | |
| 52 | if (is_directory(src, |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 53 | (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 54 | NULL) |
| 55 | ) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 56 | src_name = xstrdup(*argv); |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 57 | src = concat_path_file(src, bb_get_last_path_component_strip(src_name)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | free(src_name); |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 59 | src_name = src; |
| 60 | } |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 61 | if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 62 | // coreutils: "ln dangling_symlink new_hardlink" works |
| 63 | if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 64 | bb_simple_perror_msg(*argv); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 65 | status = EXIT_FAILURE; |
| 66 | free(src_name); |
| 67 | continue; |
| 68 | } |
Eric Andersen | 815e904 | 2000-06-06 16:15:23 +0000 | [diff] [blame] | 69 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 71 | if (opts & LN_BACKUP) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 72 | char *backup; |
| 73 | backup = xasprintf("%s%s", src, suffix); |
| 74 | if (rename(src, backup) < 0 && errno != ENOENT) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 75 | bb_simple_perror_msg(src); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 76 | status = EXIT_FAILURE; |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 77 | free(backup); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 78 | continue; |
| 79 | } |
| 80 | free(backup); |
| 81 | /* |
| 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(). |
| 85 | */ |
| 86 | unlink(src); |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 87 | } else if (opts & LN_FORCE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | unlink(src); |
| 89 | } |
| 90 | |
| 91 | link_func = link; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 92 | if (opts & LN_SYMLINK) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 93 | link_func = symlink; |
| 94 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 95 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | if (link_func(*argv, src) != 0) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 97 | bb_simple_perror_msg(src); |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 98 | status = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | free(src_name); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 102 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 103 | } while ((++argv)[1]); |
| 104 | |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 105 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 106 | } |