3 /* See Makefile for compilation details. */
6 Copyright (C) 1999-2009 Free Software Foundation, Inc.
8 This file is part of GNU Bash.
9 Bash 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 3 of the License, or
12 (at your option) any later version.
14 Bash 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
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with Bash. If not, see <http://www.gnu.org/licenses/>.
25 #include "bashtypes.h"
27 #if defined (HAVE_UNISTD_H)
31 #include "posixstat.h"
38 #include "bashgetopt.h"
45 typedef int unix_link_syscall_t __P((const char *, const char *));
47 #define LN_SYMLINK 0x01
48 #define LN_UNLINK 0x02
50 static unix_link_syscall_t *linkfn;
62 reset_internal_getopt ();
63 while ((opt = internal_getopt (list, "fs")) != -1)
86 linkfn = (flags & LN_SYMLINK) ? symlink : link;
88 if (list->next == 0) /* ln target, equivalent to ln target . */
89 return (dolink (list->word->word, ".", flags));
91 if (list->next->next == 0) /* ln target source */
92 return (dolink (list->word->word, list->next->word->word, flags));
94 /* ln target1 target2 ... directory */
96 /* find last argument: target directory, and make sure it's an existing
98 for (l = list; l->next; l = l->next)
100 sdir = l->word->word;
102 if (stat(sdir, &sb) < 0)
104 builtin_error ("%s", sdir);
105 return (EXECUTION_FAILURE);
108 if (S_ISDIR (sb.st_mode) == 0)
114 for (rval = EXECUTION_SUCCESS; list != l; list = list->next)
115 rval += dolink (list->word->word, sdir, flags);
121 mkdirpath (dir, file)
128 flen = strlen (file);
130 ret = xmalloc (2 + dlen + flen);
133 if (ret[dlen - 1] != '/')
135 strcpy (ret + dlen, file);
139 #if defined (HAVE_LSTAT)
146 dolink (src, dst, flags)
150 struct stat ssb, dsb;
154 /* If we're not doing symlinks, the source must exist and not be a
156 if ((flags & LN_SYMLINK) == 0)
158 if (stat (src, &ssb) != 0)
160 builtin_error ("%s: %s", src, strerror (errno));
161 return (EXECUTION_FAILURE);
163 if (S_ISDIR (ssb.st_mode))
166 builtin_error ("%s: %s", src, strerror (errno));
167 return (EXECUTION_FAILURE);
171 /* If the destination is a directory, create the final filename by appending
172 the basename of the source to the destination. */
174 if ((stat (dst, &dsb) == 0) && S_ISDIR (dsb.st_mode))
176 if ((p = strrchr (src, '/')) == 0)
181 dst_path = mkdirpath (dst, p);
185 exists = LSTAT (dst, &dsb) == 0;
187 /* If -f was specified, and the destination exists, unlink it. */
188 if ((flags & LN_UNLINK) && exists && unlink (dst) != 0)
190 builtin_error ("%s: cannot unlink: %s", dst, strerror (errno));
192 return (EXECUTION_FAILURE);
195 /* Perform the link. */
196 if ((*linkfn) (src, dst) != 0)
198 builtin_error ("cannot link %s to %s: %s", dst, src, strerror (errno));
200 return (EXECUTION_FAILURE);
204 return (EXECUTION_SUCCESS);
210 "Create a new directory entry with the same modes as the original",
211 "file. The -f option means to unlink any existing file, permitting",
212 "the link to occur. The -s option means to create a symbolic link.",
213 "By default, ln makes hard links.",
217 /* The standard structure describing a builtin command. bash keeps an array
218 of these structures. */
219 struct builtin ln_struct = {
220 "ln", /* builtin name */
221 ln_builtin, /* function implementing the builtin */
222 BUILTIN_ENABLED, /* initial flags for builtin */
223 ln_doc, /* array of long documentation strings. */
224 "ln [-fs] file1 [file2] OR ln [-fs] file ... directory", /* usage synopsis; becomes short_doc */
225 0 /* reserved for internal use */