diff options
author | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
---|---|---|
committer | Manuel Novoa III <mjn3@codepoet.org> | 2003-03-19 09:13:01 +0000 |
commit | cad5364599eb5062d59e0c397ed638ddd61a8d5d (patch) | |
tree | a318d0f03aa076c74b576ea45dc543a5669e8e91 /coreutils/ln.c | |
parent | e01f9662a5bd5d91be4f6b3941b57fff73cd5af1 (diff) | |
download | busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.gz busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.tar.bz2 busybox-w32-cad5364599eb5062d59e0c397ed638ddd61a8d5d.zip |
Major coreutils update.
Diffstat (limited to 'coreutils/ln.c')
-rw-r--r-- | coreutils/ln.c | 163 |
1 files changed, 68 insertions, 95 deletions
diff --git a/coreutils/ln.c b/coreutils/ln.c index 427ffcc6e..2edece104 100644 --- a/coreutils/ln.c +++ b/coreutils/ln.c | |||
@@ -21,113 +21,86 @@ | |||
21 | * | 21 | * |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include <stdio.h> | 24 | /* BB_AUDIT SUSv3 compliant */ |
25 | #include <dirent.h> | 25 | /* BB_AUDIT GNU options missing: -b, -d, -F, -i, -S, and -v. */ |
26 | #include <string.h> | 26 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/ln.html */ |
27 | |||
28 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) | ||
29 | * | ||
30 | * Fixed bug involving -n option. Essentially, -n was always in effect. | ||
31 | */ | ||
32 | |||
27 | #include <stdlib.h> | 33 | #include <stdlib.h> |
28 | #include <errno.h> | ||
29 | #include <unistd.h> | 34 | #include <unistd.h> |
30 | #include "busybox.h" | 35 | #include "busybox.h" |
31 | 36 | ||
37 | #define LN_SYMLINK 1 | ||
38 | #define LN_FORCE 2 | ||
39 | #define LN_NODEREFERENCE 4 | ||
32 | 40 | ||
33 | static const int LN_SYMLINK = 1; | 41 | extern int ln_main(int argc, char **argv) |
34 | static const int LN_FORCE = 2; | ||
35 | static const int LN_NODEREFERENCE = 4; | ||
36 | |||
37 | /* | ||
38 | * linkDestName is where the link points to, | ||
39 | * linkSrcName is the name of the link to be created. | ||
40 | */ | ||
41 | static int fs_link(const char *link_destname, const char *link_srcname, | ||
42 | const int flag) | ||
43 | { | 42 | { |
44 | int status; | 43 | int status = EXIT_SUCCESS; |
45 | int src_is_dir; | 44 | int flag; |
46 | char *src_name = 0; | 45 | char *last; |
46 | char *src_name; | ||
47 | const char *src; | 47 | const char *src; |
48 | int (*link_func)(const char *, const char *); | ||
48 | 49 | ||
49 | if (link_destname==NULL) | 50 | flag = bb_getopt_ulflags(argc, argv, "sfn"); |
50 | return(FALSE); | 51 | |
51 | 52 | if (argc == optind) { | |
52 | if (link_srcname==NULL) | 53 | bb_show_usage(); |
53 | src = link_destname; | ||
54 | else | ||
55 | src = link_srcname; | ||
56 | |||
57 | if (flag&LN_NODEREFERENCE) | ||
58 | src_is_dir = is_directory(src, TRUE, NULL); | ||
59 | else | ||
60 | src_is_dir = is_directory(src, FALSE, NULL); | ||
61 | |||
62 | if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) { | ||
63 | char* srcdir_name; | ||
64 | |||
65 | srcdir_name = xstrdup(link_destname); | ||
66 | src_name = concat_path_file(src, get_last_path_component(srcdir_name)); | ||
67 | src = src_name; | ||
68 | free(srcdir_name); | ||
69 | } | 54 | } |
70 | 55 | ||
71 | if (flag&LN_FORCE) | 56 | last = argv[argc - 1]; |
72 | unlink(src); | 57 | argv += optind; |
73 | 58 | ||
74 | if (flag&LN_SYMLINK) | 59 | if (argc == optind + 1) { |
75 | status = symlink(link_destname, src); | 60 | *--argv = last; |
76 | else | 61 | last = bb_get_last_path_component(bb_xstrdup(last)); |
77 | status = link(link_destname, src); | ||
78 | |||
79 | if (status != 0) { | ||
80 | perror_msg(src); | ||
81 | status = FALSE; | ||
82 | } else { | ||
83 | status = TRUE; | ||
84 | } | 62 | } |
85 | free(src_name); | ||
86 | return status; | ||
87 | } | ||
88 | 63 | ||
89 | extern int ln_main(int argc, char **argv) | 64 | do { |
90 | { | 65 | src_name = 0; |
91 | int status = EXIT_SUCCESS; | 66 | src = last; |
92 | int flag = 0; | 67 | |
93 | int opt; | 68 | if (is_directory(src, |
94 | 69 | (flag & LN_NODEREFERENCE) ^ LN_NODEREFERENCE, | |
95 | /* Parse any options */ | 70 | NULL)) { |
96 | while ((opt=getopt(argc, argv, "sfn")) != -1) { | 71 | src_name = bb_xstrdup(*argv); |
97 | switch(opt) { | 72 | src = concat_path_file(src, bb_get_last_path_component(src_name)); |
98 | case 's': | 73 | free(src_name); |
99 | flag |= LN_SYMLINK; | 74 | src_name = (char *)src; |
100 | break; | ||
101 | case 'f': | ||
102 | flag |= LN_FORCE; | ||
103 | break; | ||
104 | case 'n': | ||
105 | flag |= LN_NODEREFERENCE; | ||
106 | break; | ||
107 | default: | ||
108 | show_usage(); | ||
109 | } | 75 | } |
110 | } | 76 | |
111 | if (optind > (argc-1)) { | 77 | if (flag & LN_FORCE) { |
112 | show_usage(); | 78 | unlink(src); |
113 | } | 79 | } |
114 | if (optind == (argc-1)) { | 80 | |
115 | if (fs_link(argv[optind], | 81 | link_func = link; |
116 | get_last_path_component(argv[optind]), flag)==FALSE) | 82 | if (flag & LN_SYMLINK) { |
117 | status = EXIT_FAILURE; | 83 | link_func = symlink; |
118 | } | 84 | } |
119 | while(optind<(argc-1)) { | 85 | |
120 | if (fs_link(argv[optind], argv[argc-1], flag)==FALSE) | 86 | if (link_func(*argv, src) != 0) { |
121 | status = EXIT_FAILURE; | 87 | bb_perror_msg(src); |
122 | optind++; | 88 | status = EXIT_FAILURE;; |
123 | } | 89 | } |
90 | |||
91 | free(src_name); | ||
92 | |||
93 | } while ((++argv)[1]); | ||
94 | |||
124 | return status; | 95 | return status; |
125 | } | 96 | } |
126 | 97 | ||
127 | /* | 98 | |
128 | Local Variables: | 99 | |
129 | c-file-style: "linux" | 100 | |
130 | c-basic-offset: 4 | 101 | |
131 | tab-width: 4 | 102 | |
132 | End: | 103 | |
133 | */ | 104 | |
105 | |||
106 | |||