source: rtems/cpukit/libmisc/shell/print-ls.c @ 306dba9

4.104.115
Last change on this file since 306dba9 was 306dba9, checked in by Ralf Corsepius <ralf.corsepius@…>, on 10/22/09 at 04:29:55

Add DAYSPERNYEAR, SECSPERDAY.
Add casts to time_t on constants in DAYSPERNYEAR, SECSPERDAY for 16bit-int targets.

  • Property mode set to 100644
File size: 11.8 KB
Line 
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
43static 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#define DAYSPERNYEAR ((time_t)365)
72#define SECSPERDAY   ((time_t)60 * (time_t)60 * (time_t)24)
73
74
75#if RTEMS_REMOVED
76extern int termwidth;
77#endif
78
79static int      printaname(rtems_shell_ls_globals* globals, FTSENT *, int, int);
80static void     printlink(rtems_shell_ls_globals* globals, FTSENT *);
81static void     printtime(rtems_shell_ls_globals* globals, time_t);
82static int      printtype(u_int);
83
84#if RTEMS_REMOVED
85static time_t   now;
86#endif
87
88#define IS_NOPRINT(p)   ((p)->fts_number == NO_PRINT)
89
90void
91printscol(rtems_shell_ls_globals* globals, DISPLAY *dp)
92{
93        FTSENT *p;
94
95        for (p = dp->list; p; p = p->fts_link) {
96                if (IS_NOPRINT(p))
97                        continue;
98                (void)printaname(globals, p, dp->s_inode, dp->s_block);
99                (void)putchar('\n');
100        }
101}
102
103void
104printlong(rtems_shell_ls_globals* globals, DISPLAY *dp)
105{
106        struct stat *sp;
107        FTSENT *p;
108        NAMES *np;
109        char buf[20]; //, szbuf[5];
110
111        now = time(NULL);
112
113        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
114#if RTEMS_REMOVED
115                if (f_humanize) {
116                        if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
117                            "", HN_AUTOSCALE,
118                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
119                                err(exit_jump, 1, "humanize_number");
120                        (void)printf("total %s\n", szbuf);
121                } else {
122#endif
123                        (void)printf("total %llu\n",
124                            (long long)(howmany(dp->btotal, blocksize)));
125#if RTEMS_REMOVED
126                }
127#endif
128        }
129
130        for (p = dp->list; p; p = p->fts_link) {
131                if (IS_NOPRINT(p))
132                        continue;
133                sp = p->fts_statp;
134                if (f_inode)
135                        (void)printf("%*lu ", dp->s_inode,
136                            (unsigned long)sp->st_ino);
137                if (f_size && !f_humanize) {
138                        (void)printf("%*llu ", dp->s_block,
139                            (long long)howmany(sp->st_blocks, blocksize));
140                }
141                (void)strmode(sp->st_mode, buf);
142                np = p->fts_pointer;
143                (void)printf("%s %*lu ", buf, dp->s_nlink,
144                    (unsigned long)sp->st_nlink);
145                if (!f_grouponly)
146                        (void)printf("%-*s  ", dp->s_user, np->user);
147                (void)printf("%-*s  ", dp->s_group, np->group);
148                if (f_flags)
149                        (void)printf("%-*s ", dp->s_flags, np->flags);
150                if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
151                        (void)printf("%*lu, %*lu ",
152                            dp->s_major, major(sp->st_rdev), dp->s_minor,
153                            minor(sp->st_rdev));
154                else
155#if RTEMS_REMOVED
156                        if (f_humanize) {
157                                if ((humanize_number(szbuf, sizeof(szbuf),
158                                    sp->st_size, "", HN_AUTOSCALE,
159                                    (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
160                                        err(1, "humanize_number");
161                                (void)printf("%*s ", dp->s_size, szbuf);
162                        } else {
163#endif
164                                (void)printf("%*llu ", dp->s_size,
165                                    (long long)sp->st_size);
166#if RTEMS_REMOVED
167                }
168#endif
169                if (f_accesstime)
170                        printtime(globals, sp->st_atime);
171                else if (f_statustime)
172                        printtime(globals, sp->st_ctime);
173                else
174                        printtime(globals, sp->st_mtime);
175                if (f_octal || f_octal_escape)
176                        (void)safe_print(globals, p->fts_name);
177                else if (f_nonprint)
178                        (void)printescaped(globals, p->fts_name);
179                else
180                        (void)printf("%s", p->fts_name);
181
182                if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
183                        (void)printtype(sp->st_mode);
184                if (S_ISLNK(sp->st_mode))
185                        printlink(globals, p);
186                (void)putchar('\n');
187        }
188}
189
190void
191printcol(rtems_shell_ls_globals* globals, DISPLAY *dp)
192{
193        static FTSENT **array;
194        static int lastentries = -1;
195        FTSENT *p;
196        int base, chcnt, col, colwidth, num;
197        int numcols, numrows, row;
198        //char szbuf[5];
199
200        colwidth = dp->maxlen;
201        if (f_inode)
202                colwidth += dp->s_inode + 1;
203        if (f_size) {
204                if (f_humanize)
205                        colwidth += dp->s_size + 1;
206                else
207                        colwidth += dp->s_block + 1;
208        }
209        if (f_type || f_typedir)
210                colwidth += 1;
211
212        colwidth += 1;
213
214        if (termwidth < 2 * colwidth) {
215                printscol(globals, dp);
216                return;
217        }
218
219        /*
220         * Have to do random access in the linked list -- build a table
221         * of pointers.
222         */
223        if (dp->entries > lastentries) {
224                lastentries = dp->entries;
225                if ((array =
226                    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
227                        warn(NULL);
228                        printscol(globals, dp);
229                }
230        }
231        for (p = dp->list, num = 0; p; p = p->fts_link)
232                if (p->fts_number != NO_PRINT)
233                        array[num++] = p;
234
235        numcols = termwidth / colwidth;
236        colwidth = termwidth / numcols;         /* spread out if possible */
237        numrows = num / numcols;
238        if (num % numcols)
239                ++numrows;
240
241        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
242#if RTEMS_REMOVED
243                if (f_humanize) {
244                        if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
245                            "", HN_AUTOSCALE,
246                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
247                                err(1, "humanize_number");
248                        (void)printf("total %s\n", szbuf);
249                } else {
250#endif
251                        (void)printf("total %llu\n",
252                            (long long)(howmany(dp->btotal, blocksize)));
253#if RTEMS_REMOVED
254                }
255#endif
256        }
257        for (row = 0; row < numrows; ++row) {
258                for (base = row, chcnt = col = 0; col < numcols; ++col) {
259                        chcnt = printaname(globals, array[base], dp->s_inode,
260                            f_humanize && 0 ? dp->s_size : dp->s_block);
261                        if ((base += numrows) >= num)
262                                break;
263                        while (chcnt++ < colwidth)
264                                (void)putchar(' ');
265                }
266                (void)putchar('\n');
267        }
268}
269
270void
271printacol(rtems_shell_ls_globals* globals, DISPLAY *dp)
272{
273        FTSENT *p;
274        int chcnt, col, colwidth;
275        int numcols;
276        //char szbuf[5];
277
278        colwidth = dp->maxlen;
279        if (f_inode)
280                colwidth += dp->s_inode + 1;
281        if (f_size) {
282                if (f_humanize)
283                        colwidth += dp->s_size + 1;
284                else
285                        colwidth += dp->s_block + 1;
286        }
287        if (f_type || f_typedir)
288                colwidth += 1;
289
290        colwidth += 1;
291
292        if (termwidth < 2 * colwidth) {
293                printscol(globals, dp);
294                return;
295        }
296
297        numcols = termwidth / colwidth;
298        colwidth = termwidth / numcols;         /* spread out if possible */
299
300        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
301#if RTEMS_REMOVED
302                if (f_humanize) {
303                        if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
304                            "", HN_AUTOSCALE,
305                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
306                                err(1, "humanize_number");
307                        (void)printf("total %s\n", szbuf);
308                } else {
309#endif
310                        (void)printf("total %llu\n",
311                            (long long)(howmany(dp->btotal, blocksize)));
312#if RTEMS_REMOVED
313                }
314#endif
315        }
316        chcnt = col = 0;
317        for (p = dp->list; p; p = p->fts_link) {
318                if (IS_NOPRINT(p))
319                        continue;
320                if (col >= numcols) {
321                        chcnt = col = 0;
322                        (void)putchar('\n');
323                }
324                chcnt = printaname(globals, p, dp->s_inode,
325                    f_humanize && 0 ? dp->s_size : dp->s_block);
326                while (chcnt++ < colwidth)
327                        (void)putchar(' ');
328                col++;
329        }
330        (void)putchar('\n');
331}
332
333void
334printstream(rtems_shell_ls_globals* globals, DISPLAY *dp)
335{
336        FTSENT *p;
337        int col;
338        int extwidth;
339
340        extwidth = 0;
341        if (f_inode)
342                extwidth += dp->s_inode + 1;
343        if (f_size) {
344                if (f_humanize)
345                        extwidth += dp->s_size + 1;
346                else
347                        extwidth += dp->s_block + 1;
348        }
349        if (f_type)
350                extwidth += 1;
351
352        for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
353                if (IS_NOPRINT(p))
354                        continue;
355                if (col > 0) {
356                        (void)putchar(','), col++;
357                        if (col + 1 + extwidth + p->fts_namelen >= termwidth)
358                                (void)putchar('\n'), col = 0;
359                        else
360                                (void)putchar(' '), col++;
361                }
362                col += printaname(globals, p, dp->s_inode,
363                    f_humanize && 0 ? dp->s_size : dp->s_block);
364        }
365        (void)putchar('\n');
366}
367
368/*
369 * print [inode] [size] name
370 * return # of characters printed, no trailing characters.
371 */
372static int
373printaname(rtems_shell_ls_globals* globals,
374           FTSENT *p, int inodefield, int sizefield)
375{
376        struct stat *sp;
377        int chcnt;
378        //char szbuf[5];
379
380        sp = p->fts_statp;
381        chcnt = 0;
382        if (f_inode)
383                chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
384        if (f_size) {
385#if RTEMS_REMOVED
386                if (f_humanize) {
387                        if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
388                            "", HN_AUTOSCALE,
389                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
390                                err(1, "humanize_number");
391                        chcnt += printf("%*s ", sizefield, szbuf);
392                } else {
393#endif
394                        chcnt += printf("%*llu ", sizefield,
395                            (long long)howmany(sp->st_blocks, blocksize));
396#if RTEMS_REMOVED
397                }
398#endif
399        }
400        if (f_octal || f_octal_escape)
401                chcnt += safe_print(globals, p->fts_name);
402        else if (f_nonprint)
403                chcnt += printescaped(globals, p->fts_name);
404        else
405                chcnt += printf("%s", p->fts_name);
406        if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
407                chcnt += printtype(sp->st_mode);
408        return (chcnt);
409}
410
411static void
412printtime(rtems_shell_ls_globals* globals, time_t ftime)
413{
414        int i;
415        char *longstring;
416
417        longstring = ctime(&ftime);
418        for (i = 4; i < 11; ++i)
419                (void)putchar(longstring[i]);
420
421#define SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
422        if (f_sectime)
423                for (i = 11; i < 24; i++)
424                        (void)putchar(longstring[i]);
425        else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
426                for (i = 11; i < 16; ++i)
427                        (void)putchar(longstring[i]);
428        else {
429                (void)putchar(' ');
430                for (i = 20; i < 24; ++i)
431                        (void)putchar(longstring[i]);
432        }
433        (void)putchar(' ');
434}
435
436static int
437printtype(u_int mode)
438{
439        switch (mode & S_IFMT) {
440        case S_IFDIR:
441                (void)putchar('/');
442                return (1);
443        case S_IFIFO:
444                (void)putchar('|');
445                return (1);
446        case S_IFLNK:
447                (void)putchar('@');
448                return (1);
449        case S_IFSOCK:
450                (void)putchar('=');
451                return (1);
452#if RTEMS_REMOVED
453        case S_IFWHT:
454                (void)putchar('%');
455                return (1);
456#endif
457        }
458        if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
459                (void)putchar('*');
460                return (1);
461        }
462        return (0);
463}
464
465static void
466printlink(rtems_shell_ls_globals* globals, FTSENT *p)
467{
468        int lnklen;
469        char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
470
471        if (p->fts_level == FTS_ROOTLEVEL)
472                (void)snprintf(name, sizeof(name), "%s", p->fts_name);
473        else
474                (void)snprintf(name, sizeof(name),
475                    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
476        if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
477                (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
478                return;
479        }
480        path[lnklen] = '\0';
481        (void)printf(" -> ");
482        if (f_octal || f_octal_escape)
483                (void)safe_print(globals, path);
484        else if (f_nonprint)
485                (void)printescaped(globals, path);
486        else
487                (void)printf("%s", path);
488}
Note: See TracBrowser for help on using the repository browser.