Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Mini ln implementation for busybox |
| 3 | * |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame^] | 4 | * |
| 5 | * Copyright (C) 1999 by Lineo, inc. |
| 6 | * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | #include <stdio.h> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 26 | #include <dirent.h> |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 27 | #include <errno.h> |
| 28 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 29 | |
| 30 | static const char ln_usage[] = "ln [-s] [-f] original-name additional-name\n" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 31 | "\n" |
| 32 | "\tAdd a new name that refers to the same file as \"original-name\"\n" |
| 33 | "\n" |
| 34 | "\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n" |
| 35 | "\t-f:\tRemove existing destination files.\n"; |
| 36 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 37 | |
| 38 | static int symlinkFlag = FALSE; |
| 39 | static int removeoldFlag = FALSE; |
| 40 | static const char *destName; |
| 41 | |
| 42 | |
| 43 | extern int ln_main(int argc, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 44 | { |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 45 | int status; |
| 46 | char newdestName[NAME_MAX]; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 47 | |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 48 | if (argc < 3) { |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 49 | usage (ln_usage); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 50 | } |
| 51 | argc--; |
| 52 | argv++; |
| 53 | |
| 54 | /* Parse any options */ |
| 55 | while (**argv == '-') { |
| 56 | while (*++(*argv)) |
| 57 | switch (**argv) { |
| 58 | case 's': |
| 59 | symlinkFlag = TRUE; |
| 60 | break; |
| 61 | case 'f': |
| 62 | removeoldFlag = TRUE; |
| 63 | break; |
| 64 | default: |
Eric Andersen | b0e9a70 | 1999-10-18 22:28:26 +0000 | [diff] [blame] | 65 | usage (ln_usage); |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 66 | } |
| 67 | argc--; |
| 68 | argv++; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | destName = argv[argc - 1]; |
| 73 | |
| 74 | if ((argc > 3) && !(isDirectory(destName))) { |
| 75 | fprintf(stderr, "%s: not a directory\n", destName); |
| 76 | exit (FALSE); |
| 77 | } |
| 78 | |
| 79 | while (argc-- >= 2) { |
| 80 | strcpy(newdestName, destName); |
| 81 | strcat(newdestName, (*argv)+(strlen(*(++argv)))); |
| 82 | |
| 83 | if (removeoldFlag==TRUE ) { |
| 84 | status = ( unlink(newdestName) && errno != ENOENT ); |
| 85 | if ( status != 0 ) { |
| 86 | perror(newdestName); |
| 87 | exit( FALSE); |
| 88 | } |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 89 | } |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 90 | if ( symlinkFlag==TRUE) |
| 91 | status = symlink(*argv, newdestName); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 92 | else |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 93 | status = link(*argv, newdestName); |
| 94 | if ( status != 0 ) { |
| 95 | perror(newdestName); |
| 96 | exit( FALSE); |
| 97 | } |
| 98 | } |
| 99 | exit( TRUE); |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 100 | } |