Merge remote branch 'origin/master' into fedora/master
[glibc.git] / elf / sln.c
blob71707f5d30c13a5dce73573f4bea9a2edf2a5c72
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
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #endif
24 #include <error.h>
25 #include <errno.h>
26 #include <libintl.h>
27 #include <locale.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <ctype.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <limits.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)
43 #endif
45 static int makesymlink (const char *src, const char *dest);
46 static int makesymlinks (const char *file);
47 static void usage (void);
49 int
50 main (int argc, char **argv)
52 /* Set locale via LC_ALL. */
53 setlocale (LC_ALL, "");
55 /* Set the text message domain. */
56 textdomain (PACKAGE);
58 switch (argc)
60 case 2:
61 if (strcmp (argv[1], "--version") == 0) {
62 printf ("sln (GNU libc) %s\n", VERSION);
63 return 0;
64 } else if (strcmp (argv[1], "--help") == 0) {
65 usage ();
66 return 0;
68 return makesymlinks (argv [1]);
69 break;
71 case 3:
72 return makesymlink (argv [1], argv [2]);
73 break;
75 default:
76 usage ();
77 return 1;
78 break;
82 static void
83 usage (void)
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);
90 static int
91 makesymlinks (file)
92 const char *file;
94 #ifndef PATH_MAX
95 #define PATH_MAX 4095
96 #endif
97 char *buffer = NULL;
98 size_t bufferlen = 0;
99 int ret;
100 int lineno;
101 FILE *fp;
103 if (strcmp (file, "-") == 0)
104 fp = stdin;
105 else
107 fp = fopen (file, "r");
108 if (fp == NULL)
110 fprintf (stderr, _("%s: file open error: %m\n"), file);
111 return 1;
115 ret = 0;
116 lineno = 0;
117 while (!feof_unlocked (fp))
119 ssize_t n = getline (&buffer, &bufferlen, fp);
120 char *src;
121 char *dest;
122 char *cp = buffer;
124 if (n < 0)
125 break;
126 if (buffer[n - 1] == '\n')
127 buffer[n - 1] = '\0';
129 ++lineno;
130 while (isspace (*cp))
131 ++cp;
132 if (*cp == '\0')
133 /* Ignore empty lines. */
134 continue;
135 src = cp;
138 ++cp;
139 while (*cp != '\0' && ! isspace (*cp));
140 if (*cp != '\0')
141 *cp++ = '\0';
143 while (isspace (*cp))
144 ++cp;
145 if (*cp == '\0')
147 fprintf (stderr, _("No target in line %d\n"), lineno);
148 ret = 1;
149 continue;
151 dest = cp;
154 ++cp;
155 while (*cp != '\0' && ! isspace (*cp));
156 if (*cp != '\0')
157 *cp++ = '\0';
159 ret |= makesymlink (src, dest);
161 fclose (fp);
163 return ret;
166 static int
167 makesymlink (src, dest)
168 const char *src;
169 const char *dest;
171 struct stat stats;
172 const char *error;
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"),
180 dest);
181 return 1;
183 else if (unlink (dest) && errno != ENOENT)
185 fprintf (stderr, _("%s: failed to remove the old destination\n"),
186 dest);
187 return 1;
190 else if (errno != ENOENT)
192 error = strerror (errno);
193 fprintf (stderr, _("%s: invalid destination: %s\n"), dest, error);
194 return -1;
197 #ifdef S_ISLNK
198 if (symlink (src, dest) == 0)
199 #else
200 if (link (src, dest) == 0)
201 #endif
203 /* Destination must exist by now. */
204 if (access (dest, F_OK))
206 error = strerror (errno);
207 unlink (dest);
208 fprintf (stderr, _("Invalid link from \"%s\" to \"%s\": %s\n"),
209 src, dest, error);
210 return 1;
212 return 0;
214 else
216 error = strerror (errno);
217 fprintf (stderr, _("Invalid link from \"%s\" to \"%s\": %s\n"),
218 src, dest, error);
219 return 1;