print.c revision 27958
11556Srgrimes/*
21556Srgrimes * Copyright (c) 1989, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * This code is derived from software contributed to Berkeley by
61556Srgrimes * Michael Fischbein.
71556Srgrimes *
81556Srgrimes * Redistribution and use in source and binary forms, with or without
91556Srgrimes * modification, are permitted provided that the following conditions
101556Srgrimes * are met:
111556Srgrimes * 1. Redistributions of source code must retain the above copyright
121556Srgrimes *    notice, this list of conditions and the following disclaimer.
131556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
141556Srgrimes *    notice, this list of conditions and the following disclaimer in the
151556Srgrimes *    documentation and/or other materials provided with the distribution.
161556Srgrimes * 3. All advertising materials mentioning features or use of this software
171556Srgrimes *    must display the following acknowledgement:
181556Srgrimes *	This product includes software developed by the University of
191556Srgrimes *	California, Berkeley and its contributors.
201556Srgrimes * 4. Neither the name of the University nor the names of its contributors
211556Srgrimes *    may be used to endorse or promote products derived from this software
221556Srgrimes *    without specific prior written permission.
231556Srgrimes *
241556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
251556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
261556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
271556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
281556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
291556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
301556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
311556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
321556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
331556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
341556Srgrimes * SUCH DAMAGE.
353044Sdg *
3627958Ssteve *	from: @(#)print.c	8.4 (Berkeley) 4/17/94";
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
4027958Sstevestatic const char rcsid[] =
4127958Ssteve	"$Id: print.c,v 1.12 1997/02/22 14:04:01 peter Exp $";
421556Srgrimes#endif /* not lint */
431556Srgrimes
441556Srgrimes#include <sys/param.h>
451556Srgrimes#include <sys/stat.h>
461556Srgrimes
471556Srgrimes#include <err.h>
481556Srgrimes#include <errno.h>
491556Srgrimes#include <fts.h>
501556Srgrimes#include <grp.h>
511556Srgrimes#include <pwd.h>
521556Srgrimes#include <stdio.h>
531556Srgrimes#include <stdlib.h>
541556Srgrimes#include <string.h>
551556Srgrimes#include <time.h>
561556Srgrimes#include <unistd.h>
571556Srgrimes
581556Srgrimes#include "ls.h"
591556Srgrimes#include "extern.h"
601556Srgrimes
611556Srgrimesstatic int	printaname __P((FTSENT *, u_long, u_long));
621556Srgrimesstatic void	printlink __P((FTSENT *));
631556Srgrimesstatic void	printtime __P((time_t));
641556Srgrimesstatic int	printtype __P((u_int));
651556Srgrimes
661556Srgrimes#define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
671556Srgrimes
681556Srgrimesvoid
691556Srgrimesprintscol(dp)
701556Srgrimes	DISPLAY *dp;
711556Srgrimes{
721556Srgrimes	FTSENT *p;
731556Srgrimes
741556Srgrimes	for (p = dp->list; p; p = p->fts_link) {
751556Srgrimes		if (IS_NOPRINT(p))
761556Srgrimes			continue;
771556Srgrimes		(void)printaname(p, dp->s_inode, dp->s_block);
781556Srgrimes		(void)putchar('\n');
791556Srgrimes	}
801556Srgrimes}
811556Srgrimes
821556Srgrimesvoid
831556Srgrimesprintlong(dp)
841556Srgrimes	DISPLAY *dp;
851556Srgrimes{
861556Srgrimes	struct stat *sp;
871556Srgrimes	FTSENT *p;
881556Srgrimes	NAMES *np;
891556Srgrimes	char buf[20];
901556Srgrimes
911556Srgrimes	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
921556Srgrimes		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
931556Srgrimes
941556Srgrimes	for (p = dp->list; p; p = p->fts_link) {
951556Srgrimes		if (IS_NOPRINT(p))
961556Srgrimes			continue;
971556Srgrimes		sp = p->fts_statp;
981556Srgrimes		if (f_inode)
9920417Ssteve			(void)printf("%*lu ", dp->s_inode, (u_long)sp->st_ino);
1001556Srgrimes		if (f_size)
1011556Srgrimes			(void)printf("%*qd ",
1021556Srgrimes			    dp->s_block, howmany(sp->st_blocks, blocksize));
1031556Srgrimes		(void)strmode(sp->st_mode, buf);
1041556Srgrimes		np = p->fts_pointer;
1051556Srgrimes		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1061556Srgrimes		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1071556Srgrimes		    np->group);
1081556Srgrimes		if (f_flags)
1091556Srgrimes			(void)printf("%-*s ", dp->s_flags, np->flags);
1101556Srgrimes		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
11113120Sjoerg			if (minor(sp->st_rdev) > 255)
11213120Sjoerg				(void)printf("%3d, 0x%08x ",
11313120Sjoerg				    major(sp->st_rdev), minor(sp->st_rdev));
11413120Sjoerg			else
11513120Sjoerg				(void)printf("%3d, %3d ",
11613120Sjoerg				    major(sp->st_rdev), minor(sp->st_rdev));
1171556Srgrimes		else if (dp->bcfile)
1181556Srgrimes			(void)printf("%*s%*qd ",
1191556Srgrimes			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1201556Srgrimes		else
1211556Srgrimes			(void)printf("%*qd ", dp->s_size, sp->st_size);
1221556Srgrimes		if (f_accesstime)
1231556Srgrimes			printtime(sp->st_atime);
1241556Srgrimes		else if (f_statustime)
1251556Srgrimes			printtime(sp->st_ctime);
1261556Srgrimes		else
1271556Srgrimes			printtime(sp->st_mtime);
1281556Srgrimes		(void)printf("%s", p->fts_name);
1291556Srgrimes		if (f_type)
1301556Srgrimes			(void)printtype(sp->st_mode);
1311556Srgrimes		if (S_ISLNK(sp->st_mode))
1321556Srgrimes			printlink(p);
1331556Srgrimes		(void)putchar('\n');
1341556Srgrimes	}
1351556Srgrimes}
1361556Srgrimes
1371556Srgrimes#define	TAB	8
1381556Srgrimes
1391556Srgrimesvoid
1401556Srgrimesprintcol(dp)
1411556Srgrimes	DISPLAY *dp;
1421556Srgrimes{
1431556Srgrimes	extern int termwidth;
1441556Srgrimes	static FTSENT **array;
1451556Srgrimes	static int lastentries = -1;
1461556Srgrimes	FTSENT *p;
1471556Srgrimes	int base, chcnt, cnt, col, colwidth, num;
1481556Srgrimes	int endcol, numcols, numrows, row;
1491556Srgrimes
1501556Srgrimes	/*
1511556Srgrimes	 * Have to do random access in the linked list -- build a table
1521556Srgrimes	 * of pointers.
1531556Srgrimes	 */
1541556Srgrimes	if (dp->entries > lastentries) {
1551556Srgrimes		lastentries = dp->entries;
1561556Srgrimes		if ((array =
1571556Srgrimes		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
1581556Srgrimes			warn(NULL);
1591556Srgrimes			printscol(dp);
1601556Srgrimes		}
1611556Srgrimes	}
1621556Srgrimes	for (p = dp->list, num = 0; p; p = p->fts_link)
1631556Srgrimes		if (p->fts_number != NO_PRINT)
1641556Srgrimes			array[num++] = p;
1651556Srgrimes
1661556Srgrimes	colwidth = dp->maxlen;
1671556Srgrimes	if (f_inode)
1681556Srgrimes		colwidth += dp->s_inode + 1;
1691556Srgrimes	if (f_size)
1701556Srgrimes		colwidth += dp->s_block + 1;
1711556Srgrimes	if (f_type)
1721556Srgrimes		colwidth += 1;
1731556Srgrimes
1741556Srgrimes	colwidth = (colwidth + TAB) & ~(TAB - 1);
1751556Srgrimes	if (termwidth < 2 * colwidth) {
1761556Srgrimes		printscol(dp);
1771556Srgrimes		return;
1781556Srgrimes	}
1791556Srgrimes
1801556Srgrimes	numcols = termwidth / colwidth;
1811556Srgrimes	numrows = num / numcols;
1821556Srgrimes	if (num % numcols)
1831556Srgrimes		++numrows;
1841556Srgrimes
1851556Srgrimes	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1861556Srgrimes		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1871556Srgrimes	for (row = 0; row < numrows; ++row) {
1881556Srgrimes		endcol = colwidth;
1891556Srgrimes		for (base = row, chcnt = col = 0; col < numcols; ++col) {
1901556Srgrimes			chcnt += printaname(array[base], dp->s_inode,
1911556Srgrimes			    dp->s_block);
1921556Srgrimes			if ((base += numrows) >= num)
1931556Srgrimes				break;
1947165Sjoerg			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol){
1951556Srgrimes				(void)putchar('\t');
1961556Srgrimes				chcnt = cnt;
1971556Srgrimes			}
1981556Srgrimes			endcol += colwidth;
1991556Srgrimes		}
2001556Srgrimes		(void)putchar('\n');
2011556Srgrimes	}
2021556Srgrimes}
2031556Srgrimes
2041556Srgrimes/*
2051556Srgrimes * print [inode] [size] name
2061556Srgrimes * return # of characters printed, no trailing characters.
2071556Srgrimes */
2081556Srgrimesstatic int
2091556Srgrimesprintaname(p, inodefield, sizefield)
2101556Srgrimes	FTSENT *p;
2111556Srgrimes	u_long sizefield, inodefield;
2121556Srgrimes{
2131556Srgrimes	struct stat *sp;
2141556Srgrimes	int chcnt;
2151556Srgrimes
2161556Srgrimes	sp = p->fts_statp;
2171556Srgrimes	chcnt = 0;
2181556Srgrimes	if (f_inode)
21920417Ssteve		chcnt += printf("%*lu ", (int)inodefield, (u_long)sp->st_ino);
2201556Srgrimes	if (f_size)
2211556Srgrimes		chcnt += printf("%*qd ",
2221556Srgrimes		    (int)sizefield, howmany(sp->st_blocks, blocksize));
2231556Srgrimes	chcnt += printf("%s", p->fts_name);
2241556Srgrimes	if (f_type)
2251556Srgrimes		chcnt += printtype(sp->st_mode);
2261556Srgrimes	return (chcnt);
2271556Srgrimes}
2281556Srgrimes
2291556Srgrimesstatic void
2301556Srgrimesprinttime(ftime)
2311556Srgrimes	time_t ftime;
2321556Srgrimes{
2331556Srgrimes	int i;
2349991Sache	char longstring[80];
23521545Smpp	static time_t now;
2361556Srgrimes
23721545Smpp	if (now == 0)
23821545Smpp		now = time(NULL);
23921545Smpp
2409991Sache	strftime(longstring, sizeof(longstring), "%c", localtime(&ftime));
2411556Srgrimes	for (i = 4; i < 11; ++i)
2421556Srgrimes		(void)putchar(longstring[i]);
2431556Srgrimes
2449987Swollman#define	SIXMONTHS	((365 / 2) * 86400)
2451556Srgrimes	if (f_sectime)
2461556Srgrimes		for (i = 11; i < 24; i++)
2471556Srgrimes			(void)putchar(longstring[i]);
24821545Smpp	else if (ftime + SIXMONTHS > now && ftime < now + SIXMONTHS)
2491556Srgrimes		for (i = 11; i < 16; ++i)
2501556Srgrimes			(void)putchar(longstring[i]);
2511556Srgrimes	else {
2521556Srgrimes		(void)putchar(' ');
2531556Srgrimes		for (i = 20; i < 24; ++i)
2541556Srgrimes			(void)putchar(longstring[i]);
2551556Srgrimes	}
2561556Srgrimes	(void)putchar(' ');
2571556Srgrimes}
2581556Srgrimes
2591556Srgrimesstatic int
2601556Srgrimesprinttype(mode)
2611556Srgrimes	u_int mode;
2621556Srgrimes{
2631556Srgrimes	switch (mode & S_IFMT) {
2641556Srgrimes	case S_IFDIR:
2651556Srgrimes		(void)putchar('/');
2661556Srgrimes		return (1);
2671556Srgrimes	case S_IFIFO:
2681556Srgrimes		(void)putchar('|');
2691556Srgrimes		return (1);
2701556Srgrimes	case S_IFLNK:
2711556Srgrimes		(void)putchar('@');
2721556Srgrimes		return (1);
2731556Srgrimes	case S_IFSOCK:
2741556Srgrimes		(void)putchar('=');
2751556Srgrimes		return (1);
27620417Ssteve	case S_IFWHT:
27720417Ssteve		(void)putchar('%');
27820417Ssteve		return (1);
2791556Srgrimes	}
2801556Srgrimes	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
2811556Srgrimes		(void)putchar('*');
2821556Srgrimes		return (1);
2831556Srgrimes	}
2841556Srgrimes	return (0);
2851556Srgrimes}
2861556Srgrimes
2871556Srgrimesstatic void
2881556Srgrimesprintlink(p)
2891556Srgrimes	FTSENT *p;
2901556Srgrimes{
2911556Srgrimes	int lnklen;
2921556Srgrimes	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
2931556Srgrimes
2941556Srgrimes	if (p->fts_level == FTS_ROOTLEVEL)
2951556Srgrimes		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
2968855Srgrimes	else
2971556Srgrimes		(void)snprintf(name, sizeof(name),
2981556Srgrimes		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
2991556Srgrimes	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
3001556Srgrimes		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
3011556Srgrimes		return;
3021556Srgrimes	}
3031556Srgrimes	path[lnklen] = '\0';
3041556Srgrimes	(void)printf(" -> %s", path);
3051556Srgrimes}
306