ln.c revision 1557
1/*
2 * Copyright (c) 1987, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char copyright[] =
36"@(#) Copyright (c) 1987, 1993, 1994\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
42#endif /* not lint */
43
44#include <sys/param.h>
45#include <sys/stat.h>
46
47#include <err.h>
48#include <errno.h>
49#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
52#include <unistd.h>
53
54int	dirflag;			/* Undocumented directory flag. */
55int	fflag;				/* Unlink existing files. */
56int	sflag;				/* Symbolic, not hard, link. */
57					/* System link call. */
58int (*linkf) __P((const char *, const char *));
59
60int	linkit __P((char *, char *, int));
61void	usage __P((void));
62
63int
64main(argc, argv)
65	int argc;
66	char *argv[];
67{
68	extern int optind;
69	struct stat sb;
70	int ch, exitval;
71	char *sourcedir;
72
73	while ((ch = getopt(argc, argv, "Ffs")) != EOF)
74		switch (ch) {
75		case 'F':
76			dirflag = 1;	/* XXX: deliberately undocumented. */
77			break;
78		case 'f':
79			fflag = 1;
80			break;
81		case 's':
82			sflag = 1;
83			break;
84		case '?':
85		default:
86			usage();
87		}
88
89	argv += optind;
90	argc -= optind;
91
92	linkf = sflag ? symlink : link;
93
94	switch(argc) {
95	case 0:
96		usage();
97	case 1:				/* ln target */
98		exit(linkit(argv[0], ".", 1));
99	case 2:				/* ln target source */
100		exit(linkit(argv[0], argv[1], 0));
101	}
102					/* ln target1 target2 directory */
103	sourcedir = argv[argc - 1];
104	if (stat(sourcedir, &sb))
105		err(1, "%s", sourcedir);
106	if (!S_ISDIR(sb.st_mode))
107		usage();
108	for (exitval = 0; *argv != sourcedir; ++argv)
109		exitval |= linkit(*argv, sourcedir, 1);
110	exit(exitval);
111}
112
113int
114linkit(target, source, isdir)
115	char *target, *source;
116	int isdir;
117{
118	struct stat sb;
119	int exists;
120	char *p, path[MAXPATHLEN];
121
122	if (!sflag) {
123		/* If target doesn't exist, quit now. */
124		if (stat(target, &sb)) {
125			warn("%s", target);
126			return (1);
127		}
128		/* Only symbolic links to directories, unless -F option used. */
129		if (!dirflag && (sb.st_mode & S_IFMT) == S_IFDIR) {
130			warnx("%s: is a directory", target);
131			return (1);
132		}
133	}
134
135	/* If the source is a directory, append the target's name. */
136	if (isdir || (exists = !stat(source, &sb)) && S_ISDIR(sb.st_mode)) {
137		if ((p = strrchr(target, '/')) == NULL)
138			p = target;
139		else
140			++p;
141		(void)snprintf(path, sizeof(path), "%s/%s", source, p);
142		source = path;
143		exists = !stat(source, &sb);
144	} else
145		exists = !stat(source, &sb);
146
147	/*
148	 * If the file exists, and -f was specified, unlink it.
149	 * Attempt the link.
150	 */
151	if (fflag && exists && unlink(source) || (*linkf)(target, source)) {
152		warn("%s", source);
153		return (1);
154	}
155	return (0);
156}
157
158void
159usage()
160{
161	(void)fprintf(stderr,
162	    "usage:\tln [-fs] file1 file2\n\tln [-fs] file ... directory\n");
163	exit(1);
164}
165