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
22 #include <sys/types.h>
31 #if !defined S_ISDIR && defined S_IFDIR
32 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
35 static int makesymlink (const char *src
, const char *dest
);
36 static int makesymlinks (const char *file
);
39 main (int argc
, char **argv
)
44 return makesymlinks (argv
[1]);
48 return makesymlink (argv
[1], argv
[2]);
52 printf ("Usage: %s src dest|file\n", argv
[0]);
71 if (strcmp (file
, "-") == 0)
75 fp
= fopen (file
, "r");
78 fprintf (stderr
, "%s: file open error: %m\n", file
);
85 while (!feof_unlocked (fp
))
87 ssize_t n
= getline (&buffer
, &bufferlen
, fp
);
94 if (buffer
[n
- 1] == '\n')
101 /* Ignore empty lines. */
107 while (*cp
!= '\0' && ! isspace (*cp
));
111 while (isspace (*cp
))
115 fprintf (stderr
, "No target in line %d\n", lineno
);
123 while (*cp
!= '\0' && ! isspace (*cp
));
127 ret
|= makesymlink (src
, dest
);
135 makesymlink (src
, dest
)
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",
151 else if (unlink (dest
) && errno
!= ENOENT
)
153 fprintf (stderr
, "%s: failed to remove the old destination\n",
158 else if (errno
!= ENOENT
)
160 error
= strerror (errno
);
161 fprintf (stderr
, "%s: invalid destination: %s\n", dest
, error
);
166 if (symlink (src
, dest
) == 0)
168 if (link (src
, dest
) == 0)
171 /* Destination must exist by now. */
172 if (access (dest
, F_OK
))
174 error
= strerror (errno
);
176 fprintf (stderr
, "Invalid link from \"%s\" to \"%s\": %s\n",
184 error
= strerror (errno
);
185 fprintf (stderr
, "Invalid link from \"%s\" to \"%s\": %s\n",