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