NetBSD/bin/ln/ln.c

196 lines
4.9 KiB
C
Raw Normal View History

/* $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.
* 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
__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. */
int hflag; /* Check new name for symlink first. */
1994-09-22 13:24:46 +04:00
int sflag; /* Symbolic, not hard, link. */
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 *);
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]);
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;
case 'h':
case 'n':
hflag = 1;
break;
1993-03-21 12:45:37 +03:00
case 's':
sflag = 1;
break;
case 'v':
vflag = 1;
break;
1993-03-21 12:45:37 +03:00
case '?':
default:
usage();
/* NOTREACHED */
1993-03-21 12:45:37 +03:00
}
argv += optind;
argc -= optind;
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
}
1994-09-22 13:24:46 +04:00
/* ln target1 target2 directory */
sourcedir = argv[argc - 1];
if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
/* we were asked not to follow symlinks, but found one at
the target--simulate "not a directory" error */
errno = ENOTDIR;
err(EXIT_FAILURE, "%s", sourcedir);
/* NOTREACHED */
}
if (stat(sourcedir, &sb)) {
err(EXIT_FAILURE, "%s", sourcedir);
/* NOTREACHED */
}
if (!S_ISDIR(sb.st_mode)) {
1994-09-22 13:24:46 +04:00
usage();
/* 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. */
if (stat(target, &sb)) {
warn("%s", target);
1994-09-22 13:24:46 +04:00
return (1);
1993-03-21 12:45:37 +03:00
}
}
/* If the source is a directory (and not a symlink if hflag),
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;
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
1994-09-22 13:24:46 +04:00
source = path;
}
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);
return (1);
1993-03-21 12:45:37 +03:00
}
if (vflag)
(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,
"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
}