1/* $NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34__COPYRIGHT("@(#) Copyright (c) 1987, 1993, 1994\ 35 The Regents of the University of California. All rights reserved."); 36#endif /* not lint */ 37 38#ifndef lint 39#if 0 40static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94"; 41#else 42__RCSID("$NetBSD: ln.c,v 1.35 2011/08/29 14:38:30 joerg Exp $"); 43#endif 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 <locale.h> 52#include <stdio.h> 53#include <stdlib.h> 54#include <string.h> 55#include <unistd.h> 56 57static int fflag; /* Unlink existing files. */ 58static int hflag; /* Check new name for symlink first. */ 59static int iflag; /* Interactive mode. */ 60static int sflag; /* Symbolic, not hard, link. */ 61static int vflag; /* Verbose output */ 62 63 /* System link call. */ 64static int (*linkf)(const char *, const char *); 65static char linkch; 66 67static int linkit(const char *, const char *, int); 68__dead static void usage(void); 69 70int 71main(int argc, char *argv[]) 72{ 73 struct stat sb; 74 int ch, exitval; 75 char *sourcedir; 76 77 setprogname(argv[0]); 78 (void)setlocale(LC_ALL, ""); 79 80 while ((ch = getopt(argc, argv, "fhinsv")) != -1) 81 switch (ch) { 82 case 'f': 83 fflag = 1; 84 iflag = 0; 85 break; 86 case 'h': 87 case 'n': 88 hflag = 1; 89 break; 90 case 'i': 91 iflag = 1; 92 fflag = 0; 93 break; 94 case 's': 95 sflag = 1; 96 break; 97 case 'v': 98 vflag = 1; 99 break; 100 case '?': 101 default: 102 usage(); 103 /* NOTREACHED */ 104 } 105 106 argv += optind; 107 argc -= optind; 108 109 if (sflag) { 110 linkf = symlink; 111 linkch = '-'; 112 } else { 113 linkf = link; 114 linkch = '='; 115 } 116 117 switch(argc) { 118 case 0: 119 usage(); 120 /* NOTREACHED */ 121 case 1: /* ln target */ 122 exit(linkit(argv[0], ".", 1)); 123 /* NOTREACHED */ 124 case 2: /* ln target source */ 125 exit(linkit(argv[0], argv[1], 0)); 126 /* NOTREACHED */ 127 } 128 129 /* ln target1 target2 directory */ 130 sourcedir = argv[argc - 1]; 131 if (hflag && lstat(sourcedir, &sb) == 0 && S_ISLNK(sb.st_mode)) { 132 /* we were asked not to follow symlinks, but found one at 133 the target--simulate "not a directory" error */ 134 errno = ENOTDIR; 135 err(EXIT_FAILURE, "%s", sourcedir); 136 /* NOTREACHED */ 137 } 138 if (stat(sourcedir, &sb)) { 139 err(EXIT_FAILURE, "%s", sourcedir); 140 /* NOTREACHED */ 141 } 142 if (!S_ISDIR(sb.st_mode)) { 143 usage(); 144 /* NOTREACHED */ 145 } 146 for (exitval = 0; *argv != sourcedir; ++argv) 147 exitval |= linkit(*argv, sourcedir, 1); 148 exit(exitval); 149 /* NOTREACHED */ 150} 151 152static int 153linkit(const char *target, const char *source, int isdir) 154{ 155 struct stat sb; 156 const char *p; 157 char path[MAXPATHLEN]; 158 int ch, exists, first; 159 160 if (!sflag) { 161 /* If target doesn't exist, quit now. */ 162 if (stat(target, &sb)) { 163 warn("%s", target); 164 return (1); 165 } 166 } 167 168 /* If the source is a directory (and not a symlink if hflag), 169 append the target's name. */ 170 if (isdir || 171 (!lstat(source, &sb) && S_ISDIR(sb.st_mode)) || 172 (!hflag && !stat(source, &sb) && S_ISDIR(sb.st_mode))) { 173 if ((p = strrchr(target, '/')) == NULL) 174 p = target; 175 else 176 ++p; 177 (void)snprintf(path, sizeof(path), "%s/%s", source, p); 178 source = path; 179 } 180 181 exists = !lstat(source, &sb); 182 183 /* 184 * If the file exists, then unlink it forcibly if -f was specified 185 * and interactively if -i was specified. 186 */ 187 if (fflag && exists) { 188 if (unlink(source)) { 189 warn("%s", source); 190 return (1); 191 } 192 } else if (iflag && exists) { 193 fflush(stdout); 194 (void)fprintf(stderr, "replace %s? ", source); 195 196 first = ch = getchar(); 197 while (ch != '\n' && ch != EOF) 198 ch = getchar(); 199 if (first != 'y' && first != 'Y') { 200 (void)fprintf(stderr, "not replaced\n"); 201 return (1); 202 } 203 204 if (unlink(source)) { 205 warn("%s", source); 206 return (1); 207 } 208 } 209 210 /* Attempt the link. */ 211 if ((*linkf)(target, source)) { 212 warn("%s", source); 213 return (1); 214 } 215 if (vflag) 216 (void)printf("%s %c> %s\n", source, linkch, target); 217 218 return (0); 219} 220 221static void 222usage(void) 223{ 224 225 (void)fprintf(stderr, 226 "usage:\t%s [-fhinsv] file1 file2\n\t%s [-fhinsv] file ... directory\n", 227 getprogname(), getprogname()); 228 exit(1); 229 /* NOTREACHED */ 230} 231