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