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 * @(#) Copyright (c) 1987, 1993, 1994 The Regents of the University of California. All rights reserved. 34 * @(#)ln.c 8.2 (Berkeley) 3/31/94 35 * $FreeBSD: src/bin/ln/ln.c,v 1.15.2.4 2002/07/12 07:34:38 tjr Exp $ 36 * $DragonFly: src/bin/ln/ln.c,v 1.11 2006/09/25 09:27:21 corecode Exp $ 37 */ 38 39 #include <sys/param.h> 40 #include <sys/stat.h> 41 42 #include <err.h> 43 #include <errno.h> 44 #include <stdio.h> 45 #include <stdlib.h> 46 #include <string.h> 47 #include <unistd.h> 48 49 static int fflag; /* Unlink existing files. */ 50 static int hflag; /* Check new name for symlink first. */ 51 static int iflag; /* Interactive mode. */ 52 static int sflag; /* Symbolic, not hard, link. */ 53 static int vflag; /* Verbose output. */ 54 /* System link call. */ 55 static int (*linkf)(const char *, const char *); 56 static char linkch; 57 58 static int linkit(const char *, const char *, int); 59 static void usage(void); 60 61 int 62 main(int argc, char *argv[]) 63 { 64 struct stat sb; 65 char *p, *sourcedir; 66 int ch, exitval; 67 68 /* 69 * Test for the special case where the utility is called as 70 * "link", for which the functionality provided is greatly 71 * simplified. 72 */ 73 if ((p = strrchr(argv[0], '/')) == NULL) 74 p = argv[0]; 75 else 76 ++p; 77 if (strcmp(p, "link") == 0) { 78 while (getopt(argc, argv, "") != -1) 79 usage(); 80 argc -= optind; 81 argv += optind; 82 if (argc != 2) 83 usage(); 84 linkf = link; 85 exit(linkit(argv[0], argv[1], 0)); 86 } 87 88 while ((ch = getopt(argc, argv, "fhinsv")) != -1) { 89 switch (ch) { 90 case 'f': 91 fflag = 1; 92 iflag = 0; 93 break; 94 case 'h': 95 case 'n': 96 hflag = 1; 97 break; 98 case 'i': 99 iflag = 1; 100 fflag = 0; 101 break; 102 case 's': 103 sflag = 1; 104 break; 105 case 'v': 106 vflag = 1; 107 break; 108 case '?': 109 default: 110 usage(); 111 } 112 } 113 114 argv += optind; 115 argc -= optind; 116 117 linkf = sflag ? symlink : link; 118 linkch = sflag ? '-' : '='; 119 120 switch (argc) { 121 case 0: 122 usage(); 123 /* NOTREACHED */ 124 case 1: /* ln target */ 125 exit(linkit(argv[0], ".", 1)); 126 case 2: /* ln target source */ 127 exit(linkit(argv[0], argv[1], 0)); 128 default: 129 ; 130 } 131 /* ln target1 target2 directory */ 132 sourcedir = argv[argc - 1]; 133 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) { 134 /* 135 * We were asked not to follow symlinks, but found one at 136 * the target--simulate "not a directory" error 137 */ 138 errc(1, ENOTDIR, "%s", sourcedir); 139 } 140 if (stat(sourcedir, &sb) != 0) 141 err(1, "%s", sourcedir); 142 if (!S_ISDIR(sb.st_mode)) 143 usage(); 144 for (exitval = 0; *argv != sourcedir; ++argv) 145 exitval |= linkit(*argv, sourcedir, 1); 146 exit(exitval); 147 } 148 149 /* 150 * Nomenclature warning! 151 * 152 * In this source "target" and "source" are used the opposite way they 153 * are used in the ln(1) manual. Here "target" is the existing file and 154 * "source" specifies the to-be-created link to "target". 155 */ 156 static int 157 linkit(const char *target, const char *source, int isdir) 158 { 159 struct stat sb; 160 const char *p; 161 int ch, exists, first; 162 char path[PATH_MAX]; 163 164 if (!sflag) { 165 /* If target doesn't exist, quit now. */ 166 if (stat(target, &sb) != 0) { 167 warn("%s", target); 168 return (1); 169 } 170 /* Only symbolic links to directories. */ 171 if (S_ISDIR(sb.st_mode)) { 172 errno = EISDIR; 173 warn("%s", target); 174 return (1); 175 } 176 } 177 178 /* 179 * If the source is a directory (and not a symlink if hflag), 180 * append the target's name. 181 */ 182 if (isdir || 183 (lstat(source, &sb) == 0 && S_ISDIR(sb.st_mode)) || 184 (!hflag && stat(source, &sb) == 0 && S_ISDIR(sb.st_mode))) { 185 if ((p = strrchr(target, '/')) == NULL) 186 p = target; 187 else 188 ++p; 189 if (snprintf(path, sizeof(path), "%s/%s", source, p) >= 190 (int)sizeof(path)) { 191 errno = ENAMETOOLONG; 192 warn("%s", target); 193 return (1); 194 } 195 source = path; 196 } 197 198 exists = (lstat(source, &sb) == 0); 199 /* 200 * If doing hard links and the source (destination) exists and it 201 * actually is the same file like the target (existing file), we 202 * complain that the files are identical. If -f is specified, we 203 * accept the job as already done and return with success. 204 */ 205 if (exists && !sflag) { 206 struct stat tsb; 207 208 if (stat(target, &tsb) != 0) { 209 warn("%s: disappeared", target); 210 return (1); 211 } 212 213 if (tsb.st_dev == sb.st_dev && tsb.st_ino == sb.st_ino) { 214 warnx("%s and %s are identical (not linked).", target, source); 215 if (fflag) 216 return (0); 217 else 218 return (1); 219 } 220 } 221 /* 222 * If the file exists, then unlink it forcibly if -f was specified 223 * and interactively if -i was specified. 224 */ 225 if (fflag && exists) { 226 if (unlink(source) != 0) { 227 warn("%s", source); 228 return (1); 229 } 230 } else if (iflag && exists) { 231 fflush(stdout); 232 fprintf(stderr, "replace %s? ", source); 233 234 first = ch = getchar(); 235 while(ch != '\n' && ch != EOF) 236 ch = getchar(); 237 if (first != 'y' && first != 'Y') { 238 fprintf(stderr, "not replaced\n"); 239 return (1); 240 } 241 242 if (unlink(source) != 0) { 243 warn("%s", source); 244 return (1); 245 } 246 } 247 248 /* Attempt the link. */ 249 if ((*linkf)(target, source) != 0) { 250 warn("%s", source); 251 return (1); 252 } 253 if (vflag) 254 printf("%s %c> %s\n", source, linkch, target); 255 return (0); 256 } 257 258 static void 259 usage(void) 260 { 261 fprintf(stderr, "%s\n%s\n%s\n", 262 "usage: ln [-fhinsv] source_file [target_file]", 263 " ln [-fhinsv] source_file ... target_dir", 264 " link source_file target_file"); 265 exit(1); 266 } 267