blob: 720713475cada2c607320cad3e2425b483ed0a33 [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
Eric Andersencbe31da2001-02-20 06:14:08 +000014#include "busybox.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000015
Manuel Novoa III cad53642003-03-19 09:13:01 +000016#define LN_SYMLINK 1
17#define LN_FORCE 2
18#define LN_NODEREFERENCE 4
Rob Landley3071e2f2005-04-29 22:13:04 +000019#define LN_BACKUP 8
20#define LN_SUFFIX 16
Eric Andersenf6be9441999-10-13 21:12:06 +000021
Denis Vlasenko06af2162007-02-03 17:28:39 +000022int ln_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +000023int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000024{
Eric Andersen13241df2000-10-04 16:02:53 +000025 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 int flag;
27 char *last;
28 char *src_name;
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000029 char *src;
Denis Vlasenkoa41fdf32007-01-29 22:51:00 +000030 char *suffix = (char*)"~";
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000031 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000032 int (*link_func)(const char *, const char *);
33
Denis Vlasenko67b23e62006-10-03 21:00:06 +000034 flag = getopt32(argc, argv, "sfnbS:", &suffix);
Manuel Novoa III cad53642003-03-19 09:13:01 +000035
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 Landleyd921b2e2006-08-03 15:41:12 +000045 last = bb_get_last_path_component(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,
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000053 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
54 NULL)) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000055 src_name = xstrdup(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 src = concat_path_file(src, bb_get_last_path_component(src_name));
57 free(src_name);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000058 src_name = src;
59 }
Glenn L McGrath95f75a32004-01-08 10:51:09 +000060 if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000061 // 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 Andersen815e9042000-06-06 16:15:23 +000068 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000069
Rob Landley3071e2f2005-04-29 22:13:04 +000070 if (flag & LN_BACKUP) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000071 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 Landley3071e2f2005-04-29 22:13:04 +000076 free(backup);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000077 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 Landley3071e2f2005-04-29 22:13:04 +000086 } else if (flag & LN_FORCE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 unlink(src);
88 }
89
90 link_func = link;
91 if (flag & LN_SYMLINK) {
92 link_func = symlink;
93 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 if (link_func(*argv, src) != 0) {
Rob Landleycdbae772005-12-12 06:49:33 +000096 bb_perror_msg("%s", src);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000097 status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 }
99
100 free(src_name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000101
Manuel Novoa III cad53642003-03-19 09:13:01 +0000102 } while ((++argv)[1]);
103
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000104 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000105}