Renamed "internal.h" to the more sensible "busybox.h".
[oweals/busybox.git] / coreutils / ln.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ln implementation for busybox
4  *
5  * Copyright (C) 1999,2000 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 "busybox.h"
25 #define BB_DECLARE_EXTERN
26 #define bb_need_not_a_directory
27 #include "messages.c"
28
29 #include <stdio.h>
30 #include <dirent.h>
31 #include <errno.h>
32
33 static int symlinkFlag = FALSE;
34 static int removeoldFlag = FALSE;
35 static int followLinks = TRUE;
36
37 extern int ln_main(int argc, char **argv)
38 {
39         char *linkName, *dirName=NULL;
40         int linkIntoDirFlag;
41         int stopIt = FALSE;
42
43         argc--;
44         argv++;
45
46         /* Parse any options */
47         while (argc > 0 && stopIt == FALSE) {
48                 if (**argv == '-') {
49                         while (*++(*argv))
50                                 switch (**argv) {
51                                         case 's':
52                                                 symlinkFlag = TRUE;
53                                                 break;
54                                         case 'f':
55                                                 removeoldFlag = TRUE;
56                                                 break;
57                                         case 'n':
58                                                 followLinks = FALSE;
59                                                 break;
60                                         case '-':
61                                                 stopIt = TRUE;
62                                                 break;
63                                         default:
64                                                 usage(ln_usage);
65                                 }
66                         argc--;
67                         argv++;
68                 }
69                 else
70                         break;
71         }
72
73         if (argc < 2) {
74                 usage(ln_usage);
75         }
76
77         linkName = argv[argc - 1];
78
79         linkIntoDirFlag = isDirectory(linkName, followLinks, NULL);
80         if ((argc >= 3) && linkIntoDirFlag == FALSE) {
81                 errorMsg(not_a_directory, linkName);
82                 exit FALSE;
83         }
84
85         if (linkIntoDirFlag == TRUE)
86                 dirName = linkName;
87
88         while (argc-- >= 2) {
89                 int status;
90
91                 if (linkIntoDirFlag == TRUE) {
92                         char *baseName = get_last_path_component(*argv);
93                         linkName = (char *)xmalloc(strlen(dirName)+strlen(baseName)+2);
94                         strcpy(linkName, dirName);
95                         if(dirName[strlen(dirName)-1] != '/')
96                                 strcat(linkName, "/");
97                         strcat(linkName,baseName);
98                 }
99
100                 if (removeoldFlag == TRUE) {
101                         status = (unlink(linkName) && errno != ENOENT);
102                         if (status != 0) {
103                                 perror(linkName);
104                                 exit FALSE;
105                         }
106                 }
107
108                 if (symlinkFlag == TRUE)
109                         status = symlink(*argv, linkName);
110                 else
111                         status = link(*argv, linkName);
112                 if (status != 0) {
113                         perror(linkName);
114                         exit FALSE;
115                 }
116
117                 if (linkIntoDirFlag == TRUE)
118                         free(linkName);
119
120                 argv++;
121         }
122         return( TRUE);
123 }
124
125 /*
126 Local Variables:
127 c-file-style: "linux"
128 c-basic-offset: 4
129 tab-width: 4
130 End:
131 */