Updates to usage, and made tar work.
[oweals/busybox.git] / coreutils / ln.c
1 /*
2  * Mini ln implementation for busybox
3  *
4  *
5  * Copyright (C) 1999 by Lineo, inc.
6  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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
24 #include "internal.h"
25 #include <stdio.h>
26 #include <dirent.h>
27 #include <errno.h>
28
29
30 static const char ln_usage[] = "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n"
31 "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n"
32 "Options:\n"
33 "\t-s\tmake symbolic links instead of hard links\n"
34 "\t-f\tremove existing destination files\n";
35
36
37 static int symlinkFlag = FALSE;
38 static int removeoldFlag = FALSE;
39
40
41 extern int ln_main(int argc, char **argv)
42 {
43     int status;
44     static char* linkName;
45
46     if (argc < 3) {
47         usage (ln_usage);
48     }
49     argc--;
50     argv++;
51
52     /* Parse any options */
53     while (**argv == '-') {
54         while (*++(*argv))
55             switch (**argv) {
56             case 's':
57                 symlinkFlag = TRUE;
58                 break;
59             case 'f':
60                 removeoldFlag = TRUE;
61                 break;
62             default:
63                 usage (ln_usage);
64             }
65         argc--;
66         argv++;
67     }
68
69
70     linkName = argv[argc - 1];
71
72     if ((argc > 3) && !(isDirectory(linkName))) {
73         fprintf(stderr, "%s: not a directory\n", linkName);
74         exit (FALSE);
75     }
76
77     while (argc-- >= 2) {
78         if (removeoldFlag==TRUE ) {
79             status = ( unlink(linkName) && errno != ENOENT );
80             if ( status != 0 ) {
81                 perror(linkName);
82                 exit( FALSE);
83             }
84         }
85         if ( symlinkFlag==TRUE)
86                 status = symlink(*argv, linkName);
87         else
88                 status = link(*argv, linkName);
89         if ( status != 0 ) {
90             perror(linkName);
91             exit( FALSE);
92         }
93     }
94     exit( TRUE);
95 }