1 /* `sln' program to create symbolic links between files.
2 Copyright (C) 1998-2014 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
27 #include <sys/types.h>
36 #include "../version.h"
38 #define PACKAGE _libc_intl_domainname
40 #if !defined S_ISDIR && defined S_IFDIR
41 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
44 static int makesymlink (const char *src
, const char *dest
);
45 static int makesymlinks (const char *file
);
46 static void usage (void);
49 main (int argc
, char **argv
)
51 /* Set locale via LC_ALL. */
52 setlocale (LC_ALL
, "");
54 /* Set the text message domain. */
60 if (strcmp (argv
[1], "--version") == 0) {
61 printf ("sln %s%s\n", PKGVERSION
, VERSION
);
63 } else if (strcmp (argv
[1], "--help") == 0) {
67 return makesymlinks (argv
[1]);
71 return makesymlink (argv
[1], argv
[2]);
84 printf (_("Usage: sln src dest|file\n\n"));
85 printf (_("For bug reporting instructions, please see:\n\
86 %s.\n"), REPORT_BUGS_TO
);
102 if (strcmp (file
, "-") == 0)
106 fp
= fopen (file
, "r");
109 fprintf (stderr
, _("%s: file open error: %m\n"), file
);
116 while (!feof_unlocked (fp
))
118 ssize_t n
= getline (&buffer
, &bufferlen
, fp
);
125 if (buffer
[n
- 1] == '\n')
126 buffer
[n
- 1] = '\0';
129 while (isspace (*cp
))
132 /* Ignore empty lines. */
138 while (*cp
!= '\0' && ! isspace (*cp
));
142 while (isspace (*cp
))
146 fprintf (stderr
, _("No target in line %d\n"), lineno
);
154 while (*cp
!= '\0' && ! isspace (*cp
));
158 ret
|= makesymlink (src
, dest
);
166 makesymlink (src
, dest
)
173 /* Destination must not be a directory. */
174 if (lstat (dest
, &stats
) == 0)
176 if (S_ISDIR (stats
.st_mode
))
178 fprintf (stderr
, _("%s: destination must not be a directory\n"),
182 else if (unlink (dest
) && errno
!= ENOENT
)
184 fprintf (stderr
, _("%s: failed to remove the old destination\n"),
189 else if (errno
!= ENOENT
)
191 error
= strerror (errno
);
192 fprintf (stderr
, _("%s: invalid destination: %s\n"), dest
, error
);
197 if (symlink (src
, dest
) == 0)
199 if (link (src
, dest
) == 0)
202 /* Destination must exist by now. */
203 if (access (dest
, F_OK
))
205 error
= strerror (errno
);
207 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),
215 error
= strerror (errno
);
216 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),