xref: /onnv-gate/usr/src/cmd/ls/ls.c (revision 1447:4b37e9d491a0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*1447Sakaplan  * Common Development and Distribution License (the "License").
6*1447Sakaplan  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
221420Smarks  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
300Sstevel@tonic-gate /*	  All Rights Reserved	*/
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate /*
350Sstevel@tonic-gate  * List files or directories
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <sys/mkdev.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/acl.h>
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #include <wchar.h>
450Sstevel@tonic-gate #include <stdio.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <dirent.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <locale.h>
500Sstevel@tonic-gate #include <curses.h>
510Sstevel@tonic-gate #include <termios.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <widec.h>
540Sstevel@tonic-gate #include <locale.h>
550Sstevel@tonic-gate #include <wctype.h>
560Sstevel@tonic-gate #include <pwd.h>
570Sstevel@tonic-gate #include <grp.h>
580Sstevel@tonic-gate #include <limits.h>
590Sstevel@tonic-gate #include <fcntl.h>
600Sstevel@tonic-gate #include <unistd.h>
610Sstevel@tonic-gate #include <libgen.h>
620Sstevel@tonic-gate #include <errno.h>
63789Sahrens #include <aclutils.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #ifndef STANDALONE
660Sstevel@tonic-gate #define	TERMINFO
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * -DNOTERMINFO can be defined on the cc command line to prevent
710Sstevel@tonic-gate  * the use of terminfo.  This should be done on systems not having
720Sstevel@tonic-gate  * the terminfo feature(pre 6.0 sytems ?).
730Sstevel@tonic-gate  * As a result, columnar listings assume 80 columns for output,
740Sstevel@tonic-gate  * unless told otherwise via the COLUMNS environment variable.
750Sstevel@tonic-gate  */
760Sstevel@tonic-gate #ifdef NOTERMINFO
770Sstevel@tonic-gate #undef TERMINFO
780Sstevel@tonic-gate #endif
790Sstevel@tonic-gate 
800Sstevel@tonic-gate #include <term.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate #define	BFSIZE	16
830Sstevel@tonic-gate /* this bit equals 1 in lflags of structure lbuf if *namep is to be used */
840Sstevel@tonic-gate #define	ISARG	0100000
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * this flag has been added to manipulate the display of S instead of 'l' when
880Sstevel@tonic-gate  * the file is not a regular file and when group execution bit is off
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate #define	LS_NOTREG	010000
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 
930Sstevel@tonic-gate /*
940Sstevel@tonic-gate  * Date and time formats
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * b --- abbreviated month name
970Sstevel@tonic-gate  * e --- day number
980Sstevel@tonic-gate  * Y --- year in the form ccyy
990Sstevel@tonic-gate  * H --- hour(24-hour version)
1000Sstevel@tonic-gate  * M --- minute
1010Sstevel@tonic-gate  * F --- yyyy-mm-dd
1020Sstevel@tonic-gate  * T --- hh:mm:ss
1030Sstevel@tonic-gate  * z --- time zone as hours displacement from UTC
1040Sstevel@tonic-gate  * note that %F and %z are from the ISO C99 standard and are
1050Sstevel@tonic-gate  * not present in older C libraries
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate #define	FORMAT1	 " %b %e  %Y "
1080Sstevel@tonic-gate #define	FORMAT2  " %b %e %H:%M "
1090Sstevel@tonic-gate #define	FORMAT3  " %b %e %T %Y "
1100Sstevel@tonic-gate #define	FORMAT4  " %%F %%T.%.09ld %%z "
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate #undef BUFSIZ
1130Sstevel@tonic-gate #define	BUFSIZ 4096
1140Sstevel@tonic-gate #define	NUMBER_WIDTH 40
1150Sstevel@tonic-gate #define	FMTSIZE 50
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate struct ditem {
1180Sstevel@tonic-gate 	dev_t	dev;			/* directory items device number */
1190Sstevel@tonic-gate 	ino_t	ino;			/* directory items inode number */
1200Sstevel@tonic-gate 	struct ditem *parent;		/* dir items ptr to its parent's info */
1210Sstevel@tonic-gate };
1220Sstevel@tonic-gate 
1230Sstevel@tonic-gate struct	lbuf	{
1240Sstevel@tonic-gate 	union	{
1250Sstevel@tonic-gate 		char	lname[MAXNAMLEN]; /* used for filename in a directory */
1260Sstevel@tonic-gate 		char	*namep;		/* for name in ls-command; */
1270Sstevel@tonic-gate 	} ln;
1280Sstevel@tonic-gate 	char	ltype;		/* filetype */
1290Sstevel@tonic-gate 	ino_t	lnum;		/* inode number of file */
1300Sstevel@tonic-gate 	mode_t	lflags; 	/* 0777 bits used as r,w,x permissions */
1310Sstevel@tonic-gate 	nlink_t	lnl;		/* number of links to file */
1320Sstevel@tonic-gate 	uid_t	luid;
1330Sstevel@tonic-gate 	gid_t	lgid;
1340Sstevel@tonic-gate 	off_t	lsize;		/* filesize or major/minor dev numbers */
1350Sstevel@tonic-gate 	blkcnt_t	lblocks;	/* number of file blocks */
1360Sstevel@tonic-gate 	timestruc_t	lmtime;
1370Sstevel@tonic-gate 	char	*flinkto;	/* symbolic link contents */
1380Sstevel@tonic-gate 	char 	acl;		/* indicate there are additional acl entries */
1390Sstevel@tonic-gate 	int	cycle;		/* cycle detected flag */
1400Sstevel@tonic-gate 	struct ditem *ancinfo;	/* maintains ancestor info */
141789Sahrens 	acl_t *aclp;		/* ACL if present */
1420Sstevel@tonic-gate };
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate struct dchain {
1450Sstevel@tonic-gate 	char *dc_name;		/* path name */
1460Sstevel@tonic-gate 	int cycle_detected;	/* cycle detected visiting this directory */
1470Sstevel@tonic-gate 	struct ditem *myancinfo;	/* this directory's ancestry info */
1480Sstevel@tonic-gate 	struct dchain *dc_next;	/* next directory in the chain */
1490Sstevel@tonic-gate };
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*
1520Sstevel@tonic-gate  * A numbuf_t is used when converting a number to a string representation
1530Sstevel@tonic-gate  */
1540Sstevel@tonic-gate typedef char numbuf_t[NUMBER_WIDTH];
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate static struct dchain *dfirst;	/* start of the dir chain */
1570Sstevel@tonic-gate static struct dchain *cdfirst;	/* start of the current dir chain */
1580Sstevel@tonic-gate static struct dchain *dtemp;	/* temporary - used for linking */
1590Sstevel@tonic-gate static char *curdir;		/* the current directory */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate static int	first = 1;	/* true if first line is not yet printed */
1620Sstevel@tonic-gate static int	nfiles = 0;	/* number of flist entries in current use */
1630Sstevel@tonic-gate static int	nargs = 0;	/* number of flist entries used for arguments */
1640Sstevel@tonic-gate static int	maxfils = 0;	/* number of flist/lbuf entries allocated */
1650Sstevel@tonic-gate static int	maxn = 0;	/* number of flist entries with lbufs asigned */
1660Sstevel@tonic-gate static int	quantn = 64;	/* allocation growth quantum */
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate static struct lbuf	*nxtlbf;	/* ptr to next lbuf to be assigned */
1690Sstevel@tonic-gate static struct lbuf	**flist;	/* ptr to list of lbuf pointers */
1700Sstevel@tonic-gate static struct lbuf	*gstat(char *, int, struct ditem *);
1710Sstevel@tonic-gate static char		*getname(uid_t);
1720Sstevel@tonic-gate static char		*getgroup(gid_t);
1730Sstevel@tonic-gate static char		*makename(char *, char *);
1740Sstevel@tonic-gate static void		pentry(struct lbuf *);
1750Sstevel@tonic-gate static void		column(void);
1760Sstevel@tonic-gate static void		pmode(mode_t aflag);
1770Sstevel@tonic-gate static void		selection(int *);
1780Sstevel@tonic-gate static void		new_line(void);
1790Sstevel@tonic-gate static void		rddir(char *, struct ditem *);
1800Sstevel@tonic-gate static int		strcol(unsigned char *);
1810Sstevel@tonic-gate static void		pem(struct lbuf **, struct lbuf **, int);
1820Sstevel@tonic-gate static void		pdirectory(char *, int, int, int, struct ditem *);
1830Sstevel@tonic-gate static struct cachenode *findincache(struct cachenode **, long);
1840Sstevel@tonic-gate static void		csi_pprintf(unsigned char *);
1850Sstevel@tonic-gate static void		pprintf(char *, char *);
1860Sstevel@tonic-gate static int		compar(struct lbuf **pp1, struct lbuf **pp2);
1870Sstevel@tonic-gate static char 		*number_to_scaled_string(numbuf_t buf,
1880Sstevel@tonic-gate 			    unsigned long long number,
1890Sstevel@tonic-gate 			    long scale);
1900Sstevel@tonic-gate static void		record_ancestry(char *, struct stat *, struct lbuf *,
1910Sstevel@tonic-gate 			    int, struct ditem *);
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate static int		aflg;
1940Sstevel@tonic-gate static int		atflg;
1950Sstevel@tonic-gate static int		bflg;
1960Sstevel@tonic-gate static int		cflg;
1970Sstevel@tonic-gate static int		dflg;
1980Sstevel@tonic-gate static int		eflg;
1990Sstevel@tonic-gate static int		fflg;
2000Sstevel@tonic-gate static int		gflg;
2010Sstevel@tonic-gate static int		hflg;
2020Sstevel@tonic-gate static int		iflg;
2030Sstevel@tonic-gate static int		lflg;
2040Sstevel@tonic-gate static int		mflg;
2050Sstevel@tonic-gate static int		nflg;
2060Sstevel@tonic-gate static int		oflg;
2070Sstevel@tonic-gate static int		pflg;
2080Sstevel@tonic-gate static int		qflg;
2090Sstevel@tonic-gate static int		rflg = 1; /* init to 1 for special use in compar */
2100Sstevel@tonic-gate static int		sflg;
2110Sstevel@tonic-gate static int		tflg;
2120Sstevel@tonic-gate static int		uflg;
2130Sstevel@tonic-gate static int		xflg;
2140Sstevel@tonic-gate static int		Aflg;
2150Sstevel@tonic-gate static int		Cflg;
2160Sstevel@tonic-gate static int		Eflg;
2170Sstevel@tonic-gate static int		Fflg;
2180Sstevel@tonic-gate static int		Hflg;
2190Sstevel@tonic-gate static int		Lflg;
2200Sstevel@tonic-gate static int		Rflg;
2210Sstevel@tonic-gate static int		Sflg;
222789Sahrens static int		vflg;
2231420Smarks static int		Vflg;
2240Sstevel@tonic-gate static long		hscale;
2250Sstevel@tonic-gate static mode_t		flags;
2260Sstevel@tonic-gate static int		err = 0;	/* Contains return code */
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate static uid_t		lastuid	= (uid_t)-1;
2290Sstevel@tonic-gate static gid_t		lastgid = (gid_t)-1;
2300Sstevel@tonic-gate static char		*lastuname = NULL;
2310Sstevel@tonic-gate static char		*lastgname = NULL;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate /* statreq > 0 if any of sflg, (n)lflg, tflg, Sflg are on */
2340Sstevel@tonic-gate static int		statreq;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate static char		*dotp = ".";
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate static u_longlong_t 	tblocks; /* number of blocks of files in a directory */
2390Sstevel@tonic-gate static time_t		year, now;
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate static int		num_cols = 80;
2420Sstevel@tonic-gate static int		colwidth;
2430Sstevel@tonic-gate static int		filewidth;
2440Sstevel@tonic-gate static int		fixedwidth;
2450Sstevel@tonic-gate static int		nomocore;
2460Sstevel@tonic-gate static int		curcol;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate static struct	winsize	win;
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate static char	time_buf[50];	/* array to hold day and time */
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate #define	NOTWORKINGDIR(d, l)	(((l) < 2) || \
2530Sstevel@tonic-gate 				    (strcmp((d) + (l) - 2, "/.") != 0))
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate #define	NOTPARENTDIR(d, l)	(((l) < 3) || \
2560Sstevel@tonic-gate 				    (strcmp((d) + (l) - 3, "/..") != 0))
2570Sstevel@tonic-gate 
2580Sstevel@tonic-gate int
2590Sstevel@tonic-gate main(int argc, char *argv[])
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate 	int		c;
2620Sstevel@tonic-gate 	int		i;
2630Sstevel@tonic-gate 	int		width;
2640Sstevel@tonic-gate 	int		amino = 0;
2650Sstevel@tonic-gate 	int		opterr = 0;
2660Sstevel@tonic-gate 	struct lbuf	*ep;
2670Sstevel@tonic-gate 	struct lbuf	lb;
2680Sstevel@tonic-gate 	struct ditem	*myinfo;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
2710Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
2720Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
2730Sstevel@tonic-gate #endif
2740Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
2750Sstevel@tonic-gate #ifdef STANDALONE
2760Sstevel@tonic-gate 	if (argv[0][0] == '\0')
2770Sstevel@tonic-gate 		argc = getargv("ls", &argv, 0);
2780Sstevel@tonic-gate #endif
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	lb.lmtime.tv_sec = time(NULL);
2810Sstevel@tonic-gate 	lb.lmtime.tv_nsec = 0;
2820Sstevel@tonic-gate 	year = lb.lmtime.tv_sec - 6L*30L*24L*60L*60L; /* 6 months ago */
2830Sstevel@tonic-gate 	now = lb.lmtime.tv_sec + 60;
2840Sstevel@tonic-gate 	if (isatty(1)) {
2850Sstevel@tonic-gate 		Cflg = 1;
2860Sstevel@tonic-gate 		mflg = 0;
2870Sstevel@tonic-gate 	}
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	while ((c = getopt(argc, argv,
2901420Smarks 	    "aAbcCdeEfFghHilLmnopqrRsStux1@vV")) != EOF)
2910Sstevel@tonic-gate 		switch (c) {
2920Sstevel@tonic-gate 		case 'a':
2930Sstevel@tonic-gate 			aflg++;
2940Sstevel@tonic-gate 			continue;
2950Sstevel@tonic-gate 		case 'A':
2960Sstevel@tonic-gate 			Aflg++;
2970Sstevel@tonic-gate 			continue;
2980Sstevel@tonic-gate 		case 'b':
2990Sstevel@tonic-gate 			bflg = 1;
3000Sstevel@tonic-gate 			qflg = 0;
3010Sstevel@tonic-gate 			continue;
3020Sstevel@tonic-gate 		case 'c':
3030Sstevel@tonic-gate 			uflg = 0;
3040Sstevel@tonic-gate 			cflg++;
3050Sstevel@tonic-gate 			continue;
3060Sstevel@tonic-gate 		case 'C':
3070Sstevel@tonic-gate 			Cflg = 1;
3080Sstevel@tonic-gate 			mflg = 0;
3090Sstevel@tonic-gate #ifdef XPG4
3100Sstevel@tonic-gate 			lflg = 0;
3110Sstevel@tonic-gate #endif
3120Sstevel@tonic-gate 			continue;
3130Sstevel@tonic-gate 		case 'd':
3140Sstevel@tonic-gate 			dflg++;
3150Sstevel@tonic-gate 			continue;
3160Sstevel@tonic-gate 		case 'e':
3170Sstevel@tonic-gate 			eflg++;
3180Sstevel@tonic-gate 			lflg++;
3190Sstevel@tonic-gate 			statreq++;
3200Sstevel@tonic-gate 			Eflg = 0;
3210Sstevel@tonic-gate 			continue;
3220Sstevel@tonic-gate 		case 'E':
3230Sstevel@tonic-gate 			Eflg++;
3240Sstevel@tonic-gate 			lflg++;
3250Sstevel@tonic-gate 			statreq++;
3260Sstevel@tonic-gate 			eflg = 0;
3270Sstevel@tonic-gate 			continue;
3280Sstevel@tonic-gate 		case 'f':
3290Sstevel@tonic-gate 			fflg++;
3300Sstevel@tonic-gate 			continue;
3310Sstevel@tonic-gate 		case 'F':
3320Sstevel@tonic-gate 			Fflg++;
3330Sstevel@tonic-gate 			statreq++;
3340Sstevel@tonic-gate 			continue;
3350Sstevel@tonic-gate 		case 'g':
3360Sstevel@tonic-gate 			gflg++;
3370Sstevel@tonic-gate 			lflg++;
3380Sstevel@tonic-gate 			statreq++;
3390Sstevel@tonic-gate 			continue;
3400Sstevel@tonic-gate 		case 'h':
3410Sstevel@tonic-gate 			hflg++;
3420Sstevel@tonic-gate 			hscale = 1024;
3430Sstevel@tonic-gate 			continue;
3440Sstevel@tonic-gate 		case 'H':
3450Sstevel@tonic-gate 			Hflg++;
3460Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
3470Sstevel@tonic-gate 			Lflg = 0;
3480Sstevel@tonic-gate 			continue;
3490Sstevel@tonic-gate 		case 'i':
3500Sstevel@tonic-gate 			iflg++;
3510Sstevel@tonic-gate 			continue;
3520Sstevel@tonic-gate 		case 'l':
3530Sstevel@tonic-gate 			lflg++;
3540Sstevel@tonic-gate 			statreq++;
3550Sstevel@tonic-gate 			Cflg = 0;
3560Sstevel@tonic-gate 			xflg = 0;
3570Sstevel@tonic-gate 			mflg = 0;
3580Sstevel@tonic-gate 			atflg = 0;
3590Sstevel@tonic-gate 			continue;
3600Sstevel@tonic-gate 		case 'L':
3610Sstevel@tonic-gate 			Lflg++;
3620Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
3630Sstevel@tonic-gate 			Hflg = 0;
3640Sstevel@tonic-gate 			continue;
3650Sstevel@tonic-gate 		case 'm':
3660Sstevel@tonic-gate 			Cflg = 0;
3670Sstevel@tonic-gate 			mflg = 1;
3680Sstevel@tonic-gate #ifdef XPG4
3690Sstevel@tonic-gate 			lflg = 0;
3700Sstevel@tonic-gate #endif
3710Sstevel@tonic-gate 			continue;
3720Sstevel@tonic-gate 		case 'n':
3730Sstevel@tonic-gate 			nflg++;
3740Sstevel@tonic-gate 			lflg++;
3750Sstevel@tonic-gate 			statreq++;
3760Sstevel@tonic-gate 			Cflg = 0;
3770Sstevel@tonic-gate 			xflg = 0;
3780Sstevel@tonic-gate 			mflg = 0;
3790Sstevel@tonic-gate 			atflg = 0;
3800Sstevel@tonic-gate 			continue;
3810Sstevel@tonic-gate 		case 'o':
3820Sstevel@tonic-gate 			oflg++;
3830Sstevel@tonic-gate 			lflg++;
3840Sstevel@tonic-gate 			statreq++;
3850Sstevel@tonic-gate 			continue;
3860Sstevel@tonic-gate 		case 'p':
3870Sstevel@tonic-gate 			pflg++;
3880Sstevel@tonic-gate 			statreq++;
3890Sstevel@tonic-gate 			continue;
3900Sstevel@tonic-gate 		case 'q':
3910Sstevel@tonic-gate 			qflg = 1;
3920Sstevel@tonic-gate 			bflg = 0;
3930Sstevel@tonic-gate 			continue;
3940Sstevel@tonic-gate 		case 'r':
3950Sstevel@tonic-gate 			rflg = -1;
3960Sstevel@tonic-gate 			continue;
3970Sstevel@tonic-gate 		case 'R':
3980Sstevel@tonic-gate 			Rflg++;
3990Sstevel@tonic-gate 			statreq++;
4000Sstevel@tonic-gate 			continue;
4010Sstevel@tonic-gate 		case 's':
4020Sstevel@tonic-gate 			sflg++;
4030Sstevel@tonic-gate 			statreq++;
4040Sstevel@tonic-gate 			continue;
4050Sstevel@tonic-gate 		case 'S':
4060Sstevel@tonic-gate 			tflg = 0;
4070Sstevel@tonic-gate 			Sflg++;
4080Sstevel@tonic-gate 			statreq++;
4090Sstevel@tonic-gate 			continue;
4100Sstevel@tonic-gate 		case 't':
4110Sstevel@tonic-gate 			Sflg = 0;
4120Sstevel@tonic-gate 			tflg++;
4130Sstevel@tonic-gate 			statreq++;
4140Sstevel@tonic-gate 			continue;
4150Sstevel@tonic-gate 		case 'u':
4160Sstevel@tonic-gate 			cflg = 0;
4170Sstevel@tonic-gate 			uflg++;
4180Sstevel@tonic-gate 			continue;
4191420Smarks 		case 'V':
4201420Smarks 			Vflg++;
4211420Smarks 			/*FALLTHROUGH*/
422789Sahrens 		case 'v':
423789Sahrens 			vflg++;
424789Sahrens #if !defined(XPG4)
425789Sahrens 			if (lflg)
426789Sahrens 				continue;
427789Sahrens #endif
428789Sahrens 			lflg++;
429789Sahrens 			statreq++;
430789Sahrens 			Cflg = 0;
431789Sahrens 			xflg = 0;
432789Sahrens 			mflg = 0;
433789Sahrens 			continue;
4340Sstevel@tonic-gate 		case 'x':
4350Sstevel@tonic-gate 			xflg = 1;
4360Sstevel@tonic-gate 			Cflg = 1;
4370Sstevel@tonic-gate 			mflg = 0;
4380Sstevel@tonic-gate #ifdef XPG4
4390Sstevel@tonic-gate 			lflg = 0;
4400Sstevel@tonic-gate #endif
4410Sstevel@tonic-gate 			continue;
4420Sstevel@tonic-gate 		case '1':
4430Sstevel@tonic-gate 			Cflg = 0;
4440Sstevel@tonic-gate 			continue;
4450Sstevel@tonic-gate 		case '@':
4460Sstevel@tonic-gate #if !defined(XPG4)
4470Sstevel@tonic-gate 			/*
4480Sstevel@tonic-gate 			 * -l has precedence over -@
4490Sstevel@tonic-gate 			 */
4500Sstevel@tonic-gate 			if (lflg)
4510Sstevel@tonic-gate 				continue;
4520Sstevel@tonic-gate #endif
4530Sstevel@tonic-gate 			atflg++;
4540Sstevel@tonic-gate 			lflg++;
4550Sstevel@tonic-gate 			statreq++;
4560Sstevel@tonic-gate 			Cflg = 0;
4570Sstevel@tonic-gate 			xflg = 0;
4580Sstevel@tonic-gate 			mflg = 0;
4590Sstevel@tonic-gate 			continue;
4600Sstevel@tonic-gate 		case '?':
4610Sstevel@tonic-gate 			opterr++;
4620Sstevel@tonic-gate 			continue;
4630Sstevel@tonic-gate 		}
4640Sstevel@tonic-gate 	if (opterr) {
4650Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
4661420Smarks 		    "usage: ls -aAbcCdeEfFghHilLmnopqrRsStuxvV1@ [files]\n"));
4670Sstevel@tonic-gate 		exit(2);
4680Sstevel@tonic-gate 	}
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	if (fflg) {
4710Sstevel@tonic-gate 		aflg++;
4720Sstevel@tonic-gate 		lflg = 0;
4730Sstevel@tonic-gate 		sflg = 0;
4740Sstevel@tonic-gate 		tflg = 0;
4750Sstevel@tonic-gate 		Sflg = 0;
4760Sstevel@tonic-gate 		statreq = 0;
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	fixedwidth = 2;
4800Sstevel@tonic-gate 	if (pflg || Fflg)
4810Sstevel@tonic-gate 		fixedwidth++;
4820Sstevel@tonic-gate 	if (iflg)
4830Sstevel@tonic-gate 		fixedwidth += 11;
4840Sstevel@tonic-gate 	if (sflg)
4850Sstevel@tonic-gate 		fixedwidth += 5;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	if (lflg) {
4880Sstevel@tonic-gate 		if (!gflg && !oflg)
4890Sstevel@tonic-gate 			gflg = oflg = 1;
4900Sstevel@tonic-gate 		else
4910Sstevel@tonic-gate 		if (gflg && oflg)
4920Sstevel@tonic-gate 			gflg = oflg = 0;
4930Sstevel@tonic-gate 		Cflg = mflg = 0;
4940Sstevel@tonic-gate 	}
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 	if (Cflg || mflg) {
4970Sstevel@tonic-gate 		char *clptr;
4980Sstevel@tonic-gate 		if ((clptr = getenv("COLUMNS")) != NULL)
4990Sstevel@tonic-gate 			num_cols = atoi(clptr);
5000Sstevel@tonic-gate #ifdef TERMINFO
5010Sstevel@tonic-gate 		else {
5020Sstevel@tonic-gate 			if (ioctl(1, TIOCGWINSZ, &win) != -1)
5030Sstevel@tonic-gate 				num_cols = (win.ws_col == 0 ? 80 : win.ws_col);
5040Sstevel@tonic-gate 		}
5050Sstevel@tonic-gate #endif
5060Sstevel@tonic-gate 		if (num_cols < 20 || num_cols > 1000)
5070Sstevel@tonic-gate 			/* assume it is an error */
5080Sstevel@tonic-gate 			num_cols = 80;
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	/* allocate space for flist and the associated	*/
5120Sstevel@tonic-gate 	/* data structures (lbufs)			*/
5130Sstevel@tonic-gate 	maxfils = quantn;
5140Sstevel@tonic-gate 	if (((flist = malloc(maxfils * sizeof (struct lbuf *))) == NULL) ||
5150Sstevel@tonic-gate 	    ((nxtlbf = malloc(quantn * sizeof (struct lbuf))) == NULL)) {
5160Sstevel@tonic-gate 		perror("ls");
5170Sstevel@tonic-gate 		exit(2);
5180Sstevel@tonic-gate 	}
5190Sstevel@tonic-gate 	if ((amino = (argc-optind)) == 0) {
5200Sstevel@tonic-gate 					/*
5210Sstevel@tonic-gate 					 * case when no names are given
5220Sstevel@tonic-gate 					 * in ls-command and current
5230Sstevel@tonic-gate 					 * directory is to be used
5240Sstevel@tonic-gate 					 */
5250Sstevel@tonic-gate 		argv[optind] = dotp;
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	for (i = 0; i < (amino ? amino : 1); i++) {
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 		/*
5310Sstevel@tonic-gate 		 * If we are recursing, we need to make sure we don't
5320Sstevel@tonic-gate 		 * get into an endless loop.  To keep track of the inodes
5330Sstevel@tonic-gate 		 * (actually, just the directories) visited, we
5340Sstevel@tonic-gate 		 * maintain a directory ancestry list for a file
5350Sstevel@tonic-gate 		 * hierarchy.  As we go deeper into the hierarchy,
5360Sstevel@tonic-gate 		 * a parent directory passes its directory list
5370Sstevel@tonic-gate 		 * info (device id, inode number, and a pointer to
5380Sstevel@tonic-gate 		 * its parent) to each of its children.  As we
5390Sstevel@tonic-gate 		 * process a child that is a directory, we save
5400Sstevel@tonic-gate 		 * its own personal directory list info.  We then
5410Sstevel@tonic-gate 		 * check to see if the child has already been
5420Sstevel@tonic-gate 		 * processed by comparing its device id and inode
5430Sstevel@tonic-gate 		 * number from its own personal directory list info
5440Sstevel@tonic-gate 		 * to that of each of its ancestors.  If there is a
5450Sstevel@tonic-gate 		 * match, then we know we've detected a cycle.
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 		if (Rflg) {
5480Sstevel@tonic-gate 			/*
5490Sstevel@tonic-gate 			 * This is the first parent in this lineage
5500Sstevel@tonic-gate 			 * (first in a directory hierarchy), so
5510Sstevel@tonic-gate 			 * this parent's parent doesn't exist.  We
5520Sstevel@tonic-gate 			 * only initialize myinfo when we are
5530Sstevel@tonic-gate 			 * recursing, otherwise it's not used.
5540Sstevel@tonic-gate 			 */
5550Sstevel@tonic-gate 			if ((myinfo = (struct ditem *)malloc(
5560Sstevel@tonic-gate 			    sizeof (struct ditem))) == NULL) {
5570Sstevel@tonic-gate 				perror("ls");
5580Sstevel@tonic-gate 				exit(2);
5590Sstevel@tonic-gate 			} else {
5600Sstevel@tonic-gate 				myinfo->dev = 0;
5610Sstevel@tonic-gate 				myinfo->ino = 0;
5620Sstevel@tonic-gate 				myinfo->parent = NULL;
5630Sstevel@tonic-gate 			}
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate 		if (Cflg || mflg) {
5670Sstevel@tonic-gate 			width = strcol((unsigned char *)argv[optind]);
5680Sstevel@tonic-gate 			if (width > filewidth)
5690Sstevel@tonic-gate 				filewidth = width;
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 		if ((ep = gstat((*argv[optind] ? argv[optind] : dotp),
5720Sstevel@tonic-gate 		    1, myinfo)) == NULL) {
5730Sstevel@tonic-gate 			if (nomocore)
5740Sstevel@tonic-gate 				exit(2);
5750Sstevel@tonic-gate 			err = 2;
5760Sstevel@tonic-gate 			optind++;
5770Sstevel@tonic-gate 			continue;
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 		ep->ln.namep = (*argv[optind] ? argv[optind] : dotp);
5800Sstevel@tonic-gate 		ep->lflags |= ISARG;
5810Sstevel@tonic-gate 		optind++;
5820Sstevel@tonic-gate 		nargs++;	/* count good arguments stored in flist */
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 	colwidth = fixedwidth + filewidth;
5850Sstevel@tonic-gate 	qsort(flist, (unsigned)nargs, sizeof (struct lbuf *),
5860Sstevel@tonic-gate 	    (int (*)(const void *, const void *))compar);
5870Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
5880Sstevel@tonic-gate 		if (flist[i]->ltype == 'd' && dflg == 0 || fflg)
5890Sstevel@tonic-gate 			break;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 	pem(&flist[0], &flist[i], 0);
5920Sstevel@tonic-gate 	for (; i < nargs; i++) {
5930Sstevel@tonic-gate 		pdirectory(flist[i]->ln.namep, Rflg ||
5940Sstevel@tonic-gate 		    (amino > 1), nargs, 0, flist[i]->ancinfo);
5950Sstevel@tonic-gate 		if (nomocore)
5960Sstevel@tonic-gate 			exit(2);
5970Sstevel@tonic-gate 		/* -R: print subdirectories found */
5980Sstevel@tonic-gate 		while (dfirst || cdfirst) {
5990Sstevel@tonic-gate 			/* Place direct subdirs on front in right order */
6000Sstevel@tonic-gate 			while (cdfirst) {
6010Sstevel@tonic-gate 				/* reverse cdfirst onto front of dfirst */
6020Sstevel@tonic-gate 				dtemp = cdfirst;
6030Sstevel@tonic-gate 				cdfirst = cdfirst -> dc_next;
6040Sstevel@tonic-gate 				dtemp -> dc_next = dfirst;
6050Sstevel@tonic-gate 				dfirst = dtemp;
6060Sstevel@tonic-gate 			}
6070Sstevel@tonic-gate 			/* take off first dir on dfirst & print it */
6080Sstevel@tonic-gate 			dtemp = dfirst;
6090Sstevel@tonic-gate 			dfirst = dfirst->dc_next;
6100Sstevel@tonic-gate 			pdirectory(dtemp->dc_name, 1, nargs,
6110Sstevel@tonic-gate 			    dtemp->cycle_detected, dtemp->myancinfo);
6120Sstevel@tonic-gate 			if (nomocore)
6130Sstevel@tonic-gate 				exit(2);
6140Sstevel@tonic-gate 			free(dtemp->dc_name);
6150Sstevel@tonic-gate 			free(dtemp);
6160Sstevel@tonic-gate 		}
6170Sstevel@tonic-gate 	}
6180Sstevel@tonic-gate 	return (err);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate /*
6220Sstevel@tonic-gate  * pdirectory: print the directory name, labelling it if title is
6230Sstevel@tonic-gate  * nonzero, using lp as the place to start reading in the dir.
6240Sstevel@tonic-gate  */
6250Sstevel@tonic-gate static void
6260Sstevel@tonic-gate pdirectory(char *name, int title, int lp, int cdetect, struct ditem *myinfo)
6270Sstevel@tonic-gate {
6280Sstevel@tonic-gate 	struct dchain *dp;
6290Sstevel@tonic-gate 	struct lbuf *ap;
6300Sstevel@tonic-gate 	char *pname;
6310Sstevel@tonic-gate 	int j;
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 	filewidth = 0;
6340Sstevel@tonic-gate 	curdir = name;
6350Sstevel@tonic-gate 	if (title) {
6360Sstevel@tonic-gate 		if (!first)
6370Sstevel@tonic-gate 			(void) putc('\n', stdout);
6380Sstevel@tonic-gate 		pprintf(name, ":");
6390Sstevel@tonic-gate 		new_line();
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 	/*
6420Sstevel@tonic-gate 	 * If there was a cycle detected, then notify and don't report
6430Sstevel@tonic-gate 	 * further.
6440Sstevel@tonic-gate 	 */
6450Sstevel@tonic-gate 	if (cdetect) {
6460Sstevel@tonic-gate 		if (lflg || sflg) {
6470Sstevel@tonic-gate 			curcol += printf(gettext("total %d"), 0);
6480Sstevel@tonic-gate 			new_line();
6490Sstevel@tonic-gate 		}
6500Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
6510Sstevel@tonic-gate 		    "ls: cycle detected for %s\n"), name);
6520Sstevel@tonic-gate 		return;
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	nfiles = lp;
6560Sstevel@tonic-gate 	rddir(name, myinfo);
6570Sstevel@tonic-gate 	if (nomocore)
6580Sstevel@tonic-gate 		return;
6590Sstevel@tonic-gate 	if (fflg == 0)
6600Sstevel@tonic-gate 		qsort(&flist[lp], (unsigned)(nfiles - lp),
6610Sstevel@tonic-gate 		    sizeof (struct lbuf *),
6620Sstevel@tonic-gate 		    (int (*)(const void *, const void *))compar);
6630Sstevel@tonic-gate 	if (Rflg) {
6640Sstevel@tonic-gate 		for (j = nfiles - 1; j >= lp; j--) {
6650Sstevel@tonic-gate 			ap = flist[j];
6660Sstevel@tonic-gate 			if (ap->ltype == 'd' && strcmp(ap->ln.lname, ".") &&
6670Sstevel@tonic-gate 			    strcmp(ap->ln.lname, "..")) {
6680Sstevel@tonic-gate 				dp = malloc(sizeof (struct dchain));
6690Sstevel@tonic-gate 				if (dp == NULL) {
6700Sstevel@tonic-gate 					perror("ls");
6710Sstevel@tonic-gate 					exit(2);
6720Sstevel@tonic-gate 				}
6730Sstevel@tonic-gate 				pname = makename(curdir, ap->ln.lname);
6740Sstevel@tonic-gate 				if ((dp->dc_name = strdup(pname)) == NULL) {
6750Sstevel@tonic-gate 					perror("ls");
6760Sstevel@tonic-gate 					exit(2);
6770Sstevel@tonic-gate 				}
6780Sstevel@tonic-gate 				dp->cycle_detected = ap->cycle;
6790Sstevel@tonic-gate 				dp->myancinfo = ap->ancinfo;
6800Sstevel@tonic-gate 				dp->dc_next = dfirst;
6810Sstevel@tonic-gate 				dfirst = dp;
6820Sstevel@tonic-gate 			}
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 	}
6850Sstevel@tonic-gate 	if (lflg || sflg) {
6860Sstevel@tonic-gate 		curcol += printf(gettext("total %llu"), tblocks);
6870Sstevel@tonic-gate 		new_line();
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 	pem(&flist[lp], &flist[nfiles], lflg||sflg);
6900Sstevel@tonic-gate }
6910Sstevel@tonic-gate 
6920Sstevel@tonic-gate /*
6930Sstevel@tonic-gate  * pem: print 'em. Print a list of files (e.g. a directory) bounded
6940Sstevel@tonic-gate  * by slp and lp.
6950Sstevel@tonic-gate  */
6960Sstevel@tonic-gate static void
6970Sstevel@tonic-gate pem(struct lbuf **slp, struct lbuf **lp, int tot_flag)
6980Sstevel@tonic-gate {
6990Sstevel@tonic-gate 	long row, nrows, i;
7000Sstevel@tonic-gate 	int col, ncols;
7010Sstevel@tonic-gate 	struct lbuf **ep;
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate 	if (Cflg || mflg) {
7040Sstevel@tonic-gate 		if (colwidth > num_cols) {
7050Sstevel@tonic-gate 			ncols = 1;
7060Sstevel@tonic-gate 		} else {
7070Sstevel@tonic-gate 			ncols = num_cols / colwidth;
7080Sstevel@tonic-gate 		}
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (ncols == 1 || mflg || xflg || !Cflg) {
7120Sstevel@tonic-gate 		for (ep = slp; ep < lp; ep++)
7130Sstevel@tonic-gate 			pentry(*ep);
7140Sstevel@tonic-gate 		new_line();
7150Sstevel@tonic-gate 		return;
7160Sstevel@tonic-gate 	}
7170Sstevel@tonic-gate 	/* otherwise print -C columns */
7180Sstevel@tonic-gate 	if (tot_flag) {
7190Sstevel@tonic-gate 		slp--;
7200Sstevel@tonic-gate 		row = 1;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 	else
7230Sstevel@tonic-gate 		row = 0;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	nrows = (lp - slp - 1) / ncols + 1;
7260Sstevel@tonic-gate 	for (i = 0; i < nrows; i++, row++) {
7270Sstevel@tonic-gate 		for (col = 0; col < ncols; col++) {
7280Sstevel@tonic-gate 			ep = slp + (nrows * col) + row;
7290Sstevel@tonic-gate 			if (ep < lp)
7300Sstevel@tonic-gate 				pentry(*ep);
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		new_line();
7330Sstevel@tonic-gate 	}
7340Sstevel@tonic-gate }
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate /*
7370Sstevel@tonic-gate  * print one output entry;
7380Sstevel@tonic-gate  * if uid/gid is not found in the appropriate
7390Sstevel@tonic-gate  * file(passwd/group), then print uid/gid instead of
7400Sstevel@tonic-gate  * user/group name;
7410Sstevel@tonic-gate  */
7420Sstevel@tonic-gate static void
7430Sstevel@tonic-gate pentry(struct lbuf *ap)
7440Sstevel@tonic-gate {
7450Sstevel@tonic-gate 	struct lbuf *p;
7460Sstevel@tonic-gate 	numbuf_t hbuf;
7470Sstevel@tonic-gate 	char buf[BUFSIZ];
7480Sstevel@tonic-gate 	char fmt_buf[FMTSIZE];
7490Sstevel@tonic-gate 	char *dmark = "";	/* Used if -p or -F option active */
7500Sstevel@tonic-gate 	char *cp;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 	p = ap;
7530Sstevel@tonic-gate 	column();
7540Sstevel@tonic-gate 	if (iflg)
7550Sstevel@tonic-gate 		if (mflg && !lflg)
7560Sstevel@tonic-gate 			curcol += printf("%llu ", (long long)p->lnum);
7570Sstevel@tonic-gate 		else
7580Sstevel@tonic-gate 			curcol += printf("%10llu ", (long long)p->lnum);
7590Sstevel@tonic-gate 	if (sflg)
7600Sstevel@tonic-gate 		curcol += printf((mflg && !lflg) ? "%lld " :
7610Sstevel@tonic-gate 			(p->lblocks < 10000) ? "%4lld " : "%lld ",
7620Sstevel@tonic-gate 			(p->ltype != 'b' && p->ltype != 'c') ?
7630Sstevel@tonic-gate 				p->lblocks : 0LL);
7640Sstevel@tonic-gate 	if (lflg) {
7650Sstevel@tonic-gate 		(void) putchar(p->ltype);
7660Sstevel@tonic-gate 		curcol++;
7670Sstevel@tonic-gate 		pmode(p->lflags);
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 		/* ACL: additional access mode flag */
7700Sstevel@tonic-gate 		(void) putchar(p->acl);
7710Sstevel@tonic-gate 		curcol++;
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 		curcol += printf("%3lu ", (ulong_t)p->lnl);
7740Sstevel@tonic-gate 		if (oflg)
7750Sstevel@tonic-gate 			if (!nflg) {
7760Sstevel@tonic-gate 				cp = getname(p->luid);
7770Sstevel@tonic-gate 				curcol += printf("%-8s ", cp);
7780Sstevel@tonic-gate 			} else
7790Sstevel@tonic-gate 				curcol += printf("%-8lu ", (ulong_t)p->luid);
7800Sstevel@tonic-gate 		if (gflg)
7810Sstevel@tonic-gate 			if (!nflg) {
7820Sstevel@tonic-gate 				cp = getgroup(p->lgid);
7830Sstevel@tonic-gate 				curcol += printf("%-8s ", cp);
7840Sstevel@tonic-gate 			} else
7850Sstevel@tonic-gate 				curcol += printf("%-8lu ", (ulong_t)p->lgid);
7860Sstevel@tonic-gate 		if (p->ltype == 'b' || p->ltype == 'c') {
7870Sstevel@tonic-gate 			curcol += printf("%3u, %2u",
7880Sstevel@tonic-gate 			    (uint_t)major((dev_t)p->lsize),
7890Sstevel@tonic-gate 			    (uint_t)minor((dev_t)p->lsize));
7900Sstevel@tonic-gate 		} else if (hflg && (p->lsize >= hscale)) {
7910Sstevel@tonic-gate 			curcol += printf("%7s",
7920Sstevel@tonic-gate 			    number_to_scaled_string(hbuf, p->lsize, hscale));
7930Sstevel@tonic-gate 		} else {
7940Sstevel@tonic-gate 			curcol += printf((p->lsize < (off_t)10000000) ?
7950Sstevel@tonic-gate 			    "%7lld" : "%lld", p->lsize);
7960Sstevel@tonic-gate 		}
7970Sstevel@tonic-gate 		if (eflg) {
7980Sstevel@tonic-gate 			(void) strftime(time_buf, sizeof (time_buf),
7990Sstevel@tonic-gate 			    dcgettext(NULL, FORMAT3, LC_TIME),
8000Sstevel@tonic-gate 			    localtime(&p->lmtime.tv_sec));
8010Sstevel@tonic-gate 		} else if (Eflg) {
8020Sstevel@tonic-gate 			/* fill in nanoseconds first */
8030Sstevel@tonic-gate 			(void) snprintf(fmt_buf, sizeof (fmt_buf),
8040Sstevel@tonic-gate 			    FORMAT4, p->lmtime.tv_nsec);
8050Sstevel@tonic-gate 			(void) strftime(time_buf, sizeof (time_buf),
8060Sstevel@tonic-gate 			    fmt_buf, localtime(&p->lmtime.tv_sec));
8070Sstevel@tonic-gate 		} else {
8080Sstevel@tonic-gate 			if ((p->lmtime.tv_sec < year) ||
8090Sstevel@tonic-gate 			    (p->lmtime.tv_sec > now)) {
8100Sstevel@tonic-gate 				(void) strftime(time_buf, sizeof (time_buf),
8110Sstevel@tonic-gate 				    dcgettext(NULL, FORMAT1, LC_TIME),
8120Sstevel@tonic-gate 				    localtime(&p->lmtime.tv_sec));
8130Sstevel@tonic-gate 			} else {
8140Sstevel@tonic-gate 				(void) strftime(time_buf, sizeof (time_buf),
8150Sstevel@tonic-gate 				    dcgettext(NULL, FORMAT2, LC_TIME),
8160Sstevel@tonic-gate 				    localtime(&p->lmtime.tv_sec));
8170Sstevel@tonic-gate 			}
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 		curcol += printf("%s", time_buf);
8200Sstevel@tonic-gate 	}
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	/*
8230Sstevel@tonic-gate 	 * prevent both "->" and trailing marks
8240Sstevel@tonic-gate 	 * from appearing
8250Sstevel@tonic-gate 	 */
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	if (pflg && p->ltype == 'd')
8280Sstevel@tonic-gate 		dmark = "/";
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	if (Fflg && !(lflg && p->flinkto)) {
8310Sstevel@tonic-gate 		if (p->ltype == 'd')
8320Sstevel@tonic-gate 			dmark = "/";
8330Sstevel@tonic-gate 		else if (p->ltype == 'D')
8340Sstevel@tonic-gate 			dmark = ">";
8350Sstevel@tonic-gate 		else if (p->ltype == 'p')
8360Sstevel@tonic-gate 			dmark = "|";
8370Sstevel@tonic-gate 		else if (p->ltype == 'l')
8380Sstevel@tonic-gate 			dmark = "@";
8390Sstevel@tonic-gate 		else if (p->ltype == 's')
8400Sstevel@tonic-gate 			dmark = "=";
8410Sstevel@tonic-gate 		else if (p->lflags & (S_IXUSR|S_IXGRP|S_IXOTH))
8420Sstevel@tonic-gate 			dmark = "*";
8430Sstevel@tonic-gate 		else
8440Sstevel@tonic-gate 			dmark = "";
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate 	if (lflg && p->flinkto) {
8480Sstevel@tonic-gate 		(void) strncpy(buf, " -> ", 4);
8490Sstevel@tonic-gate 		(void) strcpy(buf + 4, p->flinkto);
8500Sstevel@tonic-gate 		dmark = buf;
8510Sstevel@tonic-gate 	}
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	if (p->lflags & ISARG) {
8540Sstevel@tonic-gate 		if (qflg || bflg)
8550Sstevel@tonic-gate 			pprintf(p->ln.namep, dmark);
8560Sstevel@tonic-gate 		else {
8570Sstevel@tonic-gate 			(void) printf("%s%s", p->ln.namep, dmark);
8580Sstevel@tonic-gate 			curcol += strcol((unsigned char *)p->ln.namep);
8590Sstevel@tonic-gate 			curcol += strcol((unsigned char *)dmark);
8600Sstevel@tonic-gate 		}
8610Sstevel@tonic-gate 	} else {
8620Sstevel@tonic-gate 		if (qflg || bflg)
8630Sstevel@tonic-gate 			pprintf(p->ln.lname, dmark);
8640Sstevel@tonic-gate 		else {
8650Sstevel@tonic-gate 			(void) printf("%s%s", p->ln.lname, dmark);
8660Sstevel@tonic-gate 			curcol += strcol((unsigned char *)p->ln.lname);
8670Sstevel@tonic-gate 			curcol += strcol((unsigned char *)dmark);
8680Sstevel@tonic-gate 		}
8690Sstevel@tonic-gate 	}
870789Sahrens 
871789Sahrens 	if (vflg) {
872789Sahrens 		new_line();
873789Sahrens 		if (p->aclp) {
8741420Smarks 			acl_printacl(p->aclp, num_cols, Vflg);
875789Sahrens 		}
876789Sahrens 	}
8770Sstevel@tonic-gate }
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate /* print various r,w,x permissions */
8800Sstevel@tonic-gate static void
8810Sstevel@tonic-gate pmode(mode_t aflag)
8820Sstevel@tonic-gate {
8830Sstevel@tonic-gate 	/* these arrays are declared static to allow initializations */
8840Sstevel@tonic-gate 	static int	m0[] = { 1, S_IRUSR, 'r', '-' };
8850Sstevel@tonic-gate 	static int	m1[] = { 1, S_IWUSR, 'w', '-' };
8860Sstevel@tonic-gate 	static int	m2[] = { 3, S_ISUID|S_IXUSR, 's', S_IXUSR,
8870Sstevel@tonic-gate 	    'x', S_ISUID, 'S', '-' };
8880Sstevel@tonic-gate 	static int	m3[] = { 1, S_IRGRP, 'r', '-' };
8890Sstevel@tonic-gate 	static int	m4[] = { 1, S_IWGRP, 'w', '-' };
8900Sstevel@tonic-gate 	static int	m5[] = { 4, S_ISGID|S_IXGRP, 's', S_IXGRP,
8910Sstevel@tonic-gate 				'x', S_ISGID|LS_NOTREG, 'S',
8920Sstevel@tonic-gate #ifdef XPG4
8930Sstevel@tonic-gate 		S_ISGID, 'L', '-'};
8940Sstevel@tonic-gate #else
8950Sstevel@tonic-gate 		S_ISGID, 'l', '-'};
8960Sstevel@tonic-gate #endif
8970Sstevel@tonic-gate 	static int	m6[] = { 1, S_IROTH, 'r', '-' };
8980Sstevel@tonic-gate 	static int	m7[] = { 1, S_IWOTH, 'w', '-' };
8990Sstevel@tonic-gate 	static int	m8[] = { 3, S_ISVTX|S_IXOTH, 't', S_IXOTH,
9000Sstevel@tonic-gate 	    'x', S_ISVTX, 'T', '-'};
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 	static int *m[] = { m0, m1, m2, m3, m4, m5, m6, m7, m8};
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	int **mp;
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 	flags = aflag;
9070Sstevel@tonic-gate 	for (mp = &m[0]; mp < &m[sizeof (m) / sizeof (m[0])]; mp++)
9080Sstevel@tonic-gate 		selection(*mp);
9090Sstevel@tonic-gate }
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate static void
9120Sstevel@tonic-gate selection(int *pairp)
9130Sstevel@tonic-gate {
9140Sstevel@tonic-gate 	int n;
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	n = *pairp++;
9170Sstevel@tonic-gate 	while (n-->0) {
9180Sstevel@tonic-gate 		if ((flags & *pairp) == *pairp) {
9190Sstevel@tonic-gate 			pairp++;
9200Sstevel@tonic-gate 			break;
9210Sstevel@tonic-gate 		} else {
9220Sstevel@tonic-gate 			pairp += 2;
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 	}
9250Sstevel@tonic-gate 	(void) putchar(*pairp);
9260Sstevel@tonic-gate 	curcol++;
9270Sstevel@tonic-gate }
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /*
9300Sstevel@tonic-gate  * column: get to the beginning of the next column.
9310Sstevel@tonic-gate  */
9320Sstevel@tonic-gate static void
9330Sstevel@tonic-gate column(void)
9340Sstevel@tonic-gate {
9350Sstevel@tonic-gate 	if (curcol == 0)
9360Sstevel@tonic-gate 		return;
9370Sstevel@tonic-gate 	if (mflg) {
9380Sstevel@tonic-gate 		(void) putc(',', stdout);
9390Sstevel@tonic-gate 		curcol++;
9400Sstevel@tonic-gate 		if (curcol + colwidth + 2 > num_cols) {
9410Sstevel@tonic-gate 			(void) putc('\n', stdout);
9420Sstevel@tonic-gate 			curcol = 0;
9430Sstevel@tonic-gate 			return;
9440Sstevel@tonic-gate 		}
9450Sstevel@tonic-gate 		(void) putc(' ', stdout);
9460Sstevel@tonic-gate 		curcol++;
9470Sstevel@tonic-gate 		return;
9480Sstevel@tonic-gate 	}
9490Sstevel@tonic-gate 	if (Cflg == 0) {
9500Sstevel@tonic-gate 		(void) putc('\n', stdout);
9510Sstevel@tonic-gate 		curcol = 0;
9520Sstevel@tonic-gate 		return;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	if ((curcol / colwidth + 2) * colwidth > num_cols) {
9550Sstevel@tonic-gate 		(void) putc('\n', stdout);
9560Sstevel@tonic-gate 		curcol = 0;
9570Sstevel@tonic-gate 		return;
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 	do {
9600Sstevel@tonic-gate 		(void) putc(' ', stdout);
9610Sstevel@tonic-gate 		curcol++;
9620Sstevel@tonic-gate 	} while (curcol % colwidth);
9630Sstevel@tonic-gate }
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate static void
9660Sstevel@tonic-gate new_line(void)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	if (curcol) {
9690Sstevel@tonic-gate 		first = 0;
9700Sstevel@tonic-gate 		(void) putc('\n', stdout);
9710Sstevel@tonic-gate 		curcol = 0;
9720Sstevel@tonic-gate 	}
9730Sstevel@tonic-gate }
9740Sstevel@tonic-gate 
9750Sstevel@tonic-gate /*
9760Sstevel@tonic-gate  * read each filename in directory dir and store its
9770Sstevel@tonic-gate  * status in flist[nfiles]
9780Sstevel@tonic-gate  * use makename() to form pathname dir/filename;
9790Sstevel@tonic-gate  */
9800Sstevel@tonic-gate static void
9810Sstevel@tonic-gate rddir(char *dir, struct ditem *myinfo)
9820Sstevel@tonic-gate {
9830Sstevel@tonic-gate 	struct dirent *dentry;
9840Sstevel@tonic-gate 	DIR *dirf;
9850Sstevel@tonic-gate 	int j;
9860Sstevel@tonic-gate 	struct lbuf *ep;
9870Sstevel@tonic-gate 	int width;
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if ((dirf = opendir(dir)) == NULL) {
9900Sstevel@tonic-gate 		(void) fflush(stdout);
9910Sstevel@tonic-gate 		perror(dir);
9920Sstevel@tonic-gate 		err = 2;
9930Sstevel@tonic-gate 		return;
9940Sstevel@tonic-gate 	} else {
9950Sstevel@tonic-gate 		tblocks = 0;
9960Sstevel@tonic-gate 		for (;;) {
9970Sstevel@tonic-gate 			errno = 0;
9980Sstevel@tonic-gate 			if ((dentry = readdir(dirf)) == NULL)
9990Sstevel@tonic-gate 				break;
10000Sstevel@tonic-gate 			if (aflg == 0 && dentry->d_name[0] == '.' &&
10010Sstevel@tonic-gate 			    (Aflg == 0 ||
10020Sstevel@tonic-gate 			    dentry->d_name[1] == '\0' ||
10030Sstevel@tonic-gate 			    dentry->d_name[1] == '.' &&
10040Sstevel@tonic-gate 			    dentry->d_name[2] == '\0'))
10050Sstevel@tonic-gate 				/*
10060Sstevel@tonic-gate 				 * check for directory items '.', '..',
10070Sstevel@tonic-gate 				 *  and items without valid inode-number;
10080Sstevel@tonic-gate 				 */
10090Sstevel@tonic-gate 				continue;
10100Sstevel@tonic-gate 
10110Sstevel@tonic-gate 			if (Cflg || mflg) {
10120Sstevel@tonic-gate 				width = strcol((unsigned char *)dentry->d_name);
10130Sstevel@tonic-gate 				if (width > filewidth)
10140Sstevel@tonic-gate 					filewidth = width;
10150Sstevel@tonic-gate 			}
10160Sstevel@tonic-gate 			ep = gstat(makename(dir, dentry->d_name), 0, myinfo);
10170Sstevel@tonic-gate 			if (ep == NULL) {
10180Sstevel@tonic-gate 				if (nomocore)
10190Sstevel@tonic-gate 					return;
10200Sstevel@tonic-gate 				continue;
10210Sstevel@tonic-gate 			} else {
10220Sstevel@tonic-gate 				ep->lnum = dentry->d_ino;
10230Sstevel@tonic-gate 				for (j = 0; dentry->d_name[j] != '\0'; j++)
10240Sstevel@tonic-gate 					ep->ln.lname[j] = dentry->d_name[j];
10250Sstevel@tonic-gate 				ep->ln.lname[j] = '\0';
10260Sstevel@tonic-gate 			}
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 		if (errno) {
10290Sstevel@tonic-gate 			int sav_errno = errno;
10300Sstevel@tonic-gate 
10310Sstevel@tonic-gate 			(void) fprintf(stderr,
10320Sstevel@tonic-gate 			    gettext("ls: error reading directory %s: %s\n"),
10330Sstevel@tonic-gate 			    dir, strerror(sav_errno));
10340Sstevel@tonic-gate 		}
10350Sstevel@tonic-gate 		(void) closedir(dirf);
10360Sstevel@tonic-gate 		colwidth = fixedwidth + filewidth;
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate /*
10410Sstevel@tonic-gate  * Attaching a link to an inode's ancestors.  Search
10420Sstevel@tonic-gate  * through the ancestors to check for cycles (an inode which
10430Sstevel@tonic-gate  * we have already tracked in this inodes ancestry).  If a cycle
10440Sstevel@tonic-gate  * is detected, set the exit code and record the fact so that
10450Sstevel@tonic-gate  * it is reported at the right time when printing the directory.
10460Sstevel@tonic-gate  * In addition, set the exit code.  Note:  If the -a flag was
10470Sstevel@tonic-gate  * specified, we don't want to check for cycles for directories
10480Sstevel@tonic-gate  * ending in '/.' or '/..' unless they were specified on the
10490Sstevel@tonic-gate  * command line.
10500Sstevel@tonic-gate  */
10510Sstevel@tonic-gate static void
10520Sstevel@tonic-gate record_ancestry(char *file, struct stat *pstatb, struct lbuf *rep,
10530Sstevel@tonic-gate     int argfl, struct ditem *myparent)
10540Sstevel@tonic-gate {
10550Sstevel@tonic-gate 	size_t		file_len;
10560Sstevel@tonic-gate 	struct ditem	*myinfo;
10570Sstevel@tonic-gate 	struct ditem	*tptr;
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate 	file_len = strlen(file);
10600Sstevel@tonic-gate 	if (!aflg || argfl || (NOTWORKINGDIR(file, file_len) &&
10610Sstevel@tonic-gate 	    NOTPARENTDIR(file, file_len))) {
10620Sstevel@tonic-gate 		/*
10630Sstevel@tonic-gate 		 * Add this inode's ancestry
10640Sstevel@tonic-gate 		 * info and insert it into the
10650Sstevel@tonic-gate 		 * ancestry list by pointing
10660Sstevel@tonic-gate 		 * back to its parent.  We save
10670Sstevel@tonic-gate 		 * it (in rep) with the other info
10680Sstevel@tonic-gate 		 * we're gathering for this inode.
10690Sstevel@tonic-gate 		 */
10700Sstevel@tonic-gate 		if ((myinfo = malloc(
10710Sstevel@tonic-gate 		    sizeof (struct ditem))) == NULL) {
10720Sstevel@tonic-gate 			perror("ls");
10730Sstevel@tonic-gate 			exit(2);
10740Sstevel@tonic-gate 		}
10750Sstevel@tonic-gate 		myinfo->dev = pstatb->st_dev;
10760Sstevel@tonic-gate 		myinfo->ino = pstatb->st_ino;
10770Sstevel@tonic-gate 		myinfo->parent = myparent;
10780Sstevel@tonic-gate 		rep->ancinfo = myinfo;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 		/*
10810Sstevel@tonic-gate 		 * If this node has the same device id and
10820Sstevel@tonic-gate 		 * inode number of one of its ancestors,
10830Sstevel@tonic-gate 		 * then we've detected a cycle.
10840Sstevel@tonic-gate 		 */
10850Sstevel@tonic-gate 		if (myparent != NULL) {
10860Sstevel@tonic-gate 			for (tptr = myparent; tptr->parent != NULL;
10870Sstevel@tonic-gate 			    tptr = tptr->parent) {
10880Sstevel@tonic-gate 				if ((tptr->dev == pstatb->st_dev) &&
10890Sstevel@tonic-gate 				    (tptr->ino == pstatb->st_ino)) {
10900Sstevel@tonic-gate 					/*
10910Sstevel@tonic-gate 					 * Cycle detected for this
10920Sstevel@tonic-gate 					 * directory.  Record the fact
10930Sstevel@tonic-gate 					 * it is a cycle so we don't
10940Sstevel@tonic-gate 					 * try to process this
10950Sstevel@tonic-gate 					 * directory as we are
10960Sstevel@tonic-gate 					 * walking through the
10970Sstevel@tonic-gate 					 * list of directories.
10980Sstevel@tonic-gate 					 */
10990Sstevel@tonic-gate 					rep->cycle = 1;
11000Sstevel@tonic-gate 					err = 2;
11010Sstevel@tonic-gate 					break;
11020Sstevel@tonic-gate 				}
11030Sstevel@tonic-gate 			}
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate }
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate /*
11090Sstevel@tonic-gate  * get status of file and recomputes tblocks;
11100Sstevel@tonic-gate  * argfl = 1 if file is a name in ls-command and = 0
11110Sstevel@tonic-gate  * for filename in a directory whose name is an
11120Sstevel@tonic-gate  * argument in the command;
11130Sstevel@tonic-gate  * stores a pointer in flist[nfiles] and
11140Sstevel@tonic-gate  * returns that pointer;
11150Sstevel@tonic-gate  * returns NULL if failed;
11160Sstevel@tonic-gate  */
11170Sstevel@tonic-gate static struct lbuf *
11180Sstevel@tonic-gate gstat(char *file, int argfl, struct ditem *myparent)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	struct stat statb, statb1;
11210Sstevel@tonic-gate 	struct lbuf *rep;
11220Sstevel@tonic-gate 	char buf[BUFSIZ];
11230Sstevel@tonic-gate 	ssize_t cc;
11240Sstevel@tonic-gate 	int (*statf)() = ((Lflg) || (Hflg && argfl)) ? stat : lstat;
11250Sstevel@tonic-gate 	int aclcnt;
1126789Sahrens 	int error;
11270Sstevel@tonic-gate 	aclent_t *tp;
11280Sstevel@tonic-gate 	o_mode_t groupperm, mask;
11290Sstevel@tonic-gate 	int grouppermfound, maskfound;
11300Sstevel@tonic-gate 
11310Sstevel@tonic-gate 	if (nomocore)
11320Sstevel@tonic-gate 		return (NULL);
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	if (nfiles >= maxfils) {
11350Sstevel@tonic-gate 		/*
11360Sstevel@tonic-gate 		 * all flist/lbuf pair assigned files, time to get some
11370Sstevel@tonic-gate 		 * more space
11380Sstevel@tonic-gate 		 */
11390Sstevel@tonic-gate 		maxfils += quantn;
11400Sstevel@tonic-gate 		if (((flist = realloc(flist,
11410Sstevel@tonic-gate 		    maxfils * sizeof (struct lbuf *))) == NULL) ||
11420Sstevel@tonic-gate 		    ((nxtlbf = malloc(quantn *
11430Sstevel@tonic-gate 		    sizeof (struct lbuf))) == NULL)) {
11440Sstevel@tonic-gate 			perror("ls");
11450Sstevel@tonic-gate 			nomocore = 1;
11460Sstevel@tonic-gate 			return (NULL);
11470Sstevel@tonic-gate 		}
11480Sstevel@tonic-gate 	}
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 	/*
11510Sstevel@tonic-gate 	 * nfiles is reset to nargs for each directory
11520Sstevel@tonic-gate 	 * that is given as an argument maxn is checked
11530Sstevel@tonic-gate 	 * to prevent the assignment of an lbuf to a flist entry
11540Sstevel@tonic-gate 	 * that already has one assigned.
11550Sstevel@tonic-gate 	 */
11560Sstevel@tonic-gate 	if (nfiles >= maxn) {
11570Sstevel@tonic-gate 		rep = nxtlbf++;
11580Sstevel@tonic-gate 		flist[nfiles++] = rep;
11590Sstevel@tonic-gate 		maxn = nfiles;
11600Sstevel@tonic-gate 	} else {
11610Sstevel@tonic-gate 		rep = flist[nfiles++];
11620Sstevel@tonic-gate 	}
11630Sstevel@tonic-gate 	rep->lflags = (mode_t)0;
11640Sstevel@tonic-gate 	rep->flinkto = NULL;
11650Sstevel@tonic-gate 	rep->cycle = 0;
11660Sstevel@tonic-gate 	if (argfl || statreq) {
11670Sstevel@tonic-gate 		int doacl;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 		if (lflg)
11700Sstevel@tonic-gate 			doacl = 1;
11710Sstevel@tonic-gate 		else
11720Sstevel@tonic-gate 			doacl = 0;
11730Sstevel@tonic-gate 		if ((*statf)(file, &statb) < 0) {
11740Sstevel@tonic-gate 			if (argfl || errno != ENOENT ||
11750Sstevel@tonic-gate 			    (Lflg && lstat(file, &statb) == 0)) {
11760Sstevel@tonic-gate 				/*
11770Sstevel@tonic-gate 				 * Avoid race between readdir and lstat.
11780Sstevel@tonic-gate 				 * Print error message in case of dangling link.
11790Sstevel@tonic-gate 				 */
11800Sstevel@tonic-gate 				perror(file);
11810Sstevel@tonic-gate 			}
11820Sstevel@tonic-gate 			nfiles--;
11830Sstevel@tonic-gate 			return (NULL);
11840Sstevel@tonic-gate 		}
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 		/*
11870Sstevel@tonic-gate 		 * If -H was specified, and the file linked to was
11880Sstevel@tonic-gate 		 * not a directory, then we need to get the info
11890Sstevel@tonic-gate 		 * for the symlink itself.
11900Sstevel@tonic-gate 		 */
11910Sstevel@tonic-gate 		if ((Hflg) && (argfl) &&
11920Sstevel@tonic-gate 		    ((statb.st_mode & S_IFMT) != S_IFDIR)) {
11930Sstevel@tonic-gate 			if (lstat(file, &statb) < 0) {
11940Sstevel@tonic-gate 				perror(file);
11950Sstevel@tonic-gate 			}
11960Sstevel@tonic-gate 		}
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 		rep->lnum = statb.st_ino;
11990Sstevel@tonic-gate 		rep->lsize = statb.st_size;
12000Sstevel@tonic-gate 		rep->lblocks = statb.st_blocks;
12010Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
12020Sstevel@tonic-gate 		case S_IFDIR:
12030Sstevel@tonic-gate 			rep->ltype = 'd';
12040Sstevel@tonic-gate 			if (Rflg) {
12050Sstevel@tonic-gate 				record_ancestry(file, &statb, rep,
12060Sstevel@tonic-gate 				    argfl, myparent);
12070Sstevel@tonic-gate 			}
12080Sstevel@tonic-gate 			break;
12090Sstevel@tonic-gate 		case S_IFBLK:
12100Sstevel@tonic-gate 			rep->ltype = 'b';
12110Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
12120Sstevel@tonic-gate 			break;
12130Sstevel@tonic-gate 		case S_IFCHR:
12140Sstevel@tonic-gate 			rep->ltype = 'c';
12150Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
12160Sstevel@tonic-gate 			break;
12170Sstevel@tonic-gate 		case S_IFIFO:
12180Sstevel@tonic-gate 			rep->ltype = 'p';
12190Sstevel@tonic-gate 			break;
12200Sstevel@tonic-gate 		case S_IFSOCK:
12210Sstevel@tonic-gate 			rep->ltype = 's';
12220Sstevel@tonic-gate 			rep->lsize = 0;
12230Sstevel@tonic-gate 			break;
12240Sstevel@tonic-gate 		case S_IFLNK:
12250Sstevel@tonic-gate 			/* symbolic links may not have ACLs, so elide acl() */
12260Sstevel@tonic-gate 			if ((Lflg == 0) || (Hflg == 0) ||
12270Sstevel@tonic-gate 			    ((Hflg) && (!argfl))) {
12280Sstevel@tonic-gate 				doacl = 0;
12290Sstevel@tonic-gate 			}
12300Sstevel@tonic-gate 			rep->ltype = 'l';
12310Sstevel@tonic-gate 			if (lflg) {
12320Sstevel@tonic-gate 				cc = readlink(file, buf, BUFSIZ);
12330Sstevel@tonic-gate 				if (cc >= 0) {
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 					/*
12360Sstevel@tonic-gate 					 * follow the symbolic link
12370Sstevel@tonic-gate 					 * to generate the appropriate
12380Sstevel@tonic-gate 					 * Fflg marker for the object
12390Sstevel@tonic-gate 					 * eg, /bin -> /sym/bin/
12400Sstevel@tonic-gate 					 */
12410Sstevel@tonic-gate 					if ((Fflg || pflg) &&
12420Sstevel@tonic-gate 					    (stat(file, &statb1) >= 0)) {
12430Sstevel@tonic-gate 						switch (statb1.st_mode &
12440Sstevel@tonic-gate 						    S_IFMT) {
12450Sstevel@tonic-gate 						case S_IFDIR:
12460Sstevel@tonic-gate 							buf[cc++] = '/';
12470Sstevel@tonic-gate 							break;
12480Sstevel@tonic-gate 						case S_IFSOCK:
12490Sstevel@tonic-gate 							buf[cc++] = '=';
12500Sstevel@tonic-gate 							break;
1251*1447Sakaplan 						case S_IFDOOR:
1252*1447Sakaplan 							buf[cc++] = '>';
1253*1447Sakaplan 							break;
1254*1447Sakaplan 						case S_IFIFO:
1255*1447Sakaplan 							buf[cc++] = '|';
1256*1447Sakaplan 							break;
12570Sstevel@tonic-gate 						default:
12580Sstevel@tonic-gate 							if ((statb1.st_mode &
12590Sstevel@tonic-gate 							    ~S_IFMT) &
12600Sstevel@tonic-gate 							    (S_IXUSR|S_IXGRP|
12610Sstevel@tonic-gate 							    S_IXOTH))
12620Sstevel@tonic-gate 								buf[cc++] = '*';
12630Sstevel@tonic-gate 							break;
12640Sstevel@tonic-gate 						}
12650Sstevel@tonic-gate 					}
12660Sstevel@tonic-gate 					buf[cc] = '\0';
12670Sstevel@tonic-gate 					rep->flinkto = strdup(buf);
12680Sstevel@tonic-gate 				}
12690Sstevel@tonic-gate 				break;
12700Sstevel@tonic-gate 			}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 			/*
12730Sstevel@tonic-gate 			 * ls /sym behaves differently from ls /sym/
12740Sstevel@tonic-gate 			 * when /sym is a symbolic link. This is fixed
12750Sstevel@tonic-gate 			 * when explicit arguments are specified.
12760Sstevel@tonic-gate 			 */
12770Sstevel@tonic-gate 
12780Sstevel@tonic-gate #ifdef XPG6
12790Sstevel@tonic-gate 			/* Do not follow a symlink when -F is specified */
12800Sstevel@tonic-gate 			if ((!argfl) || (argfl && Fflg) ||
12810Sstevel@tonic-gate 			    (stat(file, &statb1) < 0))
12820Sstevel@tonic-gate #else
12830Sstevel@tonic-gate 			/* Follow a symlink when -F is specified */
12840Sstevel@tonic-gate 			if (!argfl || stat(file, &statb1) < 0)
12850Sstevel@tonic-gate #endif /* XPG6 */
12860Sstevel@tonic-gate 				break;
12870Sstevel@tonic-gate 			if ((statb1.st_mode & S_IFMT) == S_IFDIR) {
12880Sstevel@tonic-gate 				statb = statb1;
12890Sstevel@tonic-gate 				rep->ltype = 'd';
12900Sstevel@tonic-gate 				rep->lsize = statb1.st_size;
12910Sstevel@tonic-gate 				if (Rflg) {
12920Sstevel@tonic-gate 					record_ancestry(file, &statb, rep,
12930Sstevel@tonic-gate 					    argfl, myparent);
12940Sstevel@tonic-gate 				}
12950Sstevel@tonic-gate 			}
12960Sstevel@tonic-gate 			break;
12970Sstevel@tonic-gate 		case S_IFDOOR:
12980Sstevel@tonic-gate 			rep->ltype = 'D';
12990Sstevel@tonic-gate 			break;
13000Sstevel@tonic-gate 		case S_IFREG:
13010Sstevel@tonic-gate 			rep->ltype = '-';
13020Sstevel@tonic-gate 			break;
13030Sstevel@tonic-gate 		case S_IFPORT:
13040Sstevel@tonic-gate 			rep->ltype = 'P';
13050Sstevel@tonic-gate 			break;
13060Sstevel@tonic-gate 		default:
13070Sstevel@tonic-gate 			rep->ltype = '?';
13080Sstevel@tonic-gate 			break;
13090Sstevel@tonic-gate 		}
13100Sstevel@tonic-gate 		rep->lflags = statb.st_mode & ~S_IFMT;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 		if (!S_ISREG(statb.st_mode))
13130Sstevel@tonic-gate 			rep->lflags |= LS_NOTREG;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 		/* ACL: check acl entries count */
13160Sstevel@tonic-gate 		if (doacl) {
13170Sstevel@tonic-gate 
1318789Sahrens 			error = acl_get(file, 0, &rep->aclp);
1319789Sahrens 			if (error) {
1320789Sahrens 				(void) fprintf(stderr,
1321789Sahrens 				    gettext("ls: can't read ACL on %s: %s\n"),
1322789Sahrens 				    file, acl_strerror(error));
1323789Sahrens 				return (NULL);
1324789Sahrens 			}
13250Sstevel@tonic-gate 
1326789Sahrens 			rep->acl = ' ';
13270Sstevel@tonic-gate 
1328789Sahrens 			if (rep->aclp &&
1329789Sahrens 			    ((acl_flags(rep->aclp) & ACL_IS_TRIVIAL) == 0)) {
1330789Sahrens 				rep->acl = '+';
13310Sstevel@tonic-gate 				/*
1332789Sahrens 				 * Special handling for ufs aka aclent_t ACL's
13330Sstevel@tonic-gate 				 */
1334789Sahrens 				if (rep->aclp &&
1335789Sahrens 				    acl_type(rep->aclp) == ACLENT_T) {
1336789Sahrens 					/*
1337789Sahrens 					 * For files with non-trivial acls, the
1338789Sahrens 					 * effective group permissions are the
1339789Sahrens 					 * intersection of the GROUP_OBJ value
1340789Sahrens 					 * and the CLASS_OBJ (acl mask) value.
1341789Sahrens 					 * Determine both the GROUP_OBJ and
1342789Sahrens 					 * CLASS_OBJ for this file and insert
1343789Sahrens 					 * the logical AND of those two values
1344789Sahrens 					 * in the group permissions field
1345789Sahrens 					 * of the lflags value for this file.
1346789Sahrens 					 */
1347789Sahrens 
1348789Sahrens 					/*
1349789Sahrens 					 * Until found in acl list, assume
1350789Sahrens 					 * maximum permissions for both group
1351789Sahrens 					 * a nd mask.  (Just in case the acl
1352789Sahrens 					 * lacks either value for some reason.)
1353789Sahrens 					 */
1354789Sahrens 					groupperm = 07;
1355789Sahrens 					mask = 07;
1356789Sahrens 					grouppermfound = 0;
1357789Sahrens 					maskfound = 0;
1358789Sahrens 					aclcnt = acl_cnt(rep->aclp);
1359789Sahrens 					for (tp =
1360789Sahrens 					    (aclent_t *)acl_data(rep->aclp);
1361789Sahrens 					    aclcnt--; tp++) {
1362789Sahrens 						if (tp->a_type == GROUP_OBJ) {
1363789Sahrens 							groupperm = tp->a_perm;
1364789Sahrens 							grouppermfound = 1;
1365789Sahrens 							continue;
1366789Sahrens 						}
1367789Sahrens 						if (tp->a_type == CLASS_OBJ) {
1368789Sahrens 							mask = tp->a_perm;
1369789Sahrens 							maskfound = 1;
1370789Sahrens 						}
1371789Sahrens 						if (grouppermfound && maskfound)
1372789Sahrens 							break;
13730Sstevel@tonic-gate 					}
1374789Sahrens 
13750Sstevel@tonic-gate 
1376789Sahrens 					/* reset all the group bits */
1377789Sahrens 					rep->lflags &= ~S_IRWXG;
13780Sstevel@tonic-gate 
1379789Sahrens 					/*
1380789Sahrens 					 * Now set them to the logical AND of
1381789Sahrens 					 * the GROUP_OBJ permissions and the
1382789Sahrens 					 * acl mask.
1383789Sahrens 					 */
13840Sstevel@tonic-gate 
1385789Sahrens 					rep->lflags |= (groupperm & mask) << 3;
13860Sstevel@tonic-gate 
1387789Sahrens 				}
13880Sstevel@tonic-gate 			}
13890Sstevel@tonic-gate 
13901420Smarks 			if (!vflg && !Vflg && rep->aclp) {
13911420Smarks 				acl_free(rep->aclp);
13921420Smarks 				rep->aclp = NULL;
13931420Smarks 			}
13941420Smarks 
13950Sstevel@tonic-gate 			if (atflg && pathconf(file, _PC_XATTR_EXISTS) == 1)
13960Sstevel@tonic-gate 				rep->acl = '@';
13970Sstevel@tonic-gate 		} else
13980Sstevel@tonic-gate 			rep->acl = ' ';
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		/* mask ISARG and other file-type bits */
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 		rep->luid = statb.st_uid;
14030Sstevel@tonic-gate 		rep->lgid = statb.st_gid;
14040Sstevel@tonic-gate 		rep->lnl = statb.st_nlink;
14050Sstevel@tonic-gate 		if (uflg)
14060Sstevel@tonic-gate 			rep->lmtime = statb.st_atim;
14070Sstevel@tonic-gate 		else if (cflg)
14080Sstevel@tonic-gate 			rep->lmtime = statb.st_ctim;
14090Sstevel@tonic-gate 		else
14100Sstevel@tonic-gate 			rep->lmtime = statb.st_mtim;
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 		if (rep->ltype != 'b' && rep->ltype != 'c')
14130Sstevel@tonic-gate 			tblocks += rep->lblocks;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	return (rep);
14160Sstevel@tonic-gate }
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate /*
14190Sstevel@tonic-gate  * returns pathname of the form dir/file;
14200Sstevel@tonic-gate  * dir and file are null-terminated strings.
14210Sstevel@tonic-gate  */
14220Sstevel@tonic-gate static char *
14230Sstevel@tonic-gate makename(char *dir, char *file)
14240Sstevel@tonic-gate {
14250Sstevel@tonic-gate 	/*
14260Sstevel@tonic-gate 	 * PATH_MAX is the maximum length of a path name.
14270Sstevel@tonic-gate 	 * MAXNAMLEN is the maximum length of any path name component.
14280Sstevel@tonic-gate 	 * Allocate space for both, plus the '/' in the middle
14290Sstevel@tonic-gate 	 * and the null character at the end.
14300Sstevel@tonic-gate 	 * dfile is static as this is returned by makename().
14310Sstevel@tonic-gate 	 */
14320Sstevel@tonic-gate 	static char dfile[PATH_MAX + 1 + MAXNAMLEN + 1];
14330Sstevel@tonic-gate 	char *dp, *fp;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	dp = dfile;
14360Sstevel@tonic-gate 	fp = dir;
14370Sstevel@tonic-gate 	while (*fp)
14380Sstevel@tonic-gate 		*dp++ = *fp++;
14390Sstevel@tonic-gate 	if (dp > dfile && *(dp - 1) != '/')
14400Sstevel@tonic-gate 		*dp++ = '/';
14410Sstevel@tonic-gate 	fp = file;
14420Sstevel@tonic-gate 	while (*fp)
14430Sstevel@tonic-gate 		*dp++ = *fp++;
14440Sstevel@tonic-gate 	*dp = '\0';
14450Sstevel@tonic-gate 	return (dfile);
14460Sstevel@tonic-gate }
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate #include <pwd.h>
14500Sstevel@tonic-gate #include <grp.h>
14510Sstevel@tonic-gate #include <utmpx.h>
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate struct	utmpx utmp;
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate #define	NMAX	(sizeof (utmp.ut_name))
14560Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
14570Sstevel@tonic-gate 
14580Sstevel@tonic-gate 
14590Sstevel@tonic-gate struct cachenode {		/* this struct must be zeroed before using */
14600Sstevel@tonic-gate 	struct cachenode *lesschild;	/* subtree whose entries < val */
14610Sstevel@tonic-gate 	struct cachenode *grtrchild;	/* subtree whose entries > val */
14620Sstevel@tonic-gate 	long val;			/* the uid or gid of this entry */
14630Sstevel@tonic-gate 	int initted;			/* name has been filled in */
14640Sstevel@tonic-gate 	char name[NMAX+1];		/* the string that val maps to */
14650Sstevel@tonic-gate };
14660Sstevel@tonic-gate static struct cachenode *names, *groups;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate static struct cachenode *
14690Sstevel@tonic-gate findincache(struct cachenode **head, long val)
14700Sstevel@tonic-gate {
14710Sstevel@tonic-gate 	struct cachenode **parent = head;
14720Sstevel@tonic-gate 	struct cachenode *c = *parent;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	while (c != NULL) {
14750Sstevel@tonic-gate 		if (val == c->val) {
14760Sstevel@tonic-gate 			/* found it */
14770Sstevel@tonic-gate 			return (c);
14780Sstevel@tonic-gate 		} else if (val < c->val) {
14790Sstevel@tonic-gate 			parent = &c->lesschild;
14800Sstevel@tonic-gate 			c = c->lesschild;
14810Sstevel@tonic-gate 		} else {
14820Sstevel@tonic-gate 			parent = &c->grtrchild;
14830Sstevel@tonic-gate 			c = c->grtrchild;
14840Sstevel@tonic-gate 		}
14850Sstevel@tonic-gate 	}
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/* not in the cache, make a new entry for it */
14880Sstevel@tonic-gate 	c = calloc(1, sizeof (struct cachenode));
14890Sstevel@tonic-gate 	if (c == NULL) {
14900Sstevel@tonic-gate 		perror("ls");
14910Sstevel@tonic-gate 		exit(2);
14920Sstevel@tonic-gate 	}
14930Sstevel@tonic-gate 	*parent = c;
14940Sstevel@tonic-gate 	c->val = val;
14950Sstevel@tonic-gate 	return (c);
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate /*
14990Sstevel@tonic-gate  * get name from cache, or passwd file for a given uid;
15000Sstevel@tonic-gate  * lastuid is set to uid.
15010Sstevel@tonic-gate  */
15020Sstevel@tonic-gate static char *
15030Sstevel@tonic-gate getname(uid_t uid)
15040Sstevel@tonic-gate {
15050Sstevel@tonic-gate 	struct passwd *pwent;
15060Sstevel@tonic-gate 	struct cachenode *c;
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	if ((uid == lastuid) && lastuname)
15090Sstevel@tonic-gate 		return (lastuname);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	c = findincache(&names, uid);
15120Sstevel@tonic-gate 	if (c->initted == 0) {
15130Sstevel@tonic-gate 		if ((pwent = getpwuid(uid)) != NULL) {
15140Sstevel@tonic-gate 			SCPYN(&c->name[0], pwent->pw_name);
15150Sstevel@tonic-gate 		} else {
15160Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)uid);
15170Sstevel@tonic-gate 		}
15180Sstevel@tonic-gate 		c->initted = 1;
15190Sstevel@tonic-gate 	}
15200Sstevel@tonic-gate 	lastuid = uid;
15210Sstevel@tonic-gate 	lastuname = &c->name[0];
15220Sstevel@tonic-gate 	return (lastuname);
15230Sstevel@tonic-gate }
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate /*
15260Sstevel@tonic-gate  * get name from cache, or group file for a given gid;
15270Sstevel@tonic-gate  * lastgid is set to gid.
15280Sstevel@tonic-gate  */
15290Sstevel@tonic-gate static char *
15300Sstevel@tonic-gate getgroup(gid_t gid)
15310Sstevel@tonic-gate {
15320Sstevel@tonic-gate 	struct group *grent;
15330Sstevel@tonic-gate 	struct cachenode *c;
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	if ((gid == lastgid) && lastgname)
15360Sstevel@tonic-gate 		return (lastgname);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	c = findincache(&groups, gid);
15390Sstevel@tonic-gate 	if (c->initted == 0) {
15400Sstevel@tonic-gate 		if ((grent = getgrgid(gid)) != NULL) {
15410Sstevel@tonic-gate 			SCPYN(&c->name[0], grent->gr_name);
15420Sstevel@tonic-gate 		} else {
15430Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)gid);
15440Sstevel@tonic-gate 		}
15450Sstevel@tonic-gate 		c->initted = 1;
15460Sstevel@tonic-gate 	}
15470Sstevel@tonic-gate 	lastgid = gid;
15480Sstevel@tonic-gate 	lastgname = &c->name[0];
15490Sstevel@tonic-gate 	return (lastgname);
15500Sstevel@tonic-gate }
15510Sstevel@tonic-gate 
15520Sstevel@tonic-gate /* return >0 if item pointed by pp2 should appear first */
15530Sstevel@tonic-gate static int
15540Sstevel@tonic-gate compar(struct lbuf **pp1, struct lbuf **pp2)
15550Sstevel@tonic-gate {
15560Sstevel@tonic-gate 	struct lbuf *p1, *p2;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	p1 = *pp1;
15590Sstevel@tonic-gate 	p2 = *pp2;
15600Sstevel@tonic-gate 	if (dflg == 0) {
15610Sstevel@tonic-gate /*
15620Sstevel@tonic-gate  * compare two names in ls-command one of which is file
15630Sstevel@tonic-gate  * and the other is a directory;
15640Sstevel@tonic-gate  * this portion is not used for comparing files within
15650Sstevel@tonic-gate  * a directory name of ls-command;
15660Sstevel@tonic-gate  */
15670Sstevel@tonic-gate 		if (p1->lflags&ISARG && p1->ltype == 'd') {
15680Sstevel@tonic-gate 			if (!(p2->lflags&ISARG && p2->ltype == 'd'))
15690Sstevel@tonic-gate 				return (1);
15700Sstevel@tonic-gate 		} else {
15710Sstevel@tonic-gate 			if (p2->lflags&ISARG && p2->ltype == 'd')
15720Sstevel@tonic-gate 				return (-1);
15730Sstevel@tonic-gate 		}
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	if (tflg) {
15760Sstevel@tonic-gate 		if (p2->lmtime.tv_sec > p1->lmtime.tv_sec)
15770Sstevel@tonic-gate 			return (rflg);
15780Sstevel@tonic-gate 		else if (p2->lmtime.tv_sec < p1->lmtime.tv_sec)
15790Sstevel@tonic-gate 			return (-rflg);
15800Sstevel@tonic-gate 		/* times are equal to the sec, check nsec */
15810Sstevel@tonic-gate 		if (p2->lmtime.tv_nsec > p1->lmtime.tv_nsec)
15820Sstevel@tonic-gate 			return (rflg);
15830Sstevel@tonic-gate 		else if (p2->lmtime.tv_nsec < p1->lmtime.tv_nsec)
15840Sstevel@tonic-gate 			return (-rflg);
15850Sstevel@tonic-gate 		/* if times are equal, fall through and sort by name */
15860Sstevel@tonic-gate 	} else if (Sflg) {
15870Sstevel@tonic-gate 		/*
15880Sstevel@tonic-gate 		 * The size stored in lsize can be either the
15890Sstevel@tonic-gate 		 * size or the major minor number (in the case of
15900Sstevel@tonic-gate 		 * block and character special devices).  If it's
15910Sstevel@tonic-gate 		 * a major minor number, then the size is considered
15920Sstevel@tonic-gate 		 * to be zero and we want to fall through and sort
15930Sstevel@tonic-gate 		 * by name.  In addition, if the size of p2 is equal
15940Sstevel@tonic-gate 		 * to the size of p1 we want to fall through and
15950Sstevel@tonic-gate 		 * sort by name.
15960Sstevel@tonic-gate 		 */
15970Sstevel@tonic-gate 		off_t	p1size = (p1->ltype == 'b') ||
15980Sstevel@tonic-gate 			    (p1->ltype == 'c') ? 0 : p1->lsize;
15990Sstevel@tonic-gate 		off_t	p2size = (p2->ltype == 'b') ||
16000Sstevel@tonic-gate 			    (p2->ltype == 'c') ? 0 : p2->lsize;
16010Sstevel@tonic-gate 		if (p2size > p1size) {
16020Sstevel@tonic-gate 			return (rflg);
16030Sstevel@tonic-gate 		} else if (p2size < p1size) {
16040Sstevel@tonic-gate 			return (-rflg);
16050Sstevel@tonic-gate 		}
16060Sstevel@tonic-gate 		/* Sizes are equal, fall through and sort by name. */
16070Sstevel@tonic-gate 	}
16080Sstevel@tonic-gate 	return (rflg * strcoll(
16090Sstevel@tonic-gate 	    p1->lflags & ISARG ? p1->ln.namep : p1->ln.lname,
16100Sstevel@tonic-gate 	    p2->lflags&ISARG ? p2->ln.namep : p2->ln.lname));
16110Sstevel@tonic-gate }
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate static void
16140Sstevel@tonic-gate pprintf(char *s1, char *s2)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s1);
16170Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s2);
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate static void
16210Sstevel@tonic-gate csi_pprintf(unsigned char *s)
16220Sstevel@tonic-gate {
16230Sstevel@tonic-gate 	unsigned char *cp;
16240Sstevel@tonic-gate 	char	c;
16250Sstevel@tonic-gate 	int	i;
16260Sstevel@tonic-gate 	int	c_len;
16270Sstevel@tonic-gate 	int	p_col;
16280Sstevel@tonic-gate 	wchar_t	pcode;
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate 	if (!qflg && !bflg) {
16310Sstevel@tonic-gate 		for (cp = s; *cp != '\0'; cp++) {
16320Sstevel@tonic-gate 			(void) putchar(*cp);
16330Sstevel@tonic-gate 			curcol++;
16340Sstevel@tonic-gate 		}
16350Sstevel@tonic-gate 		return;
16360Sstevel@tonic-gate 	}
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	for (cp = s; *cp; ) {
16390Sstevel@tonic-gate 		if (isascii(c = *cp)) {
16400Sstevel@tonic-gate 			if (!isprint(c)) {
16410Sstevel@tonic-gate 				if (qflg) {
16420Sstevel@tonic-gate 					c = '?';
16430Sstevel@tonic-gate 				} else {
16440Sstevel@tonic-gate 					curcol += 3;
16450Sstevel@tonic-gate 					(void) putc('\\', stdout);
16460Sstevel@tonic-gate 					c = '0' + ((*cp >> 6) & 07);
16470Sstevel@tonic-gate 					(void) putc(c, stdout);
16480Sstevel@tonic-gate 					c = '0' + ((*cp >> 3) & 07);
16490Sstevel@tonic-gate 					(void) putc(c, stdout);
16500Sstevel@tonic-gate 					c = '0' + (*cp & 07);
16510Sstevel@tonic-gate 				}
16520Sstevel@tonic-gate 			}
16530Sstevel@tonic-gate 			curcol++;
16540Sstevel@tonic-gate 			cp++;
16550Sstevel@tonic-gate 			(void) putc(c, stdout);
16560Sstevel@tonic-gate 			continue;
16570Sstevel@tonic-gate 		}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 		if ((c_len = mbtowc(&pcode, (char *)cp, MB_LEN_MAX)) <= 0) {
16600Sstevel@tonic-gate 			c_len = 1;
16610Sstevel@tonic-gate 			goto not_print;
16620Sstevel@tonic-gate 		}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 		if ((p_col = wcwidth(pcode)) > 0) {
16650Sstevel@tonic-gate 			(void) putwchar(pcode);
16660Sstevel@tonic-gate 			cp += c_len;
16670Sstevel@tonic-gate 			curcol += p_col;
16680Sstevel@tonic-gate 			continue;
16690Sstevel@tonic-gate 		}
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate not_print:
16720Sstevel@tonic-gate 		for (i = 0; i < c_len; i++) {
16730Sstevel@tonic-gate 			if (qflg) {
16740Sstevel@tonic-gate 				c = '?';
16750Sstevel@tonic-gate 			} else {
16760Sstevel@tonic-gate 				curcol += 3;
16770Sstevel@tonic-gate 				(void) putc('\\', stdout);
16780Sstevel@tonic-gate 				c = '0' + ((*cp >> 6) & 07);
16790Sstevel@tonic-gate 				(void) putc(c, stdout);
16800Sstevel@tonic-gate 				c = '0' + ((*cp >> 3) & 07);
16810Sstevel@tonic-gate 				(void) putc(c, stdout);
16820Sstevel@tonic-gate 				c = '0' + (*cp & 07);
16830Sstevel@tonic-gate 			}
16840Sstevel@tonic-gate 			curcol++;
16850Sstevel@tonic-gate 			(void) putc(c, stdout);
16860Sstevel@tonic-gate 			cp++;
16870Sstevel@tonic-gate 		}
16880Sstevel@tonic-gate 	}
16890Sstevel@tonic-gate }
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate static int
16920Sstevel@tonic-gate strcol(unsigned char *s1)
16930Sstevel@tonic-gate {
16940Sstevel@tonic-gate 	int	w;
16950Sstevel@tonic-gate 	int	w_col;
16960Sstevel@tonic-gate 	int	len;
16970Sstevel@tonic-gate 	wchar_t	wc;
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	w = 0;
17000Sstevel@tonic-gate 	while (*s1) {
17010Sstevel@tonic-gate 		if (isascii(*s1)) {
17020Sstevel@tonic-gate 			w++;
17030Sstevel@tonic-gate 			s1++;
17040Sstevel@tonic-gate 			continue;
17050Sstevel@tonic-gate 		}
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
17080Sstevel@tonic-gate 			w++;
17090Sstevel@tonic-gate 			s1++;
17100Sstevel@tonic-gate 			continue;
17110Sstevel@tonic-gate 		}
17120Sstevel@tonic-gate 
17130Sstevel@tonic-gate 		if ((w_col = wcwidth(wc)) < 0)
17140Sstevel@tonic-gate 			w_col = len;
17150Sstevel@tonic-gate 		s1 += len;
17160Sstevel@tonic-gate 		w += w_col;
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 	return (w);
17190Sstevel@tonic-gate }
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate /*
17220Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
17230Sstevel@tonic-gate  * result in the caller-supplied buffer.
17240Sstevel@tonic-gate  *
17250Sstevel@tonic-gate  * The number provided is a size in bytes.  The number is first
17260Sstevel@tonic-gate  * converted to an integral multiple of 'scale' bytes.  This new
17270Sstevel@tonic-gate  * number is then scaled down until it is small enough to be in a good
17280Sstevel@tonic-gate  * human readable format, i.e.  in the range 0 thru scale-1.  If the
17290Sstevel@tonic-gate  * number used to derive the final number is not a multiple of scale, and
17300Sstevel@tonic-gate  * the final number has only a single significant digit, we compute
17310Sstevel@tonic-gate  * tenths of units to provide a second significant digit.
17320Sstevel@tonic-gate  *
17330Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
17340Sstevel@tonic-gate  * converted to "-1".
17350Sstevel@tonic-gate  *
17360Sstevel@tonic-gate  * A pointer to the caller-supplied buffer is returned.
17370Sstevel@tonic-gate  */
17380Sstevel@tonic-gate static char *
17390Sstevel@tonic-gate number_to_scaled_string(
17400Sstevel@tonic-gate 			numbuf_t buf,		/* put the result here */
17410Sstevel@tonic-gate 			unsigned long long number, /* convert this number */
17420Sstevel@tonic-gate 			long scale)
17430Sstevel@tonic-gate {
17440Sstevel@tonic-gate 	unsigned long long save;
17450Sstevel@tonic-gate 	/* Measurement: kilo, mega, giga, tera, peta, exa */
17460Sstevel@tonic-gate 	char *uom = "KMGTPE";
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	if ((long long)number == (long long)-1) {
17490Sstevel@tonic-gate 		(void) strlcpy(buf, "-1", sizeof (numbuf_t));
17500Sstevel@tonic-gate 		return (buf);
17510Sstevel@tonic-gate 	}
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	save = number;
17540Sstevel@tonic-gate 	number = number / scale;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	/*
17570Sstevel@tonic-gate 	 * Now we have number as a count of scale units.
17580Sstevel@tonic-gate 	 * If no further scaling is necessary, we round up as appropriate.
17590Sstevel@tonic-gate 	 *
17600Sstevel@tonic-gate 	 * The largest value number could have had entering the routine is
17610Sstevel@tonic-gate 	 * 16 Exabytes, so running off the end of the uom array should
17620Sstevel@tonic-gate 	 * never happen.  We check for that, though, as a guard against
17630Sstevel@tonic-gate 	 * a breakdown elsewhere in the algorithm.
17640Sstevel@tonic-gate 	 */
17650Sstevel@tonic-gate 	if (number < (unsigned long long)scale) {
17660Sstevel@tonic-gate 		if ((save % scale) >= (unsigned long long)(scale / 2)) {
17670Sstevel@tonic-gate 			if (++number == (unsigned long long)scale) {
17680Sstevel@tonic-gate 				uom++;
17690Sstevel@tonic-gate 				number = 1;
17700Sstevel@tonic-gate 			}
17710Sstevel@tonic-gate 		}
17720Sstevel@tonic-gate 	} else {
17730Sstevel@tonic-gate 		while ((number >= (unsigned long long)scale) && (*uom != 'E')) {
17740Sstevel@tonic-gate 			uom++; /* next unit of measurement */
17750Sstevel@tonic-gate 			save = number;
17760Sstevel@tonic-gate 			/*
17770Sstevel@tonic-gate 			 * If we're over half way to the next unit of
17780Sstevel@tonic-gate 			 * 'scale' bytes (which means we should round
17790Sstevel@tonic-gate 			 * up), then adding half of 'scale' prior to
17800Sstevel@tonic-gate 			 * the division will push us into that next
17810Sstevel@tonic-gate 			 * unit of scale when we perform the division
17820Sstevel@tonic-gate 			 */
17830Sstevel@tonic-gate 			number = (number + (scale / 2)) / scale;
17840Sstevel@tonic-gate 		}
17850Sstevel@tonic-gate 	}
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	/* check if we should output a decimal place after the point */
17880Sstevel@tonic-gate 	if ((save / scale) < 10) {
17890Sstevel@tonic-gate 		/* snprintf() will round for us */
17900Sstevel@tonic-gate 		float fnum = (float)save / scale;
17910Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%2.1f%c",
17920Sstevel@tonic-gate 		    fnum, *uom);
17930Sstevel@tonic-gate 	} else {
17940Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%4llu%c",
17950Sstevel@tonic-gate 		    number, *uom);
17960Sstevel@tonic-gate 	}
17970Sstevel@tonic-gate 	return (buf);
17980Sstevel@tonic-gate }
1799