* sysdeps/unix/sysv/linux/s390/s390-32/clone.S (__clone): Add support for
[glibc.git] / elf / sln.c
blob8e66510bb262f3064469b48e34b9d482e3c4e198
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.
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 #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>
31 #if !defined S_ISDIR && defined S_IFDIR
32 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
33 #endif
35 static int makesymlink (const char *src, const char *dest);
36 static int makesymlinks (const char *file);
38 int
39 main (int argc, char **argv)
41 switch (argc)
43 case 2:
44 return makesymlinks (argv [1]);
45 break;
47 case 3:
48 return makesymlink (argv [1], argv [2]);
49 break;
51 default:
52 printf ("Usage: %s src dest|file\n", argv [0]);
53 return 1;
54 break;
58 static int
59 makesymlinks (file)
60 const char *file;
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;
71 if (strcmp (file, "-") == 0)
72 fp = stdin;
73 else
75 fp = fopen (file, "r");
76 if (fp == NULL)
78 fprintf (stderr, "%s: file open error: %m\n", file);
79 return 1;
83 ret = 0;
84 lineno = 0;
85 while (!feof_unlocked (fp))
87 ssize_t n = getline (&buffer, &bufferlen, fp);
88 char *src;
89 char *dest;
90 char *cp = buffer;
92 if (n < 0)
93 break;
94 if (buffer[n - 1] == '\n')
95 buffer[n - 1] = '\0';
97 ++lineno;
98 while (isspace (*cp))
99 ++cp;
100 if (*cp == '\0')
101 /* Ignore empty lines. */
102 continue;
103 src = cp;
106 ++cp;
107 while (*cp != '\0' && ! isspace (*cp));
108 if (*cp != '\0')
109 *cp++ = '\0';
111 while (isspace (*cp))
112 ++cp;
113 if (*cp == '\0')
115 fprintf (stderr, "No target in line %d\n", lineno);
116 ret = 1;
117 continue;
119 dest = cp;
122 ++cp;
123 while (*cp != '\0' && ! isspace (*cp));
124 if (*cp != '\0')
125 *cp++ = '\0';
127 ret |= makesymlink (src, dest);
129 fclose (fp);
131 return ret;
134 static int
135 makesymlink (src, dest)
136 const char *src;
137 const char *dest;
139 struct stat stats;
140 const char *error;
142 /* Destination must not be a directory. */
143 if (lstat (dest, &stats) == 0)
145 if (S_ISDIR (stats.st_mode))
147 fprintf (stderr, "%s: destination must not be a directory\n",
148 dest);
149 return 1;
151 else if (unlink (dest) && errno != ENOENT)
153 fprintf (stderr, "%s: failed to remove the old destination\n",
154 dest);
155 return 1;
158 else if (errno != ENOENT)
160 error = strerror (errno);
161 fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
162 return -1;
165 #ifdef S_ISLNK
166 if (symlink (src, dest) == 0)
167 #else
168 if (link (src, dest) == 0)
169 #endif
171 /* Destination must exist by now. */
172 if (access (dest, F_OK))
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;
180 return 0;
182 else
184 error = strerror (errno);
185 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
186 src, dest, error);
187 return 1;