blob: df183581e43a7e4cb06b42f936a8751c473248e4 [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
Rob Landleydfba7412006-03-06 20:47:33 +000022int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000023{
Eric Andersen13241df2000-10-04 16:02:53 +000024 int status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +000025 int flag;
26 char *last;
27 char *src_name;
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000028 char *src;
Rob Landley3071e2f2005-04-29 22:13:04 +000029 char *suffix = "~";
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000030 struct stat statbuf;
Manuel Novoa III cad53642003-03-19 09:13:01 +000031 int (*link_func)(const char *, const char *);
32
Rob Landley3071e2f2005-04-29 22:13:04 +000033 flag = bb_getopt_ulflags(argc, argv, "sfnbS:", &suffix);
Manuel Novoa III cad53642003-03-19 09:13:01 +000034
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 Landleyd921b2e2006-08-03 15:41:12 +000044 last = bb_get_last_path_component(xstrdup(last));
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 }
46
47 do {
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000048 src_name = NULL;
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 src = last;
50
51 if (is_directory(src,
52 (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE,
53 NULL)) {
Rob Landleyd921b2e2006-08-03 15:41:12 +000054 src_name = xstrdup(*argv);
Manuel Novoa III cad53642003-03-19 09:13:01 +000055 src = concat_path_file(src, bb_get_last_path_component(src_name));
56 free(src_name);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000057 src_name = src;
58 }
Glenn L McGrath95f75a32004-01-08 10:51:09 +000059 if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000060 bb_perror_msg("%s", *argv);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000061 status = EXIT_FAILURE;
62 free(src_name);
63 continue;
Eric Andersen815e9042000-06-06 16:15:23 +000064 }
Manuel Novoa III cad53642003-03-19 09:13:01 +000065
Rob Landley3071e2f2005-04-29 22:13:04 +000066 if (flag & LN_BACKUP) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000067 char *backup;
Rob Landleyd921b2e2006-08-03 15:41:12 +000068 backup = xasprintf("%s%s", src, suffix);
Rob Landley3071e2f2005-04-29 22:13:04 +000069 if (rename(src, backup) < 0 && errno != ENOENT) {
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +000070 bb_perror_msg("%s", src);
Rob Landley3071e2f2005-04-29 22:13:04 +000071 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 cad53642003-03-19 09:13:01 +000083 unlink(src);
84 }
85
86 link_func = link;
87 if (flag & LN_SYMLINK) {
88 link_func = symlink;
89 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +000090
Manuel Novoa III cad53642003-03-19 09:13:01 +000091 if (link_func(*argv, src) != 0) {
Rob Landleycdbae772005-12-12 06:49:33 +000092 bb_perror_msg("%s", src);
Glenn L McGrathcfc0ad42003-12-31 23:10:44 +000093 status = EXIT_FAILURE;
Manuel Novoa III cad53642003-03-19 09:13:01 +000094 }
95
96 free(src_name);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000097
Manuel Novoa III cad53642003-03-19 09:13:01 +000098 } while ((++argv)[1]);
99
Eric Andersenfcffa2c2002-04-06 05:17:57 +0000100 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000101}