xref: /freebsd/bin/ln/ln.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
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  *	$Id: ln.c,v 1.4 1994/09/24 02:55:48 davidg Exp $
34  */
35 
36 #ifndef lint
37 static char copyright[] =
38 "@(#) 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 static char sccsid[] = "@(#)ln.c	8.2 (Berkeley) 3/31/94";
44 #endif /* not lint */
45 
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 
49 #include <err.h>
50 #include <errno.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <unistd.h>
55 
56 int	dirflag;			/* Undocumented directory flag. */
57 int	fflag;				/* Unlink existing files. */
58 int	sflag;				/* Symbolic, not hard, link. */
59 					/* System link call. */
60 int (*linkf) __P((const char *, const char *));
61 
62 int	linkit __P((char *, char *, int));
63 void	usage __P((void));
64 
65 int
66 main(argc, argv)
67 	int argc;
68 	char *argv[];
69 {
70 	extern int optind;
71 	struct stat sb;
72 	int ch, exitval;
73 	char *sourcedir;
74 
75 	while ((ch = getopt(argc, argv, "Ffs")) != EOF)
76 		switch (ch) {
77 		case 'F':
78 			dirflag = 1;	/* XXX: deliberately undocumented. */
79 			break;
80 		case 'f':
81 			fflag = 1;
82 			break;
83 		case 's':
84 			sflag = 1;
85 			break;
86 		case '?':
87 		default:
88 			usage();
89 		}
90 
91 	argv += optind;
92 	argc -= optind;
93 
94 	linkf = sflag ? symlink : link;
95 
96 	switch(argc) {
97 	case 0:
98 		usage();
99 	case 1:				/* ln target */
100 		exit(linkit(argv[0], ".", 1));
101 	case 2:				/* ln target source */
102 		exit(linkit(argv[0], argv[1], 0));
103 	}
104 					/* ln target1 target2 directory */
105 	sourcedir = argv[argc - 1];
106 	if (stat(sourcedir, &sb))
107 		err(1, "%s", sourcedir);
108 	if (!S_ISDIR(sb.st_mode))
109 		usage();
110 	for (exitval = 0; *argv != sourcedir; ++argv)
111 		exitval |= linkit(*argv, sourcedir, 1);
112 	exit(exitval);
113 }
114 
115 int
116 linkit(target, source, isdir)
117 	char *target, *source;
118 	int isdir;
119 {
120 	struct stat sb;
121 	int exists;
122 	char *p, path[MAXPATHLEN];
123 
124 	if (!sflag) {
125 		/* If target doesn't exist, quit now. */
126 		if (stat(target, &sb)) {
127 			warn("%s", target);
128 			return (1);
129 		}
130 		/* Only symbolic links to directories, unless -F option used. */
131 		if (!dirflag && (sb.st_mode & S_IFMT) == S_IFDIR) {
132 			warnx("%s: is a directory", target);
133 			return (1);
134 		}
135 	}
136 
137 	/* If the source is a directory, append the target's name. */
138 	if (isdir || ((exists = !stat(source, &sb)) && S_ISDIR(sb.st_mode))) {
139 		if ((p = strrchr(target, '/')) == NULL)
140 			p = target;
141 		else
142 			++p;
143 		(void)snprintf(path, sizeof(path), "%s/%s", source, p);
144 		source = path;
145 		exists = !lstat(source, &sb);
146 	} else
147 		exists = !lstat(source, &sb);
148 
149 	/*
150 	 * If the file exists, and -f was specified, unlink it.
151 	 * Attempt the link.
152 	 */
153 	if ((fflag && exists && unlink(source)) || (*linkf)(target, source)) {
154 		warn("%s", source);
155 		return (1);
156 	}
157 	return (0);
158 }
159 
160 void
161 usage()
162 {
163 	(void)fprintf(stderr,
164 	    "usage:\tln [-fs] file1 file2\n\tln [-fs] file ... directory\n");
165 	exit(1);
166 }
167