1 /* vi: set sw=4 ts=4: */
3 * Mini ln implementation for busybox
6 * Copyright (C) 1999 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
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.
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
26 #define BB_DECLARE_EXTERN
27 #define bb_need_name_too_long
28 #define bb_need_not_a_directory
34 #include <sys/param.h> /* for PATH_MAX */
36 static const char ln_usage[] =
37 "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n"
38 "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
40 "\t-s\tmake symbolic links instead of hard links\n"
42 "\t-f\tremove existing destination files\n"
43 "\t-n\tno dereference symlinks - treat like normal file\n";
45 static int symlinkFlag = FALSE;
46 static int removeoldFlag = FALSE;
47 static int followLinks = TRUE;
49 extern int ln_main(int argc, char **argv)
60 /* Parse any options */
61 while (**argv == '-') {
80 linkName = argv[argc - 1];
82 if (strlen(linkName) > PATH_MAX) {
83 fprintf(stderr, name_too_long, "ln");
87 linkIntoDirFlag = isDirectory(linkName, TRUE, NULL);
89 if ((argc > 3) && !linkIntoDirFlag) {
90 fprintf(stderr, not_a_directory, "ln", linkName);
95 char srcName[PATH_MAX + 1];
98 if (strlen(*argv) > PATH_MAX) {
99 fprintf(stderr, name_too_long, "ln");
103 if (followLinks == FALSE) {
104 strcpy(srcName, *argv);
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';
112 if (removeoldFlag == TRUE) {
113 status = (unlink(linkName) && errno != ENOENT);
120 if (symlinkFlag == TRUE)
121 status = symlink(*argv, linkName);
123 status = link(*argv, linkName);
134 c-file-style: "linux"