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 sflag; /* Symbolic, not hard, link. */ 60 int vflag; /* Verbose output. */ 61 /* System link call. */ 62 int (*linkf) __P((const char *, const char *)); 63 char linkch; 64 65 int linkit __P((char *, char *, int)); 66 void usage __P((void)); 67 68 int 69 main(argc, argv) 70 int argc; 71 char *argv[]; 72 { 73 extern int optind; 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, "fsv")) != -1) 96 switch (ch) { 97 case 'f': 98 fflag = 1; 99 break; 100 case 's': 101 sflag = 1; 102 break; 103 case 'v': 104 vflag = 1; 105 break; 106 case '?': 107 default: 108 usage(); 109 } 110 111 argv += optind; 112 argc -= optind; 113 114 linkf = sflag ? symlink : link; 115 linkch = sflag ? '-' : '='; 116 117 switch(argc) { 118 case 0: 119 usage(); 120 case 1: /* ln target */ 121 exit(linkit(argv[0], ".", 1)); 122 case 2: /* ln target source */ 123 exit(linkit(argv[0], argv[1], 0)); 124 } 125 /* ln target1 target2 directory */ 126 sourcedir = argv[argc - 1]; 127 if (stat(sourcedir, &sb)) 128 err(1, "%s", sourcedir); 129 if (!S_ISDIR(sb.st_mode)) 130 usage(); 131 for (exitval = 0; *argv != sourcedir; ++argv) 132 exitval |= linkit(*argv, sourcedir, 1); 133 exit(exitval); 134 } 135 136 int 137 linkit(target, source, isdir) 138 char *target, *source; 139 int isdir; 140 { 141 struct stat sb; 142 int exists; 143 char *p, path[MAXPATHLEN]; 144 145 if (!sflag) { 146 /* If target doesn't exist, quit now. */ 147 if (stat(target, &sb)) { 148 warn("%s", target); 149 return (1); 150 } 151 /* Only symbolic links to directories. */ 152 if (S_ISDIR(sb.st_mode)) { 153 errno = EISDIR; 154 warn("%s", target); 155 return (1); 156 } 157 } 158 159 /* If the source is a directory, append the target's name. */ 160 if (isdir || ((exists = !stat(source, &sb)) && S_ISDIR(sb.st_mode))) { 161 if ((p = strrchr(target, '/')) == NULL) 162 p = target; 163 else 164 ++p; 165 (void)snprintf(path, sizeof(path), "%s/%s", source, p); 166 source = path; 167 exists = !lstat(source, &sb); 168 } else 169 exists = !lstat(source, &sb); 170 171 /* 172 * If the file exists, and -f was specified, unlink it. 173 * Attempt the link. 174 */ 175 if ((fflag && exists && unlink(source)) || (*linkf)(target, source)) { 176 warn("%s", source); 177 return (1); 178 } 179 if (vflag) 180 (void)printf("%s %c> %s\n", source, linkch, target); 181 return (0); 182 } 183 184 void 185 usage() 186 { 187 (void)fprintf(stderr, "%s\n%s\n%s\n", 188 "usage: ln [-fsv] file1 file2", 189 " ln [-fsv] file ... directory", 190 " link file1 file2"); 191 exit(1); 192 } 193