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