diff options
author | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
---|---|---|
committer | Erik Andersen <andersen@codepoet.org> | 2000-02-08 19:58:47 +0000 |
commit | e49d5ecbbe51718fa925b6890a735e5937cc2aa2 (patch) | |
tree | c90bda10731ad9333ce3b404f993354c9fc104b8 /ln.c | |
parent | c0bf817bbc5c7867fbe8fb76d5c39f8ee802692f (diff) | |
download | busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.gz busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.tar.bz2 busybox-w32-e49d5ecbbe51718fa925b6890a735e5937cc2aa2.zip |
Some formatting updates (ran the code through indent)
-Erik
Diffstat (limited to 'ln.c')
-rw-r--r-- | ln.c | 150 |
1 files changed, 76 insertions, 74 deletions
@@ -1,3 +1,4 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
1 | /* | 2 | /* |
2 | * Mini ln implementation for busybox | 3 | * Mini ln implementation for busybox |
3 | * | 4 | * |
@@ -30,15 +31,16 @@ | |||
30 | #include <stdio.h> | 31 | #include <stdio.h> |
31 | #include <dirent.h> | 32 | #include <dirent.h> |
32 | #include <errno.h> | 33 | #include <errno.h> |
33 | #include <sys/param.h> /* for PATH_MAX */ | 34 | #include <sys/param.h> /* for PATH_MAX */ |
34 | 35 | ||
35 | static const char ln_usage[] = | 36 | static const char ln_usage[] = |
36 | "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n" | 37 | "ln [OPTION] TARGET... LINK_NAME|DIRECTORY\n\n" |
37 | "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n" | 38 | "Create a link named LINK_NAME or DIRECTORY to the specified TARGET\n\n" |
38 | "Options:\n" | 39 | "Options:\n" |
39 | "\t-s\tmake symbolic links instead of hard links\n" | 40 | "\t-s\tmake symbolic links instead of hard links\n" |
40 | "\t-f\tremove existing destination files\n" | 41 | |
41 | "\t-n\tno dereference symlinks - treat like normal file\n"; | 42 | "\t-f\tremove existing destination files\n" |
43 | "\t-n\tno dereference symlinks - treat like normal file\n"; | ||
42 | 44 | ||
43 | static int symlinkFlag = FALSE; | 45 | static int symlinkFlag = FALSE; |
44 | static int removeoldFlag = FALSE; | 46 | static int removeoldFlag = FALSE; |
@@ -46,83 +48,83 @@ static int followLinks = TRUE; | |||
46 | 48 | ||
47 | extern int ln_main(int argc, char **argv) | 49 | extern int ln_main(int argc, char **argv) |
48 | { | 50 | { |
49 | char *linkName; | 51 | char *linkName; |
50 | int linkIntoDirFlag; | 52 | int linkIntoDirFlag; |
51 | 53 | ||
52 | if (argc < 3) { | 54 | if (argc < 3) { |
53 | usage (ln_usage); | 55 | usage(ln_usage); |
54 | } | 56 | } |
55 | argc--; | ||
56 | argv++; | ||
57 | |||
58 | /* Parse any options */ | ||
59 | while (**argv == '-') { | ||
60 | while (*++(*argv)) | ||
61 | switch (**argv) { | ||
62 | case 's': | ||
63 | symlinkFlag = TRUE; | ||
64 | break; | ||
65 | case 'f': | ||
66 | removeoldFlag = TRUE; | ||
67 | break; | ||
68 | case 'n': | ||
69 | followLinks = FALSE; | ||
70 | break; | ||
71 | default: | ||
72 | usage (ln_usage); | ||
73 | } | ||
74 | argc--; | 57 | argc--; |
75 | argv++; | 58 | argv++; |
76 | } | ||
77 | |||
78 | linkName = argv[argc - 1]; | ||
79 | |||
80 | if (strlen(linkName) > PATH_MAX) { | ||
81 | fprintf(stderr, name_too_long, "ln"); | ||
82 | exit FALSE; | ||
83 | } | ||
84 | 59 | ||
85 | linkIntoDirFlag = isDirectory(linkName, TRUE); | 60 | /* Parse any options */ |
86 | 61 | while (**argv == '-') { | |
87 | if ((argc > 3) && !linkIntoDirFlag) { | 62 | while (*++(*argv)) |
88 | fprintf(stderr, not_a_directory, "ln", linkName); | 63 | switch (**argv) { |
89 | exit FALSE; | 64 | case 's': |
90 | } | 65 | symlinkFlag = TRUE; |
66 | break; | ||
67 | case 'f': | ||
68 | removeoldFlag = TRUE; | ||
69 | break; | ||
70 | case 'n': | ||
71 | followLinks = FALSE; | ||
72 | break; | ||
73 | default: | ||
74 | usage(ln_usage); | ||
75 | } | ||
76 | argc--; | ||
77 | argv++; | ||
78 | } | ||
91 | 79 | ||
92 | while (argc-- >= 2) { | 80 | linkName = argv[argc - 1]; |
93 | char srcName[PATH_MAX + 1]; | ||
94 | int nChars, status; | ||
95 | 81 | ||
96 | if (strlen(*argv) > PATH_MAX) { | 82 | if (strlen(linkName) > PATH_MAX) { |
97 | fprintf(stderr, name_too_long, "ln"); | 83 | fprintf(stderr, name_too_long, "ln"); |
98 | exit FALSE; | 84 | exit FALSE; |
99 | } | 85 | } |
100 | 86 | ||
101 | if (followLinks == FALSE) { | 87 | linkIntoDirFlag = isDirectory(linkName, TRUE); |
102 | strcpy(srcName, *argv); | ||
103 | } else { | ||
104 | /* Warning! This can silently truncate if > PATH_MAX, but | ||
105 | I don't think that there can be one > PATH_MAX anyway. */ | ||
106 | nChars = readlink(*argv, srcName, PATH_MAX); | ||
107 | srcName[nChars] = '\0'; | ||
108 | } | ||
109 | 88 | ||
110 | if (removeoldFlag == TRUE) { | 89 | if ((argc > 3) && !linkIntoDirFlag) { |
111 | status = ( unlink(linkName) && errno != ENOENT ); | 90 | fprintf(stderr, not_a_directory, "ln", linkName); |
112 | if (status != 0) { | ||
113 | perror(linkName); | ||
114 | exit FALSE; | 91 | exit FALSE; |
115 | } | ||
116 | } | 92 | } |
117 | 93 | ||
118 | if (symlinkFlag == TRUE) | 94 | while (argc-- >= 2) { |
119 | status = symlink(*argv, linkName); | 95 | char srcName[PATH_MAX + 1]; |
120 | else | 96 | int nChars, status; |
121 | status = link(*argv, linkName); | 97 | |
122 | if (status != 0) { | 98 | if (strlen(*argv) > PATH_MAX) { |
123 | perror(linkName); | 99 | fprintf(stderr, name_too_long, "ln"); |
124 | exit FALSE; | 100 | exit FALSE; |
101 | } | ||
102 | |||
103 | if (followLinks == FALSE) { | ||
104 | strcpy(srcName, *argv); | ||
105 | } else { | ||
106 | /* Warning! This can silently truncate if > PATH_MAX, but | ||
107 | I don't think that there can be one > PATH_MAX anyway. */ | ||
108 | nChars = readlink(*argv, srcName, PATH_MAX); | ||
109 | srcName[nChars] = '\0'; | ||
110 | } | ||
111 | |||
112 | if (removeoldFlag == TRUE) { | ||
113 | status = (unlink(linkName) && errno != ENOENT); | ||
114 | if (status != 0) { | ||
115 | perror(linkName); | ||
116 | exit FALSE; | ||
117 | } | ||
118 | } | ||
119 | |||
120 | if (symlinkFlag == TRUE) | ||
121 | status = symlink(*argv, linkName); | ||
122 | else | ||
123 | status = link(*argv, linkName); | ||
124 | if (status != 0) { | ||
125 | perror(linkName); | ||
126 | exit FALSE; | ||
127 | } | ||
125 | } | 128 | } |
126 | } | 129 | exit TRUE; |
127 | exit TRUE; | ||
128 | } | 130 | } |