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>
29 #if !defined(S_ISDIR) && defined(S_IFDIR)
30 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
33 static int makesymlink __P ((const char *src, const char *dest));
34 static int makesymlinks __P ((const char *file));
44 return makesymlinks (argv [1]) == 0 ? 0 : 1;
48 return makesymlink (argv [1], argv [2]) == 0 ? 0 : 1;
52 printf ("Usage: %s src dest|file\n", argv [0]);
65 char buffer [PATH_MAX * 4];
73 fp = fopen (file, "r");
76 error = strerror (errno);
77 fprintf (stderr, "%s: file open error: %s\n", file, error);
83 while (fgets (buffer, sizeof (buffer) - 1, fp))
87 buffer [sizeof (buffer) - 1] = '\0';
88 len = strlen (buffer);
89 for (i = 0; i < len && isspace (buffer [i]); i++);
92 fprintf (stderr, "Fail to parse line %d: \"%s\"\n", lineno,
99 for (; i < len && !isspace (buffer [i]); i++);
103 for (; i < len && isspace (buffer [i]); i++);
106 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
112 for (; i < len && !isspace (buffer [i]); i++);
117 fprintf (stderr, "No target in line %d: \"%s\"\n", lineno,
123 if (makesymlink (src, dest))
125 fprintf (stderr, "Failed to make link from \"%s\" to \"%s\" in line %d\n",
136 makesymlink (src, dest)
143 /* Destination must not be a directory. */
144 if (lstat (dest, &stats) == 0)
146 if (S_ISDIR (stats.st_mode))
148 fprintf (stderr, "%s: destination must not be a directory\n",
152 else if (unlink (dest) && errno != ENOENT)
154 fprintf (stderr, "%s: failed to remove the old destination\n",
159 else if (errno != ENOENT)
161 error = strerror (errno);
162 fprintf (stderr, "%s: invalid destination: %s\n", dest, error);
167 if (symlink (src, dest) == 0)
169 if (link (src, dest) == 0)
172 /* Destination must exist by now. */
173 if (access (dest, F_OK))
175 error = strerror (errno);
177 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",
185 error = strerror (errno);
186 fprintf (stderr, "Invalid link from \"%s\" to \"%s\": %s\n",