source: rtems/cpukit/libmisc/shell/print-ls.c @ 42c4de8

4.104.115
Last change on this file since 42c4de8 was 42c4de8, checked in by Chris Johns <chrisj@…>, on 02/01/10 at 00:03:02

2010-02-01 Chris Johns <chrisj@…>

  • libmisc/shell/fts.c: Updated to the latest NetBSD version to resolve heap allocation bugs.
  • libmisc/shell/main_ls.c, libmisc/shell/print-ls.c: Fix printing size bugs.
  • libnetworking/rtems/mkrootfs.c: Fix byte order bug when creating the loopback interface address.
  • Property mode set to 100644
File size: 11.9 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      {
165        unsigned long long size = sp->st_blocks;
166        size *= sp->st_blksize;
167        if (size < 0x100000000ULL)
168          size = sp->st_size;
169                                (void)printf("%*llu ", dp->s_size, size);
170                }
171                if (f_accesstime)
172                        printtime(globals, sp->st_atime);
173                else if (f_statustime)
174                        printtime(globals, sp->st_ctime);
175                else
176                        printtime(globals, sp->st_mtime);
177                if (f_octal || f_octal_escape)
178                        (void)safe_print(globals, p->fts_name);
179                else if (f_nonprint)
180                        (void)printescaped(globals, p->fts_name);
181                else
182                        (void)printf("%s", p->fts_name);
183
184                if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
185                        (void)printtype(sp->st_mode);
186                if (S_ISLNK(sp->st_mode))
187                        printlink(globals, p);
188                (void)putchar('\n');
189        }
190}
191
192void
193printcol(rtems_shell_ls_globals* globals, DISPLAY *dp)
194{
195        static FTSENT **array;
196        static int lastentries = -1;
197        FTSENT *p;
198        int base, chcnt, col, colwidth, num;
199        int numcols, numrows, row;
200        //char szbuf[5];
201
202        colwidth = dp->maxlen;
203        if (f_inode)
204                colwidth += dp->s_inode + 1;
205        if (f_size) {
206                if (f_humanize)
207                        colwidth += dp->s_size + 1;
208                else
209                        colwidth += dp->s_block + 1;
210        }
211        if (f_type || f_typedir)
212                colwidth += 1;
213
214        colwidth += 1;
215
216        if (termwidth < 2 * colwidth) {
217                printscol(globals, dp);
218                return;
219        }
220
221        /*
222         * Have to do random access in the linked list -- build a table
223         * of pointers.
224         */
225        if (dp->entries > lastentries) {
226                lastentries = dp->entries;
227                if ((array =
228                    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
229                        warn(NULL);
230                        printscol(globals, dp);
231                }
232        }
233        for (p = dp->list, num = 0; p; p = p->fts_link)
234                if (p->fts_number != NO_PRINT)
235                        array[num++] = p;
236
237        numcols = termwidth / colwidth;
238        colwidth = termwidth / numcols;         /* spread out if possible */
239        numrows = num / numcols;
240        if (num % numcols)
241                ++numrows;
242
243        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
244#if RTEMS_REMOVED
245                if (f_humanize) {
246                        if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
247                            "", HN_AUTOSCALE,
248                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
249                                err(1, "humanize_number");
250                        (void)printf("total %s\n", szbuf);
251                } else {
252#endif
253                        (void)printf("total %llu\n",
254                            (long long)(howmany(dp->btotal, blocksize)));
255#if RTEMS_REMOVED
256                }
257#endif
258        }
259        for (row = 0; row < numrows; ++row) {
260                for (base = row, chcnt = col = 0; col < numcols; ++col) {
261                        chcnt = printaname(globals, array[base], dp->s_inode,
262                            f_humanize && 0 ? dp->s_size : dp->s_block);
263                        if ((base += numrows) >= num)
264                                break;
265                        while (chcnt++ < colwidth)
266                                (void)putchar(' ');
267                }
268                (void)putchar('\n');
269        }
270}
271
272void
273printacol(rtems_shell_ls_globals* globals, DISPLAY *dp)
274{
275        FTSENT *p;
276        int chcnt, col, colwidth;
277        int numcols;
278        //char szbuf[5];
279
280        colwidth = dp->maxlen;
281        if (f_inode)
282                colwidth += dp->s_inode + 1;
283        if (f_size) {
284                if (f_humanize)
285                        colwidth += dp->s_size + 1;
286                else
287                        colwidth += dp->s_block + 1;
288        }
289        if (f_type || f_typedir)
290                colwidth += 1;
291
292        colwidth += 1;
293
294        if (termwidth < 2 * colwidth) {
295                printscol(globals, dp);
296                return;
297        }
298
299        numcols = termwidth / colwidth;
300        colwidth = termwidth / numcols;         /* spread out if possible */
301
302        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size)) {
303#if RTEMS_REMOVED
304                if (f_humanize) {
305                        if ((humanize_number(szbuf, sizeof(szbuf), dp->stotal,
306                            "", HN_AUTOSCALE,
307                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
308                                err(1, "humanize_number");
309                        (void)printf("total %s\n", szbuf);
310                } else {
311#endif
312                        (void)printf("total %llu\n",
313                            (long long)(howmany(dp->btotal, blocksize)));
314#if RTEMS_REMOVED
315                }
316#endif
317        }
318        chcnt = col = 0;
319        for (p = dp->list; p; p = p->fts_link) {
320                if (IS_NOPRINT(p))
321                        continue;
322                if (col >= numcols) {
323                        chcnt = col = 0;
324                        (void)putchar('\n');
325                }
326                chcnt = printaname(globals, p, dp->s_inode,
327                    f_humanize && 0 ? dp->s_size : dp->s_block);
328                while (chcnt++ < colwidth)
329                        (void)putchar(' ');
330                col++;
331        }
332        (void)putchar('\n');
333}
334
335void
336printstream(rtems_shell_ls_globals* globals, DISPLAY *dp)
337{
338        FTSENT *p;
339        int col;
340        int extwidth;
341
342        extwidth = 0;
343        if (f_inode)
344                extwidth += dp->s_inode + 1;
345        if (f_size) {
346                if (f_humanize)
347                        extwidth += dp->s_size + 1;
348                else
349                        extwidth += dp->s_block + 1;
350        }
351        if (f_type)
352                extwidth += 1;
353
354        for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
355                if (IS_NOPRINT(p))
356                        continue;
357                if (col > 0) {
358                        (void)putchar(','), col++;
359                        if (col + 1 + extwidth + p->fts_namelen >= termwidth)
360                                (void)putchar('\n'), col = 0;
361                        else
362                                (void)putchar(' '), col++;
363                }
364                col += printaname(globals, p, dp->s_inode,
365                    f_humanize && 0 ? dp->s_size : dp->s_block);
366        }
367        (void)putchar('\n');
368}
369
370/*
371 * print [inode] [size] name
372 * return # of characters printed, no trailing characters.
373 */
374static int
375printaname(rtems_shell_ls_globals* globals,
376           FTSENT *p, int inodefield, int sizefield)
377{
378        struct stat *sp;
379        int chcnt;
380        //char szbuf[5];
381
382        sp = p->fts_statp;
383        chcnt = 0;
384        if (f_inode)
385                chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
386        if (f_size) {
387#if RTEMS_REMOVED
388                if (f_humanize) {
389                        if ((humanize_number(szbuf, sizeof(szbuf), sp->st_size,
390                            "", HN_AUTOSCALE,
391                            (HN_DECIMAL | HN_B | HN_NOSPACE))) == -1)
392                                err(1, "humanize_number");
393                        chcnt += printf("%*s ", sizefield, szbuf);
394                } else {
395#endif
396                        chcnt += printf("%*llu ", sizefield,
397                            (long long)howmany(sp->st_blocks, blocksize));
398#if RTEMS_REMOVED
399                }
400#endif
401        }
402        if (f_octal || f_octal_escape)
403                chcnt += safe_print(globals, p->fts_name);
404        else if (f_nonprint)
405                chcnt += printescaped(globals, p->fts_name);
406        else
407                chcnt += printf("%s", p->fts_name);
408        if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
409                chcnt += printtype(sp->st_mode);
410        return (chcnt);
411}
412
413static void
414printtime(rtems_shell_ls_globals* globals, time_t ftime)
415{
416        int i;
417        char *longstring;
418
419        longstring = ctime(&ftime);
420        for (i = 4; i < 11; ++i)
421                (void)putchar(longstring[i]);
422
423#define SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
424        if (f_sectime)
425                for (i = 11; i < 24; i++)
426                        (void)putchar(longstring[i]);
427        else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
428                for (i = 11; i < 16; ++i)
429                        (void)putchar(longstring[i]);
430        else {
431                (void)putchar(' ');
432                for (i = 20; i < 24; ++i)
433                        (void)putchar(longstring[i]);
434        }
435        (void)putchar(' ');
436}
437
438static int
439printtype(u_int mode)
440{
441        switch (mode & S_IFMT) {
442        case S_IFDIR:
443                (void)putchar('/');
444                return (1);
445        case S_IFIFO:
446                (void)putchar('|');
447                return (1);
448        case S_IFLNK:
449                (void)putchar('@');
450                return (1);
451        case S_IFSOCK:
452                (void)putchar('=');
453                return (1);
454#if RTEMS_REMOVED
455        case S_IFWHT:
456                (void)putchar('%');
457                return (1);
458#endif
459        }
460        if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
461                (void)putchar('*');
462                return (1);
463        }
464        return (0);
465}
466
467static void
468printlink(rtems_shell_ls_globals* globals, FTSENT *p)
469{
470        int lnklen;
471        char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
472
473        if (p->fts_level == FTS_ROOTLEVEL)
474                (void)snprintf(name, sizeof(name), "%s", p->fts_name);
475        else
476                (void)snprintf(name, sizeof(name),
477                    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
478        if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
479                (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
480                return;
481        }
482        path[lnklen] = '\0';
483        (void)printf(" -> ");
484        if (f_octal || f_octal_escape)
485                (void)safe_print(globals, path);
486        else if (f_nonprint)
487                (void)printescaped(globals, path);
488        else
489                (void)printf("%s", path);
490}
Note: See TracBrowser for help on using the repository browser.