#ifndef lint
static char const copyright[] =
"@(#) Copyright (c) 1987, 1993, 1994\n\
The Regents of the University of California. All rights reserved.\n";
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
#endif
#endif
#include <sys/cdefs.h>
__RCSID("$FreeBSD: src/bin/ln/ln.c,v 1.29 2002/07/31 16:53:59 markm Exp $");
#include <sys/param.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int fflag;
int hflag;
int iflag;
int sflag;
int vflag;
int (*linkf)(const char *, const char *);
char linkch;
int linkit(const char *, const char *, int);
void usage(void);
int
main(int argc, char *argv[])
{
struct stat sb;
char *p, *sourcedir;
int ch, exitval;
if ((p = rindex(argv[0], '/')) == NULL)
p = argv[0];
else
++p;
if (strcmp(p, "link") == 0) {
while (getopt(argc, argv, "") != -1)
usage();
argc -= optind;
argv += optind;
if (argc != 2)
usage();
linkf = link;
exit(linkit(argv[0], argv[1], 0));
}
while ((ch = getopt(argc, argv, "fhinsv")) != -1)
switch (ch) {
case 'f':
fflag = 1;
iflag = 0;
break;
case 'h':
case 'n':
hflag = 1;
break;
case 'i':
iflag = 1;
fflag = 0;
break;
case 's':
sflag = 1;
break;
case 'v':
vflag = 1;
break;
case '?':
default:
usage();
}
argv += optind;
argc -= optind;
linkf = sflag ? symlink : link;
linkch = sflag ? '-' : '=';
switch(argc) {
case 0:
usage();
case 1:
exit(linkit(argv[0], ".", 1));
case 2:
exit(linkit(argv[0], argv[1], 0));
default:
;
}
sourcedir = argv[argc - 1];
if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) {
errno = ENOTDIR;
err(1, "%s", sourcedir);
}
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);
}
int
linkit(const char *target, const char *source, int isdir)
{
struct stat sb;
const char *p;
int ch, exists, first;
char path[PATH_MAX];
if (!sflag) {
if (stat(target, &sb)) {
warn("%s", target);
return (1);
}
if (S_ISDIR(sb.st_mode)) {
errno = EISDIR;
warn("%s", target);
return (1);
}
}
if (isdir ||
(lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) ||
(!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) {
if ((p = strrchr(target, '/')) == NULL)
p = target;
else
++p;
if (snprintf(path, sizeof(path), "%s/%s", source, p) >=
(ssize_t)sizeof(path)) {
errno = ENAMETOOLONG;
warn("%s", target);
return (1);
}
source = path;
}
exists = !lstat(source, &sb);
if (fflag && exists) {
if (unlink(source)) {
warn("%s", source);
return (1);
}
} else if (iflag && exists) {
fflush(stdout);
fprintf(stderr, "replace %s? ", source);
first = ch = getchar();
while(ch != '\n' && ch != EOF)
ch = getchar();
if (first != 'y' && first != 'Y') {
fprintf(stderr, "not replaced\n");
return (1);
}
if (unlink(source)) {
warn("%s", source);
return (1);
}
}
if ((*linkf)(target, source)) {
warn("%s", source);
return (1);
}
if (vflag)
(void)printf("%s %c> %s\n", source, linkch, target);
return (0);
}
void
usage(void)
{
(void)fprintf(stderr, "%s\n%s\n%s\n",
"usage: ln [-fhinsv] file1 file2",
" ln [-fhinsv] file ... directory",
" link file1 file2");
exit(1);
}