1 /* `sln' program to create symbolic links between files.
2 Copyright (C) 1998, 1999, 2001, 2009 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, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/types.h>
37 #include "../version.h"
39 #define PACKAGE _libc_intl_domainname
41 #if !defined S_ISDIR && defined S_IFDIR
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
45 static int makesymlink (const char *src
, const char *dest
);
46 static int makesymlinks (const char *file
);
47 static void usage (void);
50 main (int argc
, char **argv
)
52 /* Set locale via LC_ALL. */
53 setlocale (LC_ALL
, "");
55 /* Set the text message domain. */
61 if (strcmp (argv
[1], "--version") == 0) {
62 printf ("sln (GNU libc) %s\n", VERSION
);
64 } else if (strcmp (argv
[1], "--help") == 0) {
68 return makesymlinks (argv
[1]);
72 return makesymlink (argv
[1], argv
[2]);
85 printf (_("Usage: sln src dest|file\n\n"));
86 fputs (_("For bug reporting instructions, please see:\n\
87 <http://www.gnu.org/software/libc/bugs.html>.\n"), stdout
);
103 if (strcmp (file
, "-") == 0)
107 fp
= fopen (file
, "r");
110 fprintf (stderr
, _("%s: file open error: %m\n"), file
);
117 while (!feof_unlocked (fp
))
119 ssize_t n
= getline (&buffer
, &bufferlen
, fp
);
126 if (buffer
[n
- 1] == '\n')
127 buffer
[n
- 1] = '\0';
130 while (isspace (*cp
))
133 /* Ignore empty lines. */
139 while (*cp
!= '\0' && ! isspace (*cp
));
143 while (isspace (*cp
))
147 fprintf (stderr
, _("No target in line %d\n"), lineno
);
155 while (*cp
!= '\0' && ! isspace (*cp
));
159 ret
|= makesymlink (src
, dest
);
167 makesymlink (src
, dest
)
174 /* Destination must not be a directory. */
175 if (lstat (dest
, &stats
) == 0)
177 if (S_ISDIR (stats
.st_mode
))
179 fprintf (stderr
, _("%s: destination must not be a directory\n"),
183 else if (unlink (dest
) && errno
!= ENOENT
)
185 fprintf (stderr
, _("%s: failed to remove the old destination\n"),
190 else if (errno
!= ENOENT
)
192 error
= strerror (errno
);
193 fprintf (stderr
, _("%s: invalid destination: %s\n"), dest
, error
);
198 if (symlink (src
, dest
) == 0)
200 if (link (src
, dest
) == 0)
203 /* Destination must exist by now. */
204 if (access (dest
, F_OK
))
206 error
= strerror (errno
);
208 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),
216 error
= strerror (errno
);
217 fprintf (stderr
, _("Invalid link from \"%s\" to \"%s\": %s\n"),