Home | History | Annotate | Line # | Download | only in ls
print.c revision 1.20
      1 /*	$NetBSD: print.c,v 1.20 1998/01/21 00:25:19 mycroft 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.20 1998/01/21 00:25:19 mycroft 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 static time_t	now;
     73 
     74 #define	IS_NOPRINT(p)	((p)->fts_number == NO_PRINT)
     75 
     76 void
     77 printscol(dp)
     78 	DISPLAY *dp;
     79 {
     80 	FTSENT *p;
     81 
     82 	for (p = dp->list; p; p = p->fts_link) {
     83 		if (IS_NOPRINT(p))
     84 			continue;
     85 		(void)printaname(p, dp->s_inode, dp->s_block);
     86 		(void)putchar('\n');
     87 	}
     88 }
     89 
     90 void
     91 printlong(dp)
     92 	DISPLAY *dp;
     93 {
     94 	struct stat *sp;
     95 	FTSENT *p;
     96 	NAMES *np;
     97 	char buf[20];
     98 
     99 	time(&now);
    100 
    101 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
    102 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
    103 
    104 	for (p = dp->list; p; p = p->fts_link) {
    105 		if (IS_NOPRINT(p))
    106 			continue;
    107 		sp = p->fts_statp;
    108 		if (f_inode)
    109 			(void)printf("%*u ", dp->s_inode, sp->st_ino);
    110 		if (f_size)
    111 			(void)printf("%*qd ",
    112 			    dp->s_block,
    113 			    (long long)howmany(sp->st_blocks, blocksize));
    114 		(void)strmode(sp->st_mode, buf);
    115 		np = p->fts_pointer;
    116 		(void)printf("%s %*u %-*s  %-*s  ", buf, dp->s_nlink,
    117 		    sp->st_nlink, dp->s_user, np->user, dp->s_group,
    118 		    np->group);
    119 		if (f_flags)
    120 			(void)printf("%-*s ", dp->s_flags, np->flags);
    121 		if (S_ISCHR(sp->st_mode) || S_ISBLK(sp->st_mode))
    122 			(void)printf("%*u, %*u ",
    123 			    dp->s_major, major(sp->st_rdev), dp->s_minor,
    124 			    minor(sp->st_rdev));
    125 		else
    126 			(void)printf("%*qd ", dp->s_size,
    127 			    (long long)sp->st_size);
    128 		if (f_accesstime)
    129 			printtime(sp->st_atime);
    130 		else if (f_statustime)
    131 			printtime(sp->st_ctime);
    132 		else
    133 			printtime(sp->st_mtime);
    134 		(void)printf("%s", p->fts_name);
    135 		if (f_type)
    136 			(void)printtype(sp->st_mode);
    137 		if (S_ISLNK(sp->st_mode))
    138 			printlink(p);
    139 		(void)putchar('\n');
    140 	}
    141 }
    142 
    143 void
    144 printcol(dp)
    145 	DISPLAY *dp;
    146 {
    147 	extern int termwidth;
    148 	static FTSENT **array;
    149 	static int lastentries = -1;
    150 	FTSENT *p;
    151 	int base, chcnt, col, colwidth, num;
    152 	int numcols, numrows, row;
    153 
    154 	colwidth = dp->maxlen;
    155 	if (f_inode)
    156 		colwidth += dp->s_inode + 1;
    157 	if (f_size)
    158 		colwidth += dp->s_block + 1;
    159 	if (f_type)
    160 		colwidth += 1;
    161 
    162 	colwidth += 1;
    163 
    164 	if (termwidth < 2 * colwidth) {
    165 		printscol(dp);
    166 		return;
    167 	}
    168 
    169 	/*
    170 	 * Have to do random access in the linked list -- build a table
    171 	 * of pointers.
    172 	 */
    173 	if (dp->entries > lastentries) {
    174 		lastentries = dp->entries;
    175 		if ((array =
    176 		    realloc(array, dp->entries * sizeof(FTSENT *))) == NULL) {
    177 			warn("%s", "");
    178 			printscol(dp);
    179 		}
    180 	}
    181 	for (p = dp->list, num = 0; p; p = p->fts_link)
    182 		if (p->fts_number != NO_PRINT)
    183 			array[num++] = p;
    184 
    185 	numcols = termwidth / colwidth;
    186 	colwidth = termwidth / numcols;		/* spread out if possible */
    187 	numrows = num / numcols;
    188 	if (num % numcols)
    189 		++numrows;
    190 
    191 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
    192 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
    193 	for (row = 0; row < numrows; ++row) {
    194 		for (base = row, chcnt = col = 0; col < numcols; ++col) {
    195 			chcnt = printaname(array[base], dp->s_inode,
    196 			    dp->s_block);
    197 			if ((base += numrows) >= num)
    198 				break;
    199 			while (chcnt++ < colwidth)
    200 				putchar(' ');
    201 		}
    202 		(void)putchar('\n');
    203 	}
    204 }
    205 
    206 void
    207 printacol(dp)
    208 	DISPLAY *dp;
    209 {
    210 	extern int termwidth;
    211 	FTSENT *p;
    212 	int chcnt, col, colwidth;
    213 	int numcols;
    214 
    215 	colwidth = dp->maxlen;
    216 	if (f_inode)
    217 		colwidth += dp->s_inode + 1;
    218 	if (f_size)
    219 		colwidth += dp->s_block + 1;
    220 	if (f_type)
    221 		colwidth += 1;
    222 
    223 	colwidth += 1;
    224 
    225 	if (termwidth < 2 * colwidth) {
    226 		printscol(dp);
    227 		return;
    228 	}
    229 
    230 	numcols = termwidth / colwidth;
    231 	colwidth = termwidth / numcols;		/* spread out if possible */
    232 
    233 	if (dp->list->fts_level != FTS_ROOTLEVEL && (f_longform || f_size))
    234 		(void)printf("total %lu\n", howmany(dp->btotal, blocksize));
    235 	chcnt = col = 0;
    236 	for (p = dp->list; p; p = p->fts_link) {
    237 		if (IS_NOPRINT(p))
    238 			continue;
    239 		if (col >= numcols) {
    240 			chcnt = col = 0;
    241 			putchar('\n');
    242 		}
    243 		chcnt = printaname(p, dp->s_inode, dp->s_block);
    244 		while (chcnt++ < colwidth)
    245 			putchar(' ');
    246 		col++;
    247 	}
    248 	putchar('\n');
    249 }
    250 
    251 /*
    252  * print [inode] [size] name
    253  * return # of characters printed, no trailing characters.
    254  */
    255 static int
    256 printaname(p, inodefield, sizefield)
    257 	FTSENT *p;
    258 	u_long sizefield, inodefield;
    259 {
    260 	struct stat *sp;
    261 	int chcnt;
    262 
    263 	sp = p->fts_statp;
    264 	chcnt = 0;
    265 	if (f_inode)
    266 		chcnt += printf("%*u ", (int)inodefield, sp->st_ino);
    267 	if (f_size)
    268 		chcnt += printf("%*qd ",
    269 		    (int)sizefield,
    270 		    (long long)howmany(sp->st_blocks, blocksize));
    271 	chcnt += printf("%s", p->fts_name);
    272 	if (f_type)
    273 		chcnt += printtype(sp->st_mode);
    274 	return (chcnt);
    275 }
    276 
    277 static void
    278 printtime(ftime)
    279 	time_t ftime;
    280 {
    281 	int i;
    282 	char *longstring;
    283 
    284 	longstring = ctime(&ftime);
    285 	for (i = 4; i < 11; ++i)
    286 		(void)putchar(longstring[i]);
    287 
    288 #define	SIXMONTHS	((DAYSPERNYEAR / 2) * SECSPERDAY)
    289 	if (f_sectime)
    290 		for (i = 11; i < 24; i++)
    291 			(void)putchar(longstring[i]);
    292 	else if (ftime + SIXMONTHS > now && ftime - SIXMONTHS < now)
    293 		for (i = 11; i < 16; ++i)
    294 			(void)putchar(longstring[i]);
    295 	else {
    296 		(void)putchar(' ');
    297 		for (i = 20; i < 24; ++i)
    298 			(void)putchar(longstring[i]);
    299 	}
    300 	(void)putchar(' ');
    301 }
    302 
    303 static int
    304 printtype(mode)
    305 	u_int mode;
    306 {
    307 	switch (mode & S_IFMT) {
    308 	case S_IFDIR:
    309 		(void)putchar('/');
    310 		return (1);
    311 	case S_IFIFO:
    312 		(void)putchar('|');
    313 		return (1);
    314 	case S_IFLNK:
    315 		(void)putchar('@');
    316 		return (1);
    317 	case S_IFSOCK:
    318 		(void)putchar('=');
    319 		return (1);
    320 	case S_IFWHT:
    321 		(void)putchar('%');
    322 		return (1);
    323 	}
    324 	if (mode & (S_IXUSR | S_IXGRP | S_IXOTH)) {
    325 		(void)putchar('*');
    326 		return (1);
    327 	}
    328 	return (0);
    329 }
    330 
    331 static void
    332 printlink(p)
    333 	FTSENT *p;
    334 {
    335 	int lnklen;
    336 	char name[MAXPATHLEN + 1], path[MAXPATHLEN + 1];
    337 
    338 	if (p->fts_level == FTS_ROOTLEVEL)
    339 		(void)snprintf(name, sizeof(name), "%s", p->fts_name);
    340 	else
    341 		(void)snprintf(name, sizeof(name),
    342 		    "%s/%s", p->fts_parent->fts_accpath, p->fts_name);
    343 	if ((lnklen = readlink(name, path, sizeof(path) - 1)) == -1) {
    344 		(void)fprintf(stderr, "\nls: %s: %s\n", name, strerror(errno));
    345 		return;
    346 	}
    347 	path[lnklen] = '\0';
    348 	(void)printf(" -> %s", path);
    349 }
    350