print.c revision 8855
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 *
368855Srgrimes *	$Id: print.c,v 1.3 1995/03/19 13:28:46 joerg 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 <tzfile.h>
561556Srgrimes#include <unistd.h>
571556Srgrimes#include <utmp.h>
581556Srgrimes
591556Srgrimes#include "ls.h"
601556Srgrimes#include "extern.h"
611556Srgrimes
621556Srgrimesstatic int	printaname __P((FTSENT *, u_long, u_long));
631556Srgrimesstatic void	printlink __P((FTSENT *));
641556Srgrimesstatic void	printtime __P((time_t));
651556Srgrimesstatic int	printtype __P((u_int));
661556Srgrimes
671556Srgrimes#define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
681556Srgrimes
691556Srgrimesvoid
701556Srgrimesprintscol(dp)
711556Srgrimes	DISPLAY *dp;
721556Srgrimes{
731556Srgrimes	FTSENT *p;
741556Srgrimes
751556Srgrimes	for (p = dp->list; p; p = p->fts_link) {
761556Srgrimes		if (IS_NOPRINT(p))
771556Srgrimes			continue;
781556Srgrimes		(void)printaname(p, dp->s_inode, dp->s_block);
791556Srgrimes		(void)putchar('\n');
801556Srgrimes	}
811556Srgrimes}
821556Srgrimes
831556Srgrimesvoid
841556Srgrimesprintlong(dp)
851556Srgrimes	DISPLAY *dp;
861556Srgrimes{
871556Srgrimes	struct stat *sp;
881556Srgrimes	FTSENT *p;
891556Srgrimes	NAMES *np;
901556Srgrimes	char buf[20];
911556Srgrimes
921556Srgrimes	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
931556Srgrimes		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
941556Srgrimes
951556Srgrimes	for (p = dp->list; p; p = p->fts_link) {
961556Srgrimes		if (IS_NOPRINT(p))
971556Srgrimes			continue;
981556Srgrimes		sp = p->fts_statp;
991556Srgrimes		if (f_inode)
1001556Srgrimes			(void)printf("%*lu ", dp->s_inode, sp->st_ino);
1011556Srgrimes		if (f_size)
1021556Srgrimes			(void)printf("%*qd ",
1031556Srgrimes			    dp->s_block, howmany(sp->st_blocks, blocksize));
1041556Srgrimes		(void)strmode(sp->st_mode, buf);
1051556Srgrimes		np = p->fts_pointer;
1061556Srgrimes		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
1071556Srgrimes		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
1081556Srgrimes		    np->group);
1091556Srgrimes		if (f_flags)
1101556Srgrimes			(void)printf("%-*s ", dp->s_flags, np->flags);
1111556Srgrimes		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
1121556Srgrimes			(void)printf("%3d, %3d ",
1131556Srgrimes			    major(sp->st_rdev), minor(sp->st_rdev));
1141556Srgrimes		else if (dp->bcfile)
1151556Srgrimes			(void)printf("%*s%*qd ",
1161556Srgrimes			    8 - dp->s_size, "", dp->s_size, sp->st_size);
1171556Srgrimes		else
1181556Srgrimes			(void)printf("%*qd ", dp->s_size, sp->st_size);
1191556Srgrimes		if (f_accesstime)
1201556Srgrimes			printtime(sp->st_atime);
1211556Srgrimes		else if (f_statustime)
1221556Srgrimes			printtime(sp->st_ctime);
1231556Srgrimes		else
1241556Srgrimes			printtime(sp->st_mtime);
1251556Srgrimes		(void)printf("%s", p->fts_name);
1261556Srgrimes		if (f_type)
1271556Srgrimes			(void)printtype(sp->st_mode);
1281556Srgrimes		if (S_ISLNK(sp->st_mode))
1291556Srgrimes			printlink(p);
1301556Srgrimes		(void)putchar('\n');
1311556Srgrimes	}
1321556Srgrimes}
1331556Srgrimes
1341556Srgrimes#define	TAB	8
1351556Srgrimes
1361556Srgrimesvoid
1371556Srgrimesprintcol(dp)
1381556Srgrimes	DISPLAY *dp;
1391556Srgrimes{
1401556Srgrimes	extern int termwidth;
1411556Srgrimes	static FTSENT **array;
1421556Srgrimes	static int lastentries = -1;
1431556Srgrimes	FTSENT *p;
1441556Srgrimes	int base, chcnt, cnt, col, colwidth, num;
1451556Srgrimes	int endcol, numcols, numrows, row;
1461556Srgrimes
1471556Srgrimes	/*
1481556Srgrimes	 * Have to do random access in the linked list -- build a table
1491556Srgrimes	 * of pointers.
1501556Srgrimes	 */
1511556Srgrimes	if (dp->entries > lastentries) {
1521556Srgrimes		lastentries = dp->entries;
1531556Srgrimes		if ((array =
1541556Srgrimes		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
1551556Srgrimes			warn(NULL);
1561556Srgrimes			printscol(dp);
1571556Srgrimes		}
1581556Srgrimes	}
1591556Srgrimes	for (p = dp->list, num = 0; p; p = p->fts_link)
1601556Srgrimes		if (p->fts_number != NO_PRINT)
1611556Srgrimes			array[num++] = p;
1621556Srgrimes
1631556Srgrimes	colwidth = dp->maxlen;
1641556Srgrimes	if (f_inode)
1651556Srgrimes		colwidth += dp->s_inode + 1;
1661556Srgrimes	if (f_size)
1671556Srgrimes		colwidth += dp->s_block + 1;
1681556Srgrimes	if (f_type)
1691556Srgrimes		colwidth += 1;
1701556Srgrimes
1711556Srgrimes	colwidth = (colwidth + TAB) & ~(TAB - 1);
1721556Srgrimes	if (termwidth < 2 * colwidth) {
1731556Srgrimes		printscol(dp);
1741556Srgrimes		return;
1751556Srgrimes	}
1761556Srgrimes
1771556Srgrimes	numcols = termwidth / colwidth;
1781556Srgrimes	numrows = num / numcols;
1791556Srgrimes	if (num % numcols)
1801556Srgrimes		++numrows;
1811556Srgrimes
1821556Srgrimes	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
1831556Srgrimes		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
1841556Srgrimes	for (row = 0; row < numrows; ++row) {
1851556Srgrimes		endcol = colwidth;
1861556Srgrimes		for (base = row, chcnt = col = 0; col < numcols; ++col) {
1871556Srgrimes			chcnt += printaname(array[base], dp->s_inode,
1881556Srgrimes			    dp->s_block);
1891556Srgrimes			if ((base += numrows) >= num)
1901556Srgrimes				break;
1917165Sjoerg			while ((cnt = ((chcnt + TAB) & ~(TAB - 1))) <= endcol){
1921556Srgrimes				(void)putchar('\t');
1931556Srgrimes				chcnt = cnt;
1941556Srgrimes			}
1951556Srgrimes			endcol += colwidth;
1961556Srgrimes		}
1971556Srgrimes		(void)putchar('\n');
1981556Srgrimes	}
1991556Srgrimes}
2001556Srgrimes
2011556Srgrimes/*
2021556Srgrimes * print [inode] [size] name
2031556Srgrimes * return # of characters printed, no trailing characters.
2041556Srgrimes */
2051556Srgrimesstatic int
2061556Srgrimesprintaname(p, inodefield, sizefield)
2071556Srgrimes	FTSENT *p;
2081556Srgrimes	u_long sizefield, inodefield;
2091556Srgrimes{
2101556Srgrimes	struct stat *sp;
2111556Srgrimes	int chcnt;
2121556Srgrimes
2131556Srgrimes	sp = p->fts_statp;
2141556Srgrimes	chcnt = 0;
2151556Srgrimes	if (f_inode)
2161556Srgrimes		chcnt += printf("%*lu ", (int)inodefield, sp->st_ino);
2171556Srgrimes	if (f_size)
2181556Srgrimes		chcnt += printf("%*qd ",
2191556Srgrimes		    (int)sizefield, howmany(sp->st_blocks, blocksize));
2201556Srgrimes	chcnt += printf("%s", p->fts_name);
2211556Srgrimes	if (f_type)
2221556Srgrimes		chcnt += printtype(sp->st_mode);
2231556Srgrimes	return (chcnt);
2241556Srgrimes}
2251556Srgrimes
2261556Srgrimesstatic void
2271556Srgrimesprinttime(ftime)
2281556Srgrimes	time_t ftime;
2291556Srgrimes{
2301556Srgrimes	int i;
2311556Srgrimes	char *longstring;
2321556Srgrimes
2331556Srgrimes	longstring = ctime(&ftime);
2341556Srgrimes	for (i = 4; i < 11; ++i)
2351556Srgrimes		(void)putchar(longstring[i]);
2361556Srgrimes
2371556Srgrimes#define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
2381556Srgrimes	if (f_sectime)
2391556Srgrimes		for (i = 11; i < 24; i++)
2401556Srgrimes			(void)putchar(longstring[i]);
2411556Srgrimes	else if (ftime + SIXMONTHS > time(NULL))
2421556Srgrimes		for (i = 11; i < 16; ++i)
2431556Srgrimes			(void)putchar(longstring[i]);
2441556Srgrimes	else {
2451556Srgrimes		(void)putchar(' ');
2461556Srgrimes		for (i = 20; i < 24; ++i)
2471556Srgrimes			(void)putchar(longstring[i]);
2481556Srgrimes	}
2491556Srgrimes	(void)putchar(' ');
2501556Srgrimes}
2511556Srgrimes
2521556Srgrimesstatic int
2531556Srgrimesprinttype(mode)
2541556Srgrimes	u_int mode;
2551556Srgrimes{
2561556Srgrimes	switch (mode & S_IFMT) {
2571556Srgrimes	case S_IFDIR:
2581556Srgrimes		(void)putchar('/');
2591556Srgrimes		return (1);
2601556Srgrimes	case S_IFIFO:
2611556Srgrimes		(void)putchar('|');
2621556Srgrimes		return (1);
2631556Srgrimes	case S_IFLNK:
2641556Srgrimes		(void)putchar('@');
2651556Srgrimes		return (1);
2661556Srgrimes	case S_IFSOCK:
2671556Srgrimes		(void)putchar('=');
2681556Srgrimes		return (1);
2691556Srgrimes	}
2701556Srgrimes	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
2711556Srgrimes		(void)putchar('*');
2721556Srgrimes		return (1);
2731556Srgrimes	}
2741556Srgrimes	return (0);
2751556Srgrimes}
2761556Srgrimes
2771556Srgrimesstatic void
2781556Srgrimesprintlink(p)
2791556Srgrimes	FTSENT *p;
2801556Srgrimes{
2811556Srgrimes	int lnklen;
2821556Srgrimes	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
2831556Srgrimes
2841556Srgrimes	if (p->fts_level == FTS_ROOTLEVEL)
2851556Srgrimes		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
2868855Srgrimes	else
2871556Srgrimes		(void)snprintf(name, sizeof(name),
2881556Srgrimes		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
2891556Srgrimes	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
2901556Srgrimes		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
2911556Srgrimes		return;
2921556Srgrimes	}
2931556Srgrimes	path[lnklen] = '\0';
2941556Srgrimes	(void)printf(" -> %s", path);
2951556Srgrimes}
296