blob: f977aa17774d96a14c42b0ddc0110a7913541505 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6be9441999-10-13 21:12:06 +00002/*
3 * Mini ln implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00006 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenf6be9441999-10-13 21:12:06 +00008 */
9
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* BB_AUDIT SUSv3 compliant */
Rob Landley3071e2f2005-04-29 22:13:04 +000011/* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000012/* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */
13
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000014#include "libbb.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000015
Denis Vlasenko99912ca2007-04-10 15:43:37 +000016/* This is a NOEXEC applet. Be very careful! */
17
18
Manuel Novoa III cad53642003-03-19 09:13:01 +000019#define LN_SYMLINK 1
20#define LN_FORCE 2
21#define LN_NODEREFERENCE 4
Rob Landley3071e2f2005-04-29 22:13:04 +000022#define LN_BACKUP 8
23#define LN_SUFFIX 16
Eric Andersenf6be9441999-10-13 21:12:06 +000024
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000025int ln_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000026int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000027{
Eric Andersen13241df2000-10-04 16:02:53 +000028 int status = EXIT_SUCCESS;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010029 int opts;
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 char *last;
31 char *src_name;
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000032 char *src;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000033 char *suffix = (char*)"~";
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000034 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000035 int (*link_func)(const char *, const char *);
36
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010037 opt_complementary = "-1"; /* min one arg */
38 opts = getopt32(argv, "sfnbS:", &suffix);
Manuel Novoa III cad53642003-03-19 09:13:01 +000039
40 last = argv[argc - 1];
41 argv += optind;
42
43 if (argc == optind + 1) {
44 *--argv = last;
Denis Vlasenko818322b2007-09-24 18:27:04 +000045 last = bb_get_last_path_component_strip(xstrdup(last));
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 }
47
48 do {
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000049 src_name = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 src = last;
51
52 if (is_directory(src,
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010053 (opts & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +000054 NULL)
55 ) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000056 src_name = xstrdup(*argv);
Denis Vlasenko818322b2007-09-24 18:27:04 +000057 src = concat_path_file(src, bb_get_last_path_component_strip(src_name));
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 free(src_name);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000059 src_name = src;
60 }
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010061 if (!(opts & LN_SYMLINK) && stat(*argv, &statbuf)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000062 // coreutils: "ln dangling_symlink new_hardlink" works
63 if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000064 bb_simple_perror_msg(*argv);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000065 status = EXIT_FAILURE;
66 free(src_name);
67 continue;
68 }
Eric Andersen815e9042000-06-06 16:15:23 +000069 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000070
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010071 if (opts & LN_BACKUP) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000072 char *backup;
73 backup = xasprintf("%s%s", src, suffix);
74 if (rename(src, backup) < 0 && errno != ENOENT) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000075 bb_simple_perror_msg(src);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000076 status = EXIT_FAILURE;
Rob Landley3071e2f2005-04-29 22:13:04 +000077 free(backup);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000078 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 Vlasenkoe992bae2009-11-28 15:18:53 +010087 } else if (opts & LN_FORCE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000088 unlink(src);
89 }
90
91 link_func = link;
Denys Vlasenkoe992bae2009-11-28 15:18:53 +010092 if (opts & LN_SYMLINK) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000093 link_func = symlink;
94 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000095
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 if (link_func(*argv, src) != 0) {
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +000097 bb_simple_perror_msg(src);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000098 status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 }
100
101 free(src_name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000102
Manuel Novoa III cad53642003-03-19 09:13:01 +0000103 } while ((++argv)[1]);
104
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000105 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000106}