1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com>
|
---|
2 | * All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions are met:
|
---|
6 | *
|
---|
7 | * Redistributions of source code must retain the above copyright notice, this
|
---|
8 | * list of conditions and the following disclaimer.
|
---|
9 | *
|
---|
10 | * Redistributions in binary form must reproduce the above copyright notice,
|
---|
11 | * this list of conditions and the following disclaimer in the documentation
|
---|
12 | * and/or other materials provided with the distribution.
|
---|
13 | *
|
---|
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
|
---|
16 | * software without specific prior written permission.
|
---|
17 | *
|
---|
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
|
---|
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
---|
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
---|
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
---|
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
|
---|
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
|
---|
28 | * POSSIBILITY OF SUCH DAMAGE.
|
---|
29 | */
|
---|
30 |
|
---|
31 | /* NOTE:
|
---|
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 */
|
---|
34 |
|
---|
35 | #include <stdio.h>
|
---|
36 | #include <stdlib.h>
|
---|
37 | #include <unistd.h>
|
---|
38 | #include <dirent.h>
|
---|
39 | #include <fcntl.h>
|
---|
40 | #include <sys/types.h>
|
---|
41 | #include <sys/stat.h>
|
---|
42 | #include <str.h>
|
---|
43 |
|
---|
44 | #include "errors.h"
|
---|
45 | #include "config.h"
|
---|
46 | #include "util.h"
|
---|
47 | #include "entry.h"
|
---|
48 | #include "ls.h"
|
---|
49 | #include "cmds.h"
|
---|
50 |
|
---|
51 | static const char *cmdname = "ls";
|
---|
52 |
|
---|
53 | /** Sort array of files/directories
|
---|
54 | *
|
---|
55 | * Sort an array containing files and directories,
|
---|
56 | * in alphabetical order, with files first.
|
---|
57 | *
|
---|
58 | * @param d Current directory.
|
---|
59 | * @param tab Array of file/directories to sort.
|
---|
60 | * @param nbdirs Number of elements.
|
---|
61 | *
|
---|
62 | */
|
---|
63 | static void ls_sort_dir(const char *d, char ** tab, int nbdirs)
|
---|
64 | {
|
---|
65 | int i = 0;
|
---|
66 | int j = 0;
|
---|
67 | int min = 0;
|
---|
68 | int rc;
|
---|
69 | char * buff1 = NULL;
|
---|
70 | char * buff2 = NULL;
|
---|
71 | char * tmp = NULL;
|
---|
72 | struct stat s1;
|
---|
73 | struct stat s2;
|
---|
74 |
|
---|
75 | buff1 = (char *)malloc(PATH_MAX);
|
---|
76 | if (NULL == buff1) {
|
---|
77 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
78 | return;
|
---|
79 | }
|
---|
80 |
|
---|
81 | buff2 = (char *)malloc(PATH_MAX);
|
---|
82 | if (NULL == buff2) {
|
---|
83 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | for(i=0;i<nbdirs;i++) {
|
---|
88 | min = i;
|
---|
89 |
|
---|
90 | for(j=i;j<nbdirs;j++) {
|
---|
91 | memset(buff1, 0, sizeof(buff1));
|
---|
92 | memset(buff2, 0, sizeof(buff2));
|
---|
93 | snprintf(buff1, PATH_MAX - 1, "%s/%s", d, tab[min]);
|
---|
94 | snprintf(buff2, PATH_MAX - 1, "%s/%s", d, tab[j]);
|
---|
95 |
|
---|
96 | rc = stat(buff1, &s1);
|
---|
97 | if (rc != 0) {
|
---|
98 | printf("ls: skipping bogus node %s\n", buff1);
|
---|
99 | printf("rc=%d\n", rc);
|
---|
100 | return;
|
---|
101 | }
|
---|
102 |
|
---|
103 | rc = stat(buff2, &s2);
|
---|
104 | if (rc != 0) {
|
---|
105 | printf("ls: skipping bogus node %s\n", buff2);
|
---|
106 | printf("rc=%d\n", rc);
|
---|
107 | return;
|
---|
108 | }
|
---|
109 |
|
---|
110 | if ((s2.is_file && s1.is_directory)
|
---|
111 | || (((s2.is_file && s1.is_file)
|
---|
112 | || (s2.is_directory && s1.is_directory))
|
---|
113 | && str_cmp(tab[min], tab[j]) > 0))
|
---|
114 | min = j;
|
---|
115 | }
|
---|
116 |
|
---|
117 | tmp = tab[i];
|
---|
118 | tab[i] = tab[min];
|
---|
119 | tab[min] = tmp;
|
---|
120 | }
|
---|
121 |
|
---|
122 | free(buff1);
|
---|
123 | free(buff2);
|
---|
124 |
|
---|
125 | return;
|
---|
126 | }
|
---|
127 |
|
---|
128 | /** Scan a directory.
|
---|
129 | *
|
---|
130 | * Scan the content of a directory and print it.
|
---|
131 | *
|
---|
132 | * @param d Name of the directory.
|
---|
133 | * @param dirp Directory stream.
|
---|
134 | *
|
---|
135 | */
|
---|
136 | static void ls_scan_dir(const char *d, DIR *dirp)
|
---|
137 | {
|
---|
138 | int alloc_blocks = 20;
|
---|
139 | int i = 0;
|
---|
140 | int nbdirs = 0;
|
---|
141 | char * buff = NULL;
|
---|
142 | char ** tmp = NULL;
|
---|
143 | char ** tosort = NULL;
|
---|
144 | struct dirent * dp = NULL;
|
---|
145 |
|
---|
146 | if (! dirp)
|
---|
147 | return;
|
---|
148 |
|
---|
149 | buff = (char *)malloc(PATH_MAX);
|
---|
150 | if (NULL == buff) {
|
---|
151 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
152 | return;
|
---|
153 | }
|
---|
154 |
|
---|
155 | tosort = (char **)malloc(alloc_blocks*sizeof(char *));
|
---|
156 | if (NULL == tosort) {
|
---|
157 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
158 | return;
|
---|
159 | }
|
---|
160 | memset(tosort, 0, sizeof(tosort));
|
---|
161 |
|
---|
162 | while ((dp = readdir(dirp))) {
|
---|
163 | nbdirs++;
|
---|
164 |
|
---|
165 | if (nbdirs > alloc_blocks) {
|
---|
166 | alloc_blocks += alloc_blocks;
|
---|
167 |
|
---|
168 | tmp = (char **)realloc(tosort, (alloc_blocks)*sizeof(char *));
|
---|
169 | if (NULL == tmp) {
|
---|
170 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
171 | return;
|
---|
172 | }
|
---|
173 |
|
---|
174 | tosort = tmp;
|
---|
175 | }
|
---|
176 |
|
---|
177 | tosort[nbdirs-1] = (char *)malloc(str_length(dp->d_name)+1);
|
---|
178 | if (NULL == tosort[nbdirs-1]) {
|
---|
179 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d);
|
---|
180 | return;
|
---|
181 | }
|
---|
182 | memset(tosort[nbdirs-1], 0, str_length(dp->d_name)+1);
|
---|
183 |
|
---|
184 | str_cpy(tosort[nbdirs-1], str_length(dp->d_name)+1, dp->d_name);
|
---|
185 | }
|
---|
186 |
|
---|
187 | ls_sort_dir(d, tosort, nbdirs);
|
---|
188 |
|
---|
189 | for(i=0;i<nbdirs;i++) {
|
---|
190 | memset(buff, 0, sizeof(buff));
|
---|
191 | /* Don't worry if inserting a double slash, this will be fixed by
|
---|
192 | * absolutize() later with subsequent calls to open() or readdir() */
|
---|
193 | snprintf(buff, PATH_MAX - 1, "%s/%s", d, tosort[i]);
|
---|
194 | ls_print(tosort[i], buff);
|
---|
195 | free(tosort[i]);
|
---|
196 | }
|
---|
197 |
|
---|
198 | free(tosort);
|
---|
199 | free(buff);
|
---|
200 |
|
---|
201 | return;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* ls_print currently does nothing more than print the entry.
|
---|
205 | * in the future, we will likely pass the absolute path, and
|
---|
206 | * some sort of ls_options structure that controls how each
|
---|
207 | * entry is printed and what is printed about it.
|
---|
208 | *
|
---|
209 | * Now we just print basic DOS style lists */
|
---|
210 |
|
---|
211 | static void ls_print(const char *name, const char *pathname)
|
---|
212 | {
|
---|
213 | struct stat s;
|
---|
214 | int rc;
|
---|
215 |
|
---|
216 | rc = stat(pathname, &s);
|
---|
217 | if (rc != 0) {
|
---|
218 | /* Odd chance it was deleted from the time readdir() found it */
|
---|
219 | printf("ls: skipping bogus node %s\n", pathname);
|
---|
220 | printf("rc=%d\n", rc);
|
---|
221 | return;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (s.is_file)
|
---|
225 | printf("%-40s\t%llu\n", name, (long long) s.size);
|
---|
226 | else if (s.is_directory)
|
---|
227 | printf("%-40s\t<dir>\n", name);
|
---|
228 | else
|
---|
229 | printf("%-40s\n", name);
|
---|
230 |
|
---|
231 | return;
|
---|
232 | }
|
---|
233 |
|
---|
234 | void help_cmd_ls(unsigned int level)
|
---|
235 | {
|
---|
236 | if (level == HELP_SHORT) {
|
---|
237 | printf("`%s' lists files and directories.\n", cmdname);
|
---|
238 | } else {
|
---|
239 | help_cmd_ls(HELP_SHORT);
|
---|
240 | printf(" `%s' [path], if no path is given the current "
|
---|
241 | "working directory is used.\n", cmdname);
|
---|
242 | }
|
---|
243 |
|
---|
244 | return;
|
---|
245 | }
|
---|
246 |
|
---|
247 | int cmd_ls(char **argv)
|
---|
248 | {
|
---|
249 | unsigned int argc;
|
---|
250 | struct stat s;
|
---|
251 | char *buff;
|
---|
252 | DIR *dirp;
|
---|
253 |
|
---|
254 | argc = cli_count_args(argv);
|
---|
255 |
|
---|
256 | buff = (char *) malloc(PATH_MAX);
|
---|
257 | if (NULL == buff) {
|
---|
258 | cli_error(CL_ENOMEM, "%s: ", cmdname);
|
---|
259 | return CMD_FAILURE;
|
---|
260 | }
|
---|
261 | memset(buff, 0, sizeof(buff));
|
---|
262 |
|
---|
263 | if (argc == 1)
|
---|
264 | getcwd(buff, PATH_MAX);
|
---|
265 | else
|
---|
266 | str_cpy(buff, PATH_MAX, argv[1]);
|
---|
267 |
|
---|
268 | if (stat(buff, &s)) {
|
---|
269 | cli_error(CL_ENOENT, buff);
|
---|
270 | free(buff);
|
---|
271 | return CMD_FAILURE;
|
---|
272 | }
|
---|
273 |
|
---|
274 | if (s.is_file) {
|
---|
275 | ls_print(buff, buff);
|
---|
276 | } else {
|
---|
277 | dirp = opendir(buff);
|
---|
278 | if (!dirp) {
|
---|
279 | /* May have been deleted between scoping it and opening it */
|
---|
280 | cli_error(CL_EFAIL, "Could not stat %s", buff);
|
---|
281 | free(buff);
|
---|
282 | return CMD_FAILURE;
|
---|
283 | }
|
---|
284 | ls_scan_dir(buff, dirp);
|
---|
285 | closedir(dirp);
|
---|
286 | }
|
---|
287 |
|
---|
288 | free(buff);
|
---|
289 |
|
---|
290 | return CMD_SUCCESS;
|
---|
291 | }
|
---|
292 |
|
---|