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 | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 25 | int ln_main(int argc, char **argv); |
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; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 29 | int flag; |
| 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 | |
Denis Vlasenko | 67b23e6 | 2006-10-03 21:00:06 +0000 | [diff] [blame] | 37 | flag = getopt32(argc, argv, "sfnbS:", &suffix); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | |
| 39 | if (argc == optind) { |
| 40 | bb_show_usage(); |
| 41 | } |
| 42 | |
| 43 | last = argv[argc - 1]; |
| 44 | argv += optind; |
| 45 | |
| 46 | if (argc == optind + 1) { |
| 47 | *--argv = last; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 48 | last = bb_get_last_path_component(xstrdup(last)); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | do { |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 52 | src_name = NULL; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | src = last; |
| 54 | |
| 55 | if (is_directory(src, |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 56 | (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, |
| 57 | NULL)) { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 58 | src_name = xstrdup(*argv); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | src = concat_path_file(src, bb_get_last_path_component(src_name)); |
| 60 | free(src_name); |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 61 | src_name = src; |
| 62 | } |
Glenn L McGrath | 95f75a3 | 2004-01-08 10:51:09 +0000 | [diff] [blame] | 63 | if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 64 | // coreutils: "ln dangling_symlink new_hardlink" works |
| 65 | if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) { |
| 66 | bb_perror_msg("%s", *argv); |
| 67 | status = EXIT_FAILURE; |
| 68 | free(src_name); |
| 69 | continue; |
| 70 | } |
Eric Andersen | 815e904 | 2000-06-06 16:15:23 +0000 | [diff] [blame] | 71 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 73 | if (flag & LN_BACKUP) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 74 | char *backup; |
| 75 | backup = xasprintf("%s%s", src, suffix); |
| 76 | if (rename(src, backup) < 0 && errno != ENOENT) { |
| 77 | bb_perror_msg("%s", src); |
| 78 | status = EXIT_FAILURE; |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 79 | free(backup); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 80 | continue; |
| 81 | } |
| 82 | free(backup); |
| 83 | /* |
| 84 | * When the source and dest are both hard links to the same |
| 85 | * inode, a rename may succeed even though nothing happened. |
| 86 | * Therefore, always unlink(). |
| 87 | */ |
| 88 | unlink(src); |
Rob Landley | 3071e2f | 2005-04-29 22:13:04 +0000 | [diff] [blame] | 89 | } else if (flag & LN_FORCE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 90 | unlink(src); |
| 91 | } |
| 92 | |
| 93 | link_func = link; |
| 94 | if (flag & LN_SYMLINK) { |
| 95 | link_func = symlink; |
| 96 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 97 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 98 | if (link_func(*argv, src) != 0) { |
Rob Landley | cdbae77 | 2005-12-12 06:49:33 +0000 | [diff] [blame] | 99 | bb_perror_msg("%s", src); |
Glenn L McGrath | cfc0ad4 | 2003-12-31 23:10:44 +0000 | [diff] [blame] | 100 | status = EXIT_FAILURE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | free(src_name); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 104 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | } while ((++argv)[1]); |
| 106 | |
Eric Andersen | fcffa2c | 2002-04-06 05:17:57 +0000 | [diff] [blame] | 107 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 108 | } |