Importing bin/ln

Change-Id: I318e79f2d02dcff10bda8af335d286a78be5c5a2
This commit is contained in:
Lionel Sambuc 2013-01-30 19:04:17 +01:00
parent 7104bb23a9
commit c31c70743a
7 changed files with 425 additions and 2 deletions

View file

@ -1,7 +1,7 @@
# $NetBSD: Makefile,v 1.22 2007/12/31 15:31:24 ad Exp $
# @(#)Makefile 8.1 (Berkeley) 5/31/93
SUBDIR= cat date echo ed expr kill \
SUBDIR= cat date echo ed expr kill ln \
mkdir pax pwd rm rmdir test
.include <bsd.subdir.mk>

7
bin/ln/Makefile Normal file
View file

@ -0,0 +1,7 @@
# $NetBSD: Makefile,v 1.10 1999/09/27 17:44:50 jdolecek Exp $
# @(#)Makefile 8.2 (Berkeley) 5/31/93
PROG= ln
MAN= ln.1
.include <bsd.prog.mk>

173
bin/ln/ln.1 Normal file
View file

@ -0,0 +1,173 @@
.\" $NetBSD: ln.1,v 1.25 2012/03/22 07:58:17 wiz Exp $
.\"
.\" Copyright (c) 1980, 1990, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
.\" This code is derived from software contributed to Berkeley by
.\" the Institute of Electrical and Electronics Engineers, Inc.
.\"
.\" 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
.\" 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.
.\"
.\" @(#)ln.1 8.2 (Berkeley) 12/30/93
.\"
.Dd October 29, 2007
.Dt LN 1
.Os
.Sh NAME
.Nm ln
.Nd make links
.Sh SYNOPSIS
.Nm
.Op Fl fhinsv
.Ar source_file
.Op Ar target_file
.Nm
.Op Fl fhinsv
.Ar source_file ... target_dir
.Sh DESCRIPTION
The
.Nm
utility creates a new directory entry (linked file) which has the
same modes as the original file.
It is useful for maintaining multiple copies of a file in many places
at once without using up storage for the
.Dq copies ;
instead, a link
.Dq points
to the original copy.
There are two types of links: hard links and symbolic links.
How a link
.Dq points
to a file is one of the differences between a hard or symbolic link.
.Pp
The options are as follows:
.Bl -tag -width flag
.It Fl f
Unlink any already existing file, permitting the link to occur.
.It Fl h
If the
.Ar target_file
or
.Ar target_dir
is a symbolic link, do not follow it.
This is most useful with the
.Fl f
option, to replace a symlink which may point to a directory.
.It Fl i
Cause
.Nm
to write a prompt to standard error if the target file exists.
If the response from the standard input begins with the character
.Sq Li y
or
.Sq Li Y ,
then unlink the target file so that the link may occur.
Otherwise, do not attempt the link.
(The
.Fl i
option overrides any previous
.Fl f
options.)
.It Fl n
Same as
.Fl h ,
for compatibility with other
.Nm
implementations, namely GNU coreutils.
.It Fl s
Create a symbolic link.
.It Fl v
Cause
.Nm
to be verbose, showing files as they are processed.
.El
.Pp
By default
.Nm
makes
.Em hard
links.
A hard link to a file is indistinguishable from the original directory entry;
any changes to a file are effective independent of the name used to reference
the file.
Hard links may not normally refer to directories and may not span file systems.
.Pp
A symbolic link contains the name of the file to
which it is linked.
The referenced file is used when an
.Xr open 2
operation is performed on the link.
A
.Xr stat 2
on a symbolic link will return the linked-to file; an
.Xr lstat 2
must be done to obtain information about the link.
The
.Xr readlink 2
call may be used to read the contents of a symbolic link.
Symbolic links may span file systems and may refer to directories.
.Pp
Given one or two arguments,
.Nm
creates a link to an existing file
.Ar source_file .
If
.Ar target_file
is given, the link has that name;
.Ar target_file
may also be a directory in which to place the link;
otherwise it is placed in the current directory.
If only the directory is specified, the link will be made
to the last component of
.Ar source_file .
.Pp
Given more than two arguments,
.Nm
makes links in
.Ar target_dir
to all the named source files.
The links made will have the same name as the files being linked to.
.Sh SEE ALSO
.Xr link 2 ,
.Xr lstat 2 ,
.Xr readlink 2 ,
.Xr stat 2 ,
.Xr symlink 2 ,
.Xr symlink 7
.Sh STANDARDS
The
.Nm
utility conforms to
.St -p1003.2-92 .
.Pp
The
.Fl v
option is an extension to
.St -p1003.2-92 .
.Sh HISTORY
A
.Nm
utility appeared in
.At v6 .

230
bin/ln/ln.c Normal file
View file

@ -0,0 +1,230 @@
/* $NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg Exp $ */
/*
* Copyright (c) 1987, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* 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
* 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94";
#else
__RCSID("$NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg Exp $");
#endif
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <err.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static int fflag; /* Unlink existing files. */
static int hflag; /* Check new name for symlink first. */
static int iflag; /* Interactive mode. */
static int sflag; /* Symbolic, not hard, link. */
static int vflag; /* Verbose output */
/* System link call. */
static int (*linkf)(const char *, const char *);
static char linkch;
static int linkit(const char *, const char *, int);
__dead static void usage(void);
int
main(int argc, char *argv[])
{
struct stat sb;
int ch, exitval;
char *sourcedir;
setprogname(argv[0]);
(void)setlocale(LC_ALL, "");
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();
/* NOTREACHED */
}
argv += optind;
argc -= optind;
if (sflag) {
linkf = symlink;
linkch = '-';
} else {
linkf = link;
linkch = '=';
}
switch(argc) {
case 0:
usage();
/* NOTREACHED */
case 1: /* ln target */
exit(linkit(argv[0], ".", 1));
/* NOTREACHED */
case 2: /* ln target source */
exit(linkit(argv[0], argv[1], 0));
/* NOTREACHED */
}
/* 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)) {
usage();
/* NOTREACHED */
}
for (exitval = 0; *argv != sourcedir; ++argv)
exitval |= linkit(*argv, sourcedir, 1);
exit(exitval);
/* NOTREACHED */
}
static int
linkit(const char *target, const char *source, int isdir)
{
struct stat sb;
const char *p;
char path[MAXPATHLEN];
int ch, exists, first;
if (!sflag) {
/* If target doesn't exist, quit now. */
if (stat(target, &sb)) {
warn("%s", target);
return (1);
}
}
/* If the source is a directory (and not a symlink if hflag),
append the target's name. */
if (isdir ||
(!lstat(source, &sb) && S_ISDIR(sb.st_mode)) ||
(!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) {
if ((p = strrchr(target, '/')) == NULL)
p = target;
else
++p;
(void)snprintf(path, sizeof(path), "%s/%s", source, p);
source = path;
}
exists = !lstat(source, &sb);
/*
* If the file exists, then unlink it forcibly if -f was specified
* and interactively if -i was specified.
*/
if (fflag && exists) {
if (unlink(source)) {
warn("%s", source);
return (1);
}
} else if (iflag && exists) {
fflush(stdout);
(void)fprintf(stderr, "replace %s? ", source);
first = ch = getchar();
while (ch != '\n' && ch != EOF)
ch = getchar();
if (first != 'y' && first != 'Y') {
(void)fprintf(stderr, "not replaced\n");
return (1);
}
if (unlink(source)) {
warn("%s", source);
return (1);
}
}
/* Attempt the link. */
if ((*linkf)(target, source)) {
warn("%s", source);
return (1);
}
if (vflag)
(void)printf("%s %c> %s\n", source, linkch, target);
return (0);
}
static void
usage(void)
{
(void)fprintf(stderr,
"usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n",
getprogname(), getprogname());
exit(1);
/* NOTREACHED */
}

View file

@ -5,7 +5,6 @@ BINDIR= /bin
MAN=
LINKS+= ${BINDIR}/cp ${BINDIR}/mv
LINKS+= ${BINDIR}/cp ${BINDIR}/ln
LINKS+= ${BINDIR}/cp ${BINDIR}/clone
LINKS+= ${BINDIR}/cp ${BINDIR}/cpdir

View file

@ -1,3 +1,16 @@
20130201:
Replace our native ln with NetBSD version.
As our native ln is a hard link to cp, simply overwriting ln with
the new version would cause mv, cp, etc to stop working.
Do this BEFORE a 'make build':
# rm /bin/ln
# cp /bin/cp /bin/ln
Then 'make build' as usual.
20130130:
With the shell update, and following update of 'test', some
modifications are needed in .profile and .ashrc.

View file

@ -12,6 +12,7 @@
2012/01/16 18:47:57,bin/ed
2012/10/17 12:00:00,bin/expr
2012/10/17 12:00:00,bin/kill
2012/10/17 12:00:00,bin/ln
2012/10/17 12:00:00,bin/Makefile
2012/10/17 12:00:00,bin/Makefile.inc
2008/07/20 00:52:40,bin/mkdir