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