blob: 60fe394386a8a7f7a3441da9e4f6ba8cb0dab6e2 [file] [log] [blame]
Eric Andersenf6be9441999-10-13 21:12:06 +00001/*
2 * Mini ln implementation for busybox
3 *
Eric Andersenc4996011999-10-20 22:08:37 +00004 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include "internal.h"
25#include <stdio.h>
Eric Andersenf6be9441999-10-13 21:12:06 +000026#include <dirent.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000027#include <errno.h>
28
Eric Andersenf6be9441999-10-13 21:12:06 +000029
Eric Andersend73dc5b1999-11-10 23:13:02 +000030static const char ln_usage[] = "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n"
31"Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
32"Options:\n"
Eric Andersen29d2e361999-11-06 06:07:27 +000033"\t-s\tmake symbolic links instead of hard links\n"
34"\t-f\tremove existing destination files\n";
Eric Andersencc8ed391999-10-05 16:24:54 +000035
Eric Andersenf6be9441999-10-13 21:12:06 +000036
37static int symlinkFlag = FALSE;
38static int removeoldFlag = FALSE;
Eric Andersenf6be9441999-10-13 21:12:06 +000039
40
41extern int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000042{
Eric Andersenf6be9441999-10-13 21:12:06 +000043 int status;
Eric Andersen29d2e361999-11-06 06:07:27 +000044 static char* linkName;
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Eric Andersenf6be9441999-10-13 21:12:06 +000046 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000047 usage (ln_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000048 }
49 argc--;
50 argv++;
51
52 /* Parse any options */
53 while (**argv == '-') {
54 while (*++(*argv))
55 switch (**argv) {
56 case 's':
57 symlinkFlag = TRUE;
58 break;
59 case 'f':
60 removeoldFlag = TRUE;
61 break;
62 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000063 usage (ln_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000064 }
65 argc--;
66 argv++;
67 }
68
69
Eric Andersen29d2e361999-11-06 06:07:27 +000070 linkName = argv[argc - 1];
Eric Andersenf6be9441999-10-13 21:12:06 +000071
Eric Andersen29d2e361999-11-06 06:07:27 +000072 if ((argc > 3) && !(isDirectory(linkName))) {
73 fprintf(stderr, "%s: not a directory\n", linkName);
Eric Andersenf6be9441999-10-13 21:12:06 +000074 exit (FALSE);
75 }
76
77 while (argc-- >= 2) {
Eric Andersenf6be9441999-10-13 21:12:06 +000078 if (removeoldFlag==TRUE ) {
Eric Andersen29d2e361999-11-06 06:07:27 +000079 status = ( unlink(linkName) && errno != ENOENT );
Eric Andersenf6be9441999-10-13 21:12:06 +000080 if ( status != 0 ) {
Eric Andersen29d2e361999-11-06 06:07:27 +000081 perror(linkName);
Eric Andersenf6be9441999-10-13 21:12:06 +000082 exit( FALSE);
83 }
Eric Andersencc8ed391999-10-05 16:24:54 +000084 }
Eric Andersenf6be9441999-10-13 21:12:06 +000085 if ( symlinkFlag==TRUE)
Eric Andersen29d2e361999-11-06 06:07:27 +000086 status = symlink(*argv, linkName);
Eric Andersencc8ed391999-10-05 16:24:54 +000087 else
Eric Andersen29d2e361999-11-06 06:07:27 +000088 status = link(*argv, linkName);
Eric Andersenf6be9441999-10-13 21:12:06 +000089 if ( status != 0 ) {
Eric Andersen29d2e361999-11-06 06:07:27 +000090 perror(linkName);
Eric Andersenf6be9441999-10-13 21:12:06 +000091 exit( FALSE);
92 }
93 }
94 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000095}