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