]> sourceware.org Git - glibc.git/blob - elf/sln.c
2002-12-10 Jakub Jelinek <jakub@redhat.com>
[glibc.git] / elf / sln.c
1 /* `sln' program to create symbolic links between files.
2 Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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. */
19 \f
20 #include <error.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 #include <errno.h>
26 #include <ctype.h>
27 #include <stdio.h>
28 #include <string.h>
29 #include <limits.h>
30
31 #if !defined S_ISDIR && defined S_IFDIR
32 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
33 #endif
34
35 static int makesymlink (const char *src, const char *dest);
36 static int makesymlinks (const char *file);
37
38 int
39 main (int argc, char **argv)
40 {
41 switch (argc)
42 {
43 case 2:
44 return makesymlinks (argv [1]);
45 break;
46
47 case 3:
48 return makesymlink (argv [1], argv [2]);
49 break;
50
51 default:
52 printf ("Usage: %s src dest|file\n", argv [0]);
53 return 1;
54 break;
55 }
56 }
57
58 static int
59 makesymlinks (file)
60 const char *file;
61 {
62 #ifndef PATH_MAX
63 #define PATH_MAX 4095
64 #endif
65 char *buffer = NULL;
66 size_t bufferlen = 0;
67 int ret;
68 int lineno;
69 FILE *fp;
70
71 if (strcmp (file, "-") == 0)
72 fp = stdin;
73 else
74 {
75 fp = fopen (file, "r");
76 if (fp == NULL)
77 {
78 fprintf (stderr, "%s: file open error: %m\n", file);
79 return 1;
80 }
81 }
82
83 ret = 0;
84 lineno = 0;
85 while (!feof_unlocked (fp))
86 {
87 ssize_t n = getline (&buffer, &bufferlen, fp);
88 char *src;
89 char *dest;
90 char *cp = buffer;
91
92 if (n < 0)
93 break;
94 if (buffer[n - 1] == '\n')
95 buffer[n - 1] = '\0';
96
97 ++lineno;
98 while (isspace (*cp))
99 ++cp;
100 if (*cp == '\0')
101 /* Ignore empty lines. */
102 continue;
103 src = cp;
104
105 do
106 ++cp;
107 while (*cp != '\0' && ! isspace (*cp));
108 if (*cp != '\0')
109 *cp++ = '\0';
110
111 while (isspace (*cp))
112 ++cp;
113 if (*cp == '\0')
114 {
115 fprintf (stderr, "No target in line %d\n", lineno);
116 ret = 1;
117 continue;
118 }
119 dest = cp;
120
121 do
122 ++cp;
123 while (*cp != '\0' && ! isspace (*cp));
124 if (*cp != '\0')
125 *cp++ = '\0';
126
127 ret |= makesymlink (src, dest);
128 }
129 fclose (fp);
130
131 return ret;
132 }
133
134 static int
135 makesymlink (src, dest)
136 const char *src;
137 const char *dest;
138 {
139 struct stat stats;
140 const char *error;
141
142 /* Destination must not be a directory. */
143 if (lstat (dest, &stats) == 0)
144 {
145 if (S_ISDIR (stats.st_mode))
146 {
147 fprintf (stderr, "%s: destination must not be a directory\n",
148 dest);
149 return 1;
150 }
151 else if (unlink (dest) && errno != ENOENT)
152 {
153 fprintf (stderr, "%s: failed to remove the old destination\n",
154 dest);
155 return 1;
156 }
157 }
158 else if (errno != ENOENT)
159 {
160 error = strerror (errno);
161 fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
162 return -1;
163 }
164
165 #ifdef S_ISLNK
166 if (symlink (src, dest) == 0)
167 #else
168 if (link (src, dest) == 0)
169 #endif
170 {
171 /* Destination must exist by now. */
172 if (access (dest, F_OK))
173 {
174 error = strerror (errno);
175 unlink (dest);
176 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
177 src, dest, error);
178 return 1;
179 }
180 return 0;
181 }
182 else
183 {
184 error = strerror (errno);
185 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
186 src, dest, error);
187 return 1;
188 }
189 }
This page took 0.044153 seconds and 5 git commands to generate.