[BACK]Return to print.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / bin / ls

Annotation of src/bin/ls/print.c, Revision 1.27.2.1

1.27.2.1! minoura     1: /*     $NetBSD: print.c,v 1.28 2000/06/17 16:11:26 assar Exp $ */
1.13      cgd         2:
1.1       cgd         3: /*
1.11      mycroft     4:  * Copyright (c) 1989, 1993, 1994
                      5:  *     The Regents of the University of California.  All rights reserved.
1.1       cgd         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. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the University of
                     21:  *     California, Berkeley and its contributors.
                     22:  * 4. Neither the name of the University nor the names of its contributors
                     23:  *    may be used to endorse or promote products derived from this software
                     24:  *    without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     27:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     28:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     29:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     30:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     31:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     32:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     33:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     34:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     35:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     36:  * SUCH DAMAGE.
                     37:  */
                     38:
1.16      christos   39: #include <sys/cdefs.h>
1.1       cgd        40: #ifndef lint
1.13      cgd        41: #if 0
1.14      jtc        42: static char sccsid[] = "@(#)print.c    8.5 (Berkeley) 7/28/94";
1.13      cgd        43: #else
1.27.2.1! minoura    44: __RCSID("$NetBSD: print.c,v 1.28 2000/06/17 16:11:26 assar Exp $");
1.13      cgd        45: #endif
1.1       cgd        46: #endif /* not lint */
                     47:
                     48: #include <sys/param.h>
                     49: #include <sys/stat.h>
1.11      mycroft    50:
                     51: #include <err.h>
                     52: #include <errno.h>
1.5       mycroft    53: #include <fts.h>
1.1       cgd        54: #include <grp.h>
                     55: #include <pwd.h>
1.11      mycroft    56: #include <stdio.h>
1.5       mycroft    57: #include <stdlib.h>
                     58: #include <string.h>
1.11      mycroft    59: #include <time.h>
                     60: #include <tzfile.h>
                     61: #include <unistd.h>
                     62: #include <utmp.h>
                     63:
1.1       cgd        64: #include "ls.h"
1.5       mycroft    65: #include "extern.h"
1.1       cgd        66:
1.21      lukem      67: static int     printaname __P((FTSENT *, int, int));
1.5       mycroft    68: static void    printlink __P((FTSENT *));
                     69: static void    printtime __P((time_t));
                     70: static int     printtype __P((u_int));
                     71:
1.20      mycroft    72: static time_t  now;
                     73:
1.5       mycroft    74: #define        IS_NOPRINT(p)   ((p)->fts_number == NO_PRINT)
                     75:
                     76: void
                     77: printscol(dp)
                     78:        DISPLAY *dp;
1.1       cgd        79: {
1.11      mycroft    80:        FTSENT *p;
1.5       mycroft    81:
                     82:        for (p = dp->list; p; p = p->fts_link) {
                     83:                if (IS_NOPRINT(p))
                     84:                        continue;
                     85:                (void)printaname(p, dp->s_inode, dp->s_block);
1.1       cgd        86:                (void)putchar('\n');
                     87:        }
                     88: }
                     89:
1.5       mycroft    90: void
                     91: printlong(dp)
                     92:        DISPLAY *dp;
                     93: {
1.11      mycroft    94:        struct stat *sp;
                     95:        FTSENT *p;
1.5       mycroft    96:        NAMES *np;
                     97:        char buf[20];
                     98:
1.22      mycroft    99:        now = time(NULL);
1.20      mycroft   100:
1.5       mycroft   101:        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1.24      christos  102:                (void)printf("total %llu\n",
1.21      lukem     103:                    (long long)(howmany(dp->btotal, blocksize)));
1.5       mycroft   104:
                    105:        for (p = dp->list; p; p = p->fts_link) {
                    106:                if (IS_NOPRINT(p))
                    107:                        continue;
                    108:                sp = p->fts_statp;
1.1       cgd       109:                if (f_inode)
1.23      christos  110:                        (void)printf("%*lu ", dp->s_inode,
                    111:                            (unsigned long)sp->st_ino);
1.1       cgd       112:                if (f_size)
1.24      christos  113:                        (void)printf("%*llu ", dp->s_block,
1.17      thorpej   114:                            (long long)howmany(sp->st_blocks, blocksize));
1.5       mycroft   115:                (void)strmode(sp->st_mode, buf);
                    116:                np = p->fts_pointer;
1.23      christos  117:                (void)printf("%s %*lu %-*s  %-*s  ", buf, dp->s_nlink,
                    118:                    (unsigned long)sp->st_nlink, dp->s_user, np->user,
                    119:                    dp->s_group, np->group);
1.5       mycroft   120:                if (f_flags)
                    121:                        (void)printf("%-*s ", dp->s_flags, np->flags);
                    122:                if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
1.18      mycroft   123:                        (void)printf("%*u, %*u ",
                    124:                            dp->s_major, major(sp->st_rdev), dp->s_minor,
                    125:                            minor(sp->st_rdev));
1.1       cgd       126:                else
1.24      christos  127:                        (void)printf("%*llu ", dp->s_size,
1.17      thorpej   128:                            (long long)sp->st_size);
1.1       cgd       129:                if (f_accesstime)
1.5       mycroft   130:                        printtime(sp->st_atime);
1.1       cgd       131:                else if (f_statustime)
1.5       mycroft   132:                        printtime(sp->st_ctime);
1.1       cgd       133:                else
1.5       mycroft   134:                        printtime(sp->st_mtime);
1.27.2.1! minoura   135:                if (f_nonprint)
        !           136:                        printescaped(p->fts_name);
        !           137:                else
        !           138:                        (void)printf("%s", p->fts_name);
        !           139:
1.26      kleink    140:                if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
1.5       mycroft   141:                        (void)printtype(sp->st_mode);
                    142:                if (S_ISLNK(sp->st_mode))
                    143:                        printlink(p);
1.1       cgd       144:                (void)putchar('\n');
                    145:        }
                    146: }
                    147:
1.5       mycroft   148: void
                    149: printcol(dp)
                    150:        DISPLAY *dp;
1.1       cgd       151: {
                    152:        extern int termwidth;
1.5       mycroft   153:        static FTSENT **array;
                    154:        static int lastentries = -1;
1.11      mycroft   155:        FTSENT *p;
1.16      christos  156:        int base, chcnt, col, colwidth, num;
1.15      thorpej   157:        int numcols, numrows, row;
1.1       cgd       158:
1.19      lukem     159:        colwidth = dp->maxlen;
                    160:        if (f_inode)
                    161:                colwidth += dp->s_inode + 1;
                    162:        if (f_size)
                    163:                colwidth += dp->s_block + 1;
1.26      kleink    164:        if (f_type || f_typedir)
1.19      lukem     165:                colwidth += 1;
                    166:
                    167:        colwidth += 1;
                    168:
                    169:        if (termwidth < 2 * colwidth) {
                    170:                printscol(dp);
                    171:                return;
                    172:        }
                    173:
1.5       mycroft   174:        /*
                    175:         * Have to do random access in the linked list -- build a table
                    176:         * of pointers.
                    177:         */
                    178:        if (dp->entries > lastentries) {
                    179:                lastentries = dp->entries;
                    180:                if ((array =
                    181:                    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
1.27      drochner  182:                        warn(NULL);
1.5       mycroft   183:                        printscol(dp);
                    184:                }
                    185:        }
                    186:        for (p = dp->list, num = 0; p; p = p->fts_link)
                    187:                if (p->fts_number != NO_PRINT)
                    188:                        array[num++] = p;
                    189:
1.19      lukem     190:        numcols = termwidth / colwidth;
                    191:        colwidth = termwidth / numcols;         /* spread out if possible */
                    192:        numrows = num / numcols;
                    193:        if (num % numcols)
                    194:                ++numrows;
                    195:
                    196:        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1.24      christos  197:                (void)printf("total %llu\n",
1.21      lukem     198:                    (long long)(howmany(dp->btotal, blocksize)));
1.19      lukem     199:        for (row = 0; row < numrows; ++row) {
                    200:                for (base = row, chcnt = col = 0; col < numcols; ++col) {
                    201:                        chcnt = printaname(array[base], dp->s_inode,
                    202:                            dp->s_block);
                    203:                        if ((base += numrows) >= num)
                    204:                                break;
                    205:                        while (chcnt++ < colwidth)
1.22      mycroft   206:                                (void)putchar(' ');
1.19      lukem     207:                }
                    208:                (void)putchar('\n');
                    209:        }
                    210: }
                    211:
                    212: void
                    213: printacol(dp)
                    214:        DISPLAY *dp;
                    215: {
                    216:        extern int termwidth;
                    217:        FTSENT *p;
                    218:        int chcnt, col, colwidth;
                    219:        int numcols;
                    220:
1.5       mycroft   221:        colwidth = dp->maxlen;
1.1       cgd       222:        if (f_inode)
1.5       mycroft   223:                colwidth += dp->s_inode + 1;
1.1       cgd       224:        if (f_size)
1.5       mycroft   225:                colwidth += dp->s_block + 1;
1.26      kleink    226:        if (f_type || f_typedir)
1.1       cgd       227:                colwidth += 1;
                    228:
1.15      thorpej   229:        colwidth += 1;
                    230:
1.1       cgd       231:        if (termwidth < 2 * colwidth) {
1.5       mycroft   232:                printscol(dp);
1.1       cgd       233:                return;
                    234:        }
                    235:
                    236:        numcols = termwidth / colwidth;
1.15      thorpej   237:        colwidth = termwidth / numcols;         /* spread out if possible */
1.1       cgd       238:
1.5       mycroft   239:        if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1.24      christos  240:                (void)printf("total %llu\n",
1.21      lukem     241:                    (long long)(howmany(dp->btotal, blocksize)));
1.19      lukem     242:        chcnt = col = 0;
                    243:        for (p = dp->list; p; p = p->fts_link) {
                    244:                if (IS_NOPRINT(p))
                    245:                        continue;
                    246:                if (col >= numcols) {
                    247:                        chcnt = col = 0;
1.22      mycroft   248:                        (void)putchar('\n');
1.1       cgd       249:                }
1.19      lukem     250:                chcnt = printaname(p, dp->s_inode, dp->s_block);
                    251:                while (chcnt++ < colwidth)
1.22      mycroft   252:                        (void)putchar(' ');
1.19      lukem     253:                col++;
1.25      kleink    254:        }
                    255:        (void)putchar('\n');
                    256: }
                    257:
                    258: void
                    259: printstream(dp)
                    260:        DISPLAY *dp;
                    261: {
                    262:        extern int termwidth;
                    263:        FTSENT *p;
                    264:        int col;
                    265:        int extwidth;
                    266:
                    267:        extwidth = 0;
                    268:        if (f_inode)
                    269:                extwidth += dp->s_inode + 1;
                    270:        if (f_size)
                    271:                extwidth += dp->s_block + 1;
                    272:        if (f_type)
                    273:                extwidth += 1;
                    274:
                    275:        for (col = 0, p = dp->list; p != NULL; p = p->fts_link) {
                    276:                if (IS_NOPRINT(p))
                    277:                        continue;
                    278:                if (col > 0) {
                    279:                        (void)putchar(','), col++;
                    280:                        if (col + 1 + extwidth + p->fts_namelen >= termwidth)
                    281:                                (void)putchar('\n'), col = 0;
                    282:                        else
                    283:                                (void)putchar(' '), col++;
                    284:                }
                    285:                col += printaname(p, dp->s_inode, dp->s_block);
1.1       cgd       286:        }
1.22      mycroft   287:        (void)putchar('\n');
1.1       cgd       288: }
                    289:
                    290: /*
                    291:  * print [inode] [size] name
1.5       mycroft   292:  * return # of characters printed, no trailing characters.
1.1       cgd       293:  */
1.5       mycroft   294: static int
                    295: printaname(p, inodefield, sizefield)
1.11      mycroft   296:        FTSENT *p;
1.21      lukem     297:        int sizefield, inodefield;
1.1       cgd       298: {
1.5       mycroft   299:        struct stat *sp;
1.1       cgd       300:        int chcnt;
                    301:
1.5       mycroft   302:        sp = p->fts_statp;
1.1       cgd       303:        chcnt = 0;
                    304:        if (f_inode)
1.23      christos  305:                chcnt += printf("%*lu ", inodefield, (unsigned long)sp->st_ino);
1.1       cgd       306:        if (f_size)
1.24      christos  307:                chcnt += printf("%*llu ", sizefield,
1.17      thorpej   308:                    (long long)howmany(sp->st_blocks, blocksize));
1.5       mycroft   309:        chcnt += printf("%s", p->fts_name);
1.26      kleink    310:        if (f_type || (f_typedir && S_ISDIR(sp->st_mode)))
1.5       mycroft   311:                chcnt += printtype(sp->st_mode);
                    312:        return (chcnt);
1.1       cgd       313: }
                    314:
1.5       mycroft   315: static void
1.1       cgd       316: printtime(ftime)
                    317:        time_t ftime;
                    318: {
                    319:        int i;
1.5       mycroft   320:        char *longstring;
1.1       cgd       321:
1.5       mycroft   322:        longstring = ctime(&ftime);
1.1       cgd       323:        for (i = 4; i < 11; ++i)
                    324:                (void)putchar(longstring[i]);
                    325:
                    326: #define        SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
                    327:        if (f_sectime)
                    328:                for (i = 11; i < 24; i++)
                    329:                        (void)putchar(longstring[i]);
1.20      mycroft   330:        else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
1.1       cgd       331:                for (i = 11; i < 16; ++i)
                    332:                        (void)putchar(longstring[i]);
                    333:        else {
                    334:                (void)putchar(' ');
                    335:                for (i = 20; i < 24; ++i)
                    336:                        (void)putchar(longstring[i]);
                    337:        }
                    338:        (void)putchar(' ');
                    339: }
                    340:
1.5       mycroft   341: static int
1.1       cgd       342: printtype(mode)
1.5       mycroft   343:        u_int mode;
1.1       cgd       344: {
1.11      mycroft   345:        switch (mode & S_IFMT) {
1.1       cgd       346:        case S_IFDIR:
                    347:                (void)putchar('/');
1.5       mycroft   348:                return (1);
1.11      mycroft   349:        case S_IFIFO:
                    350:                (void)putchar('|');
                    351:                return (1);
1.1       cgd       352:        case S_IFLNK:
                    353:                (void)putchar('@');
1.5       mycroft   354:                return (1);
1.1       cgd       355:        case S_IFSOCK:
                    356:                (void)putchar('=');
1.12      mycroft   357:                return (1);
                    358:        case S_IFWHT:
                    359:                (void)putchar('%');
1.6       jtc       360:                return (1);
1.1       cgd       361:        }
                    362:        if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
                    363:                (void)putchar('*');
1.5       mycroft   364:                return (1);
1.1       cgd       365:        }
1.5       mycroft   366:        return (0);
1.1       cgd       367: }
                    368:
1.5       mycroft   369: static void
                    370: printlink(p)
                    371:        FTSENT *p;
1.1       cgd       372: {
                    373:        int lnklen;
1.5       mycroft   374:        char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
                    375:
                    376:        if (p->fts_level == FTS_ROOTLEVEL)
                    377:                (void)snprintf(name, sizeof(name), "%s", p->fts_name);
                    378:        else
1.11      mycroft   379:                (void)snprintf(name, sizeof(name),
                    380:                    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
                    381:        if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
1.1       cgd       382:                (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
                    383:                return;
                    384:        }
                    385:        path[lnklen] = '\0';
1.27.2.1! minoura   386:        (void)printf(" -> ");
        !           387:        if (f_nonprint)
        !           388:                printescaped(path);
        !           389:        else
        !           390:                (void)printf("%s", path);
1.1       cgd       391: }

CVSweb <webmaster@jp.NetBSD.org>