print.c revision 9991
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 *
369991Sache *	$Id: print.c,v 1.5 1995/08/07 19:17:35 wollman Exp $
371556Srgrimes */
381556Srgrimes
391556Srgrimes#ifndef lint
401556Srgrimesstatic char sccsid[] = "@(#)print.c	8.4 (Berkeley) 4/17/94";
411556Srgrimes#endif /* not lint */
421556Srgrimes
431556Srgrimes#include <sys/param.h>
441556Srgrimes#include <sys/stat.h>
451556Srgrimes
461556Srgrimes#include <err.h>
471556Srgrimes#include <errno.h>
481556Srgrimes#include <fts.h>
491556Srgrimes#include <grp.h>
501556Srgrimes#include <pwd.h>
511556Srgrimes#include <stdio.h>
521556Srgrimes#include <stdlib.h>
531556Srgrimes#include <string.h>
541556Srgrimes#include <time.h>
551556Srgrimes#include <unistd.h>
561556Srgrimes#include <utmp.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)
991556Srgrimes			(void)printf("%*lu ", dp->s_inode, 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))
1111556Srgrimes			(void)printf("%3d, %3d ",
1121556Srgrimes			    major(sp->st_rdev), minor(sp->st_rdev));
1131556Srgrimes		else if (dp->bcfile)
1141556Srgrimes			(void)printf("%*s%*qd ",
1151556Srgrimes			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1161556Srgrimes		else
1171556Srgrimes			(void)printf("%*qd ", dp->s_size, sp->st_size);
1181556Srgrimes		if (f_accesstime)
1191556Srgrimes			printtime(sp->st_atime);
1201556Srgrimes		else if (f_statustime)
1211556Srgrimes			printtime(sp->st_ctime);
1221556Srgrimes		else
1231556Srgrimes			printtime(sp->st_mtime);
1241556Srgrimes		(void)printf("%s", p->fts_name);
1251556Srgrimes		if (f_type)
1261556Srgrimes			(void)printtype(sp->st_mode);
1271556Srgrimes		if (S_ISLNK(sp->st_mode))
1281556Srgrimes			printlink(p);
1291556Srgrimes		(void)putchar('\n');
1301556Srgrimes	}
1311556Srgrimes}
1321556Srgrimes
1331556Srgrimes#define	TAB	8
1341556Srgrimes
1351556Srgrimesvoid
1361556Srgrimesprintcol(dp)
1371556Srgrimes	DISPLAY *dp;
1381556Srgrimes{
1391556Srgrimes	extern int termwidth;
1401556Srgrimes	static FTSENT **array;
1411556Srgrimes	static int lastentries = -1;
1421556Srgrimes	FTSENT *p;
1431556Srgrimes	int base, chcnt, cnt, col, colwidth, num;
1441556Srgrimes	int endcol, numcols, numrows, row;
1451556Srgrimes
1461556Srgrimes	/*
1471556Srgrimes	 * Have to do random access in the linked list -- build a table
1481556Srgrimes	 * of pointers.
1491556Srgrimes	 */
1501556Srgrimes	if (dp->entries > lastentries) {
1511556Srgrimes		lastentries = dp->entries;
1521556Srgrimes		if ((array =
1531556Srgrimes		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
1541556Srgrimes			warn(NULL);
1551556Srgrimes			printscol(dp);
1561556Srgrimes		}
1571556Srgrimes	}
1581556Srgrimes	for (p = dp->list, num = 0; p; p = p->fts_link)
1591556Srgrimes		if (p->fts_number != NO_PRINT)
1601556Srgrimes			array[num++] = p;
1611556Srgrimes
1621556Srgrimes	colwidth = dp->maxlen;
1631556Srgrimes	if (f_inode)
1641556Srgrimes		colwidth += dp->s_inode + 1;
1651556Srgrimes	if (f_size)
1661556Srgrimes		colwidth += dp->s_block + 1;
1671556Srgrimes	if (f_type)
1681556Srgrimes		colwidth += 1;
1691556Srgrimes
1701556Srgrimes	colwidth = (colwidth + TAB) & ~(TAB - 1);
1711556Srgrimes	if (termwidth < 2 * colwidth) {
1721556Srgrimes		printscol(dp);
1731556Srgrimes		return;
1741556Srgrimes	}
1751556Srgrimes
1761556Srgrimes	numcols = termwidth / colwidth;
1771556Srgrimes	numrows = num / numcols;
1781556Srgrimes	if (num % numcols)
1791556Srgrimes		++numrows;
1801556Srgrimes
1811556Srgrimes	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1821556Srgrimes		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1831556Srgrimes	for (row = 0; row < numrows; ++row) {
1841556Srgrimes		endcol = colwidth;
1851556Srgrimes		for (base = row, chcnt = col = 0; col < numcols; ++col) {
1861556Srgrimes			chcnt += printaname(array[base], dp->s_inode,
1871556Srgrimes			    dp->s_block);
1881556Srgrimes			if ((base += numrows) >= num)
1891556Srgrimes				break;
1907165Sjoerg			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol){
1911556Srgrimes				(void)putchar('\t');
1921556Srgrimes				chcnt = cnt;
1931556Srgrimes			}
1941556Srgrimes			endcol += colwidth;
1951556Srgrimes		}
1961556Srgrimes		(void)putchar('\n');
1971556Srgrimes	}
1981556Srgrimes}
1991556Srgrimes
2001556Srgrimes/*
2011556Srgrimes * print [inode] [size] name
2021556Srgrimes * return # of characters printed, no trailing characters.
2031556Srgrimes */
2041556Srgrimesstatic int
2051556Srgrimesprintaname(p, inodefield, sizefield)
2061556Srgrimes	FTSENT *p;
2071556Srgrimes	u_long sizefield, inodefield;
2081556Srgrimes{
2091556Srgrimes	struct stat *sp;
2101556Srgrimes	int chcnt;
2111556Srgrimes
2121556Srgrimes	sp = p->fts_statp;
2131556Srgrimes	chcnt = 0;
2141556Srgrimes	if (f_inode)
2151556Srgrimes		chcnt += printf("%*lu ", (int)inodefield, sp->st_ino);
2161556Srgrimes	if (f_size)
2171556Srgrimes		chcnt += printf("%*qd ",
2181556Srgrimes		    (int)sizefield, howmany(sp->st_blocks, blocksize));
2191556Srgrimes	chcnt += printf("%s", p->fts_name);
2201556Srgrimes	if (f_type)
2211556Srgrimes		chcnt += printtype(sp->st_mode);
2221556Srgrimes	return (chcnt);
2231556Srgrimes}
2241556Srgrimes
2251556Srgrimesstatic void
2261556Srgrimesprinttime(ftime)
2271556Srgrimes	time_t ftime;
2281556Srgrimes{
2291556Srgrimes	int i;
2309991Sache	char longstring[80];
2311556Srgrimes
2329991Sache	strftime(longstring, sizeof(longstring), "%c", localtime(&ftime));
2331556Srgrimes	for (i = 4; i < 11; ++i)
2341556Srgrimes		(void)putchar(longstring[i]);
2351556Srgrimes
2369987Swollman#define	SIXMONTHS	((365 / 2) * 86400)
2371556Srgrimes	if (f_sectime)
2381556Srgrimes		for (i = 11; i < 24; i++)
2391556Srgrimes			(void)putchar(longstring[i]);
2401556Srgrimes	else if (ftime + SIXMONTHS > time(NULL))
2411556Srgrimes		for (i = 11; i < 16; ++i)
2421556Srgrimes			(void)putchar(longstring[i]);
2431556Srgrimes	else {
2441556Srgrimes		(void)putchar(' ');
2451556Srgrimes		for (i = 20; i < 24; ++i)
2461556Srgrimes			(void)putchar(longstring[i]);
2471556Srgrimes	}
2481556Srgrimes	(void)putchar(' ');
2491556Srgrimes}
2501556Srgrimes
2511556Srgrimesstatic int
2521556Srgrimesprinttype(mode)
2531556Srgrimes	u_int mode;
2541556Srgrimes{
2551556Srgrimes	switch (mode & S_IFMT) {
2561556Srgrimes	case S_IFDIR:
2571556Srgrimes		(void)putchar('/');
2581556Srgrimes		return (1);
2591556Srgrimes	case S_IFIFO:
2601556Srgrimes		(void)putchar('|');
2611556Srgrimes		return (1);
2621556Srgrimes	case S_IFLNK:
2631556Srgrimes		(void)putchar('@');
2641556Srgrimes		return (1);
2651556Srgrimes	case S_IFSOCK:
2661556Srgrimes		(void)putchar('=');
2671556Srgrimes		return (1);
2681556Srgrimes	}
2691556Srgrimes	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
2701556Srgrimes		(void)putchar('*');
2711556Srgrimes		return (1);
2721556Srgrimes	}
2731556Srgrimes	return (0);
2741556Srgrimes}
2751556Srgrimes
2761556Srgrimesstatic void
2771556Srgrimesprintlink(p)
2781556Srgrimes	FTSENT *p;
2791556Srgrimes{
2801556Srgrimes	int lnklen;
2811556Srgrimes	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
2821556Srgrimes
2831556Srgrimes	if (p->fts_level == FTS_ROOTLEVEL)
2841556Srgrimes		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
2858855Srgrimes	else
2861556Srgrimes		(void)snprintf(name, sizeof(name),
2871556Srgrimes		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
2881556Srgrimes	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
2891556Srgrimes		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
2901556Srgrimes		return;
2911556Srgrimes	}
2921556Srgrimes	path[lnklen] = '\0';
2931556Srgrimes	(void)printf(" -> %s", path);
2941556Srgrimes}
295