summaryrefslogtreecommitdiff
path: root/ln.c
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-10-13 21:12:06 +0000
committerEric Andersen <andersen@codepoet.org>1999-10-13 21:12:06 +0000
commitf6be944a6ae612c70ce010582d9c3cdd72f7144f (patch)
tree36ab89d86aad17d6922566cad8e49ba33c5ede81 /ln.c
parent305a73f5eacd94eefe2f2a5a00034d579ef6b1e7 (diff)
downloadbusybox-w32-f6be944a6ae612c70ce010582d9c3cdd72f7144f.tar.gz
busybox-w32-f6be944a6ae612c70ce010582d9c3cdd72f7144f.tar.bz2
busybox-w32-f6be944a6ae612c70ce010582d9c3cdd72f7144f.zip
More stuff
Diffstat (limited to 'ln.c')
-rw-r--r--ln.c114
1 files changed, 81 insertions, 33 deletions
diff --git a/ln.c b/ln.c
index 3e87b579e..cd3cb4e45 100644
--- a/ln.c
+++ b/ln.c
@@ -1,52 +1,100 @@
1/*
2 * Mini ln implementation for busybox
3 *
4 * Copyright (C) 1998 by Erik Andersen <andersee@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
1#include "internal.h" 22#include "internal.h"
2#include <stdio.h> 23#include <stdio.h>
3#include <sys/stat.h> 24#include <dirent.h>
4#include <sys/param.h>
5#include <errno.h> 25#include <errno.h>
6 26
7const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n" 27
28static const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n"
8"\n" 29"\n"
9"\tAdd a new name that refers to the same file as \"original-name\"\n" 30"\tAdd a new name that refers to the same file as \"original-name\"\n"
10"\n" 31"\n"
11"\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n" 32"\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n"
12"\t-f:\tRemove existing destination files.\n"; 33"\t-f:\tRemove existing destination files.\n";
13 34
14int 35
15ln_fn(const struct FileInfo * i) 36static int symlinkFlag = FALSE;
37static int removeoldFlag = FALSE;
38static const char *destName;
39
40
41extern int ln_main(int argc, char **argv)
16{ 42{
17 int status = 0; 43 int status;
18 char d[PATH_MAX]; 44 char newdestName[NAME_MAX];
19 const char * destination = i->destination;
20 45
21 if ( !i->makeSymbolicLink && (i->stat.st_mode & S_IFMT) == S_IFDIR ) { 46 if (argc < 3) {
22 fprintf(stderr, "Please use \"ln -s\" to link directories.\n"); 47 fprintf(stderr, "Usage: %s", ln_usage);
23 return 1; 48 exit (FALSE);
24 } 49 }
50 argc--;
51 argv++;
25 52
26 /* 53 /* Parse any options */
27 * If the destination is a directory, create a file within it. 54 while (**argv == '-') {
28 */ 55 while (*++(*argv))
29 if ( is_a_directory(i->destination) ) { 56 switch (**argv) {
30 destination = join_paths( 57 case 's':
31 d 58 symlinkFlag = TRUE;
32 ,i->destination 59 break;
33 ,&i->source[i->directoryLength]); 60 case 'f':
34 } 61 removeoldFlag = TRUE;
62 break;
63 default:
64 fprintf(stderr, "Usage: %s\n", ln_usage);
65 exit(FALSE);
66 }
67 argc--;
68 argv++;
69 }
35 70
36 if ( i->force )
37 status = ( unlink(destination) && errno != ENOENT );
38 71
39 if ( status == 0 ) { 72 destName = argv[argc - 1];
40 if ( i->makeSymbolicLink )
41 status = symlink(i->source, destination);
42 else
43 status = link(i->source, destination);
44 }
45 73
46 if ( status != 0 ) { 74 if ((argc > 3) && !(isDirectory(destName))) {
47 name_and_error(destination); 75 fprintf(stderr, "%s: not a directory\n", destName);
48 return 1; 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 }
49 } 89 }
90 if ( symlinkFlag==TRUE)
91 status = symlink(*argv, newdestName);
50 else 92 else
51 return 0; 93 status = link(*argv, newdestName);
94 if ( status != 0 ) {
95 perror(newdestName);
96 exit( FALSE);
97 }
98 }
99 exit( TRUE);
52} 100}