Annotation of 43BSDReno/bin/ls/print.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * Copyright (c) 1989 The Regents of the University of California.
        !             3:  * All rights reserved.
        !             4:  *
        !             5:  * This code is derived from software contributed to Berkeley by
        !             6:  * Michael Fischbein.
        !             7:  *
        !             8:  * Redistribution and use in source and binary forms are permitted
        !             9:  * provided that: (1) source distributions retain this entire copyright
        !            10:  * notice and comment, and (2) distributions including binaries display
        !            11:  * the following acknowledgement:  ``This product includes software
        !            12:  * developed by the University of California, Berkeley and its contributors''
        !            13:  * in the documentation or other materials provided with the distribution
        !            14:  * and in all advertising materials mentioning features or use of this
        !            15:  * software. Neither the name of the University nor the names of its
        !            16:  * contributors may be used to endorse or promote products derived
        !            17:  * from this software without specific prior written permission.
        !            18:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
        !            19:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
        !            20:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
        !            21:  */
        !            22: 
        !            23: #ifndef lint
        !            24: static char sccsid[] = "@(#)print.c    5.22 (Berkeley) 5/10/90";
        !            25: #endif /* not lint */
        !            26: 
        !            27: #include <sys/param.h>
        !            28: #include <sys/stat.h>
        !            29: #include <stdio.h>
        !            30: #include <grp.h>
        !            31: #include <pwd.h>
        !            32: #include <utmp.h>
        !            33: #include <tzfile.h>
        !            34: #include "ls.h"
        !            35: 
        !            36: printscol(stats, num)
        !            37:        register LS *stats;
        !            38:        register int num;
        !            39: {
        !            40:        for (; num--; ++stats) {
        !            41:                (void)printaname(stats);
        !            42:                (void)putchar('\n');
        !            43:        }
        !            44: }
        !            45: 
        !            46: printlong(stats, num)
        !            47:        LS *stats;
        !            48:        register int num;
        !            49: {
        !            50:        extern int errno;
        !            51:        char modep[15], *user_from_uid(), *group_from_gid(), *strerror();
        !            52: 
        !            53:        if (f_total)
        !            54:                (void)printf("total %lu\n", f_kblocks ?
        !            55:                    howmany(stats[0].lstat.st_btotal, 2) :
        !            56:                    stats[0].lstat.st_btotal);
        !            57:        for (; num--; ++stats) {
        !            58:                if (f_inode)
        !            59:                        (void)printf("%6lu ", stats->lstat.st_ino);
        !            60:                if (f_size)
        !            61:                        (void)printf("%4ld ", f_kblocks ?
        !            62:                            howmany(stats->lstat.st_blocks, 2) :
        !            63:                            stats->lstat.st_blocks);
        !            64:                (void)strmode(stats->lstat.st_mode, modep);
        !            65:                (void)printf("%s %3u %-*s ", modep, stats->lstat.st_nlink,
        !            66:                    UT_NAMESIZE, user_from_uid(stats->lstat.st_uid, 0));
        !            67:                if (f_group)
        !            68:                        (void)printf("%-*s ", UT_NAMESIZE,
        !            69:                            group_from_gid(stats->lstat.st_gid, 0));
        !            70:                if (S_ISCHR(stats->lstat.st_mode) ||
        !            71:                    S_ISBLK(stats->lstat.st_mode))
        !            72:                        (void)printf("%3d, %3d ", major(stats->lstat.st_rdev),
        !            73:                            minor(stats->lstat.st_rdev));
        !            74:                else
        !            75:                        (void)printf("%8ld ", stats->lstat.st_size);
        !            76:                if (f_accesstime)
        !            77:                        printtime(stats->lstat.st_atime);
        !            78:                else if (f_statustime)
        !            79:                        printtime(stats->lstat.st_ctime);
        !            80:                else
        !            81:                        printtime(stats->lstat.st_mtime);
        !            82:                (void)printf("%s", stats->name);
        !            83:                if (f_type)
        !            84:                        (void)printtype(stats->lstat.st_mode);
        !            85:                if (S_ISLNK(stats->lstat.st_mode))
        !            86:                        printlink(stats->name);
        !            87:                (void)putchar('\n');
        !            88:        }
        !            89: }
        !            90: 
        !            91: #define        TAB     8
        !            92: 
        !            93: printcol(stats, num)
        !            94:        LS *stats;
        !            95:        int num;
        !            96: {
        !            97:        extern int termwidth;
        !            98:        register int base, chcnt, cnt, col, colwidth;
        !            99:        int endcol, numcols, numrows, row;
        !           100: 
        !           101:        colwidth = stats[0].lstat.st_maxlen;
        !           102:        if (f_inode)
        !           103:                colwidth += 6;
        !           104:        if (f_size)
        !           105:                colwidth += 5;
        !           106:        if (f_type)
        !           107:                colwidth += 1;
        !           108: 
        !           109:        colwidth = (colwidth + TAB) & ~(TAB - 1);
        !           110:        if (termwidth < 2 * colwidth) {
        !           111:                printscol(stats, num);
        !           112:                return;
        !           113:        }
        !           114: 
        !           115:        numcols = termwidth / colwidth;
        !           116:        numrows = num / numcols;
        !           117:        if (num % numcols)
        !           118:                ++numrows;
        !           119: 
        !           120:        if (f_size && f_total)
        !           121:                (void)printf("total %lu\n", f_kblocks ?
        !           122:                    howmany(stats[0].lstat.st_btotal, 2) :
        !           123:                    stats[0].lstat.st_btotal);
        !           124:        for (row = 0; row < numrows; ++row) {
        !           125:                endcol = colwidth;
        !           126:                for (base = row, chcnt = col = 0; col < numcols; ++col) {
        !           127:                        chcnt += printaname(stats + base);
        !           128:                        if ((base += numrows) >= num)
        !           129:                                break;
        !           130:                        while ((cnt = (chcnt + TAB & ~(TAB - 1))) <= endcol) {
        !           131:                                (void)putchar('\t');
        !           132:                                chcnt = cnt;
        !           133:                        }
        !           134:                        endcol += colwidth;
        !           135:                }
        !           136:                putchar('\n');
        !           137:        }
        !           138: }
        !           139: 
        !           140: /*
        !           141:  * print [inode] [size] name
        !           142:  * return # of characters printed, no trailing characters
        !           143:  */
        !           144: printaname(lp)
        !           145:        LS *lp;
        !           146: {
        !           147:        int chcnt;
        !           148: 
        !           149:        chcnt = 0;
        !           150:        if (f_inode)
        !           151:                chcnt += printf("%5lu ", lp->lstat.st_ino);
        !           152:        if (f_size)
        !           153:                chcnt += printf("%4ld ", f_kblocks ?
        !           154:                    howmany(lp->lstat.st_blocks, 2) : lp->lstat.st_blocks);
        !           155:        chcnt += printf("%s", lp->name);
        !           156:        if (f_type)
        !           157:                chcnt += printtype(lp->lstat.st_mode);
        !           158:        return(chcnt);
        !           159: }
        !           160: 
        !           161: printtime(ftime)
        !           162:        time_t ftime;
        !           163: {
        !           164:        int i;
        !           165:        char *longstring, *ctime();
        !           166:        time_t time();
        !           167: 
        !           168:        longstring = ctime((long *)&ftime);
        !           169:        for (i = 4; i < 11; ++i)
        !           170:                (void)putchar(longstring[i]);
        !           171: 
        !           172: #define        SIXMONTHS       ((DAYSPERNYEAR / 2) * SECSPERDAY)
        !           173:        if (ftime + SIXMONTHS > time((time_t *)NULL))
        !           174:                for (i = 11; i < 16; ++i)
        !           175:                        (void)putchar(longstring[i]);
        !           176:        else {
        !           177:                (void)putchar(' ');
        !           178:                for (i = 20; i < 24; ++i)
        !           179:                        (void)putchar(longstring[i]);
        !           180:        }
        !           181:        (void)putchar(' ');
        !           182: }
        !           183: 
        !           184: printtype(mode)
        !           185:        mode_t mode;
        !           186: {
        !           187:        switch(mode & S_IFMT) {
        !           188:        case S_IFDIR:
        !           189:                (void)putchar('/');
        !           190:                return(1);
        !           191:        case S_IFLNK:
        !           192:                (void)putchar('@');
        !           193:                return(1);
        !           194:        case S_IFSOCK:
        !           195:                (void)putchar('=');
        !           196:                return(1);
        !           197:        }
        !           198:        if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
        !           199:                (void)putchar('*');
        !           200:                return(1);
        !           201:        }
        !           202:        return(0);
        !           203: }
        !           204: 
        !           205: printlink(name)
        !           206:        char *name;
        !           207: {
        !           208:        int lnklen;
        !           209:        char path[MAXPATHLEN + 1], *strerror();
        !           210: 
        !           211:        if ((lnklen = readlink(name, path, MAXPATHLEN)) == -1) {
        !           212:                (void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
        !           213:                return;
        !           214:        }
        !           215:        path[lnklen] = '\0';
        !           216:        (void)printf(" -> %s", path);
        !           217: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.