Fixed up copyright notices and such
[oweals/busybox.git] / coreutils / ln.c
1 /*
2  * Mini ln implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7  *
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
24 #include "internal.h"
25 #include <stdio.h>
26 #include <dirent.h>
27 #include <errno.h>
28
29
30 static const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n"
31 "\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
37
38 static int symlinkFlag = FALSE;
39 static int removeoldFlag = FALSE;
40 static const char *destName;
41
42
43 extern int ln_main(int argc, char **argv)
44 {
45     int status;
46     char newdestName[NAME_MAX];
47
48     if (argc < 3) {
49         usage (ln_usage);
50     }
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:
65                 usage (ln_usage);
66             }
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             }
89         }
90         if ( symlinkFlag==TRUE)
91                 status = symlink(*argv, newdestName);
92         else
93                 status = link(*argv, newdestName);
94         if ( status != 0 ) {
95             perror(newdestName);
96             exit( FALSE);
97         }
98     }
99     exit( TRUE);
100 }