blob: bc51cb0d58cd55584de1f84929d71d63721164f0 [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 Andersenc4996011999-10-20 22:08:37 +00005 *
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Eric Andersencc8ed391999-10-05 16:24:54 +000025#include "internal.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000026#define BB_DECLARE_EXTERN
27#define bb_need_name_too_long
28#define bb_need_not_a_directory
29#include "messages.c"
30
Eric Andersencc8ed391999-10-05 16:24:54 +000031#include <stdio.h>
Eric Andersenf6be9441999-10-13 21:12:06 +000032#include <dirent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000033#include <errno.h>
Erik Andersene49d5ec2000-02-08 19:58:47 +000034#include <sys/param.h> /* for PATH_MAX */
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Erik Andersenfac10d72000-02-07 05:29:42 +000036static const char ln_usage[] =
Erik Andersene49d5ec2000-02-08 19:58:47 +000037 "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n"
38 "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
39 "Options:\n"
40 "\t-s\tmake symbolic links instead of hard links\n"
41
42 "\t-f\tremove existing destination files\n"
43 "\t-n\tno dereference symlinks - treat like normal file\n";
Eric Andersenf6be9441999-10-13 21:12:06 +000044
45static int symlinkFlag = FALSE;
46static int removeoldFlag = FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +000047static int followLinks = TRUE;
Eric Andersenf6be9441999-10-13 21:12:06 +000048
49extern int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000050{
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 char *linkName;
52 int linkIntoDirFlag;
Eric Andersencc8ed391999-10-05 16:24:54 +000053
Erik Andersene49d5ec2000-02-08 19:58:47 +000054 if (argc < 3) {
55 usage(ln_usage);
56 }
Eric Andersenf6be9441999-10-13 21:12:06 +000057 argc--;
58 argv++;
Eric Andersenf6be9441999-10-13 21:12:06 +000059
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 /* Parse any options */
61 while (**argv == '-') {
62 while (*++(*argv))
63 switch (**argv) {
64 case 's':
65 symlinkFlag = TRUE;
66 break;
67 case 'f':
68 removeoldFlag = TRUE;
69 break;
70 case 'n':
71 followLinks = FALSE;
72 break;
73 default:
74 usage(ln_usage);
75 }
76 argc--;
77 argv++;
Erik Andersenfac10d72000-02-07 05:29:42 +000078 }
79
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 linkName = argv[argc - 1];
Erik Andersenfac10d72000-02-07 05:29:42 +000081
Erik Andersene49d5ec2000-02-08 19:58:47 +000082 if (strlen(linkName) > PATH_MAX) {
83 fprintf(stderr, name_too_long, "ln");
Erik Andersenfac10d72000-02-07 05:29:42 +000084 exit FALSE;
Eric Andersencc8ed391999-10-05 16:24:54 +000085 }
Erik Andersenfac10d72000-02-07 05:29:42 +000086
Erik Andersene49d5ec2000-02-08 19:58:47 +000087 linkIntoDirFlag = isDirectory(linkName, TRUE);
88
89 if ((argc > 3) && !linkIntoDirFlag) {
90 fprintf(stderr, not_a_directory, "ln", linkName);
91 exit FALSE;
Eric Andersenf6be9441999-10-13 21:12:06 +000092 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000093
94 while (argc-- >= 2) {
95 char srcName[PATH_MAX + 1];
96 int nChars, status;
97
98 if (strlen(*argv) > PATH_MAX) {
99 fprintf(stderr, name_too_long, "ln");
100 exit FALSE;
101 }
102
103 if (followLinks == FALSE) {
104 strcpy(srcName, *argv);
105 } else {
106 /* Warning! This can silently truncate if > PATH_MAX, but
107 I don't think that there can be one > PATH_MAX anyway. */
108 nChars = readlink(*argv, srcName, PATH_MAX);
109 srcName[nChars] = '\0';
110 }
111
112 if (removeoldFlag == TRUE) {
113 status = (unlink(linkName) && errno != ENOENT);
114 if (status != 0) {
115 perror(linkName);
116 exit FALSE;
117 }
118 }
119
120 if (symlinkFlag == TRUE)
121 status = symlink(*argv, linkName);
122 else
123 status = link(*argv, linkName);
124 if (status != 0) {
125 perror(linkName);
126 exit FALSE;
127 }
128 }
129 exit TRUE;
Eric Andersencc8ed391999-10-05 16:24:54 +0000130}