Rev 3386 | Rev 4327 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3386 | Rev 4153 | ||
---|---|---|---|
Line 48... | Line 48... | ||
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 | ||
Line 94... | Line 112... | ||
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); |
Line 118... | Line 136... | ||
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 CMD_VOID; |
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; |
Line 172... | Line 190... | ||
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 */ |