1 /* `sln' program to create symboblic links between files.
2 Copyright (C) 1998, 1999 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>
29 #if !defined(S_ISDIR) && defined(S_IFDIR)
30 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
33 static int makesymlink (const char *src, const char *dest);
34 static int makesymlinks (const char *file);
37 main (int argc, char **argv)
42 return makesymlinks (argv [1]) == 0 ? 0 : 1;
46 return makesymlink (argv [1], argv [2]) == 0 ? 0 : 1;
50 printf ("Usage: %s src dest|file\n", argv [0]);
63 char buffer [PATH_MAX * 4];
71 fp = fopen (file, "r");
74 error = strerror (errno);
75 fprintf (stderr, "%s: file open error: %s\n", file, error);
81 while (fgets (buffer, sizeof (buffer) - 1, fp))
85 buffer [sizeof (buffer) - 1] = '\0';
86 len = strlen (buffer);
87 for (i = 0; i < len && isspace (buffer [i]); i++);
90 fprintf (stderr, "Fail to parse line %d: \"%s\"\n", lineno,
97 for (; i < len && !isspace (buffer [i]); i++);
101 for (; i < len && isspace (buffer [i]); i++);
104 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
110 for (; i < len && !isspace (buffer [i]); i++);
115 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
121 if (makesymlink (src, dest))
123 fprintf (stderr, "Failed to make link from \"%s\" to \"%s\" in line %d\n",
134 makesymlink (src, dest)
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",
150 else if (unlink (dest) && errno != ENOENT)
152 fprintf (stderr, "%s: failed to remove the old destination\n",
157 else if (errno != ENOENT)
159 error = strerror (errno);
160 fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
165 if (symlink (src, dest) == 0)
167 if (link (src, dest) == 0)
170 /* Destination must exist by now. */
171 if (access (dest, F_OK))
173 error = strerror (errno);
175 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
183 error = strerror (errno);
184 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",