c54026c6255ebfd988f4c90469345ed05d7ae067
[platform/upstream/busybox.git] / ln.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini ln implementation for busybox
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include "internal.h"
26 #define BB_DECLARE_EXTERN
27 #define bb_need_name_too_long
28 #define bb_need_not_a_directory
29 #include "messages.c"
30
31 #include <stdio.h>
32 #include <dirent.h>
33 #include <errno.h>
34 #include <sys/param.h>                  /* for PATH_MAX */
35
36 static const char ln_usage[] =
37         "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n"
38         "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
39         "Options:\n"
40         "\t-s\tmake symbolic links instead of hard links\n"
41
42         "\t-f\tremove existing destination files\n"
43         "\t-n\tno dereference symlinks - treat like normal file\n";
44
45 static int symlinkFlag = FALSE;
46 static int removeoldFlag = FALSE;
47 static int followLinks = TRUE;
48
49 extern int ln_main(int argc, char **argv)
50 {
51         char *linkName;
52         int linkIntoDirFlag;
53
54         if (argc < 3) {
55                 usage(ln_usage);
56         }
57         argc--;
58         argv++;
59
60         /* Parse any options */
61         while (**argv == '-') {
62                 while (*++(*argv))
63                         switch (**argv) {
64                         case 's':
65                                 symlinkFlag = TRUE;
66                                 break;
67                         case 'f':
68                                 removeoldFlag = TRUE;
69                                 break;
70                         case 'n':
71                                 followLinks = FALSE;
72                                 break;
73                         default:
74                                 usage(ln_usage);
75                         }
76                 argc--;
77                 argv++;
78         }
79
80         linkName = argv[argc - 1];
81
82         if (strlen(linkName) > PATH_MAX) {
83                 fprintf(stderr, name_too_long, "ln");
84                 exit FALSE;
85         }
86
87         linkIntoDirFlag = isDirectory(linkName, TRUE, NULL);
88
89         if ((argc > 3) && !linkIntoDirFlag) {
90                 fprintf(stderr, not_a_directory, "ln", linkName);
91                 exit FALSE;
92         }
93
94         while (argc-- >= 2) {
95                 char srcName[PATH_MAX + 1];
96                 int nChars, status;
97
98                 if (strlen(*argv) > PATH_MAX) {
99                         fprintf(stderr, name_too_long, "ln");
100                         exit FALSE;
101                 }
102
103                 if (followLinks == FALSE) {
104                         strcpy(srcName, *argv);
105                 } else {
106                         /* Warning!  This can silently truncate if > PATH_MAX, but
107                            I don't think that there can be one > PATH_MAX anyway. */
108                         nChars = readlink(*argv, srcName, PATH_MAX);
109                         srcName[nChars] = '\0';
110                 }
111
112                 if (removeoldFlag == TRUE) {
113                         status = (unlink(linkName) && errno != ENOENT);
114                         if (status != 0) {
115                                 perror(linkName);
116                                 exit FALSE;
117                         }
118                 }
119
120                 if (symlinkFlag == TRUE)
121                         status = symlink(*argv, linkName);
122                 else
123                         status = link(*argv, linkName);
124                 if (status != 0) {
125                         perror(linkName);
126                         exit FALSE;
127                 }
128         }
129         exit TRUE;
130 }
131
132 /*
133 Local Variables:
134 c-file-style: "linux"
135 c-basic-offset: 4
136 tab-width: 4
137 End:
138 */