ln.c (7042B)
1 /* see LICENSE file for copyright and license details */ 2 3 #if FEATURE_LN_RELATIVE 4 #ifndef _DEFAULT_SOURCE 5 #define _DEFAULT_SOURCE 6 #endif 7 #endif 8 #include <sys/stat.h> 9 10 #include <errno.h> 11 #include <fcntl.h> 12 #include <libgen.h> 13 #include <limits.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <unistd.h> 17 18 #include "util.h" 19 20 #if FEATURE_LN_RELATIVE 21 #define MAXPATHPARTS (PATH_MAX / 2) 22 23 /* splits an absolute, realpath()-canonicalized path in place on '/', 24 * returning pointers to each component; safe only against realpath() 25 * output, which has no trailing or doubled slashes to worry about */ 26 static int 27 split_path(char *path, char **parts, int max) 28 { 29 char *p = path; 30 int n = 0; 31 32 if (*p == '/') 33 p++; 34 while (*p && n < max) { 35 parts[n++] = p; 36 p = strchr(p, '/'); 37 if (!p) 38 break; 39 *p++ = '\0'; 40 } 41 return n; 42 } 43 44 /* the text a relative symlink at from_dir/name needs to store to reach 45 * to_path: the common prefix of both canonicalized paths, a ".." per 46 * remaining from_dir component, then whatever is left of to_path */ 47 static void 48 relpath(const char *from_dir, const char *to_path, char *out, size_t outsz) 49 { 50 char from_real[PATH_MAX], to_real[PATH_MAX], to_dir_buf[PATH_MAX], to_base_buf[PATH_MAX]; 51 char *from_parts[MAXPATHPARTS], *to_parts[MAXPATHPARTS]; 52 int nfrom, nto, ncommon, i; 53 54 if (!realpath(from_dir, from_real)) 55 eprintf("realpath %s:", from_dir); 56 57 strlcpy(to_dir_buf, to_path, sizeof(to_dir_buf)); 58 strlcpy(to_base_buf, to_path, sizeof(to_base_buf)); 59 if (!realpath(dirname(to_dir_buf), to_real)) 60 eprintf("realpath %s:", to_path); 61 62 nfrom = split_path(from_real, from_parts, MAXPATHPARTS); 63 nto = split_path(to_real, to_parts, MAXPATHPARTS); 64 65 ncommon = 0; 66 while (ncommon < nfrom && ncommon < nto && strcmp(from_parts[ncommon], to_parts[ncommon]) == 0) 67 ncommon++; 68 69 out[0] = '\0'; 70 for (i = ncommon; i < nfrom; i++) 71 strlcat(out, "../", outsz); 72 for (i = ncommon; i < nto; i++) { 73 strlcat(out, to_parts[i], outsz); 74 strlcat(out, "/", outsz); 75 } 76 strlcat(out, basename(to_base_buf), outsz); 77 } 78 #endif 79 80 static void 81 usage(void) 82 { 83 #if FEATURE_LN_RELATIVE 84 #if FEATURE_LN_NO_TARGET_DIR 85 eprintf( 86 "usage: %s [-f] [-L | -P | -r | -s] [-T] target [name]\n" 87 " %s [-f] [-L | -P | -r | -s] target ... dir\n", 88 argv0, 89 argv0 90 ); 91 #else 92 eprintf( 93 "usage: %s [-f] [-L | -P | -r | -s] target [name]\n" 94 " %s [-f] [-L | -P | -r | -s] target ... dir\n", 95 argv0, 96 argv0 97 ); 98 #endif 99 #else 100 #if FEATURE_LN_NO_TARGET_DIR 101 eprintf( 102 "usage: %s [-f] [-L | -P | -s] [-T] target [name]\n" 103 " %s [-f] [-L | -P | -s] target ... dir\n", 104 argv0, 105 argv0 106 ); 107 #else 108 eprintf( 109 "usage: %s [-f] [-L | -P | -s] target [name]\n" 110 " %s [-f] [-L | -P | -s] target ... dir\n", 111 argv0, 112 argv0 113 ); 114 #endif 115 #endif 116 } 117 118 // ?man ln: make links between files 119 // ?man arguments: target [name] 120 // ?man create hard or symbolic links between files 121 int 122 main(int argc, char *argv[]) 123 { 124 char *targetdir = ".", *target = NULL; 125 int ret = 0, sflag = 0, fflag = 0, dirfd = AT_FDCWD, hastarget = 0, flags = AT_SYMLINK_FOLLOW; 126 struct stat st, tst; 127 #if FEATURE_LN_RELATIVE 128 int rflag = 0; 129 char relbuf[PATH_MAX]; 130 #endif 131 int nflag = 0; 132 #if FEATURE_LN_NO_TARGET_DIR 133 int Tflag = 0; 134 #endif 135 136 ARGBEGIN 137 { 138 // ?man -f: force creation of links by removing existing destination files 139 case 'f': 140 fflag = 1; 141 break; 142 // ?man -L: specify option flag 143 case 'L': 144 flags |= AT_SYMLINK_FOLLOW; 145 break; 146 // ?man -P: specify option flag 147 case 'P': 148 flags &= ~AT_SYMLINK_FOLLOW; 149 break; 150 #if FEATURE_LN_RELATIVE 151 // ?man -r: with -s, store the target as a path relative to name's own directory instead of 152 // ?man copying it as given 153 case 'r': 154 rflag = 1; 155 sflag = 1; 156 break; 157 #endif 158 // ?man -s: make symbolic links instead of hard links 159 case 's': 160 sflag = 1; 161 break; 162 #if FEATURE_LN_NO_TARGET_DIR 163 // ?man -T: treat name as a regular file, never as a directory to place the link into, even if a 164 // ?man file of that name is currently a directory 165 case 'T': 166 Tflag = 1; 167 break; 168 #endif 169 // ?man -n: with two arguments, treat name as a file even when it is a symlink to a directory 170 case 'n': 171 nflag = 1; 172 break; 173 default: 174 usage(); 175 } 176 ARGEND 177 #if FEATURE_LN_NO_TARGET_DIR 178 if (nflag && argc == 2) 179 Tflag = 1; 180 #endif 181 182 if (!argc) 183 usage(); 184 185 if (argc > 1) { 186 #if FEATURE_LN_NO_TARGET_DIR 187 if (Tflag && argc != 2) 188 eprintf("%s: extra operand\n", argv[2]); 189 if (!Tflag && !stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) { 190 #else 191 if (!stat(argv[argc - 1], &st) && S_ISDIR(st.st_mode)) { 192 #endif 193 if ((dirfd = open(argv[argc - 1], O_RDONLY)) < 0) 194 eprintf("open %s:", argv[argc - 1]); 195 targetdir = argv[argc - 1]; 196 if (targetdir[strlen(targetdir) - 1] == '/') 197 targetdir[strlen(targetdir) - 1] = '\0'; 198 } else if (argc == 2) { 199 hastarget = 1; 200 target = argv[argc - 1]; 201 } else { 202 eprintf("%s: not a directory\n", argv[argc - 1]); 203 } 204 argv[argc - 1] = NULL; 205 argc--; 206 } 207 208 for (; *argv; argc--, argv++) { 209 if (!hastarget) 210 target = basename(*argv); 211 212 if (!sflag) { 213 if (stat(*argv, &st) < 0) { 214 weprintf("stat %s:", *argv); 215 ret = 1; 216 continue; 217 } else if (fstatat(dirfd, target, &tst, AT_SYMLINK_NOFOLLOW) < 0) { 218 if (errno != ENOENT) { 219 weprintf("fstatat %s %s:", targetdir, target); 220 ret = 1; 221 continue; 222 } 223 } else if (st.st_dev == tst.st_dev && st.st_ino == tst.st_ino) { 224 if (!fflag) { 225 weprintf( 226 "%s and %s/%s are the same " 227 "file\n", 228 *argv, 229 targetdir, 230 target 231 ); 232 ret = 1; 233 } 234 continue; 235 } 236 } 237 238 if (fflag && unlinkat(dirfd, target, 0) < 0 && errno != ENOENT) { 239 weprintf("unlinkat %s %s:", targetdir, target); 240 ret = 1; 241 continue; 242 } 243 244 #if FEATURE_LN_RELATIVE 245 if (rflag) { 246 /* targetdir is the real destination dir only when name defaulted from 247 * the basename of target. an explicit name may carry a dir prefix 248 * (ln -r target dir/name), which then names the destination */ 249 char dirbuf[PATH_MAX]; 250 const char *fromdir = targetdir; 251 252 if (hastarget) { 253 strlcpy(dirbuf, target, sizeof(dirbuf)); 254 fromdir = dirname(dirbuf); 255 } 256 relpath(fromdir, *argv, relbuf, sizeof(relbuf)); 257 } 258 #endif 259 260 if ((sflag ? 261 #if FEATURE_LN_RELATIVE 262 symlinkat(rflag ? relbuf : *argv, dirfd, target) 263 #else 264 symlinkat(*argv, dirfd, target) 265 #endif 266 : linkat(AT_FDCWD, *argv, dirfd, target, flags)) 267 < 0) { 268 weprintf("%s %s <- %s/%s:", sflag ? "symlinkat" : "linkat", *argv, targetdir, target); 269 ret = 1; 270 } 271 } 272 273 return ret; 274 }