1 | /* $NetBSD: print.c,v 1.40 2004/11/17 17:00:00 mycroft Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 1989, 1993, 1994 |
---|
5 | * The Regents of the University of California. All rights reserved. |
---|
6 | * |
---|
7 | * This code is derived from software contributed to Berkeley by |
---|
8 | * Michael Fischbein. |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * 3. Neither the name of the University nor the names of its contributors |
---|
19 | * may be used to endorse or promote products derived from this software |
---|
20 | * without specific prior written permission. |
---|
21 | * |
---|
22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
32 | * SUCH DAMAGE. |
---|
33 | */ |
---|
34 | |
---|
35 | #ifdef HAVE_CONFIG_H |
---|
36 | #include "config.h" |
---|
37 | #endif |
---|
38 | |
---|
39 | #if 0 |
---|
40 | #include <sys/cdefs.h> |
---|
41 | #ifndef lint |
---|
42 | #if 0 |
---|
43 | static char sccsid[] = "@(#)print.c 8.5 (Berkeley) 7/28/94"; |
---|
44 | #else |
---|
45 | __RCSID("$NetBSD: print.c,v 1.40 2004/11/17 17:00:00 mycroft Exp $"); |
---|
46 | #endif |
---|
47 | #endif /* not lint */ |
---|
48 | #endif |
---|
49 | |
---|
50 | #include <rtems.h> |
---|
51 | #include <rtems/libio.h> |
---|
52 | |
---|
53 | #include <sys/param.h> |
---|
54 | #include <sys/stat.h> |
---|
55 | |
---|
56 | #include <err.h> |
---|
57 | #include <errno.h> |
---|
58 | #include <fts.h> |
---|
59 | #include <grp.h> |
---|
60 | #include <pwd.h> |
---|
61 | #include <stdio.h> |
---|
62 | #include <stdlib.h> |
---|
63 | #include <string.h> |
---|
64 | #include <time.h> |
---|
65 | //#include <tzfile.h> |
---|
66 | #include <unistd.h> |
---|
67 | //#include <util.h> |
---|
68 | |
---|
69 | #include "extern-ls.h" |
---|
70 | |
---|
71 | #if RTEMS_REMOVED |
---|
72 | extern int termwidth; |
---|
73 | #endif |
---|
74 | |
---|
75 | static int printaname(rtems_shell_ls_globals* globals, FTSENT *, int, int); |
---|
76 | static void printlink(rtems_shell_ls_globals* globals, FTSENT *); |
---|
77 | static void printtime(rtems_shell_ls_globals* globals, time_t); |
---|
78 | static int printtype(u_int); |
---|
79 | |
---|
80 | #if RTEMS_REMOVED |
---|
81 | static time_t now; |
---|
82 | #endif |
---|
83 | |
---|
84 | #define IS_NOPRINT(p) ((p)->fts_number == NO_PRINT) |
---|
85 | |
---|
86 | void |
---|
87 | printscol(rtems_shell_ls_globals* globals, DISPLAY *dp) |
---|
88 | { |
---|
89 | FTSENT *p; |
---|
90 | |
---|
91 | for (p = dp->list; p; p = p->fts_link) { |
---|
92 | if (IS_NOPRINT(p)) |
---|
93 | continue; |
---|
94 | (void)printaname(globals, p, dp->s_inode, dp->s_block); |
---|
95 | (void)putchar('\n'); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | void |
---|
100 | printlong(rtems_shell_ls_globals* globals, DISPLAY *dp) |
---|
101 | { |
---|
102 | struct stat *sp; |
---|
103 | FTSENT *p; |
---|
104 | NAMES *np; |
---|
105 | char buf[20]; //, szbuf[5]; |
---|
106 | |
---|
107 | now = time(NULL); |
---|
108 | |
---|
109 | if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) { |
---|
110 | #if RTEMS_REMOVED |
---|
111 | if (f_humanize) { |
---|
112 | if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal, |
---|
113 | "", HN_AUTOSCALE, |
---|
114 | (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) |
---|
115 | err(exit_jump, 1, "humanize_number"); |
---|
116 | (void)printf("total %s\n", szbuf); |
---|
117 | } else { |
---|
118 | #endif |
---|
119 | (void)printf("total %llu\n", |
---|
120 | (long long)(howmany(dp->btotal, blocksize))); |
---|
121 | #if RTEMS_REMOVED |
---|
122 | } |
---|
123 | #endif |
---|
124 | } |
---|
125 | |
---|
126 | for (p = dp->list; p; p = p->fts_link) { |
---|
127 | if (IS_NOPRINT(p)) |
---|
128 | continue; |
---|
129 | sp = p->fts_statp; |
---|
130 | if (f_inode) |
---|
131 | (void)printf("%*lu ", dp->s_inode, |
---|
132 | (unsigned long)sp->st_ino); |
---|
133 | if (f_size && !f_humanize) { |
---|
134 | (void)printf("%*llu ", dp->s_block, |
---|
135 | (long long)howmany(sp->st_blocks, blocksize)); |
---|
136 | } |
---|
137 | (void)strmode(sp->st_mode, buf); |
---|
138 | np = p->fts_pointer; |
---|
139 | (void)printf("%s %*lu ", buf, dp->s_nlink, |
---|
140 | (unsigned long)sp->st_nlink); |
---|
141 | if (!f_grouponly) |
---|
142 | (void)printf("%-*s ", dp->s_user, np->user); |
---|
143 | (void)printf("%-*s ", dp->s_group, np->group); |
---|
144 | if (f_flags) |
---|
145 | (void)printf("%-*s ", dp->s_flags, np->flags); |
---|
146 | if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode)) |
---|
147 | (void)printf("%*lu, %*lu ", |
---|
148 | dp->s_major, major(sp->st_rdev), dp->s_minor, |
---|
149 | minor(sp->st_rdev)); |
---|
150 | else |
---|
151 | #if RTEMS_REMOVED |
---|
152 | if (f_humanize) { |
---|
153 | if ((humanize_number(szbuf, sizeof(szbuf), |
---|
154 | sp->st_size, "", HN_AUTOSCALE, |
---|
155 | (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) |
---|
156 | err(1, "humanize_number"); |
---|
157 | (void)printf("%*s ", dp->s_size, szbuf); |
---|
158 | } else { |
---|
159 | #endif |
---|
160 | (void)printf("%*llu ", dp->s_size, |
---|
161 | (long long)sp->st_size); |
---|
162 | #if RTEMS_REMOVED |
---|
163 | } |
---|
164 | #endif |
---|
165 | if (f_accesstime) |
---|
166 | printtime(globals, sp->st_atime); |
---|
167 | else if (f_statustime) |
---|
168 | printtime(globals, sp->st_ctime); |
---|
169 | else |
---|
170 | printtime(globals, sp->st_mtime); |
---|
171 | if (f_octal || f_octal_escape) |
---|
172 | (void)safe_print(globals, p->fts_name); |
---|
173 | else if (f_nonprint) |
---|
174 | (void)printescaped(globals, p->fts_name); |
---|
175 | else |
---|
176 | (void)printf("%s", p->fts_name); |
---|
177 | |
---|
178 | if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) |
---|
179 | (void)printtype(sp->st_mode); |
---|
180 | if (S_ISLNK(sp->st_mode)) |
---|
181 | printlink(globals, p); |
---|
182 | (void)putchar('\n'); |
---|
183 | } |
---|
184 | } |
---|
185 | |
---|
186 | void |
---|
187 | printcol(rtems_shell_ls_globals* globals, DISPLAY *dp) |
---|
188 | { |
---|
189 | static FTSENT **array; |
---|
190 | static int lastentries = -1; |
---|
191 | FTSENT *p; |
---|
192 | int base, chcnt, col, colwidth, num; |
---|
193 | int numcols, numrows, row; |
---|
194 | //char szbuf[5]; |
---|
195 | |
---|
196 | colwidth = dp->maxlen; |
---|
197 | if (f_inode) |
---|
198 | colwidth += dp->s_inode + 1; |
---|
199 | if (f_size) { |
---|
200 | if (f_humanize) |
---|
201 | colwidth += dp->s_size + 1; |
---|
202 | else |
---|
203 | colwidth += dp->s_block + 1; |
---|
204 | } |
---|
205 | if (f_type || f_typedir) |
---|
206 | colwidth += 1; |
---|
207 | |
---|
208 | colwidth += 1; |
---|
209 | |
---|
210 | if (termwidth < 2 * colwidth) { |
---|
211 | printscol(globals, dp); |
---|
212 | return; |
---|
213 | } |
---|
214 | |
---|
215 | /* |
---|
216 | * Have to do random access in the linked list -- build a table |
---|
217 | * of pointers. |
---|
218 | */ |
---|
219 | if (dp->entries > lastentries) { |
---|
220 | lastentries = dp->entries; |
---|
221 | if ((array = |
---|
222 | realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) { |
---|
223 | warn(NULL); |
---|
224 | printscol(globals, dp); |
---|
225 | } |
---|
226 | } |
---|
227 | for (p = dp->list, num = 0; p; p = p->fts_link) |
---|
228 | if (p->fts_number != NO_PRINT) |
---|
229 | array[num++] = p; |
---|
230 | |
---|
231 | numcols = termwidth / colwidth; |
---|
232 | colwidth = termwidth / numcols; /* spread out if possible */ |
---|
233 | numrows = num / numcols; |
---|
234 | if (num % numcols) |
---|
235 | ++numrows; |
---|
236 | |
---|
237 | if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) { |
---|
238 | #if RTEMS_REMOVED |
---|
239 | if (f_humanize) { |
---|
240 | if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal, |
---|
241 | "", HN_AUTOSCALE, |
---|
242 | (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) |
---|
243 | err(1, "humanize_number"); |
---|
244 | (void)printf("total %s\n", szbuf); |
---|
245 | } else { |
---|
246 | #endif |
---|
247 | (void)printf("total %llu\n", |
---|
248 | (long long)(howmany(dp->btotal, blocksize))); |
---|
249 | #if RTEMS_REMOVED |
---|
250 | } |
---|
251 | #endif |
---|
252 | } |
---|
253 | for (row = 0; row < numrows; ++row) { |
---|
254 | for (base = row, chcnt = col = 0; col < numcols; ++col) { |
---|
255 | chcnt = printaname(globals, array[base], dp->s_inode, |
---|
256 | f_humanize && 0 ? dp->s_size : dp->s_block); |
---|
257 | if ((base += numrows) >= num) |
---|
258 | break; |
---|
259 | while (chcnt++ < colwidth) |
---|
260 | (void)putchar(' '); |
---|
261 | } |
---|
262 | (void)putchar('\n'); |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | void |
---|
267 | printacol(rtems_shell_ls_globals* globals, DISPLAY *dp) |
---|
268 | { |
---|
269 | FTSENT *p; |
---|
270 | int chcnt, col, colwidth; |
---|
271 | int numcols; |
---|
272 | //char szbuf[5]; |
---|
273 | |
---|
274 | colwidth = dp->maxlen; |
---|
275 | if (f_inode) |
---|
276 | colwidth += dp->s_inode + 1; |
---|
277 | if (f_size) { |
---|
278 | if (f_humanize) |
---|
279 | colwidth += dp->s_size + 1; |
---|
280 | else |
---|
281 | colwidth += dp->s_block + 1; |
---|
282 | } |
---|
283 | if (f_type || f_typedir) |
---|
284 | colwidth += 1; |
---|
285 | |
---|
286 | colwidth += 1; |
---|
287 | |
---|
288 | if (termwidth < 2 * colwidth) { |
---|
289 | printscol(globals, dp); |
---|
290 | return; |
---|
291 | } |
---|
292 | |
---|
293 | numcols = termwidth / colwidth; |
---|
294 | colwidth = termwidth / numcols; /* spread out if possible */ |
---|
295 | |
---|
296 | if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) { |
---|
297 | #if RTEMS_REMOVED |
---|
298 | if (f_humanize) { |
---|
299 | if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal, |
---|
300 | "", HN_AUTOSCALE, |
---|
301 | (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) |
---|
302 | err(1, "humanize_number"); |
---|
303 | (void)printf("total %s\n", szbuf); |
---|
304 | } else { |
---|
305 | #endif |
---|
306 | (void)printf("total %llu\n", |
---|
307 | (long long)(howmany(dp->btotal, blocksize))); |
---|
308 | #if RTEMS_REMOVED |
---|
309 | } |
---|
310 | #endif |
---|
311 | } |
---|
312 | chcnt = col = 0; |
---|
313 | for (p = dp->list; p; p = p->fts_link) { |
---|
314 | if (IS_NOPRINT(p)) |
---|
315 | continue; |
---|
316 | if (col >= numcols) { |
---|
317 | chcnt = col = 0; |
---|
318 | (void)putchar('\n'); |
---|
319 | } |
---|
320 | chcnt = printaname(globals, p, dp->s_inode, |
---|
321 | f_humanize && 0 ? dp->s_size : dp->s_block); |
---|
322 | while (chcnt++ < colwidth) |
---|
323 | (void)putchar(' '); |
---|
324 | col++; |
---|
325 | } |
---|
326 | (void)putchar('\n'); |
---|
327 | } |
---|
328 | |
---|
329 | void |
---|
330 | printstream(rtems_shell_ls_globals* globals, DISPLAY *dp) |
---|
331 | { |
---|
332 | FTSENT *p; |
---|
333 | int col; |
---|
334 | int extwidth; |
---|
335 | |
---|
336 | extwidth = 0; |
---|
337 | if (f_inode) |
---|
338 | extwidth += dp->s_inode + 1; |
---|
339 | if (f_size) { |
---|
340 | if (f_humanize) |
---|
341 | extwidth += dp->s_size + 1; |
---|
342 | else |
---|
343 | extwidth += dp->s_block + 1; |
---|
344 | } |
---|
345 | if (f_type) |
---|
346 | extwidth += 1; |
---|
347 | |
---|
348 | for (col = 0, p = dp->list; p != NULL; p = p->fts_link) { |
---|
349 | if (IS_NOPRINT(p)) |
---|
350 | continue; |
---|
351 | if (col > 0) { |
---|
352 | (void)putchar(','), col++; |
---|
353 | if (col + 1 + extwidth + p->fts_namelen >= termwidth) |
---|
354 | (void)putchar('\n'), col = 0; |
---|
355 | else |
---|
356 | (void)putchar(' '), col++; |
---|
357 | } |
---|
358 | col += printaname(globals, p, dp->s_inode, |
---|
359 | f_humanize && 0 ? dp->s_size : dp->s_block); |
---|
360 | } |
---|
361 | (void)putchar('\n'); |
---|
362 | } |
---|
363 | |
---|
364 | /* |
---|
365 | * print [inode] [size] name |
---|
366 | * return # of characters printed, no trailing characters. |
---|
367 | */ |
---|
368 | static int |
---|
369 | printaname(rtems_shell_ls_globals* globals, |
---|
370 | FTSENT *p, int inodefield, int sizefield) |
---|
371 | { |
---|
372 | struct stat *sp; |
---|
373 | int chcnt; |
---|
374 | //char szbuf[5]; |
---|
375 | |
---|
376 | sp = p->fts_statp; |
---|
377 | chcnt = 0; |
---|
378 | if (f_inode) |
---|
379 | chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino); |
---|
380 | if (f_size) { |
---|
381 | #if RTEMS_REMOVED |
---|
382 | if (f_humanize) { |
---|
383 | if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size, |
---|
384 | "", HN_AUTOSCALE, |
---|
385 | (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1) |
---|
386 | err(1, "humanize_number"); |
---|
387 | chcnt += printf("%*s ", sizefield, szbuf); |
---|
388 | } else { |
---|
389 | #endif |
---|
390 | chcnt += printf("%*llu ", sizefield, |
---|
391 | (long long)howmany(sp->st_blocks, blocksize)); |
---|
392 | #if RTEMS_REMOVED |
---|
393 | } |
---|
394 | #endif |
---|
395 | } |
---|
396 | if (f_octal || f_octal_escape) |
---|
397 | chcnt += safe_print(globals, p->fts_name); |
---|
398 | else if (f_nonprint) |
---|
399 | chcnt += printescaped(globals, p->fts_name); |
---|
400 | else |
---|
401 | chcnt += printf("%s", p->fts_name); |
---|
402 | if (f_type || (f_typedir && S_ISDIR(sp->st_mode))) |
---|
403 | chcnt += printtype(sp->st_mode); |
---|
404 | return (chcnt); |
---|
405 | } |
---|
406 | |
---|
407 | static void |
---|
408 | printtime(rtems_shell_ls_globals* globals, time_t ftime) |
---|
409 | { |
---|
410 | int i; |
---|
411 | char *longstring; |
---|
412 | |
---|
413 | longstring = ctime(&ftime); |
---|
414 | for (i = 4; i < 11; ++i) |
---|
415 | (void)putchar(longstring[i]); |
---|
416 | |
---|
417 | #define SIXMONTHS ((DAYSPERNYEAR / 2) * SECSPERDAY) |
---|
418 | if (f_sectime) |
---|
419 | for (i = 11; i < 24; i++) |
---|
420 | (void)putchar(longstring[i]); |
---|
421 | else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now) |
---|
422 | for (i = 11; i < 16; ++i) |
---|
423 | (void)putchar(longstring[i]); |
---|
424 | else { |
---|
425 | (void)putchar(' '); |
---|
426 | for (i = 20; i < 24; ++i) |
---|
427 | (void)putchar(longstring[i]); |
---|
428 | } |
---|
429 | (void)putchar(' '); |
---|
430 | } |
---|
431 | |
---|
432 | static int |
---|
433 | printtype(u_int mode) |
---|
434 | { |
---|
435 | switch (mode & S_IFMT) { |
---|
436 | case S_IFDIR: |
---|
437 | (void)putchar('/'); |
---|
438 | return (1); |
---|
439 | case S_IFIFO: |
---|
440 | (void)putchar('|'); |
---|
441 | return (1); |
---|
442 | case S_IFLNK: |
---|
443 | (void)putchar('@'); |
---|
444 | return (1); |
---|
445 | case S_IFSOCK: |
---|
446 | (void)putchar('='); |
---|
447 | return (1); |
---|
448 | #if RTEMS_REMOVED |
---|
449 | case S_IFWHT: |
---|
450 | (void)putchar('%'); |
---|
451 | return (1); |
---|
452 | #endif |
---|
453 | } |
---|
454 | if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) { |
---|
455 | (void)putchar('*'); |
---|
456 | return (1); |
---|
457 | } |
---|
458 | return (0); |
---|
459 | } |
---|
460 | |
---|
461 | static void |
---|
462 | printlink(rtems_shell_ls_globals* globals, FTSENT *p) |
---|
463 | { |
---|
464 | int lnklen; |
---|
465 | char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1]; |
---|
466 | |
---|
467 | if (p->fts_level == FTS_ROOTLEVEL) |
---|
468 | (void)snprintf(name, sizeof(name), "%s", p->fts_name); |
---|
469 | else |
---|
470 | (void)snprintf(name, sizeof(name), |
---|
471 | "%s/%s", p->fts_parent->fts_accpath, p->fts_name); |
---|
472 | if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) { |
---|
473 | (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno)); |
---|
474 | return; |
---|
475 | } |
---|
476 | path[lnklen] = '\0'; |
---|
477 | (void)printf(" -> "); |
---|
478 | if (f_octal || f_octal_escape) |
---|
479 | (void)safe_print(globals, path); |
---|
480 | else if (f_nonprint) |
---|
481 | (void)printescaped(globals, path); |
---|
482 | else |
---|
483 | (void)printf("%s", path); |
---|
484 | } |
---|