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