Rev 3309 | Rev 3326 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3309 | Rev 3310 | ||
---|---|---|---|
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 | unsigned int ls_scope(const char *path) |
53 | 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 | void ls_print(const char *f) |
73 | void ls_print(const char *f) |
74 | { |
74 | { |
75 | unsigned int scope; |
75 | unsigned int scope; |
76 | 76 | ||
77 | scope = ls_scope(f); |
77 | scope = ls_scope(f); |
78 | 78 | ||
79 | /* If this function is called in a readdir loop, LS_BOGUS |
79 | /* If this function is called in a readdir loop, LS_BOGUS |
80 | * can be treated as LS_FILE, since we aren't presenting |
80 | * can be treated as LS_FILE, since we aren't presenting |
81 | * the full path to open(), just pointer->d_name as obtained |
81 | * the full path to open(), just pointer->d_name as obtained |
82 | * from opendir(). This will not happen if ../path/to/file |
82 | * from opendir(). This will not happen if ../path/to/file |
83 | * is passed, as may be the argument to ls */ |
83 | * is passed, as may be the argument to ls */ |
84 | 84 | ||
85 | switch (scope) { |
85 | switch (scope) { |
86 | case LS_DIR: |
86 | case LS_DIR: |
87 | printf("%-40s <DIR>\n", f); |
87 | printf("%-40s <DIR>\n", f); |
88 | break; |
88 | break; |
89 | case LS_FILE: |
89 | case LS_FILE: |
90 | printf("%-40s\n", f); |
90 | printf("%-40s\n", f); |
91 | break; |
91 | break; |
92 | /* This is never reached unless in a readdir() loop */ |
92 | /* This is never reached unless in a readdir() loop */ |
93 | case LS_BOGUS: |
93 | case LS_BOGUS: |
94 | printf("%-40s\n", f); |
94 | printf("%-40s\n", f); |
95 | break; |
95 | break; |
96 | } |
96 | } |
97 | return; |
97 | return; |
98 | } |
98 | } |
99 | 99 | ||
100 | /* Dispays help for ls in various levels */ |
100 | /* Dispays help for ls in various levels */ |
101 | void * help_cmd_ls(unsigned int level) |
101 | void * help_cmd_ls(unsigned int level) |
102 | { |
102 | { |
103 | if (level == HELP_SHORT) { |
103 | if (level == HELP_SHORT) { |
104 | printf("`%s' lists files and directories.\n", cmdname); |
104 | printf("`%s' lists files and directories.\n", cmdname); |
105 | } else { |
105 | } else { |
106 | help_cmd_ls(HELP_SHORT); |
106 | help_cmd_ls(HELP_SHORT); |
107 | printf(" `%s' [path], if no path is given the current " |
107 | printf(" `%s' [path], if no path is given the current " |
108 | "working directory is used.\n", cmdname); |
108 | "working directory is used.\n", cmdname); |
109 | } |
109 | } |
110 | 110 | ||
111 | return CMD_VOID; |
111 | return CMD_VOID; |
112 | } |
112 | } |
113 | 113 | ||
114 | int * cmd_ls(char **argv) |
114 | int * cmd_ls(char **argv) |
115 | { |
115 | { |
116 | unsigned int argc; |
116 | unsigned int argc; |
117 | unsigned int scope; |
117 | unsigned int scope; |
118 | char *buff; |
118 | char *buff; |
119 | char s[PATH_MAX]; |
- | |
120 | 119 | ||
121 | DIR *dirp; |
120 | DIR *dirp; |
122 | struct dirent *dp; |
121 | struct dirent *dp; |
123 | 122 | ||
124 | /* Count the arguments */ |
123 | /* Count the arguments */ |
125 | for (argc = 0; argv[argc] != NULL; argc ++); |
124 | for (argc = 0; argv[argc] != NULL; argc ++); |
126 | 125 | ||
127 | if (argc > 2) { |
126 | if (argc > 2) { |
128 | printf("%s - Too many arguments. Try `help %s extended'\n", |
127 | printf("%s - Too many arguments. Try `help %s extended'\n", |
129 | cmdname, cmdname); |
128 | cmdname, cmdname); |
130 | return CMD_FAILURE; |
129 | return CMD_FAILURE; |
131 | } |
130 | } |
132 | 131 | ||
133 | buff = (char *) malloc(PATH_MAX); |
132 | buff = (char *) malloc(PATH_MAX); |
134 | if (NULL == buff) { |
133 | if (NULL == buff) { |
135 | cli_error(CL_ENOMEM, "%s: ", cmdname); |
134 | cli_error(CL_ENOMEM, "%s: ", cmdname); |
136 | return CMD_FAILURE; |
135 | return CMD_FAILURE; |
137 | } |
136 | } |
138 | memset(buff, 0, sizeof(buff)); |
137 | memset(buff, 0, sizeof(buff)); |
139 | 138 | ||
140 | if (argc == 1) |
139 | if (argc == 1) |
141 | getcwd(buff, PATH_MAX); |
140 | getcwd(buff, PATH_MAX); |
142 | else |
141 | else |
143 | strncpy(buff, argv[1], PATH_MAX); |
142 | strncpy(buff, argv[1], PATH_MAX); |
144 | 143 | ||
145 | scope = ls_scope(buff); |
144 | scope = ls_scope(buff); |
146 | 145 | ||
147 | switch (scope) { |
146 | switch (scope) { |
148 | case LS_BOGUS: |
147 | case LS_BOGUS: |
149 | cli_error(CL_ENOENT, buff); |
148 | cli_error(CL_ENOENT, buff); |
150 | free(buff); |
149 | free(buff); |
151 | return CMD_FAILURE; |
150 | return CMD_FAILURE; |
152 | case LS_FILE: |
151 | case LS_FILE: |
153 | ls_print(buff); |
152 | ls_print(buff); |
154 | break; |
153 | break; |
155 | case LS_DIR: |
154 | case LS_DIR: |
156 | dirp = opendir(buff); |
155 | dirp = opendir(buff); |
157 | while ((dp = readdir(dirp))) |
156 | while ((dp = readdir(dirp))) |
158 | ls_print(dp->d_name); |
157 | ls_print(dp->d_name); |
159 | closedir(dirp); |
158 | closedir(dirp); |
160 | break; |
159 | break; |
161 | } |
160 | } |
162 | 161 | ||
163 | free(buff); |
162 | free(buff); |
164 | 163 | ||
165 | return CMD_SUCCESS; |
164 | return CMD_SUCCESS; |
166 | 165 | ||
167 | } |
166 | } |
168 | 167 | ||
169 | 168 |