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