latest and greatest.
[oweals/busybox.git] / coreutils / ln.c
1 #include "internal.h"
2 #include <stdio.h>
3 #include <sys/stat.h>
4 #include <sys/param.h>
5 #include <errno.h>
6
7 const char      ln_usage[] = "ln [-s] [-f] original-name additional-name\n"
8 "\n"
9 "\tAdd a new name that refers to the same file as \"original-name\"\n"
10 "\n"
11 "\t-s:\tUse a \"symbolic\" link, instead of a \"hard\" link.\n"
12 "\t-f:\tRemove existing destination files.\n";
13
14 int
15 ln_fn(const struct FileInfo * i)
16 {
17         int                             status = 0;
18         char                    d[PATH_MAX];
19         const char *    destination = i->destination;
20
21         if ( !i->makeSymbolicLink && (i->stat.st_mode & S_IFMT) == S_IFDIR ) {
22                 fprintf(stderr, "Please use \"ln -s\" to link directories.\n");
23                 return 1;
24         }
25
26         /*
27          * If the destination is a directory, create a file within it.
28          */
29         if ( is_a_directory(i->destination) ) {
30                         destination = join_paths(
31                          d
32                         ,i->destination
33                         ,&i->source[i->directoryLength]);
34         }
35
36         if ( i->force )
37                 status = ( unlink(destination) && errno != ENOENT );
38
39         if ( status == 0 ) {
40                 if ( i->makeSymbolicLink )
41                         status = symlink(i->source, destination);
42                 else
43                         status = link(i->source, destination);
44         }
45
46         if ( status != 0 ) {
47                 name_and_error(destination);
48                 return 1;
49         }
50         else
51                 return 0;
52 }