1 /* `sln' program to create symbolic links between files.
2 Copyright (C) 1998-2023 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 <https://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
32 #include "../version.h"
34 #define PACKAGE _libc_intl_domainname
36 static int makesymlink (const char *src
, const char *dest
);
37 static int makesymlinks (const char *file
);
38 static void usage (void);
41 main (int argc
, char **argv
)
43 /* Set locale via LC_ALL. */
44 setlocale (LC_ALL
, "");
46 /* Set the text message domain. */
52 if (strcmp (argv
[1], "--version") == 0) {
53 printf ("sln %s%s\n", PKGVERSION
, VERSION
);
55 } else if (strcmp (argv
[1], "--help") == 0) {
59 return makesymlinks (argv
[1]);
63 return makesymlink (argv
[1], argv
[2]);
76 printf (_("Usage: sln src dest|file\n\n"));
77 printf (_("For bug reporting instructions, please see:\n\
78 %s.\n"), REPORT_BUGS_TO
);
82 makesymlinks (const char *file
)
90 if (strcmp (file
, "-") == 0)
94 fp
= fopen (file
, "r");
97 fprintf (stderr
, _("%s: file open error: %m\n"), file
);
104 while (!feof_unlocked (fp
))
106 ssize_t n
= getline (&buffer
, &bufferlen
, fp
);
113 if (buffer
[n
- 1] == '\n')
114 buffer
[n
- 1] = '\0';
117 while (isspace (*cp
))
120 /* Ignore empty lines. */
126 while (*cp
!= '\0' && ! isspace (*cp
));
130 while (isspace (*cp
))
134 fprintf (stderr
, _("No target in line %d\n"), lineno
);
142 while (*cp
!= '\0' && ! isspace (*cp
));
146 ret
|= makesymlink (src
, dest
);
154 makesymlink (const char *src
, const char *dest
)
159 /* Destination must not be a directory. */
160 if (lstat (dest
, &stats
) == 0)
162 if (S_ISDIR (stats
.st_mode
))
164 fprintf (stderr
, _("%s: destination must not be a directory\n"),
168 else if (unlink (dest
) && errno
!= ENOENT
)
170 fprintf (stderr
, _("%s: failed to remove the old destination\n"),
175 else if (errno
!= ENOENT
)
177 error
= strerror (errno
);
178 fprintf (stderr
, _("%s: invalid destination: %s\n"), dest
, error
);
182 if (symlink (src
, dest
) == 0)
184 /* Destination must exist by now. */
185 if (access (dest
, F_OK
))
187 error
= strerror (errno
);
189 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),
197 error
= strerror (errno
);
198 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),