3 /* See Makefile for compilation details. */
9 #if defined (HAVE_UNISTD_H)
13 #include "posixstat.h"
20 #include "bashgetopt.h"
26 #define LN_SYMLINK 0x01
27 #define LN_UNLINK 0x02
29 static Function *linkfn;
41 reset_internal_getopt ();
42 while ((opt = internal_getopt (list, "fs")) != -1)
65 linkfn = (flags & LN_SYMLINK) ? symlink : link;
67 if (list->next == 0) /* ln target, equivalent to ln target . */
68 return (dolink (list->word->word, ".", flags));
70 if (list->next->next == 0) /* ln target source */
71 return (dolink (list->word->word, list->next->word->word, flags));
73 /* ln target1 target2 ... directory */
75 /* find last argument: target directory, and make sure it's an existing
77 for (l = list; l->next; l = l->next)
81 if (stat(sdir, &sb) < 0)
83 builtin_error ("%s", sdir);
84 return (EXECUTION_FAILURE);
87 if (S_ISDIR (sb.st_mode) == 0)
93 for (rval = EXECUTION_SUCCESS; list != l; list = list->next)
94 rval += dolink (list->word->word, sdir, flags);
100 mkdirpath (dir, file)
107 flen = strlen (file);
109 ret = xmalloc (2 + dlen + flen);
112 if (ret[dlen - 1] != '/')
114 strcpy (ret + dlen, file);
118 #if defined (HAVE_LSTAT)
125 dolink (src, dst, flags)
129 struct stat ssb, dsb;
133 /* If we're not doing symlinks, the source must exist and not be a
135 if ((flags & LN_SYMLINK) == 0)
137 if (stat (src, &ssb) != 0)
139 builtin_error ("%s: %s", src, strerror (errno));
140 return (EXECUTION_FAILURE);
142 if (S_ISDIR (ssb.st_mode))
145 builtin_error ("%s: %s", src, strerror (errno));
146 return (EXECUTION_FAILURE);
150 /* If the destination is a directory, create the final filename by appending
151 the basename of the source to the destination. */
153 if ((stat (dst, &dsb) == 0) && S_ISDIR (dsb.st_mode))
155 if ((p = strrchr (src, '/')) == 0)
160 dst_path = mkdirpath (dst, p);
164 exists = LSTAT (dst, &dsb) == 0;
166 /* If -f was specified, and the destination exists, unlink it. */
167 if ((flags & LN_UNLINK) && exists && unlink (dst) != 0)
169 builtin_error ("%s: cannot unlink: %s", dst, strerror (errno));
171 return (EXECUTION_FAILURE);
174 /* Perform the link. */
175 if ((*linkfn) (src, dst) != 0)
177 builtin_error ("cannot link %s to %s: %s", dst, src, strerror (errno));
179 return (EXECUTION_FAILURE);
183 return (EXECUTION_SUCCESS);
187 "Create a new directory entry with the same modes as the original",
188 "file. The -f option means to unlink any existing file, permitting",
189 "the link to occur. The -s option means to create a symbolic link.",
190 "By default, ln makes hard links.",
194 /* The standard structure describing a builtin command. bash keeps an array
195 of these structures. */
196 struct builtin ln_struct = {
197 "ln", /* builtin name */
198 ln_builtin, /* function implementing the builtin */
199 BUILTIN_ENABLED, /* initial flags for builtin */
200 ln_doc, /* array of long documentation strings. */
201 "ln [-fs] file1 [file2] OR ln [-fs] file ... directory", /* usage synopsis; becomes short_doc */
202 0 /* reserved for internal use */