Rev 3403 | Rev 4341 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3403 | Rev 3448 | ||
---|---|---|---|
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 unsigned int ls_scope(const char *path) |
53 | static unsigned int ls_scope(const char *path) |
54 | { |
54 | { |
55 | int fd; |
55 | int fd; |
56 | DIR *dirp; |
56 | DIR *dirp; |
57 | 57 | ||
58 | dirp = opendir(path); |
58 | dirp = opendir(path); |
59 | if (dirp) { |
59 | if (dirp) { |
60 | closedir(dirp); |
60 | closedir(dirp); |
61 | return LS_DIR; |
61 | return LS_DIR; |
62 | } |
62 | } |
63 | 63 | ||
64 | fd = open(path, O_RDONLY); |
64 | fd = open(path, O_RDONLY); |
65 | if (fd > 0) { |
65 | if (fd > 0) { |
66 | close(fd); |
66 | close(fd); |
67 | return LS_FILE; |
67 | return LS_FILE; |
68 | } |
68 | } |
69 | 69 | ||
70 | return LS_BOGUS; |
70 | return LS_BOGUS; |
71 | } |
71 | } |
72 | 72 | ||
73 | static void ls_scan_dir(const char *d, DIR *dirp) |
73 | static void ls_scan_dir(const char *d, DIR *dirp) |
74 | { |
74 | { |
75 | struct dirent *dp; |
75 | struct dirent *dp; |
76 | unsigned int scope; |
76 | unsigned int scope; |
77 | char *buff; |
77 | char *buff; |
78 | 78 | ||
79 | if (! dirp) |
79 | if (! dirp) |
80 | return; |
80 | return; |
81 | 81 | ||
82 | buff = (char *)malloc(PATH_MAX); |
82 | buff = (char *)malloc(PATH_MAX); |
83 | if (NULL == buff) { |
83 | if (NULL == buff) { |
84 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d); |
84 | cli_error(CL_ENOMEM, "ls: failed to scan %s", d); |
85 | return; |
85 | return; |
86 | } |
86 | } |
87 | 87 | ||
88 | while ((dp = readdir(dirp))) { |
88 | while ((dp = readdir(dirp))) { |
89 | memset(buff, 0, sizeof(buff)); |
89 | memset(buff, 0, sizeof(buff)); |
90 | /* Don't worry if inserting a double slash, this will be fixed by |
90 | /* Don't worry if inserting a double slash, this will be fixed by |
91 | * absolutize() later with subsequent calls to open() or readdir() */ |
91 | * absolutize() later with subsequent calls to open() or readdir() */ |
92 | snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name); |
92 | snprintf(buff, PATH_MAX - 1, "%s/%s", d, dp->d_name); |
93 | scope = ls_scope(buff); |
93 | scope = ls_scope(buff); |
94 | switch (scope) { |
94 | switch (scope) { |
95 | case LS_DIR: |
95 | case LS_DIR: |
96 | ls_print_dir(dp->d_name); |
96 | ls_print_dir(dp->d_name); |
97 | break; |
97 | break; |
98 | case LS_FILE: |
98 | case LS_FILE: |
99 | ls_print_file(dp->d_name); |
99 | ls_print_file(dp->d_name); |
100 | break; |
100 | break; |
101 | case LS_BOGUS: |
101 | case LS_BOGUS: |
102 | /* Odd chance it was deleted from the time readdir() found |
102 | /* Odd chance it was deleted from the time readdir() found |
103 | * it and the time that it was scoped */ |
103 | * it and the time that it was scoped */ |
104 | printf("ls: skipping bogus node %s\n", dp->d_name); |
104 | printf("ls: skipping bogus node %s\n", dp->d_name); |
105 | break; |
105 | break; |
106 | } |
106 | } |
107 | } |
107 | } |
108 | 108 | ||
109 | free(buff); |
109 | free(buff); |
110 | 110 | ||
111 | return; |
111 | return; |
112 | } |
112 | } |
113 | 113 | ||
114 | /* ls_print_* currently does nothing more than print the entry. |
114 | /* ls_print_* currently does nothing more than print the entry. |
115 | * in the future, we will likely pass the absolute path, and |
115 | * in the future, we will likely pass the absolute path, and |
116 | * some sort of ls_options structure that controls how each |
116 | * some sort of ls_options structure that controls how each |
117 | * entry is printed and what is printed about it. |
117 | * entry is printed and what is printed about it. |
118 | * |
118 | * |
119 | * Now we just print basic DOS style lists */ |
119 | * Now we just print basic DOS style lists */ |
120 | 120 | ||
121 | static void ls_print_dir(const char *d) |
121 | static void ls_print_dir(const char *d) |
122 | { |
122 | { |
123 | printf("%-40s\t<DIR>\n", d); |
123 | printf("%-40s\t<DIR>\n", d); |
124 | 124 | ||
125 | return; |
125 | return; |
126 | } |
126 | } |
127 | 127 | ||
128 | static void ls_print_file(const char *f) |
128 | static void ls_print_file(const char *f) |
129 | { |
129 | { |
130 | printf("%-40s\n", f); |
130 | printf("%-40s\n", f); |
131 | 131 | ||
132 | return; |
132 | return; |
133 | } |
133 | } |
134 | 134 | ||
135 | void * help_cmd_ls(unsigned int level) |
135 | void help_cmd_ls(unsigned int level) |
136 | { |
136 | { |
137 | if (level == HELP_SHORT) { |
137 | if (level == HELP_SHORT) { |
138 | printf("`%s' lists files and directories.\n", cmdname); |
138 | printf("`%s' lists files and directories.\n", cmdname); |
139 | } else { |
139 | } else { |
140 | help_cmd_ls(HELP_SHORT); |
140 | help_cmd_ls(HELP_SHORT); |
141 | printf(" `%s' [path], if no path is given the current " |
141 | printf(" `%s' [path], if no path is given the current " |
142 | "working directory is used.\n", cmdname); |
142 | "working directory is used.\n", cmdname); |
143 | } |
143 | } |
144 | 144 | ||
145 | return CMD_VOID; |
145 | return; |
146 | } |
146 | } |
147 | 147 | ||
148 | int * cmd_ls(char **argv) |
148 | int cmd_ls(char **argv) |
149 | { |
149 | { |
150 | unsigned int argc; |
150 | unsigned int argc; |
151 | unsigned int scope; |
151 | unsigned int scope; |
152 | char *buff; |
152 | char *buff; |
153 | DIR *dirp; |
153 | DIR *dirp; |
154 | 154 | ||
155 | argc = cli_count_args(argv); |
155 | argc = cli_count_args(argv); |
156 | 156 | ||
157 | buff = (char *) malloc(PATH_MAX); |
157 | buff = (char *) malloc(PATH_MAX); |
158 | if (NULL == buff) { |
158 | if (NULL == buff) { |
159 | cli_error(CL_ENOMEM, "%s: ", cmdname); |
159 | cli_error(CL_ENOMEM, "%s: ", cmdname); |
160 | return CMD_FAILURE; |
160 | return CMD_FAILURE; |
161 | } |
161 | } |
162 | memset(buff, 0, sizeof(buff)); |
162 | memset(buff, 0, sizeof(buff)); |
163 | 163 | ||
164 | if (argc == 1) |
164 | if (argc == 1) |
165 | getcwd(buff, PATH_MAX); |
165 | getcwd(buff, PATH_MAX); |
166 | else |
166 | else |
167 | strncpy(buff, argv[1], PATH_MAX); |
167 | strncpy(buff, argv[1], PATH_MAX); |
168 | 168 | ||
169 | scope = ls_scope(buff); |
169 | scope = ls_scope(buff); |
170 | 170 | ||
171 | switch (scope) { |
171 | switch (scope) { |
172 | case LS_BOGUS: |
172 | case LS_BOGUS: |
173 | cli_error(CL_ENOENT, buff); |
173 | cli_error(CL_ENOENT, buff); |
174 | free(buff); |
174 | free(buff); |
175 | return CMD_FAILURE; |
175 | return CMD_FAILURE; |
176 | case LS_FILE: |
176 | case LS_FILE: |
177 | ls_print_file(buff); |
177 | ls_print_file(buff); |
178 | break; |
178 | break; |
179 | case LS_DIR: |
179 | case LS_DIR: |
180 | dirp = opendir(buff); |
180 | dirp = opendir(buff); |
181 | if (! dirp) { |
181 | if (! dirp) { |
182 | /* May have been deleted between scoping it and opening it */ |
182 | /* May have been deleted between scoping it and opening it */ |
183 | cli_error(CL_EFAIL, "Could not stat %s", buff); |
183 | cli_error(CL_EFAIL, "Could not stat %s", buff); |
184 | free(buff); |
184 | free(buff); |
185 | return CMD_FAILURE; |
185 | return CMD_FAILURE; |
186 | } |
186 | } |
187 | ls_scan_dir(buff, dirp); |
187 | ls_scan_dir(buff, dirp); |
188 | closedir(dirp); |
188 | closedir(dirp); |
189 | break; |
189 | break; |
190 | } |
190 | } |
191 | 191 | ||
192 | free(buff); |
192 | free(buff); |
193 | 193 | ||
194 | return CMD_SUCCESS; |
194 | return CMD_SUCCESS; |
195 | } |
195 | } |
196 | 196 | ||
197 | 197 |