Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini ln implementation for busybox |
| 4 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1999 by Lineo, inc. |
| 7 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | * General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 22 | * |
| 23 | */ |
| 24 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 25 | #include "internal.h" |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 26 | #define BB_DECLARE_EXTERN |
| 27 | #define bb_need_name_too_long |
| 28 | #define bb_need_not_a_directory |
| 29 | #include "messages.c" |
| 30 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 32 | #include <dirent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 33 | #include <errno.h> |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 34 | #include <sys/param.h> /* for PATH_MAX */ |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 35 | |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 36 | static const char ln_usage[] = |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 37 | "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n" |
| 38 | "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n" |
| 39 | "Options:\n" |
| 40 | "\t-s\tmake symbolic links instead of hard links\n" |
| 41 | |
| 42 | "\t-f\tremove existing destination files\n" |
| 43 | "\t-n\tno dereference symlinks - treat like normal file\n"; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 44 | |
| 45 | static int symlinkFlag = FALSE; |
| 46 | static int removeoldFlag = FALSE; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 47 | static int followLinks = TRUE; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 48 | |
| 49 | extern int ln_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 50 | { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 51 | char *linkName; |
| 52 | int linkIntoDirFlag; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 53 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 54 | if (argc < 3) { |
| 55 | usage(ln_usage); |
| 56 | } |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 57 | argc--; |
| 58 | argv++; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 59 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 60 | /* Parse any options */ |
| 61 | while (**argv == '-') { |
| 62 | while (*++(*argv)) |
| 63 | switch (**argv) { |
| 64 | case 's': |
| 65 | symlinkFlag = TRUE; |
| 66 | break; |
| 67 | case 'f': |
| 68 | removeoldFlag = TRUE; |
| 69 | break; |
| 70 | case 'n': |
| 71 | followLinks = FALSE; |
| 72 | break; |
| 73 | default: |
| 74 | usage(ln_usage); |
| 75 | } |
| 76 | argc--; |
| 77 | argv++; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | linkName = argv[argc - 1]; |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 81 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 82 | if (strlen(linkName) > PATH_MAX) { |
| 83 | fprintf(stderr, name_too_long, "ln"); |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 84 | exit FALSE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 85 | } |
Erik Andersen | fac10d7 | 2000-02-07 05:29:42 +0000 | [diff] [blame] | 86 | |
Erik Andersen | 9ffdaa6 | 2000-02-11 21:55:04 +0000 | [diff] [blame] | 87 | linkIntoDirFlag = isDirectory(linkName, TRUE, NULL); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 88 | |
| 89 | if ((argc > 3) && !linkIntoDirFlag) { |
| 90 | fprintf(stderr, not_a_directory, "ln", linkName); |
| 91 | exit FALSE; |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 92 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 93 | |
| 94 | while (argc-- >= 2) { |
| 95 | char srcName[PATH_MAX + 1]; |
| 96 | int nChars, status; |
| 97 | |
| 98 | if (strlen(*argv) > PATH_MAX) { |
| 99 | fprintf(stderr, name_too_long, "ln"); |
| 100 | exit FALSE; |
| 101 | } |
| 102 | |
| 103 | if (followLinks == FALSE) { |
| 104 | strcpy(srcName, *argv); |
| 105 | } else { |
| 106 | /* Warning! This can silently truncate if > PATH_MAX, but |
| 107 | I don't think that there can be one > PATH_MAX anyway. */ |
| 108 | nChars = readlink(*argv, srcName, PATH_MAX); |
| 109 | srcName[nChars] = '\0'; |
| 110 | } |
| 111 | |
| 112 | if (removeoldFlag == TRUE) { |
| 113 | status = (unlink(linkName) && errno != ENOENT); |
| 114 | if (status != 0) { |
| 115 | perror(linkName); |
| 116 | exit FALSE; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (symlinkFlag == TRUE) |
| 121 | status = symlink(*argv, linkName); |
| 122 | else |
| 123 | status = link(*argv, linkName); |
| 124 | if (status != 0) { |
| 125 | perror(linkName); |
| 126 | exit FALSE; |
| 127 | } |
| 128 | } |
| 129 | exit TRUE; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 130 | } |
Erik Andersen | 029011b | 2000-03-04 21:19:32 +0000 | [diff] [blame] | 131 | |
| 132 | /* |
| 133 | Local Variables: |
| 134 | c-file-style: "linux" |
| 135 | c-basic-offset: 4 |
| 136 | tab-width: 4 |
| 137 | End: |
| 138 | */ |