Home | History | Annotate | Line # | Download | only in ls
print.c revision 1.15
      1 /*	$NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej 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. 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 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)print.c	8.5 (Berkeley) 7/28/94";
     42 #else
     43 static char rcsid[] = "$NetBSD: print.c,v 1.15 1996/12/11 03:25:39 thorpej Exp $";
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <sys/param.h>
     48 #include <sys/stat.h>
     49 
     50 #include <err.h>
     51 #include <errno.h>
     52 #include <fts.h>
     53 #include <grp.h>
     54 #include <pwd.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <time.h>
     59 #include <tzfile.h>
     60 #include <unistd.h>
     61 #include <utmp.h>
     62 
     63 #include "ls.h"
     64 #include "extern.h"
     65 
     66 static int	printaname __P((FTSENT *, u_long, u_long));
     67 static void	printlink __P((FTSENT *));
     68 static void	printtime __P((time_t));
     69 static int	printtype __P((u_int));
     70 
     71 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
     72 
     73 void
     74 printscol(dp)
     75 	DISPLAY *dp;
     76 {
     77 	FTSENT *p;
     78 
     79 	for (p = dp->list; p; p = p->fts_link) {
     80 		if (IS_NOPRINT(p))
     81 			continue;
     82 		(void)printaname(p, dp->s_inode, dp->s_block);
     83 		(void)putchar('\n');
     84 	}
     85 }
     86 
     87 void
     88 printlong(dp)
     89 	DISPLAY *dp;
     90 {
     91 	struct stat *sp;
     92 	FTSENT *p;
     93 	NAMES *np;
     94 	char buf[20];
     95 
     96 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
     97 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
     98 
     99 	for (p = dp->list; p; p = p->fts_link) {
    100 		if (IS_NOPRINT(p))
    101 			continue;
    102 		sp = p->fts_statp;
    103 		if (f_inode)
    104 			(void)printf("%*lu ", dp->s_inode, sp->st_ino);
    105 		if (f_size)
    106 			(void)printf("%*qd ",
    107 			    dp->s_block, howmany(sp->st_blocks, blocksize));
    108 		(void)strmode(sp->st_mode, buf);
    109 		np = p->fts_pointer;
    110 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
    111 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
    112 		    np->group);
    113 		if (f_flags)
    114 			(void)printf("%-*s ", dp->s_flags, np->flags);
    115 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
    116 			(void)printf("%3d, %3d ",
    117 			    major(sp->st_rdev), minor(sp->st_rdev));
    118 		else if (dp->bcfile)
    119 			(void)printf("%*s%*qd ",
    120 			    8 - dp->s_size, "", dp->s_size, sp->st_size);
    121 		else
    122 			(void)printf("%*qd ", dp->s_size, sp->st_size);
    123 		if (f_accesstime)
    124 			printtime(sp->st_atime);
    125 		else if (f_statustime)
    126 			printtime(sp->st_ctime);
    127 		else
    128 			printtime(sp->st_mtime);
    129 		(void)printf("%s", p->fts_name);
    130 		if (f_type)
    131 			(void)printtype(sp->st_mode);
    132 		if (S_ISLNK(sp->st_mode))
    133 			printlink(p);
    134 		(void)putchar('\n');
    135 	}
    136 }
    137 
    138 void
    139 printcol(dp)
    140 	DISPLAY *dp;
    141 {
    142 	extern int termwidth;
    143 	static FTSENT **array;
    144 	static int lastentries = -1;
    145 	FTSENT *p;
    146 	int base, chcnt, cnt, col, colwidth, num;
    147 	int numcols, numrows, row;
    148 
    149 	/*
    150 	 * Have to do random access in the linked list -- build a table
    151 	 * of pointers.
    152 	 */
    153 	if (dp->entries > lastentries) {
    154 		lastentries = dp->entries;
    155 		if ((array =
    156 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
    157 			warn(NULL);
    158 			printscol(dp);
    159 		}
    160 	}
    161 	for (p = dp->list, num = 0; p; p = p->fts_link)
    162 		if (p->fts_number != NO_PRINT)
    163 			array[num++] = p;
    164 
    165 	colwidth = dp->maxlen;
    166 	if (f_inode)
    167 		colwidth += dp->s_inode + 1;
    168 	if (f_size)
    169 		colwidth += dp->s_block + 1;
    170 	if (f_type)
    171 		colwidth += 1;
    172 
    173 	colwidth += 1;
    174 
    175 	if (termwidth < 2 * colwidth) {
    176 		printscol(dp);
    177 		return;
    178 	}
    179 
    180 	numcols = termwidth / colwidth;
    181 	colwidth = termwidth / numcols;		/* spread out if possible */
    182 	numrows = num / numcols;
    183 	if (num % numcols)
    184 		++numrows;
    185 
    186 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
    187 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
    188 	for (row = 0; row < numrows; ++row) {
    189 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
    190 			chcnt = printaname(array[base], dp->s_inode, dp->s_block);
    191 			if ((base += numrows) >= num)
    192 				break;
    193 			while (chcnt++ < colwidth)
    194 				putchar(' ');
    195 		}
    196 		(void)putchar('\n');
    197 	}
    198 }
    199 
    200 /*
    201  * print [inode] [size] name
    202  * return # of characters printed, no trailing characters.
    203  */
    204 static int
    205 printaname(p, inodefield, sizefield)
    206 	FTSENT *p;
    207 	u_long sizefield, inodefield;
    208 {
    209 	struct stat *sp;
    210 	int chcnt;
    211 
    212 	sp = p->fts_statp;
    213 	chcnt = 0;
    214 	if (f_inode)
    215 		chcnt += printf("%*lu ", (int)inodefield, sp->st_ino);
    216 	if (f_size)
    217 		chcnt += printf("%*qd ",
    218 		    (int)sizefield, howmany(sp->st_blocks, blocksize));
    219 	chcnt += printf("%s", p->fts_name);
    220 	if (f_type)
    221 		chcnt += printtype(sp->st_mode);
    222 	return (chcnt);
    223 }
    224 
    225 static void
    226 printtime(ftime)
    227 	time_t ftime;
    228 {
    229 	int i;
    230 	char *longstring;
    231 
    232 	longstring = ctime(&ftime);
    233 	for (i = 4; i < 11; ++i)
    234 		(void)putchar(longstring[i]);
    235 
    236 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
    237 	if (f_sectime)
    238 		for (i = 11; i < 24; i++)
    239 			(void)putchar(longstring[i]);
    240 	else if (ftime + SIXMONTHS > time(NULL))
    241 		for (i = 11; i < 16; ++i)
    242 			(void)putchar(longstring[i]);
    243 	else {
    244 		(void)putchar(' ');
    245 		for (i = 20; i < 24; ++i)
    246 			(void)putchar(longstring[i]);
    247 	}
    248 	(void)putchar(' ');
    249 }
    250 
    251 static int
    252 printtype(mode)
    253 	u_int mode;
    254 {
    255 	switch (mode & S_IFMT) {
    256 	case S_IFDIR:
    257 		(void)putchar('/');
    258 		return (1);
    259 	case S_IFIFO:
    260 		(void)putchar('|');
    261 		return (1);
    262 	case S_IFLNK:
    263 		(void)putchar('@');
    264 		return (1);
    265 	case S_IFSOCK:
    266 		(void)putchar('=');
    267 		return (1);
    268 	case S_IFWHT:
    269 		(void)putchar('%');
    270 		return (1);
    271 	}
    272 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
    273 		(void)putchar('*');
    274 		return (1);
    275 	}
    276 	return (0);
    277 }
    278 
    279 static void
    280 printlink(p)
    281 	FTSENT *p;
    282 {
    283 	int lnklen;
    284 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
    285 
    286 	if (p->fts_level == FTS_ROOTLEVEL)
    287 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
    288 	else
    289 		(void)snprintf(name, sizeof(name),
    290 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
    291 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
    292 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
    293 		return;
    294 	}
    295 	path[lnklen] = '\0';
    296 	(void)printf(" -> %s", path);
    297 }
    298