2001-02-28 01:59:27 +08:00
|
|
|
/* `sln' program to create symbolic links between files.
|
2016-01-05 00:05:18 +08:00
|
|
|
Copyright (C) 1998-2016 Free Software Foundation, Inc.
|
1998-08-14 03:44:05 +08:00
|
|
|
This file is part of the GNU C Library.
|
|
|
|
|
|
|
|
The GNU C Library is free software; you can redistribute it and/or
|
2001-07-06 12:58:11 +08:00
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
1998-08-14 03:44:05 +08:00
|
|
|
|
|
|
|
The GNU C Library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2001-07-06 12:58:11 +08:00
|
|
|
Lesser General Public License for more details.
|
1998-08-14 03:44:05 +08:00
|
|
|
|
2001-07-06 12:58:11 +08:00
|
|
|
You should have received a copy of the GNU Lesser General Public
|
2012-02-10 07:18:22 +08:00
|
|
|
License along with the GNU C Library; if not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
2009-02-07 04:13:07 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2001-02-28 01:59:27 +08:00
|
|
|
#include <error.h>
|
|
|
|
#include <errno.h>
|
2009-02-07 04:13:07 +08:00
|
|
|
#include <libintl.h>
|
|
|
|
#include <locale.h>
|
1998-08-14 03:44:05 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
2009-02-07 04:13:07 +08:00
|
|
|
#include "../version.h"
|
|
|
|
|
|
|
|
#define PACKAGE _libc_intl_domainname
|
|
|
|
|
2001-02-28 01:59:27 +08:00
|
|
|
#if !defined S_ISDIR && defined S_IFDIR
|
1998-08-14 03:44:05 +08:00
|
|
|
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
|
|
|
#endif
|
|
|
|
|
2000-01-01 02:51:25 +08:00
|
|
|
static int makesymlink (const char *src, const char *dest);
|
|
|
|
static int makesymlinks (const char *file);
|
2009-02-07 04:13:07 +08:00
|
|
|
static void usage (void);
|
1998-08-14 03:44:05 +08:00
|
|
|
|
|
|
|
int
|
1998-08-21 01:40:36 +08:00
|
|
|
main (int argc, char **argv)
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
2009-02-07 04:13:07 +08:00
|
|
|
/* Set locale via LC_ALL. */
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
|
|
|
|
/* Set the text message domain. */
|
|
|
|
textdomain (PACKAGE);
|
|
|
|
|
1998-08-14 03:44:05 +08:00
|
|
|
switch (argc)
|
|
|
|
{
|
|
|
|
case 2:
|
2009-02-07 04:13:07 +08:00
|
|
|
if (strcmp (argv[1], "--version") == 0) {
|
2012-11-10 06:13:45 +08:00
|
|
|
printf ("sln %s%s\n", PKGVERSION, VERSION);
|
2009-02-07 04:13:07 +08:00
|
|
|
return 0;
|
|
|
|
} else if (strcmp (argv[1], "--help") == 0) {
|
|
|
|
usage ();
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-28 01:59:27 +08:00
|
|
|
return makesymlinks (argv [1]);
|
1998-08-14 03:44:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 3:
|
2001-02-28 01:59:27 +08:00
|
|
|
return makesymlink (argv [1], argv [2]);
|
1998-08-14 03:44:05 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2009-02-07 04:13:07 +08:00
|
|
|
usage ();
|
1998-08-14 03:44:05 +08:00
|
|
|
return 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-07 04:13:07 +08:00
|
|
|
static void
|
|
|
|
usage (void)
|
|
|
|
{
|
|
|
|
printf (_("Usage: sln src dest|file\n\n"));
|
2012-11-10 06:13:45 +08:00
|
|
|
printf (_("For bug reporting instructions, please see:\n\
|
|
|
|
%s.\n"), REPORT_BUGS_TO);
|
2009-02-07 04:13:07 +08:00
|
|
|
}
|
|
|
|
|
1998-08-14 03:44:05 +08:00
|
|
|
static int
|
2015-10-17 04:21:49 +08:00
|
|
|
makesymlinks (const char *file)
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
|
|
|
#ifndef PATH_MAX
|
|
|
|
#define PATH_MAX 4095
|
|
|
|
#endif
|
2001-02-28 01:59:27 +08:00
|
|
|
char *buffer = NULL;
|
|
|
|
size_t bufferlen = 0;
|
|
|
|
int ret;
|
1998-08-14 03:44:05 +08:00
|
|
|
int lineno;
|
|
|
|
FILE *fp;
|
|
|
|
|
2001-02-28 01:59:27 +08:00
|
|
|
if (strcmp (file, "-") == 0)
|
|
|
|
fp = stdin;
|
|
|
|
else
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
2001-02-28 01:59:27 +08:00
|
|
|
fp = fopen (file, "r");
|
|
|
|
if (fp == NULL)
|
|
|
|
{
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("%s: file open error: %m\n"), file);
|
2001-02-28 01:59:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
1998-08-14 03:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
lineno = 0;
|
2001-02-28 01:59:27 +08:00
|
|
|
while (!feof_unlocked (fp))
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
2001-02-28 01:59:27 +08:00
|
|
|
ssize_t n = getline (&buffer, &bufferlen, fp);
|
|
|
|
char *src;
|
|
|
|
char *dest;
|
|
|
|
char *cp = buffer;
|
|
|
|
|
|
|
|
if (n < 0)
|
|
|
|
break;
|
|
|
|
if (buffer[n - 1] == '\n')
|
|
|
|
buffer[n - 1] = '\0';
|
|
|
|
|
|
|
|
++lineno;
|
|
|
|
while (isspace (*cp))
|
|
|
|
++cp;
|
|
|
|
if (*cp == '\0')
|
|
|
|
/* Ignore empty lines. */
|
|
|
|
continue;
|
|
|
|
src = cp;
|
|
|
|
|
|
|
|
do
|
|
|
|
++cp;
|
|
|
|
while (*cp != '\0' && ! isspace (*cp));
|
|
|
|
if (*cp != '\0')
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
|
|
|
while (isspace (*cp))
|
|
|
|
++cp;
|
|
|
|
if (*cp == '\0')
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("No target in line %d\n"), lineno);
|
2001-02-28 01:59:27 +08:00
|
|
|
ret = 1;
|
1998-08-14 03:44:05 +08:00
|
|
|
continue;
|
|
|
|
}
|
2001-02-28 01:59:27 +08:00
|
|
|
dest = cp;
|
1998-08-14 03:44:05 +08:00
|
|
|
|
2001-02-28 01:59:27 +08:00
|
|
|
do
|
|
|
|
++cp;
|
|
|
|
while (*cp != '\0' && ! isspace (*cp));
|
|
|
|
if (*cp != '\0')
|
|
|
|
*cp++ = '\0';
|
|
|
|
|
|
|
|
ret |= makesymlink (src, dest);
|
1998-08-14 03:44:05 +08:00
|
|
|
}
|
|
|
|
fclose (fp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2015-10-17 04:21:49 +08:00
|
|
|
makesymlink (const char *src, const char *dest)
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
2016-03-07 09:18:21 +08:00
|
|
|
struct stat64 stats;
|
1998-08-14 03:44:05 +08:00
|
|
|
const char *error;
|
|
|
|
|
|
|
|
/* Destination must not be a directory. */
|
2016-03-07 09:18:21 +08:00
|
|
|
if (lstat64 (dest, &stats) == 0)
|
1998-08-14 03:44:05 +08:00
|
|
|
{
|
|
|
|
if (S_ISDIR (stats.st_mode))
|
|
|
|
{
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("%s: destination must not be a directory\n"),
|
2013-06-07 01:36:03 +08:00
|
|
|
dest);
|
2001-02-28 01:59:27 +08:00
|
|
|
return 1;
|
1998-08-14 03:44:05 +08:00
|
|
|
}
|
|
|
|
else if (unlink (dest) && errno != ENOENT)
|
|
|
|
{
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("%s: failed to remove the old destination\n"),
|
2013-06-07 01:36:03 +08:00
|
|
|
dest);
|
2001-02-28 01:59:27 +08:00
|
|
|
return 1;
|
1998-08-14 03:44:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (errno != ENOENT)
|
|
|
|
{
|
|
|
|
error = strerror (errno);
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("%s: invalid destination: %s\n"), dest, error);
|
1998-08-14 03:44:05 +08:00
|
|
|
return -1;
|
|
|
|
}
|
1998-08-21 01:40:36 +08:00
|
|
|
|
1998-08-14 03:44:05 +08:00
|
|
|
#ifdef S_ISLNK
|
|
|
|
if (symlink (src, dest) == 0)
|
|
|
|
#else
|
|
|
|
if (link (src, dest) == 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* Destination must exist by now. */
|
|
|
|
if (access (dest, F_OK))
|
|
|
|
{
|
|
|
|
error = strerror (errno);
|
|
|
|
unlink (dest);
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("Invalid link from \"%s\" to \"%s\": %s\n"),
|
2013-06-07 01:36:03 +08:00
|
|
|
src, dest, error);
|
2001-02-28 01:59:27 +08:00
|
|
|
return 1;
|
1998-08-14 03:44:05 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
error = strerror (errno);
|
2009-02-07 04:13:07 +08:00
|
|
|
fprintf (stderr, _("Invalid link from \"%s\" to \"%s\": %s\n"),
|
2013-06-07 01:36:03 +08:00
|
|
|
src, dest, error);
|
2001-02-28 01:59:27 +08:00
|
|
|
return 1;
|
1998-08-14 03:44:05 +08:00
|
|
|
}
|
|
|
|
}
|