]> git.ipfire.org Git - thirdparty/glibc.git/blob - elf/sln.c
Update.
[thirdparty/glibc.git] / elf / sln.c
1 /* `sln' program to create symboblic links between files.
2 Copyright (C) 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19 \f
20 #include <sys/types.h>
21 #include <sys/stat.h>
22 #include <unistd.h>
23 #include <errno.h>
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <limits.h>
28
29 #if !defined(S_ISDIR) && defined(S_IFDIR)
30 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
31 #endif
32
33 static int makesymlink __P ((const char *src, const char *dest));
34 static int makesymlinks __P ((const char *file));
35
36 int
37 main (argc, argv)
38 int argc;
39 char **argv;
40 {
41 switch (argc)
42 {
43 case 2:
44 return makesymlinks (argv [1]) == 0 ? 0 : 1;
45 break;
46
47 case 3:
48 return makesymlink (argv [1], argv [2]) == 0 ? 0 : 1;
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 [PATH_MAX * 4];
66 int i, ret, len;
67 int lineno;
68 const char *src;
69 const char *dest;
70 const char *error;
71 FILE *fp;
72
73 fp = fopen (file, "r");
74 if (fp == NULL)
75 {
76 error = strerror (errno);
77 fprintf (stderr, "%s: file open error: %s\n", file, error);
78 return -1;
79 }
80
81 ret = 0;
82 lineno = 0;
83 while (fgets (buffer, sizeof (buffer) - 1, fp))
84 {
85 lineno++;
86 src = dest = NULL;
87 buffer [sizeof (buffer) - 1] = '\0';
88 len = strlen (buffer);
89 for (i = 0; i < len && isspace (buffer [i]); i++);
90 if (i >= len)
91 {
92 fprintf (stderr, "Fail to parse line %d: \"%s\"\n", lineno,
93 buffer);
94 ret--;
95 continue;
96 }
97
98 src = &buffer [i];
99 for (; i < len && !isspace (buffer [i]); i++);
100 if (i < len)
101 {
102 buffer [i++] = '\0';
103 for (; i < len && isspace (buffer [i]); i++);
104 if (i >= len)
105 {
106 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
107 buffer);
108 ret--;
109 continue;
110 }
111 dest = &buffer [i];
112 for (; i < len && !isspace (buffer [i]); i++);
113 buffer [i] = '\0';
114 }
115 else
116 {
117 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
118 buffer);
119 ret--;
120 continue;
121 }
122
123 if (makesymlink (src, dest))
124 {
125 fprintf (stderr, "Failed to make link from \"%s\" to \"%s\" in line %d\n",
126 src, dest, lineno);
127 ret--;
128 }
129 }
130 fclose (fp);
131
132 return ret;
133 }
134
135 static int
136 makesymlink (src, dest)
137 const char *src;
138 const char *dest;
139 {
140 struct stat stats;
141 const char *error;
142
143 /* Destination must not be a directory. */
144 if (lstat (dest, &stats) == 0)
145 {
146 if (S_ISDIR (stats.st_mode))
147 {
148 fprintf (stderr, "%s: destination must not be a directory\n",
149 dest);
150 return -1;
151 }
152 else if (unlink (dest) && errno != ENOENT)
153 {
154 fprintf (stderr, "%s: failed to remove the old destination\n",
155 dest);
156 return -1;
157 }
158 }
159 else if (errno != ENOENT)
160 {
161 error = strerror (errno);
162 fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
163 return -1;
164 }
165
166 #ifdef S_ISLNK
167 if (symlink (src, dest) == 0)
168 #else
169 if (link (src, dest) == 0)
170 #endif
171 {
172 /* Destination must exist by now. */
173 if (access (dest, F_OK))
174 {
175 error = strerror (errno);
176 unlink (dest);
177 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
178 src, dest, error);
179 return -1;
180 }
181 return 0;
182 }
183 else
184 {
185 error = strerror (errno);
186 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
187 src, dest, error);
188 return -1;
189 }
190 }