1 /* `sln' program to create symbolic links between files.
2 Copyright (C) 1998-2016 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
);
90 makesymlinks (const char *file
)
101 if (strcmp (file
, "-") == 0)
105 fp
= fopen (file
, "r");
108 fprintf (stderr
, _("%s: file open error: %m\n"), file
);
115 while (!feof_unlocked (fp
))
117 ssize_t n
= getline (&buffer
, &bufferlen
, fp
);
124 if (buffer
[n
- 1] == '\n')
125 buffer
[n
- 1] = '\0';
128 while (isspace (*cp
))
131 /* Ignore empty lines. */
137 while (*cp
!= '\0' && ! isspace (*cp
));
141 while (isspace (*cp
))
145 fprintf (stderr
, _("No target in line %d\n"), lineno
);
153 while (*cp
!= '\0' && ! isspace (*cp
));
157 ret
|= makesymlink (src
, dest
);
165 makesymlink (const char *src
, const char *dest
)
170 /* Destination must not be a directory. */
171 if (lstat (dest
, &stats
) == 0)
173 if (S_ISDIR (stats
.st_mode
))
175 fprintf (stderr
, _("%s: destination must not be a directory\n"),
179 else if (unlink (dest
) && errno
!= ENOENT
)
181 fprintf (stderr
, _("%s: failed to remove the old destination\n"),
186 else if (errno
!= ENOENT
)
188 error
= strerror (errno
);
189 fprintf (stderr
, _("%s: invalid destination: %s\n"), dest
, error
);
194 if (symlink (src
, dest
) == 0)
196 if (link (src
, dest
) == 0)
199 /* Destination must exist by now. */
200 if (access (dest
, F_OK
))
202 error
= strerror (errno
);
204 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),
212 error
= strerror (errno
);
213 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),