NetBSD/bin/ln/ln.c

178 lines
4.7 KiB
C
Raw Normal View History

1997-07-20 21:44:40 +04:00
/* $NetBSD: ln.c,v 1.13 1997/07/20 17:44:42 christos 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* 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
1997-07-20 21:44:40 +04:00
__RCSID("$NetBSD: ln.c,v 1.13 1997/07/20 17:44:42 christos 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. */
/* System link call. */
int (*linkf) __P((const char *, const char *));
int linkit __P((char *, char *, int));
void usage __P((void));
1997-07-20 21:44:40 +04:00
int main __P((int, char *[]));
1993-03-21 12:45:37 +03:00
1994-01-27 04:44:15 +03:00
int
1993-03-21 12:45:37 +03:00
main(argc, argv)
int argc;
1994-09-22 13:24:46 +04:00
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;
while ((ch = getopt(argc, argv, "fhns")) != -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 '?':
default:
usage();
}
argv += optind;
argc -= optind;
linkf = sflag ? symlink : link;
switch(argc) {
case 0:
usage();
case 1: /* ln target */
exit(linkit(argv[0], ".", 1));
case 2: /* ln target source */
exit(linkit(argv[0], argv[1], 0));
}
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(1, "%s", sourcedir);
}
1994-09-22 13:24:46 +04:00
if (stat(sourcedir, &sb))
err(1, "%s", sourcedir);
if (!S_ISDIR(sb.st_mode))
usage();
for (exitval = 0; *argv != sourcedir; ++argv)
exitval |= linkit(*argv, sourcedir, 1);
exit(exitval);
1993-03-21 12:45:37 +03:00
}
1994-09-22 13:24:46 +04:00
int
linkit(target, source, isdir)
char *target, *source;
1993-03-21 12:45:37 +03:00
int isdir;
{
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);
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);
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
}
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
1993-03-21 12:45:37 +03:00
usage()
{
1994-09-22 13:24:46 +04:00
1997-07-20 21:44:40 +04:00
extern char *__progname;
1993-03-21 12:45:37 +03:00
(void)fprintf(stderr,
1997-07-20 21:44:40 +04:00
"Usage:\t%s [-fhns] file1 file2\n\t%s [-fhns] file ... directory\n",
__progname, __progname);
1993-03-21 12:45:37 +03:00
exit(1);
}