Rev 3425 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3425 | Rev 4377 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * Redistributions of source code must retain the above copyright notice, this |
7 | * Redistributions of source code must retain the above copyright notice, this |
8 | * list of conditions and the following disclaimer. |
8 | * list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * this list of conditions and the following disclaimer in the documentation |
11 | * this list of conditions and the following disclaimer in the documentation |
12 | * and/or other materials provided with the distribution. |
12 | * and/or other materials provided with the distribution. |
13 | * |
13 | * |
14 | * Neither the name of the original program's authors nor the names of its |
14 | * Neither the name of the original program's authors nor the names of its |
15 | * contributors may be used to endorse or promote products derived from this |
15 | * contributors may be used to endorse or promote products derived from this |
16 | * software without specific prior written permission. |
16 | * software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | /* NOTE: |
31 | /* NOTE: |
32 | * This is a bit of an ugly hack, working around the absence of fstat / etc. |
32 | * This is a bit of an ugly hack, working around the absence of fstat / etc. |
33 | * As more stuff is completed and exposed in libc, this will improve */ |
33 | * As more stuff is completed and exposed in libc, this will improve */ |
34 | 34 | ||
35 | #include <stdio.h> |
35 | #include <stdio.h> |
36 | #include <stdlib.h> |
36 | #include <stdlib.h> |
37 | #include <unistd.h> |
37 | #include <unistd.h> |
38 | #include <dirent.h> |
38 | #include <dirent.h> |
39 | #include <fcntl.h> |
39 | #include <fcntl.h> |
40 | #include <sys/types.h> |
40 | #include <sys/types.h> |
41 | #include <sys/stat.h> |
41 | #include <sys/stat.h> |
42 | #include <string.h> |
42 | #include <string.h> |
43 | 43 | ||
44 | #include "errors.h" |
44 | #include "errors.h" |
45 | #include "config.h" |
45 | #include "config.h" |
46 | #include "util.h" |
46 | #include "util.h" |
47 | #include "entry.h" |
47 | #include "entry.h" |
48 | #include "ls.h" |
48 | #include "ls.h" |
49 | #include "cmds.h" |
49 | #include "cmds.h" |
50 | 50 | ||
51 | static char *cmdname = "ls"; |
51 | static char *cmdname = "ls"; |
52 | 52 | ||
- | 53 | static inline off_t flen(const char *f) |
|
- | 54 | { |
|
- | 55 | int fd; |
|
- | 56 | off_t size; |
|
- | 57 | ||
- | 58 | fd = open(f, O_RDONLY); |
|
- | 59 | if (fd == -1) |
|
- | 60 | return 0; |
|
- | 61 | ||
- | 62 | size = lseek(fd, 0, SEEK_END); |
|
- | 63 | close(fd); |
|
- | 64 | ||
- | 65 | if (size < 0) |
|
- | 66 | size = 0; |
|
- | 67 | ||
- | 68 | return size; |
|
- | 69 | } |
|
- | 70 | ||
53 | static unsigned int ls_scope(const char *path) |
71 | static unsigned int ls_scope(const char *path) |
54 | { |
72 | { |
55 | int fd; |
73 | int fd; |
56 | DIR *dirp; |
74 | DIR *dirp; |
57 | 75 | ||
58 | dirp = opendir(path); |
76 | dirp = opendir(path); |
59 | if (dirp) { |
77 | if (dirp) { |
60 | closedir(dirp); |
78 | closedir(dirp); |
61 | return LS_DIR; |
79 | return LS_DIR; |
62 | } |
80 | } |
63 | 81 | ||
64 | fd = open(path, O_RDONLY); |
82 | fd = open(path, O_RDONLY); |
65 | if (fd > 0) { |
83 | if (fd > 0) { |
66 | close(fd); |
84 | close(fd); |
67 | return LS_FILE; |
85 | return LS_FILE; |
68 | } |
86 | } |
69 | 87 | ||
70 | return LS_BOGUS; |
88 | return LS_BOGUS; |
71 | } |
89 | } |
72 | 90 | ||
73 | static void ls_scan_dir(const char *d, DIR *dirp) |
91 | static void ls_scan_dir(const char *d, DIR *dirp) |
74 | { |
92 | { |
75 | struct dirent *dp; |
93 | struct dirent *dp; |
76 | unsigned int scope; |
94 | unsigned int scope; |
77 | char *buff; |
95 | char *buff; |
78 | 96 | ||
79 | if (! dirp) |
97 | if (! dirp) |
80 | return; |
98 | return; |
81 | 99 | ||
82 | buff = (char *)malloc(PATH_MAX); |
100 | buff = (char *)malloc(PATH_MAX); |
83 | if (NULL == buff) { |
101 | if (NULL == buff) { |
84 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d); |
102 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d); |
85 | return; |
103 | return; |
86 | } |
104 | } |
87 | 105 | ||
88 | while ((dp = readdir(dirp))) { |
106 | while ((dp = readdir(dirp))) { |
89 | memset(buff, 0, sizeof(buff)); |
107 | memset(buff, 0, sizeof(buff)); |
90 | /* Don't worry if inserting a double slash, this will be fixed by |
108 | /* Don't worry if inserting a double slash, this will be fixed by |
91 | * absolutize() later with subsequent calls to open() or readdir() */ |
109 | * absolutize() later with subsequent calls to open() or readdir() */ |
92 | snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name); |
110 | snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name); |
93 | scope = ls_scope(buff); |
111 | scope = ls_scope(buff); |
94 | switch (scope) { |
112 | switch (scope) { |
95 | case LS_DIR: |
113 | case LS_DIR: |
96 | ls_print_dir(dp->d_name); |
114 | ls_print_dir(dp->d_name); |
97 | break; |
115 | break; |
98 | case LS_FILE: |
116 | case LS_FILE: |
99 | ls_print_file(dp->d_name); |
117 | ls_print_file(dp->d_name, buff); |
100 | break; |
118 | break; |
101 | case LS_BOGUS: |
119 | case LS_BOGUS: |
102 | /* Odd chance it was deleted from the time readdir() found |
120 | /* Odd chance it was deleted from the time readdir() found |
103 | * it and the time that it was scoped */ |
121 | * it and the time that it was scoped */ |
104 | printf("ls: skipping bogus node %s\n", dp->d_name); |
122 | printf("ls: skipping bogus node %s\n", dp->d_name); |
105 | break; |
123 | break; |
106 | } |
124 | } |
107 | } |
125 | } |
108 | 126 | ||
109 | free(buff); |
127 | free(buff); |
110 | 128 | ||
111 | return; |
129 | return; |
112 | } |
130 | } |
113 | 131 | ||
114 | /* ls_print_* currently does nothing more than print the entry. |
132 | /* ls_print_* currently does nothing more than print the entry. |
115 | * in the future, we will likely pass the absolute path, and |
133 | * in the future, we will likely pass the absolute path, and |
116 | * some sort of ls_options structure that controls how each |
134 | * some sort of ls_options structure that controls how each |
117 | * entry is printed and what is printed about it. |
135 | * entry is printed and what is printed about it. |
118 | * |
136 | * |
119 | * Now we just print basic DOS style lists */ |
137 | * Now we just print basic DOS style lists */ |
120 | 138 | ||
121 | static void ls_print_dir(const char *d) |
139 | static void ls_print_dir(const char *d) |
122 | { |
140 | { |
123 | printf("%-40s\t<DIR>\n", d); |
141 | printf("%-40s\t<dir>\n", d); |
124 | 142 | ||
125 | return; |
143 | return; |
126 | } |
144 | } |
127 | 145 | ||
128 | static void ls_print_file(const char *f) |
146 | static void ls_print_file(const char *name, const char *pathname) |
129 | { |
147 | { |
130 | printf("%-40s\n", f); |
148 | printf("%-40s\t%llu\n", name, (long long) flen(pathname)); |
131 | 149 | ||
132 | return; |
150 | return; |
133 | } |
151 | } |
134 | 152 | ||
135 | void help_cmd_ls(unsigned int level) |
153 | void help_cmd_ls(unsigned int level) |
136 | { |
154 | { |
137 | if (level == HELP_SHORT) { |
155 | if (level == HELP_SHORT) { |
138 | printf("`%s' lists files and directories.\n", cmdname); |
156 | printf("`%s' lists files and directories.\n", cmdname); |
139 | } else { |
157 | } else { |
140 | help_cmd_ls(HELP_SHORT); |
158 | help_cmd_ls(HELP_SHORT); |
141 | printf(" `%s' [path], if no path is given the current " |
159 | printf(" `%s' [path], if no path is given the current " |
142 | "working directory is used.\n", cmdname); |
160 | "working directory is used.\n", cmdname); |
143 | } |
161 | } |
144 | 162 | ||
145 | return; |
163 | return; |
146 | } |
164 | } |
147 | 165 | ||
148 | int cmd_ls(char **argv) |
166 | int cmd_ls(char **argv) |
149 | { |
167 | { |
150 | unsigned int argc; |
168 | unsigned int argc; |
151 | unsigned int scope; |
169 | unsigned int scope; |
152 | char *buff; |
170 | char *buff; |
153 | DIR *dirp; |
171 | DIR *dirp; |
154 | 172 | ||
155 | argc = cli_count_args(argv); |
173 | argc = cli_count_args(argv); |
156 | 174 | ||
157 | buff = (char *) malloc(PATH_MAX); |
175 | buff = (char *) malloc(PATH_MAX); |
158 | if (NULL == buff) { |
176 | if (NULL == buff) { |
159 | cli_error(CL_ENOMEM, "%s: ", cmdname); |
177 | cli_error(CL_ENOMEM, "%s: ", cmdname); |
160 | return CMD_FAILURE; |
178 | return CMD_FAILURE; |
161 | } |
179 | } |
162 | memset(buff, 0, sizeof(buff)); |
180 | memset(buff, 0, sizeof(buff)); |
163 | 181 | ||
164 | if (argc == 1) |
182 | if (argc == 1) |
165 | getcwd(buff, PATH_MAX); |
183 | getcwd(buff, PATH_MAX); |
166 | else |
184 | else |
167 | strncpy(buff, argv[1], PATH_MAX); |
185 | str_cpy(buff, PATH_MAX, argv[1]); |
168 | 186 | ||
169 | scope = ls_scope(buff); |
187 | scope = ls_scope(buff); |
170 | 188 | ||
171 | switch (scope) { |
189 | switch (scope) { |
172 | case LS_BOGUS: |
190 | case LS_BOGUS: |
173 | cli_error(CL_ENOENT, buff); |
191 | cli_error(CL_ENOENT, buff); |
174 | free(buff); |
192 | free(buff); |
175 | return CMD_FAILURE; |
193 | return CMD_FAILURE; |
176 | case LS_FILE: |
194 | case LS_FILE: |
177 | ls_print_file(buff); |
195 | ls_print_file(buff, buff); |
178 | break; |
196 | break; |
179 | case LS_DIR: |
197 | case LS_DIR: |
180 | dirp = opendir(buff); |
198 | dirp = opendir(buff); |
181 | if (! dirp) { |
199 | if (! dirp) { |
182 | /* May have been deleted between scoping it and opening it */ |
200 | /* May have been deleted between scoping it and opening it */ |
183 | cli_error(CL_EFAIL, "Could not stat %s", buff); |
201 | cli_error(CL_EFAIL, "Could not stat %s", buff); |
184 | free(buff); |
202 | free(buff); |
185 | return CMD_FAILURE; |
203 | return CMD_FAILURE; |
186 | } |
204 | } |
187 | ls_scan_dir(buff, dirp); |
205 | ls_scan_dir(buff, dirp); |
188 | closedir(dirp); |
206 | closedir(dirp); |
189 | break; |
207 | break; |
190 | } |
208 | } |
191 | 209 | ||
192 | free(buff); |
210 | free(buff); |
193 | 211 | ||
194 | return CMD_SUCCESS; |
212 | return CMD_SUCCESS; |
195 | } |
213 | } |
196 | 214 | ||
197 | 215 |