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 34 #ifndef lint 35 static char const copyright[] = 36 "@(#) Copyright (c) 1987, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)ln.c 8.2 (Berkeley) 3/31/94"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/stat.h> 50 51 #include <err.h> 52 #include <errno.h> 53 #include <stdio.h> 54 #include <stdlib.h> 55 #include <string.h> 56 #include <unistd.h> 57 58 int fflag; /* Unlink existing files. */ 59 int iflag; /* Interactive mode. */ 60 int sflag; /* Symbolic, not hard, link. */ 61 int vflag; /* Verbose output. */ 62 /* System link call. */ 63 int (*linkf) __P((const char *, const char *)); 64 char linkch; 65 66 int linkit __P((char *, char *, int)); 67 void usage __P((void)); 68 69 int 70 main(argc, argv) 71 int argc; 72 char *argv[]; 73 { 74 struct stat sb; 75 int ch, exitval; 76 char *p, *sourcedir; 77 78 /* 79 * Test for the special case where the utility is called as 80 * "link", for which the functionality provided is greatly 81 * simplified. 82 */ 83 if ((p = rindex(argv[0], '/')) == NULL) 84 p = argv[0]; 85 else 86 ++p; 87 if (strcmp(p, "link") == 0) { 88 if (argc == 3) { 89 linkf = link; 90 exit(linkit(argv[1], argv[2], 0)); 91 } else 92 usage(); 93 } 94 95 while ((ch = getopt(argc, argv, "fisv")) != -1) 96 switch (ch) { 97 case 'f': 98 fflag = 1; 99 iflag = 0; 100 break; 101 case 'i': 102 iflag = 1; 103 fflag = 0; 104 break; 105 case 's': 106 sflag = 1; 107 break; 108 case 'v': 109 vflag = 1; 110 break; 111 case '?': 112 default: 113 usage(); 114 } 115 116 argv += optind; 117 argc -= optind; 118 119 linkf = sflag ? symlink : link; 120 linkch = sflag ? '-' : '='; 121 122 switch(argc) { 123 case 0: 124 usage(); 125 case 1: /* ln target */ 126 exit(linkit(argv[0], ".", 1)); 127 case 2: /* ln target source */ 128 exit(linkit(argv[0], argv[1], 0)); 129 } 130 /* ln target1 target2 directory */ 131 sourcedir = argv[argc - 1]; 132 if (stat(sourcedir, &sb)) 133 err(1, "%s", sourcedir); 134 if (!S_ISDIR(sb.st_mode)) 135 usage(); 136 for (exitval = 0; *argv != sourcedir; ++argv) 137 exitval |= linkit(*argv, sourcedir, 1); 138 exit(exitval); 139 } 140 141 int 142 linkit(target, source, isdir) 143 char *target, *source; 144 int isdir; 145 { 146 struct stat sb; 147 int ch, exists, first; 148 char *p, path[MAXPATHLEN]; 149 150 if (!sflag) { 151 /* If target doesn't exist, quit now. */ 152 if (stat(target, &sb)) { 153 warn("%s", target); 154 return (1); 155 } 156 /* Only symbolic links to directories. */ 157 if (S_ISDIR(sb.st_mode)) { 158 errno = EISDIR; 159 warn("%s", target); 160 return (1); 161 } 162 } 163 164 /* If the source is a directory, append the target's name. */ 165 if (isdir || ((exists = !stat(source, &sb)) && S_ISDIR(sb.st_mode))) { 166 if ((p = strrchr(target, '/')) == NULL) 167 p = target; 168 else 169 ++p; 170 (void)snprintf(path, sizeof(path), "%s/%s", source, p); 171 source = path; 172 exists = !lstat(source, &sb); 173 } else 174 exists = !lstat(source, &sb); 175 176 /* 177 * If the file exists, then unlink it forcibly if -f was specified 178 * and interactively if -i was specified. 179 */ 180 if (fflag && exists) { 181 if (unlink(source)) { 182 warn("%s", source); 183 return (1); 184 } 185 } else if (iflag && exists) { 186 fflush(stdout); 187 fprintf(stderr, "replace %s? ", source); 188 189 first = ch = getchar(); 190 while(ch != '\n' && ch != EOF) 191 ch = getchar(); 192 if (first != 'y' && first != 'Y') { 193 fprintf(stderr, "not replaced\n"); 194 return (1); 195 } 196 197 if (unlink(source)) { 198 warn("%s", source); 199 return (1); 200 } 201 } 202 203 /* Attempt the link. */ 204 if ((*linkf)(target, source)) { 205 warn("%s", source); 206 return (1); 207 } 208 if (vflag) 209 (void)printf("%s %c> %s\n", source, linkch, target); 210 return (0); 211 } 212 213 void 214 usage() 215 { 216 (void)fprintf(stderr, "%s\n%s\n%s\n", 217 "usage: ln [-fisv] file1 file2", 218 " ln [-fisv] file ... directory", 219 " link file1 file2"); 220 exit(1); 221 } 222