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