blob: 1e30e2b2914b8f7321bb683456354fcf552e2293 [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
30static const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n"
Eric Andersencc8ed391999-10-05 16:24:54 +000031"\n"
32"\tAdd a new name that refers to the same file as \"original-name\"\n"
33"\n"
34"\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n"
35"\t-f:\tRemove existing destination files.\n";
36
Eric Andersenf6be9441999-10-13 21:12:06 +000037
38static int symlinkFlag = FALSE;
39static int removeoldFlag = FALSE;
40static const char *destName;
41
42
43extern int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000044{
Eric Andersenf6be9441999-10-13 21:12:06 +000045 int status;
46 char newdestName[NAME_MAX];
Eric Andersencc8ed391999-10-05 16:24:54 +000047
Eric Andersenf6be9441999-10-13 21:12:06 +000048 if (argc < 3) {
Eric Andersenb0e9a701999-10-18 22:28:26 +000049 usage (ln_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000050 }
51 argc--;
52 argv++;
53
54 /* Parse any options */
55 while (**argv == '-') {
56 while (*++(*argv))
57 switch (**argv) {
58 case 's':
59 symlinkFlag = TRUE;
60 break;
61 case 'f':
62 removeoldFlag = TRUE;
63 break;
64 default:
Eric Andersenb0e9a701999-10-18 22:28:26 +000065 usage (ln_usage);
Eric Andersenf6be9441999-10-13 21:12:06 +000066 }
67 argc--;
68 argv++;
69 }
70
71
72 destName = argv[argc - 1];
73
74 if ((argc > 3) && !(isDirectory(destName))) {
75 fprintf(stderr, "%s: not a directory\n", destName);
76 exit (FALSE);
77 }
78
79 while (argc-- >= 2) {
80 strcpy(newdestName, destName);
81 strcat(newdestName, (*argv)+(strlen(*(++argv))));
82
83 if (removeoldFlag==TRUE ) {
84 status = ( unlink(newdestName) && errno != ENOENT );
85 if ( status != 0 ) {
86 perror(newdestName);
87 exit( FALSE);
88 }
Eric Andersencc8ed391999-10-05 16:24:54 +000089 }
Eric Andersenf6be9441999-10-13 21:12:06 +000090 if ( symlinkFlag==TRUE)
91 status = symlink(*argv, newdestName);
Eric Andersencc8ed391999-10-05 16:24:54 +000092 else
Eric Andersenf6be9441999-10-13 21:12:06 +000093 status = link(*argv, newdestName);
94 if ( status != 0 ) {
95 perror(newdestName);
96 exit( FALSE);
97 }
98 }
99 exit( TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +0000100}