2004-01-06 02:23:32 +03:00
|
|
|
/* $NetBSD: ln.c,v 1.28 2004/01/05 23:23:32 jmmv Exp $ */
|
1995-03-21 12:01:59 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-09-22 13:24:46 +04:00
|
|
|
* Copyright (c) 1987, 1993, 1994
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 13:05:01 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
1993-03-21 12:45:37 +03:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
1997-07-20 21:44:40 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-07-20 21:44:40 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1995-03-21 12:01:59 +03:00
|
|
|
#if 0
|
|
|
|
static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
|
|
|
|
#else
|
2004-01-06 02:23:32 +03:00
|
|
|
__RCSID("$NetBSD: ln.c,v 1.28 2004/01/05 23:23:32 jmmv Exp $");
|
1995-03-21 12:01:59 +03:00
|
|
|
#endif
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
1994-09-22 13:24:46 +04:00
|
|
|
|
|
|
|
#include <err.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <errno.h>
|
1994-09-22 13:24:46 +04:00
|
|
|
#include <stdio.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdlib.h>
|
1994-09-22 13:24:46 +04:00
|
|
|
#include <string.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <unistd.h>
|
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
int fflag; /* Unlink existing files. */
|
1997-05-16 18:44:01 +04:00
|
|
|
int hflag; /* Check new name for symlink first. */
|
1994-09-22 13:24:46 +04:00
|
|
|
int sflag; /* Symbolic, not hard, link. */
|
2003-08-05 02:31:21 +04:00
|
|
|
int vflag; /* Verbose output */
|
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
/* System link call. */
|
2001-09-17 02:14:40 +04:00
|
|
|
int (*linkf)(const char *, const char *);
|
2002-12-27 06:50:05 +03:00
|
|
|
char linkch;
|
1994-09-22 13:24:46 +04:00
|
|
|
|
2001-09-17 02:14:40 +04:00
|
|
|
int linkit(char *, char *, int);
|
|
|
|
void usage(void);
|
|
|
|
int main(int, char *[]);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-01-27 04:44:15 +03:00
|
|
|
int
|
2001-09-17 02:14:40 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-09-22 13:24:46 +04:00
|
|
|
struct stat sb;
|
1994-01-27 04:44:15 +03:00
|
|
|
int ch, exitval;
|
1993-03-21 12:45:37 +03:00
|
|
|
char *sourcedir;
|
|
|
|
|
2001-09-17 02:14:40 +04:00
|
|
|
setprogname(argv[0]);
|
2002-12-27 06:50:05 +03:00
|
|
|
while ((ch = getopt(argc, argv, "fhnsv")) != -1)
|
1994-09-22 13:24:46 +04:00
|
|
|
switch (ch) {
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1997-05-16 18:44:01 +04:00
|
|
|
case 'h':
|
|
|
|
case 'n':
|
|
|
|
hflag = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case 's':
|
|
|
|
sflag = 1;
|
|
|
|
break;
|
2002-12-27 06:50:05 +03:00
|
|
|
case 'v':
|
2004-01-04 19:04:18 +03:00
|
|
|
vflag = 1;
|
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
2003-08-05 02:31:21 +04:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
2002-12-27 06:50:05 +03:00
|
|
|
if (sflag) {
|
|
|
|
linkf = symlink;
|
|
|
|
linkch = '-';
|
|
|
|
} else {
|
|
|
|
linkf = link;
|
|
|
|
linkch = '=';
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
switch(argc) {
|
|
|
|
case 0:
|
|
|
|
usage();
|
1997-11-06 00:17:14 +03:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 1: /* ln target */
|
|
|
|
exit(linkit(argv[0], ".", 1));
|
1997-11-06 00:17:14 +03:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
case 2: /* ln target source */
|
|
|
|
exit(linkit(argv[0], argv[1], 0));
|
1997-11-06 00:17:14 +03:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2003-08-05 02:31:21 +04:00
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
/* ln target1 target2 directory */
|
|
|
|
sourcedir = argv[argc - 1];
|
1997-05-16 18:44:01 +04:00
|
|
|
if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
|
1997-05-16 06:59:38 +04:00
|
|
|
/* we were asked not to follow symlinks, but found one at
|
|
|
|
the target--simulate "not a directory" error */
|
|
|
|
errno = ENOTDIR;
|
2003-09-14 23:20:16 +04:00
|
|
|
err(EXIT_FAILURE, "%s", sourcedir);
|
2003-08-05 02:31:21 +04:00
|
|
|
/* NOTREACHED */
|
1997-05-16 06:59:38 +04:00
|
|
|
}
|
2003-08-05 02:31:21 +04:00
|
|
|
if (stat(sourcedir, &sb)) {
|
2003-09-14 23:20:16 +04:00
|
|
|
err(EXIT_FAILURE, "%s", sourcedir);
|
2003-08-05 02:31:21 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
|
|
|
if (!S_ISDIR(sb.st_mode)) {
|
1994-09-22 13:24:46 +04:00
|
|
|
usage();
|
2003-08-05 02:31:21 +04:00
|
|
|
/* NOTREACHED */
|
|
|
|
}
|
1994-09-22 13:24:46 +04:00
|
|
|
for (exitval = 0; *argv != sourcedir; ++argv)
|
|
|
|
exitval |= linkit(*argv, sourcedir, 1);
|
|
|
|
exit(exitval);
|
1997-11-06 00:17:14 +03:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
int
|
2001-09-17 02:14:40 +04:00
|
|
|
linkit(char *target, char *source, int isdir)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-09-22 13:24:46 +04:00
|
|
|
struct stat sb;
|
|
|
|
char *p, path[MAXPATHLEN];
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!sflag) {
|
1994-09-22 13:24:46 +04:00
|
|
|
/* If target doesn't exist, quit now. */
|
2002-10-31 01:52:10 +03:00
|
|
|
if (stat(target, &sb)) {
|
2003-09-14 23:20:16 +04:00
|
|
|
warn("%s", target);
|
1994-09-22 13:24:46 +04:00
|
|
|
return (1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1997-05-16 18:44:01 +04:00
|
|
|
/* If the source is a directory (and not a symlink if hflag),
|
1997-05-16 06:59:38 +04:00
|
|
|
append the target's name. */
|
|
|
|
if (isdir ||
|
1997-07-20 21:44:40 +04:00
|
|
|
(!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
|
|
|
|
(!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
|
1994-09-22 13:24:46 +04:00
|
|
|
if ((p = strrchr(target, '/')) == NULL)
|
|
|
|
p = target;
|
1993-03-21 12:45:37 +03:00
|
|
|
else
|
1994-09-22 13:24:46 +04:00
|
|
|
++p;
|
2003-09-14 23:20:16 +04:00
|
|
|
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
|
1994-09-22 13:24:46 +04:00
|
|
|
source = path;
|
1993-08-17 05:03:49 +04:00
|
|
|
}
|
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
/*
|
|
|
|
* If the file exists, and -f was specified, unlink it.
|
|
|
|
* Attempt the link.
|
|
|
|
*/
|
1997-07-20 21:44:40 +04:00
|
|
|
if ((fflag && unlink(source) < 0 && errno != ENOENT) ||
|
1994-09-22 13:24:46 +04:00
|
|
|
(*linkf)(target, source)) {
|
|
|
|
warn("%s", source);
|
1993-08-17 05:03:49 +04:00
|
|
|
return (1);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
2002-12-27 06:50:05 +03:00
|
|
|
if (vflag)
|
2003-09-14 23:20:16 +04:00
|
|
|
(void)printf("%s %c> %s\n", source, linkch, target);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
return (0);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
1994-09-22 13:24:46 +04:00
|
|
|
void
|
2001-09-17 02:14:40 +04:00
|
|
|
usage(void)
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1994-09-22 13:24:46 +04:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
(void)fprintf(stderr,
|
2004-01-06 02:23:32 +03:00
|
|
|
"usage:\t%s [-fhns] file1 file2\n\t%s [-fhnsv] file ... directory\n",
|
2001-02-20 01:39:39 +03:00
|
|
|
getprogname(), getprogname());
|
1993-03-21 12:45:37 +03:00
|
|
|
exit(1);
|
1998-07-28 09:31:22 +04:00
|
|
|
/* NOTREACHED */
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|