Update.
[glibc.git] / elf / sln.c
blobf9344692e6182607dc07abca263a00b002aabb4d
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.
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.
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.
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. */
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>
29 #if !defined(S_ISDIR) && defined(S_IFDIR)
30 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
31 #endif
33 static int makesymlink __P ((const char *src, const char *dest));
34 static int makesymlinks __P ((const char *file));
36 int
37 main (int argc, char **argv)
39 switch (argc)
41 case 2:
42 return makesymlinks (argv [1]) == 0 ? 0 : 1;
43 break;
45 case 3:
46 return makesymlink (argv [1], argv [2]) == 0 ? 0 : 1;
47 break;
49 default:
50 printf ("Usage: %s src dest|file\n", argv [0]);
51 return 1;
52 break;
56 static int
57 makesymlinks (file)
58 const char *file;
60 #ifndef PATH_MAX
61 #define PATH_MAX 4095
62 #endif
63 char buffer [PATH_MAX * 4];
64 int i, ret, len;
65 int lineno;
66 const char *src;
67 const char *dest;
68 const char *error;
69 FILE *fp;
71 fp = fopen (file, "r");
72 if (fp == NULL)
74 error = strerror (errno);
75 fprintf (stderr, "%s: file open error: %s\n", file, error);
76 return -1;
79 ret = 0;
80 lineno = 0;
81 while (fgets (buffer, sizeof (buffer) - 1, fp))
83 lineno++;
84 src = dest = NULL;
85 buffer [sizeof (buffer) - 1] = '\0';
86 len = strlen (buffer);
87 for (i = 0; i < len && isspace (buffer [i]); i++);
88 if (i >= len)
90 fprintf (stderr, "Fail to parse line %d: \"%s\"\n", lineno,
91 buffer);
92 ret--;
93 continue;
96 src = &buffer [i];
97 for (; i < len && !isspace (buffer [i]); i++);
98 if (i < len)
100 buffer [i++] = '\0';
101 for (; i < len && isspace (buffer [i]); i++);
102 if (i >= len)
104 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
105 buffer);
106 ret--;
107 continue;
109 dest = &buffer [i];
110 for (; i < len && !isspace (buffer [i]); i++);
111 buffer [i] = '\0';
113 else
115 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
116 buffer);
117 ret--;
118 continue;
121 if (makesymlink (src, dest))
123 fprintf (stderr, "Failed to make link from \"%s\" to \"%s\" in line %d\n",
124 src, dest, lineno);
125 ret--;
128 fclose (fp);
130 return ret;
133 static int
134 makesymlink (src, dest)
135 const char *src;
136 const char *dest;
138 struct stat stats;
139 const char *error;
141 /* Destination must not be a directory. */
142 if (lstat (dest, &stats) == 0)
144 if (S_ISDIR (stats.st_mode))
146 fprintf (stderr, "%s: destination must not be a directory\n",
147 dest);
148 return -1;
150 else if (unlink (dest) && errno != ENOENT)
152 fprintf (stderr, "%s: failed to remove the old destination\n",
153 dest);
154 return -1;
157 else if (errno != ENOENT)
159 error = strerror (errno);
160 fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
161 return -1;
164 #ifdef S_ISLNK
165 if (symlink (src, dest) == 0)
166 #else
167 if (link (src, dest) == 0)
168 #endif
170 /* Destination must exist by now. */
171 if (access (dest, F_OK))
173 error = strerror (errno);
174 unlink (dest);
175 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
176 src, dest, error);
177 return -1;
179 return 0;
181 else
183 error = strerror (errno);
184 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
185 src, dest, error);
186 return -1;