Contents of /trunk/mkinitrd-magellan/busybox/coreutils/ln.c
Parent Directory | Revision Log
Revision 532 -
(show annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2305 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 8 months ago) by niro
File MIME type: text/plain
File size: 2305 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini ln implementation for busybox |
4 | * |
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 | * |
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
8 | */ |
9 | |
10 | /* BB_AUDIT SUSv3 compliant */ |
11 | /* BB_AUDIT GNU options missing: -d, -F, -i, and -v. */ |
12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ |
13 | |
14 | #include "busybox.h" |
15 | |
16 | #define LN_SYMLINK 1 |
17 | #define LN_FORCE 2 |
18 | #define LN_NODEREFERENCE 4 |
19 | #define LN_BACKUP 8 |
20 | #define LN_SUFFIX 16 |
21 | |
22 | int ln_main(int argc, char **argv) |
23 | { |
24 | int status = EXIT_SUCCESS; |
25 | int flag; |
26 | char *last; |
27 | char *src_name; |
28 | char *src; |
29 | char *suffix = "~"; |
30 | struct stat statbuf; |
31 | int (*link_func)(const char *, const char *); |
32 | |
33 | flag = getopt32(argc, argv, "sfnbS:", &suffix); |
34 | |
35 | if (argc == optind) { |
36 | bb_show_usage(); |
37 | } |
38 | |
39 | last = argv[argc - 1]; |
40 | argv += optind; |
41 | |
42 | if (argc == optind + 1) { |
43 | *--argv = last; |
44 | last = bb_get_last_path_component(xstrdup(last)); |
45 | } |
46 | |
47 | do { |
48 | src_name = NULL; |
49 | src = last; |
50 | |
51 | if (is_directory(src, |
52 | (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, |
53 | NULL)) { |
54 | src_name = xstrdup(*argv); |
55 | src = concat_path_file(src, bb_get_last_path_component(src_name)); |
56 | free(src_name); |
57 | src_name = src; |
58 | } |
59 | if (!(flag & LN_SYMLINK) && stat(*argv, &statbuf)) { |
60 | // coreutils: "ln dangling_symlink new_hardlink" works |
61 | if (lstat(*argv, &statbuf) || !S_ISLNK(statbuf.st_mode)) { |
62 | bb_perror_msg("%s", *argv); |
63 | status = EXIT_FAILURE; |
64 | free(src_name); |
65 | continue; |
66 | } |
67 | } |
68 | |
69 | if (flag & LN_BACKUP) { |
70 | char *backup; |
71 | backup = xasprintf("%s%s", src, suffix); |
72 | if (rename(src, backup) < 0 && errno != ENOENT) { |
73 | bb_perror_msg("%s", src); |
74 | status = EXIT_FAILURE; |
75 | free(backup); |
76 | continue; |
77 | } |
78 | free(backup); |
79 | /* |
80 | * When the source and dest are both hard links to the same |
81 | * inode, a rename may succeed even though nothing happened. |
82 | * Therefore, always unlink(). |
83 | */ |
84 | unlink(src); |
85 | } else if (flag & LN_FORCE) { |
86 | unlink(src); |
87 | } |
88 | |
89 | link_func = link; |
90 | if (flag & LN_SYMLINK) { |
91 | link_func = symlink; |
92 | } |
93 | |
94 | if (link_func(*argv, src) != 0) { |
95 | bb_perror_msg("%s", src); |
96 | status = EXIT_FAILURE; |
97 | } |
98 | |
99 | free(src_name); |
100 | |
101 | } while ((++argv)[1]); |
102 | |
103 | return status; |
104 | } |