xref: /onnv-gate/usr/src/cmd/ls/ls.c (revision 6373:87d720ca7819)
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
51447Sakaplan  * Common Development and Distribution License (the "License").
61447Sakaplan  * 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 /*
225902Smarks  * Copyright 2008 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>
645331Samw #include <libnvpair.h>
655331Samw #include <libcmdutils.h>
665331Samw #include <attr.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #ifndef STANDALONE
690Sstevel@tonic-gate #define	TERMINFO
700Sstevel@tonic-gate #endif
710Sstevel@tonic-gate 
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate  * -DNOTERMINFO can be defined on the cc command line to prevent
740Sstevel@tonic-gate  * the use of terminfo.  This should be done on systems not having
755331Samw  * the terminfo feature(pre 6.0 systems ?).
760Sstevel@tonic-gate  * As a result, columnar listings assume 80 columns for output,
770Sstevel@tonic-gate  * unless told otherwise via the COLUMNS environment variable.
780Sstevel@tonic-gate  */
790Sstevel@tonic-gate #ifdef NOTERMINFO
800Sstevel@tonic-gate #undef TERMINFO
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #include <term.h>
840Sstevel@tonic-gate 
850Sstevel@tonic-gate #define	BFSIZE	16
860Sstevel@tonic-gate /* this bit equals 1 in lflags of structure lbuf if *namep is to be used */
870Sstevel@tonic-gate #define	ISARG	0100000
880Sstevel@tonic-gate 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * this flag has been added to manipulate the display of S instead of 'l' when
910Sstevel@tonic-gate  * the file is not a regular file and when group execution bit is off
920Sstevel@tonic-gate  */
930Sstevel@tonic-gate #define	LS_NOTREG	010000
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Date and time formats
980Sstevel@tonic-gate  *
990Sstevel@tonic-gate  * b --- abbreviated month name
1000Sstevel@tonic-gate  * e --- day number
1010Sstevel@tonic-gate  * Y --- year in the form ccyy
1020Sstevel@tonic-gate  * H --- hour(24-hour version)
1030Sstevel@tonic-gate  * M --- minute
1040Sstevel@tonic-gate  * F --- yyyy-mm-dd
1050Sstevel@tonic-gate  * T --- hh:mm:ss
1060Sstevel@tonic-gate  * z --- time zone as hours displacement from UTC
1070Sstevel@tonic-gate  * note that %F and %z are from the ISO C99 standard and are
1080Sstevel@tonic-gate  * not present in older C libraries
1090Sstevel@tonic-gate  */
1100Sstevel@tonic-gate #define	FORMAT1	 " %b %e  %Y "
1110Sstevel@tonic-gate #define	FORMAT2  " %b %e %H:%M "
1120Sstevel@tonic-gate #define	FORMAT3  " %b %e %T %Y "
1130Sstevel@tonic-gate #define	FORMAT4  " %%F %%T.%.09ld %%z "
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate #undef BUFSIZ
1160Sstevel@tonic-gate #define	BUFSIZ 4096
1170Sstevel@tonic-gate #define	NUMBER_WIDTH 40
1180Sstevel@tonic-gate #define	FMTSIZE 50
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate struct ditem {
1210Sstevel@tonic-gate 	dev_t	dev;			/* directory items device number */
1220Sstevel@tonic-gate 	ino_t	ino;			/* directory items inode number */
1230Sstevel@tonic-gate 	struct ditem *parent;		/* dir items ptr to its parent's info */
1240Sstevel@tonic-gate };
1255331Samw /* Holds boolean extended system attributes */
1265331Samw struct attrb {
1275331Samw 	char		*name;
1285331Samw };
1295331Samw /* Holds timestamp extended system attributes */
1305331Samw struct attrtm {
1315331Samw 	char		*name;
1325331Samw 	uint64_t	stm;
1335331Samw 	uint64_t	nstm;
1345331Samw };
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate struct	lbuf	{
1370Sstevel@tonic-gate 	union	{
1380Sstevel@tonic-gate 		char	lname[MAXNAMLEN]; /* used for filename in a directory */
1390Sstevel@tonic-gate 		char	*namep;		/* for name in ls-command; */
1400Sstevel@tonic-gate 	} ln;
1410Sstevel@tonic-gate 	char	ltype;		/* filetype */
1420Sstevel@tonic-gate 	ino_t	lnum;		/* inode number of file */
1430Sstevel@tonic-gate 	mode_t	lflags; 	/* 0777 bits used as r,w,x permissions */
1440Sstevel@tonic-gate 	nlink_t	lnl;		/* number of links to file */
1450Sstevel@tonic-gate 	uid_t	luid;
1460Sstevel@tonic-gate 	gid_t	lgid;
1470Sstevel@tonic-gate 	off_t	lsize;		/* filesize or major/minor dev numbers */
1480Sstevel@tonic-gate 	blkcnt_t	lblocks;	/* number of file blocks */
1490Sstevel@tonic-gate 	timestruc_t	lmtime;
1505331Samw 	timestruc_t	lat;
1515331Samw 	timestruc_t	lct;
1525331Samw 	timestruc_t	lmt;
1530Sstevel@tonic-gate 	char	*flinkto;	/* symbolic link contents */
1540Sstevel@tonic-gate 	char 	acl;		/* indicate there are additional acl entries */
1550Sstevel@tonic-gate 	int	cycle;		/* cycle detected flag */
1560Sstevel@tonic-gate 	struct ditem *ancinfo;	/* maintains ancestor info */
157789Sahrens 	acl_t *aclp;		/* ACL if present */
1585331Samw 	struct attrb *exttr;	/* boolean extended system attributes */
1595331Samw 	struct attrtm *extm;	/* timestamp extended system attributes */
1600Sstevel@tonic-gate };
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate struct dchain {
1630Sstevel@tonic-gate 	char *dc_name;		/* path name */
1640Sstevel@tonic-gate 	int cycle_detected;	/* cycle detected visiting this directory */
1650Sstevel@tonic-gate 	struct ditem *myancinfo;	/* this directory's ancestry info */
1660Sstevel@tonic-gate 	struct dchain *dc_next;	/* next directory in the chain */
1670Sstevel@tonic-gate };
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate /*
1700Sstevel@tonic-gate  * A numbuf_t is used when converting a number to a string representation
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate typedef char numbuf_t[NUMBER_WIDTH];
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate static struct dchain *dfirst;	/* start of the dir chain */
1750Sstevel@tonic-gate static struct dchain *cdfirst;	/* start of the current dir chain */
1760Sstevel@tonic-gate static struct dchain *dtemp;	/* temporary - used for linking */
1770Sstevel@tonic-gate static char *curdir;		/* the current directory */
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate static int	first = 1;	/* true if first line is not yet printed */
1800Sstevel@tonic-gate static int	nfiles = 0;	/* number of flist entries in current use */
1810Sstevel@tonic-gate static int	nargs = 0;	/* number of flist entries used for arguments */
1820Sstevel@tonic-gate static int	maxfils = 0;	/* number of flist/lbuf entries allocated */
1830Sstevel@tonic-gate static int	maxn = 0;	/* number of flist entries with lbufs asigned */
1840Sstevel@tonic-gate static int	quantn = 64;	/* allocation growth quantum */
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate static struct lbuf	*nxtlbf;	/* ptr to next lbuf to be assigned */
1870Sstevel@tonic-gate static struct lbuf	**flist;	/* ptr to list of lbuf pointers */
1880Sstevel@tonic-gate static struct lbuf	*gstat(char *, int, struct ditem *);
1890Sstevel@tonic-gate static char		*getname(uid_t);
1900Sstevel@tonic-gate static char		*getgroup(gid_t);
1910Sstevel@tonic-gate static char		*makename(char *, char *);
1920Sstevel@tonic-gate static void		pentry(struct lbuf *);
1930Sstevel@tonic-gate static void		column(void);
1940Sstevel@tonic-gate static void		pmode(mode_t aflag);
1950Sstevel@tonic-gate static void		selection(int *);
1960Sstevel@tonic-gate static void		new_line(void);
1970Sstevel@tonic-gate static void		rddir(char *, struct ditem *);
1980Sstevel@tonic-gate static int		strcol(unsigned char *);
1990Sstevel@tonic-gate static void		pem(struct lbuf **, struct lbuf **, int);
2000Sstevel@tonic-gate static void		pdirectory(char *, int, int, int, struct ditem *);
2010Sstevel@tonic-gate static struct cachenode *findincache(struct cachenode **, long);
2020Sstevel@tonic-gate static void		csi_pprintf(unsigned char *);
2030Sstevel@tonic-gate static void		pprintf(char *, char *);
2040Sstevel@tonic-gate static int		compar(struct lbuf **pp1, struct lbuf **pp2);
2050Sstevel@tonic-gate static char 		*number_to_scaled_string(numbuf_t buf,
2060Sstevel@tonic-gate 			    unsigned long long number,
2070Sstevel@tonic-gate 			    long scale);
2080Sstevel@tonic-gate static void		record_ancestry(char *, struct stat *, struct lbuf *,
2090Sstevel@tonic-gate 			    int, struct ditem *);
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate static int		aflg;
2120Sstevel@tonic-gate static int		atflg;
2130Sstevel@tonic-gate static int		bflg;
2140Sstevel@tonic-gate static int		cflg;
2150Sstevel@tonic-gate static int		dflg;
2160Sstevel@tonic-gate static int		eflg;
2170Sstevel@tonic-gate static int		fflg;
2180Sstevel@tonic-gate static int		gflg;
2190Sstevel@tonic-gate static int		hflg;
2200Sstevel@tonic-gate static int		iflg;
2210Sstevel@tonic-gate static int		lflg;
2220Sstevel@tonic-gate static int		mflg;
2230Sstevel@tonic-gate static int		nflg;
2240Sstevel@tonic-gate static int		oflg;
2250Sstevel@tonic-gate static int		pflg;
2260Sstevel@tonic-gate static int		qflg;
2270Sstevel@tonic-gate static int		rflg = 1; /* init to 1 for special use in compar */
2280Sstevel@tonic-gate static int		sflg;
2290Sstevel@tonic-gate static int		tflg;
2300Sstevel@tonic-gate static int		uflg;
2310Sstevel@tonic-gate static int		xflg;
2320Sstevel@tonic-gate static int		Aflg;
2330Sstevel@tonic-gate static int		Cflg;
2340Sstevel@tonic-gate static int		Eflg;
2350Sstevel@tonic-gate static int		Fflg;
2360Sstevel@tonic-gate static int		Hflg;
2370Sstevel@tonic-gate static int		Lflg;
2380Sstevel@tonic-gate static int		Rflg;
2390Sstevel@tonic-gate static int		Sflg;
240789Sahrens static int		vflg;
2411420Smarks static int		Vflg;
2425331Samw static int		saflg;		/* boolean extended system attr. */
2435331Samw static int		sacnt;		/* number of extended system attr. */
2445331Samw static int		copt;
2455331Samw static int		vopt;
2465331Samw static int		tmflg;		/* create time ext. system attr. */
2475331Samw static int		ctm;
2485331Samw static int		atm;
2495331Samw static int		mtm;
2505331Samw static int		crtm;
2515331Samw static int		alltm;
2520Sstevel@tonic-gate static long		hscale;
2530Sstevel@tonic-gate static mode_t		flags;
2540Sstevel@tonic-gate static int		err = 0;	/* Contains return code */
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate static uid_t		lastuid	= (uid_t)-1;
2570Sstevel@tonic-gate static gid_t		lastgid = (gid_t)-1;
2580Sstevel@tonic-gate static char		*lastuname = NULL;
2590Sstevel@tonic-gate static char		*lastgname = NULL;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /* statreq > 0 if any of sflg, (n)lflg, tflg, Sflg are on */
2620Sstevel@tonic-gate static int		statreq;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate static char		*dotp = ".";
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate static u_longlong_t 	tblocks; /* number of blocks of files in a directory */
2670Sstevel@tonic-gate static time_t		year, now;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate static int		num_cols = 80;
2700Sstevel@tonic-gate static int		colwidth;
2710Sstevel@tonic-gate static int		filewidth;
2720Sstevel@tonic-gate static int		fixedwidth;
2730Sstevel@tonic-gate static int		nomocore;
2740Sstevel@tonic-gate static int		curcol;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate static struct	winsize	win;
2770Sstevel@tonic-gate 
2785331Samw static char	time_buf[FMTSIZE];	/* array to hold day and time */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate #define	NOTWORKINGDIR(d, l)	(((l) < 2) || \
2810Sstevel@tonic-gate 				    (strcmp((d) + (l) - 2, "/.") != 0))
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate #define	NOTPARENTDIR(d, l)	(((l) < 3) || \
2840Sstevel@tonic-gate 				    (strcmp((d) + (l) - 3, "/..") != 0))
2855331Samw /* Extended system attributes support */
2865331Samw static int get_sysxattr(char *, struct lbuf *);
2875331Samw static void set_sysattrb_display(char *, boolean_t, struct lbuf *);
2885331Samw static void set_sysattrtm_display(char *, struct lbuf *);
2895331Samw static void format_time(const char *, time_t);
2905331Samw static void format_etime(const char *, time_t, time_t);
2915331Samw static void print_time(struct lbuf *);
2925331Samw static void format_attrtime(struct lbuf *);
2935331Samw static void *xmalloc(size_t, struct lbuf *);
2945331Samw static void free_sysattr(struct lbuf *);
2955331Samw static nvpair_t *pair;
2965331Samw static nvlist_t	*response;
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate int
2990Sstevel@tonic-gate main(int argc, char *argv[])
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	int		c;
3020Sstevel@tonic-gate 	int		i;
3030Sstevel@tonic-gate 	int		width;
3040Sstevel@tonic-gate 	int		amino = 0;
3050Sstevel@tonic-gate 	int		opterr = 0;
3060Sstevel@tonic-gate 	struct lbuf	*ep;
3070Sstevel@tonic-gate 	struct lbuf	lb;
3080Sstevel@tonic-gate 	struct ditem	*myinfo;
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3110Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3120Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3130Sstevel@tonic-gate #endif
3140Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3150Sstevel@tonic-gate #ifdef STANDALONE
3160Sstevel@tonic-gate 	if (argv[0][0] == '\0')
3170Sstevel@tonic-gate 		argc = getargv("ls", &argv, 0);
3180Sstevel@tonic-gate #endif
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate 	lb.lmtime.tv_sec = time(NULL);
3210Sstevel@tonic-gate 	lb.lmtime.tv_nsec = 0;
3220Sstevel@tonic-gate 	year = lb.lmtime.tv_sec - 6L*30L*24L*60L*60L; /* 6 months ago */
3230Sstevel@tonic-gate 	now = lb.lmtime.tv_sec + 60;
3240Sstevel@tonic-gate 	if (isatty(1)) {
3250Sstevel@tonic-gate 		Cflg = 1;
3260Sstevel@tonic-gate 		mflg = 0;
3270Sstevel@tonic-gate 	}
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	while ((c = getopt(argc, argv,
3305331Samw 	    "aAbcCdeEfFghHilLmnopqrRsStux1@vV/:%:")) != EOF)
3310Sstevel@tonic-gate 		switch (c) {
3320Sstevel@tonic-gate 		case 'a':
3330Sstevel@tonic-gate 			aflg++;
3340Sstevel@tonic-gate 			continue;
3350Sstevel@tonic-gate 		case 'A':
3360Sstevel@tonic-gate 			Aflg++;
3370Sstevel@tonic-gate 			continue;
3380Sstevel@tonic-gate 		case 'b':
3390Sstevel@tonic-gate 			bflg = 1;
3400Sstevel@tonic-gate 			qflg = 0;
3410Sstevel@tonic-gate 			continue;
3420Sstevel@tonic-gate 		case 'c':
3430Sstevel@tonic-gate 			uflg = 0;
3445331Samw 			atm = 0;
3455331Samw 			ctm = 0;
3465331Samw 			mtm = 0;
3475331Samw 			crtm = 0;
3480Sstevel@tonic-gate 			cflg++;
3490Sstevel@tonic-gate 			continue;
3500Sstevel@tonic-gate 		case 'C':
3510Sstevel@tonic-gate 			Cflg = 1;
3520Sstevel@tonic-gate 			mflg = 0;
3530Sstevel@tonic-gate #ifdef XPG4
3540Sstevel@tonic-gate 			lflg = 0;
3550Sstevel@tonic-gate #endif
3560Sstevel@tonic-gate 			continue;
3570Sstevel@tonic-gate 		case 'd':
3580Sstevel@tonic-gate 			dflg++;
3590Sstevel@tonic-gate 			continue;
3600Sstevel@tonic-gate 		case 'e':
3610Sstevel@tonic-gate 			eflg++;
3620Sstevel@tonic-gate 			lflg++;
3630Sstevel@tonic-gate 			statreq++;
3640Sstevel@tonic-gate 			Eflg = 0;
3650Sstevel@tonic-gate 			continue;
3660Sstevel@tonic-gate 		case 'E':
3670Sstevel@tonic-gate 			Eflg++;
3680Sstevel@tonic-gate 			lflg++;
3690Sstevel@tonic-gate 			statreq++;
3700Sstevel@tonic-gate 			eflg = 0;
3710Sstevel@tonic-gate 			continue;
3720Sstevel@tonic-gate 		case 'f':
3730Sstevel@tonic-gate 			fflg++;
3740Sstevel@tonic-gate 			continue;
3750Sstevel@tonic-gate 		case 'F':
3760Sstevel@tonic-gate 			Fflg++;
3770Sstevel@tonic-gate 			statreq++;
3780Sstevel@tonic-gate 			continue;
3790Sstevel@tonic-gate 		case 'g':
3800Sstevel@tonic-gate 			gflg++;
3810Sstevel@tonic-gate 			lflg++;
3820Sstevel@tonic-gate 			statreq++;
3830Sstevel@tonic-gate 			continue;
3840Sstevel@tonic-gate 		case 'h':
3850Sstevel@tonic-gate 			hflg++;
3860Sstevel@tonic-gate 			hscale = 1024;
3870Sstevel@tonic-gate 			continue;
3880Sstevel@tonic-gate 		case 'H':
3890Sstevel@tonic-gate 			Hflg++;
3900Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
3910Sstevel@tonic-gate 			Lflg = 0;
3920Sstevel@tonic-gate 			continue;
3930Sstevel@tonic-gate 		case 'i':
3940Sstevel@tonic-gate 			iflg++;
3950Sstevel@tonic-gate 			continue;
3960Sstevel@tonic-gate 		case 'l':
3970Sstevel@tonic-gate 			lflg++;
3980Sstevel@tonic-gate 			statreq++;
3990Sstevel@tonic-gate 			Cflg = 0;
4000Sstevel@tonic-gate 			xflg = 0;
4010Sstevel@tonic-gate 			mflg = 0;
4020Sstevel@tonic-gate 			atflg = 0;
4030Sstevel@tonic-gate 			continue;
4040Sstevel@tonic-gate 		case 'L':
4050Sstevel@tonic-gate 			Lflg++;
4060Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
4070Sstevel@tonic-gate 			Hflg = 0;
4080Sstevel@tonic-gate 			continue;
4090Sstevel@tonic-gate 		case 'm':
4100Sstevel@tonic-gate 			Cflg = 0;
4110Sstevel@tonic-gate 			mflg = 1;
4120Sstevel@tonic-gate #ifdef XPG4
4130Sstevel@tonic-gate 			lflg = 0;
4140Sstevel@tonic-gate #endif
4150Sstevel@tonic-gate 			continue;
4160Sstevel@tonic-gate 		case 'n':
4170Sstevel@tonic-gate 			nflg++;
4180Sstevel@tonic-gate 			lflg++;
4190Sstevel@tonic-gate 			statreq++;
4200Sstevel@tonic-gate 			Cflg = 0;
4210Sstevel@tonic-gate 			xflg = 0;
4220Sstevel@tonic-gate 			mflg = 0;
4230Sstevel@tonic-gate 			atflg = 0;
4240Sstevel@tonic-gate 			continue;
4250Sstevel@tonic-gate 		case 'o':
4260Sstevel@tonic-gate 			oflg++;
4270Sstevel@tonic-gate 			lflg++;
4280Sstevel@tonic-gate 			statreq++;
4290Sstevel@tonic-gate 			continue;
4300Sstevel@tonic-gate 		case 'p':
4310Sstevel@tonic-gate 			pflg++;
4320Sstevel@tonic-gate 			statreq++;
4330Sstevel@tonic-gate 			continue;
4340Sstevel@tonic-gate 		case 'q':
4350Sstevel@tonic-gate 			qflg = 1;
4360Sstevel@tonic-gate 			bflg = 0;
4370Sstevel@tonic-gate 			continue;
4380Sstevel@tonic-gate 		case 'r':
4390Sstevel@tonic-gate 			rflg = -1;
4400Sstevel@tonic-gate 			continue;
4410Sstevel@tonic-gate 		case 'R':
4420Sstevel@tonic-gate 			Rflg++;
4430Sstevel@tonic-gate 			statreq++;
4440Sstevel@tonic-gate 			continue;
4450Sstevel@tonic-gate 		case 's':
4460Sstevel@tonic-gate 			sflg++;
4470Sstevel@tonic-gate 			statreq++;
4480Sstevel@tonic-gate 			continue;
4490Sstevel@tonic-gate 		case 'S':
4500Sstevel@tonic-gate 			tflg = 0;
4510Sstevel@tonic-gate 			Sflg++;
4520Sstevel@tonic-gate 			statreq++;
4530Sstevel@tonic-gate 			continue;
4540Sstevel@tonic-gate 		case 't':
4550Sstevel@tonic-gate 			Sflg = 0;
4560Sstevel@tonic-gate 			tflg++;
4570Sstevel@tonic-gate 			statreq++;
4580Sstevel@tonic-gate 			continue;
4590Sstevel@tonic-gate 		case 'u':
4600Sstevel@tonic-gate 			cflg = 0;
4615331Samw 			atm = 0;
4625331Samw 			ctm = 0;
4635331Samw 			mtm = 0;
4645331Samw 			crtm = 0;
4650Sstevel@tonic-gate 			uflg++;
4660Sstevel@tonic-gate 			continue;
4671420Smarks 		case 'V':
4681420Smarks 			Vflg++;
4691420Smarks 			/*FALLTHROUGH*/
470789Sahrens 		case 'v':
471789Sahrens 			vflg++;
472789Sahrens #if !defined(XPG4)
473789Sahrens 			if (lflg)
474789Sahrens 				continue;
475789Sahrens #endif
476789Sahrens 			lflg++;
477789Sahrens 			statreq++;
478789Sahrens 			Cflg = 0;
479789Sahrens 			xflg = 0;
480789Sahrens 			mflg = 0;
481789Sahrens 			continue;
4820Sstevel@tonic-gate 		case 'x':
4830Sstevel@tonic-gate 			xflg = 1;
4840Sstevel@tonic-gate 			Cflg = 1;
4850Sstevel@tonic-gate 			mflg = 0;
4860Sstevel@tonic-gate #ifdef XPG4
4870Sstevel@tonic-gate 			lflg = 0;
4880Sstevel@tonic-gate #endif
4890Sstevel@tonic-gate 			continue;
4900Sstevel@tonic-gate 		case '1':
4910Sstevel@tonic-gate 			Cflg = 0;
4920Sstevel@tonic-gate 			continue;
4930Sstevel@tonic-gate 		case '@':
4940Sstevel@tonic-gate #if !defined(XPG4)
4950Sstevel@tonic-gate 			/*
4960Sstevel@tonic-gate 			 * -l has precedence over -@
4970Sstevel@tonic-gate 			 */
4980Sstevel@tonic-gate 			if (lflg)
4990Sstevel@tonic-gate 				continue;
5000Sstevel@tonic-gate #endif
5010Sstevel@tonic-gate 			atflg++;
5020Sstevel@tonic-gate 			lflg++;
5030Sstevel@tonic-gate 			statreq++;
5040Sstevel@tonic-gate 			Cflg = 0;
5050Sstevel@tonic-gate 			xflg = 0;
5060Sstevel@tonic-gate 			mflg = 0;
5070Sstevel@tonic-gate 			continue;
5085331Samw 		case '/':
5095331Samw 			saflg++;
5105331Samw 			if (optarg != NULL) {
5115331Samw 				if (strcmp(optarg, "c") == 0) {
5125331Samw 					copt++;
5135331Samw 					vopt = 0;
5145331Samw 				} else if (strcmp(optarg, "v") == 0) {
5155331Samw 					vopt++;
5165331Samw 					copt = 0;
5175331Samw 				} else
5185331Samw 					opterr++;
5195331Samw 			} else
5205331Samw 				opterr++;
5215331Samw 			lflg++;
5225331Samw 			statreq++;
5235331Samw 			Cflg = 0;
5245331Samw 			xflg = 0;
5255331Samw 			mflg = 0;
5265331Samw 			continue;
5275331Samw 		case '%':
5285331Samw 			tmflg++;
5295331Samw 			if (optarg != NULL) {
5305331Samw 				if (strcmp(optarg, "ctime") == 0) {
5315331Samw 					ctm++;
5325331Samw 					atm = 0;
5335331Samw 					mtm = 0;
5345331Samw 					crtm = 0;
5355331Samw 				} else if (strcmp(optarg, "atime") == 0) {
5365331Samw 					atm++;
5375331Samw 					ctm = 0;
5385331Samw 					mtm = 0;
5395331Samw 					crtm = 0;
5405331Samw 					uflg = 0;
5415331Samw 					cflg = 0;
5425331Samw 				} else if (strcmp(optarg, "mtime") == 0) {
5435331Samw 					mtm++;
5445331Samw 					atm = 0;
5455331Samw 					ctm = 0;
5465331Samw 					crtm = 0;
5475331Samw 					uflg = 0;
5485331Samw 					cflg = 0;
5495331Samw 				} else if (strcmp(optarg, "crtime") == 0) {
5505331Samw 					crtm++;
5515331Samw 					atm = 0;
5525331Samw 					ctm = 0;
5535331Samw 					mtm = 0;
5545331Samw 					uflg = 0;
5555331Samw 					cflg = 0;
5565331Samw 				} else if (strcmp(optarg, "all") == 0) {
5575331Samw 					alltm++;
5585331Samw 					atm = 0;
5595331Samw 					ctm = 0;
5605331Samw 					mtm = 0;
5615331Samw 					crtm = 0;
5625331Samw 				} else
5635331Samw 					opterr++;
5645331Samw 			} else
5655331Samw 				opterr++;
5665331Samw 
5675331Samw 			Sflg = 0;
5685331Samw 			statreq++;
5695331Samw 			mflg = 0;
5705331Samw 			continue;
5710Sstevel@tonic-gate 		case '?':
5720Sstevel@tonic-gate 			opterr++;
5730Sstevel@tonic-gate 			continue;
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate 	if (opterr) {
5760Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
5775331Samw 		    "usage: ls -aAbcCdeEfFghHilLmnopqrRsStuxvV1@/%[c | v]"
5785331Samw 		    "%%[atime | crtime | ctime | mtime | all]"
5795331Samw 		    " [files]\n"));
5800Sstevel@tonic-gate 		exit(2);
5810Sstevel@tonic-gate 	}
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 	if (fflg) {
5840Sstevel@tonic-gate 		aflg++;
5850Sstevel@tonic-gate 		lflg = 0;
5860Sstevel@tonic-gate 		sflg = 0;
5870Sstevel@tonic-gate 		tflg = 0;
5880Sstevel@tonic-gate 		Sflg = 0;
5890Sstevel@tonic-gate 		statreq = 0;
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	fixedwidth = 2;
5930Sstevel@tonic-gate 	if (pflg || Fflg)
5940Sstevel@tonic-gate 		fixedwidth++;
5950Sstevel@tonic-gate 	if (iflg)
5960Sstevel@tonic-gate 		fixedwidth += 11;
5970Sstevel@tonic-gate 	if (sflg)
5980Sstevel@tonic-gate 		fixedwidth += 5;
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (lflg) {
6010Sstevel@tonic-gate 		if (!gflg && !oflg)
6020Sstevel@tonic-gate 			gflg = oflg = 1;
6030Sstevel@tonic-gate 		else
6040Sstevel@tonic-gate 		if (gflg && oflg)
6050Sstevel@tonic-gate 			gflg = oflg = 0;
6060Sstevel@tonic-gate 		Cflg = mflg = 0;
6070Sstevel@tonic-gate 	}
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate 	if (Cflg || mflg) {
6100Sstevel@tonic-gate 		char *clptr;
6110Sstevel@tonic-gate 		if ((clptr = getenv("COLUMNS")) != NULL)
6120Sstevel@tonic-gate 			num_cols = atoi(clptr);
6130Sstevel@tonic-gate #ifdef TERMINFO
6140Sstevel@tonic-gate 		else {
6150Sstevel@tonic-gate 			if (ioctl(1, TIOCGWINSZ, &win) != -1)
6160Sstevel@tonic-gate 				num_cols = (win.ws_col == 0 ? 80 : win.ws_col);
6170Sstevel@tonic-gate 		}
6180Sstevel@tonic-gate #endif
6190Sstevel@tonic-gate 		if (num_cols < 20 || num_cols > 1000)
6200Sstevel@tonic-gate 			/* assume it is an error */
6210Sstevel@tonic-gate 			num_cols = 80;
6220Sstevel@tonic-gate 	}
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	/* allocate space for flist and the associated	*/
6250Sstevel@tonic-gate 	/* data structures (lbufs)			*/
6260Sstevel@tonic-gate 	maxfils = quantn;
6270Sstevel@tonic-gate 	if (((flist = malloc(maxfils * sizeof (struct lbuf *))) == NULL) ||
6280Sstevel@tonic-gate 	    ((nxtlbf = malloc(quantn * sizeof (struct lbuf))) == NULL)) {
6290Sstevel@tonic-gate 		perror("ls");
6300Sstevel@tonic-gate 		exit(2);
6310Sstevel@tonic-gate 	}
6320Sstevel@tonic-gate 	if ((amino = (argc-optind)) == 0) {
6330Sstevel@tonic-gate 					/*
6340Sstevel@tonic-gate 					 * case when no names are given
6350Sstevel@tonic-gate 					 * in ls-command and current
6360Sstevel@tonic-gate 					 * directory is to be used
6370Sstevel@tonic-gate 					 */
6380Sstevel@tonic-gate 		argv[optind] = dotp;
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	for (i = 0; i < (amino ? amino : 1); i++) {
6420Sstevel@tonic-gate 
6430Sstevel@tonic-gate 		/*
6440Sstevel@tonic-gate 		 * If we are recursing, we need to make sure we don't
6450Sstevel@tonic-gate 		 * get into an endless loop.  To keep track of the inodes
6460Sstevel@tonic-gate 		 * (actually, just the directories) visited, we
6470Sstevel@tonic-gate 		 * maintain a directory ancestry list for a file
6480Sstevel@tonic-gate 		 * hierarchy.  As we go deeper into the hierarchy,
6490Sstevel@tonic-gate 		 * a parent directory passes its directory list
6500Sstevel@tonic-gate 		 * info (device id, inode number, and a pointer to
6510Sstevel@tonic-gate 		 * its parent) to each of its children.  As we
6520Sstevel@tonic-gate 		 * process a child that is a directory, we save
6530Sstevel@tonic-gate 		 * its own personal directory list info.  We then
6540Sstevel@tonic-gate 		 * check to see if the child has already been
6550Sstevel@tonic-gate 		 * processed by comparing its device id and inode
6560Sstevel@tonic-gate 		 * number from its own personal directory list info
6570Sstevel@tonic-gate 		 * to that of each of its ancestors.  If there is a
6580Sstevel@tonic-gate 		 * match, then we know we've detected a cycle.
6590Sstevel@tonic-gate 		 */
6600Sstevel@tonic-gate 		if (Rflg) {
6610Sstevel@tonic-gate 			/*
6620Sstevel@tonic-gate 			 * This is the first parent in this lineage
6630Sstevel@tonic-gate 			 * (first in a directory hierarchy), so
6640Sstevel@tonic-gate 			 * this parent's parent doesn't exist.  We
6650Sstevel@tonic-gate 			 * only initialize myinfo when we are
6660Sstevel@tonic-gate 			 * recursing, otherwise it's not used.
6670Sstevel@tonic-gate 			 */
6680Sstevel@tonic-gate 			if ((myinfo = (struct ditem *)malloc(
6690Sstevel@tonic-gate 			    sizeof (struct ditem))) == NULL) {
6700Sstevel@tonic-gate 				perror("ls");
6710Sstevel@tonic-gate 				exit(2);
6720Sstevel@tonic-gate 			} else {
6730Sstevel@tonic-gate 				myinfo->dev = 0;
6740Sstevel@tonic-gate 				myinfo->ino = 0;
6750Sstevel@tonic-gate 				myinfo->parent = NULL;
6760Sstevel@tonic-gate 			}
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 		if (Cflg || mflg) {
6800Sstevel@tonic-gate 			width = strcol((unsigned char *)argv[optind]);
6810Sstevel@tonic-gate 			if (width > filewidth)
6820Sstevel@tonic-gate 				filewidth = width;
6830Sstevel@tonic-gate 		}
6840Sstevel@tonic-gate 		if ((ep = gstat((*argv[optind] ? argv[optind] : dotp),
6850Sstevel@tonic-gate 		    1, myinfo)) == NULL) {
6860Sstevel@tonic-gate 			if (nomocore)
6870Sstevel@tonic-gate 				exit(2);
6880Sstevel@tonic-gate 			err = 2;
6890Sstevel@tonic-gate 			optind++;
6900Sstevel@tonic-gate 			continue;
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		ep->ln.namep = (*argv[optind] ? argv[optind] : dotp);
6930Sstevel@tonic-gate 		ep->lflags |= ISARG;
6940Sstevel@tonic-gate 		optind++;
6950Sstevel@tonic-gate 		nargs++;	/* count good arguments stored in flist */
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	colwidth = fixedwidth + filewidth;
6980Sstevel@tonic-gate 	qsort(flist, (unsigned)nargs, sizeof (struct lbuf *),
6990Sstevel@tonic-gate 	    (int (*)(const void *, const void *))compar);
7000Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
7010Sstevel@tonic-gate 		if (flist[i]->ltype == 'd' && dflg == 0 || fflg)
7020Sstevel@tonic-gate 			break;
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 	pem(&flist[0], &flist[i], 0);
7050Sstevel@tonic-gate 	for (; i < nargs; i++) {
7060Sstevel@tonic-gate 		pdirectory(flist[i]->ln.namep, Rflg ||
7070Sstevel@tonic-gate 		    (amino > 1), nargs, 0, flist[i]->ancinfo);
7080Sstevel@tonic-gate 		if (nomocore)
7090Sstevel@tonic-gate 			exit(2);
7100Sstevel@tonic-gate 		/* -R: print subdirectories found */
7110Sstevel@tonic-gate 		while (dfirst || cdfirst) {
7120Sstevel@tonic-gate 			/* Place direct subdirs on front in right order */
7130Sstevel@tonic-gate 			while (cdfirst) {
7140Sstevel@tonic-gate 				/* reverse cdfirst onto front of dfirst */
7150Sstevel@tonic-gate 				dtemp = cdfirst;
7160Sstevel@tonic-gate 				cdfirst = cdfirst -> dc_next;
7170Sstevel@tonic-gate 				dtemp -> dc_next = dfirst;
7180Sstevel@tonic-gate 				dfirst = dtemp;
7190Sstevel@tonic-gate 			}
7200Sstevel@tonic-gate 			/* take off first dir on dfirst & print it */
7210Sstevel@tonic-gate 			dtemp = dfirst;
7220Sstevel@tonic-gate 			dfirst = dfirst->dc_next;
7230Sstevel@tonic-gate 			pdirectory(dtemp->dc_name, 1, nargs,
7240Sstevel@tonic-gate 			    dtemp->cycle_detected, dtemp->myancinfo);
7250Sstevel@tonic-gate 			if (nomocore)
7260Sstevel@tonic-gate 				exit(2);
7270Sstevel@tonic-gate 			free(dtemp->dc_name);
7280Sstevel@tonic-gate 			free(dtemp);
7290Sstevel@tonic-gate 		}
7300Sstevel@tonic-gate 	}
7310Sstevel@tonic-gate 	return (err);
7320Sstevel@tonic-gate }
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate /*
7350Sstevel@tonic-gate  * pdirectory: print the directory name, labelling it if title is
7360Sstevel@tonic-gate  * nonzero, using lp as the place to start reading in the dir.
7370Sstevel@tonic-gate  */
7380Sstevel@tonic-gate static void
7390Sstevel@tonic-gate pdirectory(char *name, int title, int lp, int cdetect, struct ditem *myinfo)
7400Sstevel@tonic-gate {
7410Sstevel@tonic-gate 	struct dchain *dp;
7420Sstevel@tonic-gate 	struct lbuf *ap;
7430Sstevel@tonic-gate 	char *pname;
7440Sstevel@tonic-gate 	int j;
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	filewidth = 0;
7470Sstevel@tonic-gate 	curdir = name;
7480Sstevel@tonic-gate 	if (title) {
7490Sstevel@tonic-gate 		if (!first)
7500Sstevel@tonic-gate 			(void) putc('\n', stdout);
7510Sstevel@tonic-gate 		pprintf(name, ":");
7520Sstevel@tonic-gate 		new_line();
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 	/*
7550Sstevel@tonic-gate 	 * If there was a cycle detected, then notify and don't report
7560Sstevel@tonic-gate 	 * further.
7570Sstevel@tonic-gate 	 */
7580Sstevel@tonic-gate 	if (cdetect) {
7590Sstevel@tonic-gate 		if (lflg || sflg) {
7600Sstevel@tonic-gate 			curcol += printf(gettext("total %d"), 0);
7610Sstevel@tonic-gate 			new_line();
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7640Sstevel@tonic-gate 		    "ls: cycle detected for %s\n"), name);
7650Sstevel@tonic-gate 		return;
7660Sstevel@tonic-gate 	}
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate 	nfiles = lp;
7690Sstevel@tonic-gate 	rddir(name, myinfo);
7700Sstevel@tonic-gate 	if (nomocore)
7710Sstevel@tonic-gate 		return;
7720Sstevel@tonic-gate 	if (fflg == 0)
7730Sstevel@tonic-gate 		qsort(&flist[lp], (unsigned)(nfiles - lp),
7740Sstevel@tonic-gate 		    sizeof (struct lbuf *),
7750Sstevel@tonic-gate 		    (int (*)(const void *, const void *))compar);
7760Sstevel@tonic-gate 	if (Rflg) {
7770Sstevel@tonic-gate 		for (j = nfiles - 1; j >= lp; j--) {
7780Sstevel@tonic-gate 			ap = flist[j];
7790Sstevel@tonic-gate 			if (ap->ltype == 'd' && strcmp(ap->ln.lname, ".") &&
7800Sstevel@tonic-gate 			    strcmp(ap->ln.lname, "..")) {
7810Sstevel@tonic-gate 				dp = malloc(sizeof (struct dchain));
7820Sstevel@tonic-gate 				if (dp == NULL) {
7830Sstevel@tonic-gate 					perror("ls");
7840Sstevel@tonic-gate 					exit(2);
7850Sstevel@tonic-gate 				}
7860Sstevel@tonic-gate 				pname = makename(curdir, ap->ln.lname);
7870Sstevel@tonic-gate 				if ((dp->dc_name = strdup(pname)) == NULL) {
7880Sstevel@tonic-gate 					perror("ls");
7890Sstevel@tonic-gate 					exit(2);
7900Sstevel@tonic-gate 				}
7910Sstevel@tonic-gate 				dp->cycle_detected = ap->cycle;
7920Sstevel@tonic-gate 				dp->myancinfo = ap->ancinfo;
7930Sstevel@tonic-gate 				dp->dc_next = dfirst;
7940Sstevel@tonic-gate 				dfirst = dp;
7950Sstevel@tonic-gate 			}
7960Sstevel@tonic-gate 		}
7970Sstevel@tonic-gate 	}
7980Sstevel@tonic-gate 	if (lflg || sflg) {
7990Sstevel@tonic-gate 		curcol += printf(gettext("total %llu"), tblocks);
8000Sstevel@tonic-gate 		new_line();
8010Sstevel@tonic-gate 	}
8020Sstevel@tonic-gate 	pem(&flist[lp], &flist[nfiles], lflg||sflg);
8030Sstevel@tonic-gate }
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate /*
8060Sstevel@tonic-gate  * pem: print 'em. Print a list of files (e.g. a directory) bounded
8070Sstevel@tonic-gate  * by slp and lp.
8080Sstevel@tonic-gate  */
8090Sstevel@tonic-gate static void
8100Sstevel@tonic-gate pem(struct lbuf **slp, struct lbuf **lp, int tot_flag)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate 	long row, nrows, i;
8130Sstevel@tonic-gate 	int col, ncols;
8140Sstevel@tonic-gate 	struct lbuf **ep;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	if (Cflg || mflg) {
8170Sstevel@tonic-gate 		if (colwidth > num_cols) {
8180Sstevel@tonic-gate 			ncols = 1;
8190Sstevel@tonic-gate 		} else {
8200Sstevel@tonic-gate 			ncols = num_cols / colwidth;
8210Sstevel@tonic-gate 		}
8220Sstevel@tonic-gate 	}
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	if (ncols == 1 || mflg || xflg || !Cflg) {
8250Sstevel@tonic-gate 		for (ep = slp; ep < lp; ep++)
8260Sstevel@tonic-gate 			pentry(*ep);
8270Sstevel@tonic-gate 		new_line();
8280Sstevel@tonic-gate 		return;
8290Sstevel@tonic-gate 	}
8300Sstevel@tonic-gate 	/* otherwise print -C columns */
8310Sstevel@tonic-gate 	if (tot_flag) {
8320Sstevel@tonic-gate 		slp--;
8330Sstevel@tonic-gate 		row = 1;
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	else
8360Sstevel@tonic-gate 		row = 0;
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate 	nrows = (lp - slp - 1) / ncols + 1;
8390Sstevel@tonic-gate 	for (i = 0; i < nrows; i++, row++) {
8400Sstevel@tonic-gate 		for (col = 0; col < ncols; col++) {
8410Sstevel@tonic-gate 			ep = slp + (nrows * col) + row;
8420Sstevel@tonic-gate 			if (ep < lp)
8430Sstevel@tonic-gate 				pentry(*ep);
8440Sstevel@tonic-gate 		}
8450Sstevel@tonic-gate 		new_line();
8460Sstevel@tonic-gate 	}
8470Sstevel@tonic-gate }
8480Sstevel@tonic-gate 
8490Sstevel@tonic-gate /*
8500Sstevel@tonic-gate  * print one output entry;
8510Sstevel@tonic-gate  * if uid/gid is not found in the appropriate
8520Sstevel@tonic-gate  * file(passwd/group), then print uid/gid instead of
8530Sstevel@tonic-gate  * user/group name;
8540Sstevel@tonic-gate  */
8550Sstevel@tonic-gate static void
8560Sstevel@tonic-gate pentry(struct lbuf *ap)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	struct lbuf *p;
8590Sstevel@tonic-gate 	numbuf_t hbuf;
8600Sstevel@tonic-gate 	char buf[BUFSIZ];
8610Sstevel@tonic-gate 	char *dmark = "";	/* Used if -p or -F option active */
8620Sstevel@tonic-gate 	char *cp;
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	p = ap;
8650Sstevel@tonic-gate 	column();
8660Sstevel@tonic-gate 	if (iflg)
8670Sstevel@tonic-gate 		if (mflg && !lflg)
8680Sstevel@tonic-gate 			curcol += printf("%llu ", (long long)p->lnum);
8690Sstevel@tonic-gate 		else
8700Sstevel@tonic-gate 			curcol += printf("%10llu ", (long long)p->lnum);
8710Sstevel@tonic-gate 	if (sflg)
8720Sstevel@tonic-gate 		curcol += printf((mflg && !lflg) ? "%lld " :
8735331Samw 		    (p->lblocks < 10000) ? "%4lld " : "%lld ",
8745331Samw 		    (p->ltype != 'b' && p->ltype != 'c') ?
8755331Samw 		    p->lblocks : 0LL);
8760Sstevel@tonic-gate 	if (lflg) {
8770Sstevel@tonic-gate 		(void) putchar(p->ltype);
8780Sstevel@tonic-gate 		curcol++;
8790Sstevel@tonic-gate 		pmode(p->lflags);
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 		/* ACL: additional access mode flag */
8820Sstevel@tonic-gate 		(void) putchar(p->acl);
8830Sstevel@tonic-gate 		curcol++;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		curcol += printf("%3lu ", (ulong_t)p->lnl);
8860Sstevel@tonic-gate 		if (oflg)
8870Sstevel@tonic-gate 			if (!nflg) {
8880Sstevel@tonic-gate 				cp = getname(p->luid);
8890Sstevel@tonic-gate 				curcol += printf("%-8s ", cp);
8900Sstevel@tonic-gate 			} else
8910Sstevel@tonic-gate 				curcol += printf("%-8lu ", (ulong_t)p->luid);
8920Sstevel@tonic-gate 		if (gflg)
8930Sstevel@tonic-gate 			if (!nflg) {
8940Sstevel@tonic-gate 				cp = getgroup(p->lgid);
8950Sstevel@tonic-gate 				curcol += printf("%-8s ", cp);
8960Sstevel@tonic-gate 			} else
8970Sstevel@tonic-gate 				curcol += printf("%-8lu ", (ulong_t)p->lgid);
8980Sstevel@tonic-gate 		if (p->ltype == 'b' || p->ltype == 'c') {
8990Sstevel@tonic-gate 			curcol += printf("%3u, %2u",
9000Sstevel@tonic-gate 			    (uint_t)major((dev_t)p->lsize),
9010Sstevel@tonic-gate 			    (uint_t)minor((dev_t)p->lsize));
9020Sstevel@tonic-gate 		} else if (hflg && (p->lsize >= hscale)) {
9030Sstevel@tonic-gate 			curcol += printf("%7s",
9040Sstevel@tonic-gate 			    number_to_scaled_string(hbuf, p->lsize, hscale));
9050Sstevel@tonic-gate 		} else {
9060Sstevel@tonic-gate 			curcol += printf((p->lsize < (off_t)10000000) ?
9070Sstevel@tonic-gate 			    "%7lld" : "%lld", p->lsize);
9080Sstevel@tonic-gate 		}
9095331Samw 		if (eflg)
9105331Samw 			format_time(FORMAT3, p->lmtime.tv_sec);
9115331Samw 		else if (Eflg)
9120Sstevel@tonic-gate 			/* fill in nanoseconds first */
9135331Samw 			format_etime(FORMAT4, p->lmtime.tv_sec,
9145331Samw 			    p->lmtime.tv_nsec);
9155331Samw 		else {
9160Sstevel@tonic-gate 			if ((p->lmtime.tv_sec < year) ||
9175331Samw 			    (p->lmtime.tv_sec > now))
9185331Samw 				format_time(FORMAT1, p->lmtime.tv_sec);
9195331Samw 			else
9205331Samw 				format_time(FORMAT2, p->lmtime.tv_sec);
9210Sstevel@tonic-gate 		}
9225331Samw 		/* format extended system attribute time */
9235331Samw 		if (tmflg && crtm)
9245331Samw 			format_attrtime(p);
9255331Samw 
9260Sstevel@tonic-gate 		curcol += printf("%s", time_buf);
9275331Samw 
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 	/*
9300Sstevel@tonic-gate 	 * prevent both "->" and trailing marks
9310Sstevel@tonic-gate 	 * from appearing
9320Sstevel@tonic-gate 	 */
9330Sstevel@tonic-gate 
9340Sstevel@tonic-gate 	if (pflg && p->ltype == 'd')
9350Sstevel@tonic-gate 		dmark = "/";
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate 	if (Fflg && !(lflg && p->flinkto)) {
9380Sstevel@tonic-gate 		if (p->ltype == 'd')
9390Sstevel@tonic-gate 			dmark = "/";
9400Sstevel@tonic-gate 		else if (p->ltype == 'D')
9410Sstevel@tonic-gate 			dmark = ">";
9420Sstevel@tonic-gate 		else if (p->ltype == 'p')
9430Sstevel@tonic-gate 			dmark = "|";
9440Sstevel@tonic-gate 		else if (p->ltype == 'l')
9450Sstevel@tonic-gate 			dmark = "@";
9460Sstevel@tonic-gate 		else if (p->ltype == 's')
9470Sstevel@tonic-gate 			dmark = "=";
9480Sstevel@tonic-gate 		else if (p->lflags & (S_IXUSR|S_IXGRP|S_IXOTH))
9490Sstevel@tonic-gate 			dmark = "*";
9500Sstevel@tonic-gate 		else
9510Sstevel@tonic-gate 			dmark = "";
9520Sstevel@tonic-gate 	}
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate 	if (lflg && p->flinkto) {
9550Sstevel@tonic-gate 		(void) strncpy(buf, " -> ", 4);
9560Sstevel@tonic-gate 		(void) strcpy(buf + 4, p->flinkto);
9570Sstevel@tonic-gate 		dmark = buf;
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	if (p->lflags & ISARG) {
9610Sstevel@tonic-gate 		if (qflg || bflg)
9620Sstevel@tonic-gate 			pprintf(p->ln.namep, dmark);
9630Sstevel@tonic-gate 		else {
9640Sstevel@tonic-gate 			(void) printf("%s%s", p->ln.namep, dmark);
9650Sstevel@tonic-gate 			curcol += strcol((unsigned char *)p->ln.namep);
9660Sstevel@tonic-gate 			curcol += strcol((unsigned char *)dmark);
9670Sstevel@tonic-gate 		}
9680Sstevel@tonic-gate 	} else {
9690Sstevel@tonic-gate 		if (qflg || bflg)
9700Sstevel@tonic-gate 			pprintf(p->ln.lname, dmark);
9710Sstevel@tonic-gate 		else {
9720Sstevel@tonic-gate 			(void) printf("%s%s", p->ln.lname, dmark);
9730Sstevel@tonic-gate 			curcol += strcol((unsigned char *)p->ln.lname);
9740Sstevel@tonic-gate 			curcol += strcol((unsigned char *)dmark);
9750Sstevel@tonic-gate 		}
9760Sstevel@tonic-gate 	}
977789Sahrens 
9785331Samw 	/* Display extended system attributes */
9795331Samw 	if (saflg) {
9805331Samw 		int i;
9815331Samw 
9825331Samw 		new_line();
9835331Samw 		(void) printf("	\t{");
9845331Samw 		if (p->exttr != NULL) {
9855331Samw 			int k = 0;
9865331Samw 			for (i = 0; i < sacnt; i++) {
9875331Samw 				if (p->exttr[i].name != NULL)
9885331Samw 					k++;
9895331Samw 			}
9905331Samw 			for (i = 0; i < sacnt; i++) {
9915331Samw 				if (p->exttr[i].name != NULL) {
9925331Samw 					(void) printf("%s", p->exttr[i].name);
9935331Samw 					k--;
9945331Samw 					if (vopt && (k != 0))
9955331Samw 						(void) printf(",");
9965331Samw 				}
9975331Samw 			}
9985331Samw 		}
9995331Samw 		(void) printf("}\n");
10005331Samw 	}
10015331Samw 	/* Display file timestamps and extended system attribute timestamps */
10025331Samw 	if (tmflg && alltm) {
10035331Samw 		new_line();
10045331Samw 		print_time(p);
10055331Samw 		new_line();
10065331Samw 	}
1007789Sahrens 	if (vflg) {
1008789Sahrens 		new_line();
1009789Sahrens 		if (p->aclp) {
10101420Smarks 			acl_printacl(p->aclp, num_cols, Vflg);
1011789Sahrens 		}
1012789Sahrens 	}
10135331Samw 	/* Free extended system attribute lists */
10145331Samw 	if (saflg || tmflg)
10155331Samw 		free_sysattr(p);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate /* print various r,w,x permissions */
10190Sstevel@tonic-gate static void
10200Sstevel@tonic-gate pmode(mode_t aflag)
10210Sstevel@tonic-gate {
10220Sstevel@tonic-gate 	/* these arrays are declared static to allow initializations */
10230Sstevel@tonic-gate 	static int	m0[] = { 1, S_IRUSR, 'r', '-' };
10240Sstevel@tonic-gate 	static int	m1[] = { 1, S_IWUSR, 'w', '-' };
10250Sstevel@tonic-gate 	static int	m2[] = { 3, S_ISUID|S_IXUSR, 's', S_IXUSR,
10260Sstevel@tonic-gate 	    'x', S_ISUID, 'S', '-' };
10270Sstevel@tonic-gate 	static int	m3[] = { 1, S_IRGRP, 'r', '-' };
10280Sstevel@tonic-gate 	static int	m4[] = { 1, S_IWGRP, 'w', '-' };
10290Sstevel@tonic-gate 	static int	m5[] = { 4, S_ISGID|S_IXGRP, 's', S_IXGRP,
10300Sstevel@tonic-gate 				'x', S_ISGID|LS_NOTREG, 'S',
10310Sstevel@tonic-gate #ifdef XPG4
10320Sstevel@tonic-gate 		S_ISGID, 'L', '-'};
10330Sstevel@tonic-gate #else
10340Sstevel@tonic-gate 		S_ISGID, 'l', '-'};
10350Sstevel@tonic-gate #endif
10360Sstevel@tonic-gate 	static int	m6[] = { 1, S_IROTH, 'r', '-' };
10370Sstevel@tonic-gate 	static int	m7[] = { 1, S_IWOTH, 'w', '-' };
10380Sstevel@tonic-gate 	static int	m8[] = { 3, S_ISVTX|S_IXOTH, 't', S_IXOTH,
10390Sstevel@tonic-gate 	    'x', S_ISVTX, 'T', '-'};
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	static int *m[] = { m0, m1, m2, m3, m4, m5, m6, m7, m8};
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	int **mp;
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	flags = aflag;
10460Sstevel@tonic-gate 	for (mp = &m[0]; mp < &m[sizeof (m) / sizeof (m[0])]; mp++)
10470Sstevel@tonic-gate 		selection(*mp);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate static void
10510Sstevel@tonic-gate selection(int *pairp)
10520Sstevel@tonic-gate {
10530Sstevel@tonic-gate 	int n;
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	n = *pairp++;
10560Sstevel@tonic-gate 	while (n-->0) {
10570Sstevel@tonic-gate 		if ((flags & *pairp) == *pairp) {
10580Sstevel@tonic-gate 			pairp++;
10590Sstevel@tonic-gate 			break;
10600Sstevel@tonic-gate 		} else {
10610Sstevel@tonic-gate 			pairp += 2;
10620Sstevel@tonic-gate 		}
10630Sstevel@tonic-gate 	}
10640Sstevel@tonic-gate 	(void) putchar(*pairp);
10650Sstevel@tonic-gate 	curcol++;
10660Sstevel@tonic-gate }
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate /*
10690Sstevel@tonic-gate  * column: get to the beginning of the next column.
10700Sstevel@tonic-gate  */
10710Sstevel@tonic-gate static void
10720Sstevel@tonic-gate column(void)
10730Sstevel@tonic-gate {
10740Sstevel@tonic-gate 	if (curcol == 0)
10750Sstevel@tonic-gate 		return;
10760Sstevel@tonic-gate 	if (mflg) {
10770Sstevel@tonic-gate 		(void) putc(',', stdout);
10780Sstevel@tonic-gate 		curcol++;
10790Sstevel@tonic-gate 		if (curcol + colwidth + 2 > num_cols) {
10800Sstevel@tonic-gate 			(void) putc('\n', stdout);
10810Sstevel@tonic-gate 			curcol = 0;
10820Sstevel@tonic-gate 			return;
10830Sstevel@tonic-gate 		}
10840Sstevel@tonic-gate 		(void) putc(' ', stdout);
10850Sstevel@tonic-gate 		curcol++;
10860Sstevel@tonic-gate 		return;
10870Sstevel@tonic-gate 	}
10880Sstevel@tonic-gate 	if (Cflg == 0) {
10890Sstevel@tonic-gate 		(void) putc('\n', stdout);
10900Sstevel@tonic-gate 		curcol = 0;
10910Sstevel@tonic-gate 		return;
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 	if ((curcol / colwidth + 2) * colwidth > num_cols) {
10940Sstevel@tonic-gate 		(void) putc('\n', stdout);
10950Sstevel@tonic-gate 		curcol = 0;
10960Sstevel@tonic-gate 		return;
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 	do {
10990Sstevel@tonic-gate 		(void) putc(' ', stdout);
11000Sstevel@tonic-gate 		curcol++;
11010Sstevel@tonic-gate 	} while (curcol % colwidth);
11020Sstevel@tonic-gate }
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate static void
11050Sstevel@tonic-gate new_line(void)
11060Sstevel@tonic-gate {
11070Sstevel@tonic-gate 	if (curcol) {
11080Sstevel@tonic-gate 		first = 0;
11090Sstevel@tonic-gate 		(void) putc('\n', stdout);
11100Sstevel@tonic-gate 		curcol = 0;
11110Sstevel@tonic-gate 	}
11120Sstevel@tonic-gate }
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate /*
11150Sstevel@tonic-gate  * read each filename in directory dir and store its
11160Sstevel@tonic-gate  * status in flist[nfiles]
11170Sstevel@tonic-gate  * use makename() to form pathname dir/filename;
11180Sstevel@tonic-gate  */
11190Sstevel@tonic-gate static void
11200Sstevel@tonic-gate rddir(char *dir, struct ditem *myinfo)
11210Sstevel@tonic-gate {
11220Sstevel@tonic-gate 	struct dirent *dentry;
11230Sstevel@tonic-gate 	DIR *dirf;
11240Sstevel@tonic-gate 	int j;
11250Sstevel@tonic-gate 	struct lbuf *ep;
11260Sstevel@tonic-gate 	int width;
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate 	if ((dirf = opendir(dir)) == NULL) {
11290Sstevel@tonic-gate 		(void) fflush(stdout);
11300Sstevel@tonic-gate 		perror(dir);
11310Sstevel@tonic-gate 		err = 2;
11320Sstevel@tonic-gate 		return;
11330Sstevel@tonic-gate 	} else {
11340Sstevel@tonic-gate 		tblocks = 0;
11350Sstevel@tonic-gate 		for (;;) {
11360Sstevel@tonic-gate 			errno = 0;
11370Sstevel@tonic-gate 			if ((dentry = readdir(dirf)) == NULL)
11380Sstevel@tonic-gate 				break;
11390Sstevel@tonic-gate 			if (aflg == 0 && dentry->d_name[0] == '.' &&
11400Sstevel@tonic-gate 			    (Aflg == 0 ||
11410Sstevel@tonic-gate 			    dentry->d_name[1] == '\0' ||
11420Sstevel@tonic-gate 			    dentry->d_name[1] == '.' &&
11430Sstevel@tonic-gate 			    dentry->d_name[2] == '\0'))
11440Sstevel@tonic-gate 				/*
11450Sstevel@tonic-gate 				 * check for directory items '.', '..',
11460Sstevel@tonic-gate 				 *  and items without valid inode-number;
11470Sstevel@tonic-gate 				 */
11480Sstevel@tonic-gate 				continue;
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate 			if (Cflg || mflg) {
11510Sstevel@tonic-gate 				width = strcol((unsigned char *)dentry->d_name);
11520Sstevel@tonic-gate 				if (width > filewidth)
11530Sstevel@tonic-gate 					filewidth = width;
11540Sstevel@tonic-gate 			}
11550Sstevel@tonic-gate 			ep = gstat(makename(dir, dentry->d_name), 0, myinfo);
11560Sstevel@tonic-gate 			if (ep == NULL) {
11570Sstevel@tonic-gate 				if (nomocore)
11585331Samw 					exit(2);
11590Sstevel@tonic-gate 				continue;
11600Sstevel@tonic-gate 			} else {
11610Sstevel@tonic-gate 				ep->lnum = dentry->d_ino;
11620Sstevel@tonic-gate 				for (j = 0; dentry->d_name[j] != '\0'; j++)
11630Sstevel@tonic-gate 					ep->ln.lname[j] = dentry->d_name[j];
11640Sstevel@tonic-gate 				ep->ln.lname[j] = '\0';
11650Sstevel@tonic-gate 			}
11660Sstevel@tonic-gate 		}
11670Sstevel@tonic-gate 		if (errno) {
11680Sstevel@tonic-gate 			int sav_errno = errno;
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate 			(void) fprintf(stderr,
11710Sstevel@tonic-gate 			    gettext("ls: error reading directory %s: %s\n"),
11720Sstevel@tonic-gate 			    dir, strerror(sav_errno));
11730Sstevel@tonic-gate 		}
11740Sstevel@tonic-gate 		(void) closedir(dirf);
11750Sstevel@tonic-gate 		colwidth = fixedwidth + filewidth;
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate }
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate /*
11800Sstevel@tonic-gate  * Attaching a link to an inode's ancestors.  Search
11810Sstevel@tonic-gate  * through the ancestors to check for cycles (an inode which
11820Sstevel@tonic-gate  * we have already tracked in this inodes ancestry).  If a cycle
11830Sstevel@tonic-gate  * is detected, set the exit code and record the fact so that
11840Sstevel@tonic-gate  * it is reported at the right time when printing the directory.
11850Sstevel@tonic-gate  * In addition, set the exit code.  Note:  If the -a flag was
11860Sstevel@tonic-gate  * specified, we don't want to check for cycles for directories
11870Sstevel@tonic-gate  * ending in '/.' or '/..' unless they were specified on the
11880Sstevel@tonic-gate  * command line.
11890Sstevel@tonic-gate  */
11900Sstevel@tonic-gate static void
11910Sstevel@tonic-gate record_ancestry(char *file, struct stat *pstatb, struct lbuf *rep,
11920Sstevel@tonic-gate     int argfl, struct ditem *myparent)
11930Sstevel@tonic-gate {
11940Sstevel@tonic-gate 	size_t		file_len;
11950Sstevel@tonic-gate 	struct ditem	*myinfo;
11960Sstevel@tonic-gate 	struct ditem	*tptr;
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	file_len = strlen(file);
11990Sstevel@tonic-gate 	if (!aflg || argfl || (NOTWORKINGDIR(file, file_len) &&
12000Sstevel@tonic-gate 	    NOTPARENTDIR(file, file_len))) {
12010Sstevel@tonic-gate 		/*
12020Sstevel@tonic-gate 		 * Add this inode's ancestry
12030Sstevel@tonic-gate 		 * info and insert it into the
12040Sstevel@tonic-gate 		 * ancestry list by pointing
12050Sstevel@tonic-gate 		 * back to its parent.  We save
12060Sstevel@tonic-gate 		 * it (in rep) with the other info
12070Sstevel@tonic-gate 		 * we're gathering for this inode.
12080Sstevel@tonic-gate 		 */
12090Sstevel@tonic-gate 		if ((myinfo = malloc(
12100Sstevel@tonic-gate 		    sizeof (struct ditem))) == NULL) {
12110Sstevel@tonic-gate 			perror("ls");
12120Sstevel@tonic-gate 			exit(2);
12130Sstevel@tonic-gate 		}
12140Sstevel@tonic-gate 		myinfo->dev = pstatb->st_dev;
12150Sstevel@tonic-gate 		myinfo->ino = pstatb->st_ino;
12160Sstevel@tonic-gate 		myinfo->parent = myparent;
12170Sstevel@tonic-gate 		rep->ancinfo = myinfo;
12180Sstevel@tonic-gate 
12190Sstevel@tonic-gate 		/*
12200Sstevel@tonic-gate 		 * If this node has the same device id and
12210Sstevel@tonic-gate 		 * inode number of one of its ancestors,
12220Sstevel@tonic-gate 		 * then we've detected a cycle.
12230Sstevel@tonic-gate 		 */
12240Sstevel@tonic-gate 		if (myparent != NULL) {
12250Sstevel@tonic-gate 			for (tptr = myparent; tptr->parent != NULL;
12260Sstevel@tonic-gate 			    tptr = tptr->parent) {
12270Sstevel@tonic-gate 				if ((tptr->dev == pstatb->st_dev) &&
12280Sstevel@tonic-gate 				    (tptr->ino == pstatb->st_ino)) {
12290Sstevel@tonic-gate 					/*
12300Sstevel@tonic-gate 					 * Cycle detected for this
12310Sstevel@tonic-gate 					 * directory.  Record the fact
12320Sstevel@tonic-gate 					 * it is a cycle so we don't
12330Sstevel@tonic-gate 					 * try to process this
12340Sstevel@tonic-gate 					 * directory as we are
12350Sstevel@tonic-gate 					 * walking through the
12360Sstevel@tonic-gate 					 * list of directories.
12370Sstevel@tonic-gate 					 */
12380Sstevel@tonic-gate 					rep->cycle = 1;
12390Sstevel@tonic-gate 					err = 2;
12400Sstevel@tonic-gate 					break;
12410Sstevel@tonic-gate 				}
12420Sstevel@tonic-gate 			}
12430Sstevel@tonic-gate 		}
12440Sstevel@tonic-gate 	}
12450Sstevel@tonic-gate }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate /*
12486178Sny155746  * Do re-calculate the mode for group for ACE_T type of acls.
12496178Sny155746  * This is because, if the server's FS happens to be UFS, supporting
12506178Sny155746  * POSIX ACL's, then it does a special calculation of group mode
12516178Sny155746  * to be the bitwise OR of CLASS_OBJ and GROUP_OBJ (see PSARC/2001/717.)
12526178Sny155746  *
12536178Sny155746  * This algorithm is from the NFSv4 ACL Draft. Here a part of that
12546178Sny155746  * algorithm is used for the group mode calculation only.
12556178Sny155746  * What is modified here from the algorithm is that only the
12566178Sny155746  * entries with flags ACE_GROUP are considered. For each entry
12576178Sny155746  * with ACE_GROUP flag, the first occurance of a specific access
12586178Sny155746  * is checked if it is allowed.
1259*6373Sny155746  * We are not interested in perms for user and other, as they
12606178Sny155746  * were taken from st_mode value.
12616178Sny155746  * We are not interested in a_who field of ACE, as we need just
12626178Sny155746  * unix mode bits for the group.
12636178Sny155746  */
1264*6373Sny155746 
1265*6373Sny155746 #define	OWNED_GROUP	(ACE_GROUP | ACE_IDENTIFIER_GROUP)
1266*6373Sny155746 #define	IS_TYPE_ALLOWED(type)	((type) == ACE_ACCESS_ALLOWED_ACE_TYPE)
1267*6373Sny155746 
12686178Sny155746 int
12696178Sny155746 grp_mask_to_mode(acl_t *acep)
12706178Sny155746 {
12716178Sny155746 	int mode = 0, seen = 0;
12726178Sny155746 	int acecnt;
1273*6373Sny155746 	int flags;
12746178Sny155746 	ace_t *ap;
12756178Sny155746 
12766178Sny155746 	acecnt = acl_cnt(acep);
12776178Sny155746 	for (ap = (ace_t *)acl_data(acep); acecnt--; ap++) {
1278*6373Sny155746 
1279*6373Sny155746 		if (ap->a_type != ACE_ACCESS_ALLOWED_ACE_TYPE &&
1280*6373Sny155746 		    ap->a_type != ACE_ACCESS_DENIED_ACE_TYPE)
1281*6373Sny155746 			continue;
1282*6373Sny155746 
1283*6373Sny155746 		if (ap->a_flags & ACE_INHERIT_ONLY_ACE)
1284*6373Sny155746 			continue;
1285*6373Sny155746 
1286*6373Sny155746 		/*
1287*6373Sny155746 		 * if it is first group@ or first everyone@
1288*6373Sny155746 		 * for each of read, write and execute, then
1289*6373Sny155746 		 * that will be the group mode bit.
1290*6373Sny155746 		 */
1291*6373Sny155746 		flags = ap->a_flags & ACE_TYPE_FLAGS;
1292*6373Sny155746 		if (flags == OWNED_GROUP || flags == ACE_EVERYONE) {
1293*6373Sny155746 			if (ap->a_access_mask & ACE_READ_DATA) {
1294*6373Sny155746 				if (!(seen & S_IRGRP)) {
1295*6373Sny155746 					seen |= S_IRGRP;
1296*6373Sny155746 					if (IS_TYPE_ALLOWED(ap->a_type))
1297*6373Sny155746 						mode |= S_IRGRP;
12986178Sny155746 				}
1299*6373Sny155746 			}
1300*6373Sny155746 			if (ap->a_access_mask & ACE_WRITE_DATA) {
1301*6373Sny155746 				if (!(seen & S_IWGRP)) {
1302*6373Sny155746 					seen |= S_IWGRP;
1303*6373Sny155746 					if (IS_TYPE_ALLOWED(ap->a_type))
1304*6373Sny155746 						mode |= S_IWGRP;
13056178Sny155746 				}
1306*6373Sny155746 			}
1307*6373Sny155746 			if (ap->a_access_mask & ACE_EXECUTE) {
1308*6373Sny155746 				if (!(seen & S_IXGRP)) {
1309*6373Sny155746 					seen |= S_IXGRP;
1310*6373Sny155746 					if (IS_TYPE_ALLOWED(ap->a_type))
1311*6373Sny155746 						mode |= S_IXGRP;
13126178Sny155746 				}
13136178Sny155746 			}
13146178Sny155746 		}
13156178Sny155746 	}
13166178Sny155746 	return (mode);
13176178Sny155746 }
13186178Sny155746 
13196178Sny155746 /*
13200Sstevel@tonic-gate  * get status of file and recomputes tblocks;
13210Sstevel@tonic-gate  * argfl = 1 if file is a name in ls-command and = 0
13220Sstevel@tonic-gate  * for filename in a directory whose name is an
13230Sstevel@tonic-gate  * argument in the command;
13240Sstevel@tonic-gate  * stores a pointer in flist[nfiles] and
13250Sstevel@tonic-gate  * returns that pointer;
13260Sstevel@tonic-gate  * returns NULL if failed;
13270Sstevel@tonic-gate  */
13280Sstevel@tonic-gate static struct lbuf *
13290Sstevel@tonic-gate gstat(char *file, int argfl, struct ditem *myparent)
13300Sstevel@tonic-gate {
13310Sstevel@tonic-gate 	struct stat statb, statb1;
13320Sstevel@tonic-gate 	struct lbuf *rep;
13330Sstevel@tonic-gate 	char buf[BUFSIZ];
13340Sstevel@tonic-gate 	ssize_t cc;
13350Sstevel@tonic-gate 	int (*statf)() = ((Lflg) || (Hflg && argfl)) ? stat : lstat;
13360Sstevel@tonic-gate 	int aclcnt;
1337789Sahrens 	int error;
13380Sstevel@tonic-gate 	aclent_t *tp;
13390Sstevel@tonic-gate 	o_mode_t groupperm, mask;
13400Sstevel@tonic-gate 	int grouppermfound, maskfound;
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	if (nomocore)
13430Sstevel@tonic-gate 		return (NULL);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	if (nfiles >= maxfils) {
13460Sstevel@tonic-gate 		/*
13470Sstevel@tonic-gate 		 * all flist/lbuf pair assigned files, time to get some
13480Sstevel@tonic-gate 		 * more space
13490Sstevel@tonic-gate 		 */
13500Sstevel@tonic-gate 		maxfils += quantn;
13510Sstevel@tonic-gate 		if (((flist = realloc(flist,
13520Sstevel@tonic-gate 		    maxfils * sizeof (struct lbuf *))) == NULL) ||
13530Sstevel@tonic-gate 		    ((nxtlbf = malloc(quantn *
13540Sstevel@tonic-gate 		    sizeof (struct lbuf))) == NULL)) {
13550Sstevel@tonic-gate 			perror("ls");
13560Sstevel@tonic-gate 			nomocore = 1;
13570Sstevel@tonic-gate 			return (NULL);
13580Sstevel@tonic-gate 		}
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	/*
13620Sstevel@tonic-gate 	 * nfiles is reset to nargs for each directory
13630Sstevel@tonic-gate 	 * that is given as an argument maxn is checked
13640Sstevel@tonic-gate 	 * to prevent the assignment of an lbuf to a flist entry
13650Sstevel@tonic-gate 	 * that already has one assigned.
13660Sstevel@tonic-gate 	 */
13670Sstevel@tonic-gate 	if (nfiles >= maxn) {
13680Sstevel@tonic-gate 		rep = nxtlbf++;
13690Sstevel@tonic-gate 		flist[nfiles++] = rep;
13700Sstevel@tonic-gate 		maxn = nfiles;
13710Sstevel@tonic-gate 	} else {
13720Sstevel@tonic-gate 		rep = flist[nfiles++];
13730Sstevel@tonic-gate 	}
13740Sstevel@tonic-gate 	rep->lflags = (mode_t)0;
13750Sstevel@tonic-gate 	rep->flinkto = NULL;
13760Sstevel@tonic-gate 	rep->cycle = 0;
13770Sstevel@tonic-gate 	if (argfl || statreq) {
13780Sstevel@tonic-gate 		int doacl;
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 		if (lflg)
13810Sstevel@tonic-gate 			doacl = 1;
13820Sstevel@tonic-gate 		else
13830Sstevel@tonic-gate 			doacl = 0;
13840Sstevel@tonic-gate 		if ((*statf)(file, &statb) < 0) {
13850Sstevel@tonic-gate 			if (argfl || errno != ENOENT ||
13860Sstevel@tonic-gate 			    (Lflg && lstat(file, &statb) == 0)) {
13870Sstevel@tonic-gate 				/*
13880Sstevel@tonic-gate 				 * Avoid race between readdir and lstat.
13890Sstevel@tonic-gate 				 * Print error message in case of dangling link.
13900Sstevel@tonic-gate 				 */
13910Sstevel@tonic-gate 				perror(file);
13920Sstevel@tonic-gate 			}
13930Sstevel@tonic-gate 			nfiles--;
13940Sstevel@tonic-gate 			return (NULL);
13950Sstevel@tonic-gate 		}
13960Sstevel@tonic-gate 
13970Sstevel@tonic-gate 		/*
13980Sstevel@tonic-gate 		 * If -H was specified, and the file linked to was
13990Sstevel@tonic-gate 		 * not a directory, then we need to get the info
14000Sstevel@tonic-gate 		 * for the symlink itself.
14010Sstevel@tonic-gate 		 */
14020Sstevel@tonic-gate 		if ((Hflg) && (argfl) &&
14030Sstevel@tonic-gate 		    ((statb.st_mode & S_IFMT) != S_IFDIR)) {
14040Sstevel@tonic-gate 			if (lstat(file, &statb) < 0) {
14050Sstevel@tonic-gate 				perror(file);
14060Sstevel@tonic-gate 			}
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 		rep->lnum = statb.st_ino;
14100Sstevel@tonic-gate 		rep->lsize = statb.st_size;
14110Sstevel@tonic-gate 		rep->lblocks = statb.st_blocks;
14120Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
14130Sstevel@tonic-gate 		case S_IFDIR:
14140Sstevel@tonic-gate 			rep->ltype = 'd';
14150Sstevel@tonic-gate 			if (Rflg) {
14160Sstevel@tonic-gate 				record_ancestry(file, &statb, rep,
14170Sstevel@tonic-gate 				    argfl, myparent);
14180Sstevel@tonic-gate 			}
14190Sstevel@tonic-gate 			break;
14200Sstevel@tonic-gate 		case S_IFBLK:
14210Sstevel@tonic-gate 			rep->ltype = 'b';
14220Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
14230Sstevel@tonic-gate 			break;
14240Sstevel@tonic-gate 		case S_IFCHR:
14250Sstevel@tonic-gate 			rep->ltype = 'c';
14260Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
14270Sstevel@tonic-gate 			break;
14280Sstevel@tonic-gate 		case S_IFIFO:
14290Sstevel@tonic-gate 			rep->ltype = 'p';
14300Sstevel@tonic-gate 			break;
14310Sstevel@tonic-gate 		case S_IFSOCK:
14320Sstevel@tonic-gate 			rep->ltype = 's';
14330Sstevel@tonic-gate 			rep->lsize = 0;
14340Sstevel@tonic-gate 			break;
14350Sstevel@tonic-gate 		case S_IFLNK:
14360Sstevel@tonic-gate 			/* symbolic links may not have ACLs, so elide acl() */
14370Sstevel@tonic-gate 			if ((Lflg == 0) || (Hflg == 0) ||
14380Sstevel@tonic-gate 			    ((Hflg) && (!argfl))) {
14390Sstevel@tonic-gate 				doacl = 0;
14400Sstevel@tonic-gate 			}
14410Sstevel@tonic-gate 			rep->ltype = 'l';
14420Sstevel@tonic-gate 			if (lflg) {
14430Sstevel@tonic-gate 				cc = readlink(file, buf, BUFSIZ);
14440Sstevel@tonic-gate 				if (cc >= 0) {
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate 					/*
14470Sstevel@tonic-gate 					 * follow the symbolic link
14480Sstevel@tonic-gate 					 * to generate the appropriate
14490Sstevel@tonic-gate 					 * Fflg marker for the object
14500Sstevel@tonic-gate 					 * eg, /bin -> /sym/bin/
14510Sstevel@tonic-gate 					 */
14520Sstevel@tonic-gate 					if ((Fflg || pflg) &&
14530Sstevel@tonic-gate 					    (stat(file, &statb1) >= 0)) {
14540Sstevel@tonic-gate 						switch (statb1.st_mode &
14550Sstevel@tonic-gate 						    S_IFMT) {
14560Sstevel@tonic-gate 						case S_IFDIR:
14570Sstevel@tonic-gate 							buf[cc++] = '/';
14580Sstevel@tonic-gate 							break;
14590Sstevel@tonic-gate 						case S_IFSOCK:
14600Sstevel@tonic-gate 							buf[cc++] = '=';
14610Sstevel@tonic-gate 							break;
14621447Sakaplan 						case S_IFDOOR:
14631447Sakaplan 							buf[cc++] = '>';
14641447Sakaplan 							break;
14651447Sakaplan 						case S_IFIFO:
14661447Sakaplan 							buf[cc++] = '|';
14671447Sakaplan 							break;
14680Sstevel@tonic-gate 						default:
14690Sstevel@tonic-gate 							if ((statb1.st_mode &
14700Sstevel@tonic-gate 							    ~S_IFMT) &
14710Sstevel@tonic-gate 							    (S_IXUSR|S_IXGRP|
14720Sstevel@tonic-gate 							    S_IXOTH))
14730Sstevel@tonic-gate 								buf[cc++] = '*';
14740Sstevel@tonic-gate 							break;
14750Sstevel@tonic-gate 						}
14760Sstevel@tonic-gate 					}
14770Sstevel@tonic-gate 					buf[cc] = '\0';
14780Sstevel@tonic-gate 					rep->flinkto = strdup(buf);
14790Sstevel@tonic-gate 				}
14800Sstevel@tonic-gate 				break;
14810Sstevel@tonic-gate 			}
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate 			/*
14840Sstevel@tonic-gate 			 * ls /sym behaves differently from ls /sym/
14850Sstevel@tonic-gate 			 * when /sym is a symbolic link. This is fixed
14860Sstevel@tonic-gate 			 * when explicit arguments are specified.
14870Sstevel@tonic-gate 			 */
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate #ifdef XPG6
14900Sstevel@tonic-gate 			/* Do not follow a symlink when -F is specified */
14910Sstevel@tonic-gate 			if ((!argfl) || (argfl && Fflg) ||
14920Sstevel@tonic-gate 			    (stat(file, &statb1) < 0))
14930Sstevel@tonic-gate #else
14940Sstevel@tonic-gate 			/* Follow a symlink when -F is specified */
14950Sstevel@tonic-gate 			if (!argfl || stat(file, &statb1) < 0)
14960Sstevel@tonic-gate #endif /* XPG6 */
14970Sstevel@tonic-gate 				break;
14980Sstevel@tonic-gate 			if ((statb1.st_mode & S_IFMT) == S_IFDIR) {
14990Sstevel@tonic-gate 				statb = statb1;
15000Sstevel@tonic-gate 				rep->ltype = 'd';
15010Sstevel@tonic-gate 				rep->lsize = statb1.st_size;
15020Sstevel@tonic-gate 				if (Rflg) {
15030Sstevel@tonic-gate 					record_ancestry(file, &statb, rep,
15040Sstevel@tonic-gate 					    argfl, myparent);
15050Sstevel@tonic-gate 				}
15060Sstevel@tonic-gate 			}
15070Sstevel@tonic-gate 			break;
15080Sstevel@tonic-gate 		case S_IFDOOR:
15090Sstevel@tonic-gate 			rep->ltype = 'D';
15100Sstevel@tonic-gate 			break;
15110Sstevel@tonic-gate 		case S_IFREG:
15120Sstevel@tonic-gate 			rep->ltype = '-';
15130Sstevel@tonic-gate 			break;
15140Sstevel@tonic-gate 		case S_IFPORT:
15150Sstevel@tonic-gate 			rep->ltype = 'P';
15160Sstevel@tonic-gate 			break;
15170Sstevel@tonic-gate 		default:
15180Sstevel@tonic-gate 			rep->ltype = '?';
15190Sstevel@tonic-gate 			break;
15200Sstevel@tonic-gate 		}
15210Sstevel@tonic-gate 		rep->lflags = statb.st_mode & ~S_IFMT;
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 		if (!S_ISREG(statb.st_mode))
15240Sstevel@tonic-gate 			rep->lflags |= LS_NOTREG;
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 		/* ACL: check acl entries count */
15270Sstevel@tonic-gate 		if (doacl) {
15280Sstevel@tonic-gate 
1529789Sahrens 			error = acl_get(file, 0, &rep->aclp);
1530789Sahrens 			if (error) {
1531789Sahrens 				(void) fprintf(stderr,
1532789Sahrens 				    gettext("ls: can't read ACL on %s: %s\n"),
1533789Sahrens 				    file, acl_strerror(error));
1534789Sahrens 				return (NULL);
1535789Sahrens 			}
15360Sstevel@tonic-gate 
1537789Sahrens 			rep->acl = ' ';
15380Sstevel@tonic-gate 
1539789Sahrens 			if (rep->aclp &&
1540789Sahrens 			    ((acl_flags(rep->aclp) & ACL_IS_TRIVIAL) == 0)) {
1541789Sahrens 				rep->acl = '+';
15420Sstevel@tonic-gate 				/*
1543789Sahrens 				 * Special handling for ufs aka aclent_t ACL's
15440Sstevel@tonic-gate 				 */
15456178Sny155746 				if (acl_type(rep->aclp) == ACLENT_T) {
1546789Sahrens 					/*
1547789Sahrens 					 * For files with non-trivial acls, the
1548789Sahrens 					 * effective group permissions are the
1549789Sahrens 					 * intersection of the GROUP_OBJ value
1550789Sahrens 					 * and the CLASS_OBJ (acl mask) value.
1551789Sahrens 					 * Determine both the GROUP_OBJ and
1552789Sahrens 					 * CLASS_OBJ for this file and insert
1553789Sahrens 					 * the logical AND of those two values
1554789Sahrens 					 * in the group permissions field
1555789Sahrens 					 * of the lflags value for this file.
1556789Sahrens 					 */
1557789Sahrens 
1558789Sahrens 					/*
1559789Sahrens 					 * Until found in acl list, assume
1560789Sahrens 					 * maximum permissions for both group
1561789Sahrens 					 * a nd mask.  (Just in case the acl
1562789Sahrens 					 * lacks either value for some reason.)
1563789Sahrens 					 */
1564789Sahrens 					groupperm = 07;
1565789Sahrens 					mask = 07;
1566789Sahrens 					grouppermfound = 0;
1567789Sahrens 					maskfound = 0;
1568789Sahrens 					aclcnt = acl_cnt(rep->aclp);
1569789Sahrens 					for (tp =
1570789Sahrens 					    (aclent_t *)acl_data(rep->aclp);
1571789Sahrens 					    aclcnt--; tp++) {
1572789Sahrens 						if (tp->a_type == GROUP_OBJ) {
1573789Sahrens 							groupperm = tp->a_perm;
1574789Sahrens 							grouppermfound = 1;
1575789Sahrens 							continue;
1576789Sahrens 						}
1577789Sahrens 						if (tp->a_type == CLASS_OBJ) {
1578789Sahrens 							mask = tp->a_perm;
1579789Sahrens 							maskfound = 1;
1580789Sahrens 						}
1581789Sahrens 						if (grouppermfound && maskfound)
1582789Sahrens 							break;
15830Sstevel@tonic-gate 					}
1584789Sahrens 
15850Sstevel@tonic-gate 
1586789Sahrens 					/* reset all the group bits */
1587789Sahrens 					rep->lflags &= ~S_IRWXG;
15880Sstevel@tonic-gate 
1589789Sahrens 					/*
1590789Sahrens 					 * Now set them to the logical AND of
1591789Sahrens 					 * the GROUP_OBJ permissions and the
1592789Sahrens 					 * acl mask.
1593789Sahrens 					 */
15940Sstevel@tonic-gate 
1595789Sahrens 					rep->lflags |= (groupperm & mask) << 3;
15960Sstevel@tonic-gate 
15976178Sny155746 				} else if (acl_type(rep->aclp) == ACE_T) {
15986178Sny155746 					int mode;
15996178Sny155746 					mode = grp_mask_to_mode(rep->aclp);
16006178Sny155746 					rep->lflags &= ~S_IRWXG;
16016178Sny155746 					rep->lflags |= mode;
1602789Sahrens 				}
16030Sstevel@tonic-gate 			}
16040Sstevel@tonic-gate 
16051420Smarks 			if (!vflg && !Vflg && rep->aclp) {
16061420Smarks 				acl_free(rep->aclp);
16071420Smarks 				rep->aclp = NULL;
16081420Smarks 			}
16091420Smarks 
16100Sstevel@tonic-gate 			if (atflg && pathconf(file, _PC_XATTR_EXISTS) == 1)
16110Sstevel@tonic-gate 				rep->acl = '@';
16125331Samw 
16130Sstevel@tonic-gate 		} else
16140Sstevel@tonic-gate 			rep->acl = ' ';
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate 		/* mask ISARG and other file-type bits */
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 		rep->luid = statb.st_uid;
16190Sstevel@tonic-gate 		rep->lgid = statb.st_gid;
16200Sstevel@tonic-gate 		rep->lnl = statb.st_nlink;
16215331Samw 		if (uflg || (tmflg && atm))
16220Sstevel@tonic-gate 			rep->lmtime = statb.st_atim;
16235331Samw 		else if (cflg || (tmflg && ctm))
16240Sstevel@tonic-gate 			rep->lmtime = statb.st_ctim;
16250Sstevel@tonic-gate 		else
16260Sstevel@tonic-gate 			rep->lmtime = statb.st_mtim;
16275331Samw 		rep->lat = statb.st_atim;
16285331Samw 		rep->lct = statb.st_ctim;
16295331Samw 		rep->lmt = statb.st_mtim;
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 		if (rep->ltype != 'b' && rep->ltype != 'c')
16320Sstevel@tonic-gate 			tblocks += rep->lblocks;
16335331Samw 
16345331Samw 		/* Get extended system attributes */
16355331Samw 
16365331Samw 		rep->exttr = NULL;
16375331Samw 		rep->extm = NULL;
16385331Samw 		if ((saflg || (tmflg && crtm) || (tmflg && alltm)) &&
16395331Samw 		    (sysattr_support(file, _PC_SATTR_EXISTS) == 1)) {
16405331Samw 			int i;
16415331Samw 
16425331Samw 			sacnt = attr_count();
16435331Samw 			/*
16445331Samw 			 * Allocate 'sacnt' size array to hold extended
16455331Samw 			 * system attribute name (verbose) or respective
16465331Samw 			 * symbol represenation (compact).
16475331Samw 			 */
16485331Samw 			rep->exttr = xmalloc(sacnt * sizeof (struct attrb),
16495331Samw 			    rep);
16505331Samw 
16515331Samw 			/* initialize boolean attribute list */
16525331Samw 			for (i = 0; i < sacnt; i++)
16535331Samw 				rep->exttr[i].name = NULL;
16545331Samw 			if (get_sysxattr(file, rep) != 0) {
16555331Samw 				(void) fprintf(stderr,
16565331Samw 				    gettext("ls:Failed to retrieve "
16575331Samw 				    "extended system attribute from "
16585331Samw 				    "%s\n"), file);
16595331Samw 				rep->exttr[0].name = xmalloc(2, rep);
16605331Samw 				(void) strlcpy(rep->exttr[0].name, "?", 2);
16615331Samw 			}
16625331Samw 		}
16630Sstevel@tonic-gate 	}
16640Sstevel@tonic-gate 	return (rep);
16650Sstevel@tonic-gate }
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate /*
16680Sstevel@tonic-gate  * returns pathname of the form dir/file;
16690Sstevel@tonic-gate  * dir and file are null-terminated strings.
16700Sstevel@tonic-gate  */
16710Sstevel@tonic-gate static char *
16720Sstevel@tonic-gate makename(char *dir, char *file)
16730Sstevel@tonic-gate {
16740Sstevel@tonic-gate 	/*
16750Sstevel@tonic-gate 	 * PATH_MAX is the maximum length of a path name.
16760Sstevel@tonic-gate 	 * MAXNAMLEN is the maximum length of any path name component.
16770Sstevel@tonic-gate 	 * Allocate space for both, plus the '/' in the middle
16780Sstevel@tonic-gate 	 * and the null character at the end.
16790Sstevel@tonic-gate 	 * dfile is static as this is returned by makename().
16800Sstevel@tonic-gate 	 */
16810Sstevel@tonic-gate 	static char dfile[PATH_MAX + 1 + MAXNAMLEN + 1];
16820Sstevel@tonic-gate 	char *dp, *fp;
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	dp = dfile;
16850Sstevel@tonic-gate 	fp = dir;
16860Sstevel@tonic-gate 	while (*fp)
16870Sstevel@tonic-gate 		*dp++ = *fp++;
16880Sstevel@tonic-gate 	if (dp > dfile && *(dp - 1) != '/')
16890Sstevel@tonic-gate 		*dp++ = '/';
16900Sstevel@tonic-gate 	fp = file;
16910Sstevel@tonic-gate 	while (*fp)
16920Sstevel@tonic-gate 		*dp++ = *fp++;
16930Sstevel@tonic-gate 	*dp = '\0';
16940Sstevel@tonic-gate 	return (dfile);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate #include <pwd.h>
16990Sstevel@tonic-gate #include <grp.h>
17000Sstevel@tonic-gate #include <utmpx.h>
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate struct	utmpx utmp;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate #define	NMAX	(sizeof (utmp.ut_name))
17050Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
17060Sstevel@tonic-gate 
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate struct cachenode {		/* this struct must be zeroed before using */
17090Sstevel@tonic-gate 	struct cachenode *lesschild;	/* subtree whose entries < val */
17100Sstevel@tonic-gate 	struct cachenode *grtrchild;	/* subtree whose entries > val */
17110Sstevel@tonic-gate 	long val;			/* the uid or gid of this entry */
17120Sstevel@tonic-gate 	int initted;			/* name has been filled in */
17130Sstevel@tonic-gate 	char name[NMAX+1];		/* the string that val maps to */
17140Sstevel@tonic-gate };
17150Sstevel@tonic-gate static struct cachenode *names, *groups;
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate static struct cachenode *
17180Sstevel@tonic-gate findincache(struct cachenode **head, long val)
17190Sstevel@tonic-gate {
17200Sstevel@tonic-gate 	struct cachenode **parent = head;
17210Sstevel@tonic-gate 	struct cachenode *c = *parent;
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	while (c != NULL) {
17240Sstevel@tonic-gate 		if (val == c->val) {
17250Sstevel@tonic-gate 			/* found it */
17260Sstevel@tonic-gate 			return (c);
17270Sstevel@tonic-gate 		} else if (val < c->val) {
17280Sstevel@tonic-gate 			parent = &c->lesschild;
17290Sstevel@tonic-gate 			c = c->lesschild;
17300Sstevel@tonic-gate 		} else {
17310Sstevel@tonic-gate 			parent = &c->grtrchild;
17320Sstevel@tonic-gate 			c = c->grtrchild;
17330Sstevel@tonic-gate 		}
17340Sstevel@tonic-gate 	}
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	/* not in the cache, make a new entry for it */
17370Sstevel@tonic-gate 	c = calloc(1, sizeof (struct cachenode));
17380Sstevel@tonic-gate 	if (c == NULL) {
17390Sstevel@tonic-gate 		perror("ls");
17400Sstevel@tonic-gate 		exit(2);
17410Sstevel@tonic-gate 	}
17420Sstevel@tonic-gate 	*parent = c;
17430Sstevel@tonic-gate 	c->val = val;
17440Sstevel@tonic-gate 	return (c);
17450Sstevel@tonic-gate }
17460Sstevel@tonic-gate 
17470Sstevel@tonic-gate /*
17480Sstevel@tonic-gate  * get name from cache, or passwd file for a given uid;
17490Sstevel@tonic-gate  * lastuid is set to uid.
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate static char *
17520Sstevel@tonic-gate getname(uid_t uid)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	struct passwd *pwent;
17550Sstevel@tonic-gate 	struct cachenode *c;
17560Sstevel@tonic-gate 
17570Sstevel@tonic-gate 	if ((uid == lastuid) && lastuname)
17580Sstevel@tonic-gate 		return (lastuname);
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate 	c = findincache(&names, uid);
17610Sstevel@tonic-gate 	if (c->initted == 0) {
17620Sstevel@tonic-gate 		if ((pwent = getpwuid(uid)) != NULL) {
17630Sstevel@tonic-gate 			SCPYN(&c->name[0], pwent->pw_name);
17640Sstevel@tonic-gate 		} else {
17650Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)uid);
17660Sstevel@tonic-gate 		}
17670Sstevel@tonic-gate 		c->initted = 1;
17680Sstevel@tonic-gate 	}
17690Sstevel@tonic-gate 	lastuid = uid;
17700Sstevel@tonic-gate 	lastuname = &c->name[0];
17710Sstevel@tonic-gate 	return (lastuname);
17720Sstevel@tonic-gate }
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate /*
17750Sstevel@tonic-gate  * get name from cache, or group file for a given gid;
17760Sstevel@tonic-gate  * lastgid is set to gid.
17770Sstevel@tonic-gate  */
17780Sstevel@tonic-gate static char *
17790Sstevel@tonic-gate getgroup(gid_t gid)
17800Sstevel@tonic-gate {
17810Sstevel@tonic-gate 	struct group *grent;
17820Sstevel@tonic-gate 	struct cachenode *c;
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	if ((gid == lastgid) && lastgname)
17850Sstevel@tonic-gate 		return (lastgname);
17860Sstevel@tonic-gate 
17870Sstevel@tonic-gate 	c = findincache(&groups, gid);
17880Sstevel@tonic-gate 	if (c->initted == 0) {
17890Sstevel@tonic-gate 		if ((grent = getgrgid(gid)) != NULL) {
17900Sstevel@tonic-gate 			SCPYN(&c->name[0], grent->gr_name);
17910Sstevel@tonic-gate 		} else {
17920Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)gid);
17930Sstevel@tonic-gate 		}
17940Sstevel@tonic-gate 		c->initted = 1;
17950Sstevel@tonic-gate 	}
17960Sstevel@tonic-gate 	lastgid = gid;
17970Sstevel@tonic-gate 	lastgname = &c->name[0];
17980Sstevel@tonic-gate 	return (lastgname);
17990Sstevel@tonic-gate }
18000Sstevel@tonic-gate 
18010Sstevel@tonic-gate /* return >0 if item pointed by pp2 should appear first */
18020Sstevel@tonic-gate static int
18030Sstevel@tonic-gate compar(struct lbuf **pp1, struct lbuf **pp2)
18040Sstevel@tonic-gate {
18050Sstevel@tonic-gate 	struct lbuf *p1, *p2;
18060Sstevel@tonic-gate 
18070Sstevel@tonic-gate 	p1 = *pp1;
18080Sstevel@tonic-gate 	p2 = *pp2;
18090Sstevel@tonic-gate 	if (dflg == 0) {
18100Sstevel@tonic-gate /*
18110Sstevel@tonic-gate  * compare two names in ls-command one of which is file
18120Sstevel@tonic-gate  * and the other is a directory;
18130Sstevel@tonic-gate  * this portion is not used for comparing files within
18140Sstevel@tonic-gate  * a directory name of ls-command;
18150Sstevel@tonic-gate  */
18160Sstevel@tonic-gate 		if (p1->lflags&ISARG && p1->ltype == 'd') {
18170Sstevel@tonic-gate 			if (!(p2->lflags&ISARG && p2->ltype == 'd'))
18180Sstevel@tonic-gate 				return (1);
18190Sstevel@tonic-gate 		} else {
18200Sstevel@tonic-gate 			if (p2->lflags&ISARG && p2->ltype == 'd')
18210Sstevel@tonic-gate 				return (-1);
18220Sstevel@tonic-gate 		}
18230Sstevel@tonic-gate 	}
18240Sstevel@tonic-gate 	if (tflg) {
18250Sstevel@tonic-gate 		if (p2->lmtime.tv_sec > p1->lmtime.tv_sec)
18260Sstevel@tonic-gate 			return (rflg);
18270Sstevel@tonic-gate 		else if (p2->lmtime.tv_sec < p1->lmtime.tv_sec)
18280Sstevel@tonic-gate 			return (-rflg);
18290Sstevel@tonic-gate 		/* times are equal to the sec, check nsec */
18300Sstevel@tonic-gate 		if (p2->lmtime.tv_nsec > p1->lmtime.tv_nsec)
18310Sstevel@tonic-gate 			return (rflg);
18320Sstevel@tonic-gate 		else if (p2->lmtime.tv_nsec < p1->lmtime.tv_nsec)
18330Sstevel@tonic-gate 			return (-rflg);
18340Sstevel@tonic-gate 		/* if times are equal, fall through and sort by name */
18350Sstevel@tonic-gate 	} else if (Sflg) {
18360Sstevel@tonic-gate 		/*
18370Sstevel@tonic-gate 		 * The size stored in lsize can be either the
18380Sstevel@tonic-gate 		 * size or the major minor number (in the case of
18390Sstevel@tonic-gate 		 * block and character special devices).  If it's
18400Sstevel@tonic-gate 		 * a major minor number, then the size is considered
18410Sstevel@tonic-gate 		 * to be zero and we want to fall through and sort
18420Sstevel@tonic-gate 		 * by name.  In addition, if the size of p2 is equal
18430Sstevel@tonic-gate 		 * to the size of p1 we want to fall through and
18440Sstevel@tonic-gate 		 * sort by name.
18450Sstevel@tonic-gate 		 */
18460Sstevel@tonic-gate 		off_t	p1size = (p1->ltype == 'b') ||
18475331Samw 		    (p1->ltype == 'c') ? 0 : p1->lsize;
18480Sstevel@tonic-gate 		off_t	p2size = (p2->ltype == 'b') ||
18495331Samw 		    (p2->ltype == 'c') ? 0 : p2->lsize;
18500Sstevel@tonic-gate 		if (p2size > p1size) {
18510Sstevel@tonic-gate 			return (rflg);
18520Sstevel@tonic-gate 		} else if (p2size < p1size) {
18530Sstevel@tonic-gate 			return (-rflg);
18540Sstevel@tonic-gate 		}
18550Sstevel@tonic-gate 		/* Sizes are equal, fall through and sort by name. */
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 	return (rflg * strcoll(
18580Sstevel@tonic-gate 	    p1->lflags & ISARG ? p1->ln.namep : p1->ln.lname,
18590Sstevel@tonic-gate 	    p2->lflags&ISARG ? p2->ln.namep : p2->ln.lname));
18600Sstevel@tonic-gate }
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate static void
18630Sstevel@tonic-gate pprintf(char *s1, char *s2)
18640Sstevel@tonic-gate {
18650Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s1);
18660Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s2);
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate static void
18700Sstevel@tonic-gate csi_pprintf(unsigned char *s)
18710Sstevel@tonic-gate {
18720Sstevel@tonic-gate 	unsigned char *cp;
18730Sstevel@tonic-gate 	char	c;
18740Sstevel@tonic-gate 	int	i;
18750Sstevel@tonic-gate 	int	c_len;
18760Sstevel@tonic-gate 	int	p_col;
18770Sstevel@tonic-gate 	wchar_t	pcode;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	if (!qflg && !bflg) {
18800Sstevel@tonic-gate 		for (cp = s; *cp != '\0'; cp++) {
18810Sstevel@tonic-gate 			(void) putchar(*cp);
18820Sstevel@tonic-gate 			curcol++;
18830Sstevel@tonic-gate 		}
18840Sstevel@tonic-gate 		return;
18850Sstevel@tonic-gate 	}
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	for (cp = s; *cp; ) {
18880Sstevel@tonic-gate 		if (isascii(c = *cp)) {
18890Sstevel@tonic-gate 			if (!isprint(c)) {
18900Sstevel@tonic-gate 				if (qflg) {
18910Sstevel@tonic-gate 					c = '?';
18920Sstevel@tonic-gate 				} else {
18930Sstevel@tonic-gate 					curcol += 3;
18940Sstevel@tonic-gate 					(void) putc('\\', stdout);
18950Sstevel@tonic-gate 					c = '0' + ((*cp >> 6) & 07);
18960Sstevel@tonic-gate 					(void) putc(c, stdout);
18970Sstevel@tonic-gate 					c = '0' + ((*cp >> 3) & 07);
18980Sstevel@tonic-gate 					(void) putc(c, stdout);
18990Sstevel@tonic-gate 					c = '0' + (*cp & 07);
19000Sstevel@tonic-gate 				}
19010Sstevel@tonic-gate 			}
19020Sstevel@tonic-gate 			curcol++;
19030Sstevel@tonic-gate 			cp++;
19040Sstevel@tonic-gate 			(void) putc(c, stdout);
19050Sstevel@tonic-gate 			continue;
19060Sstevel@tonic-gate 		}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 		if ((c_len = mbtowc(&pcode, (char *)cp, MB_LEN_MAX)) <= 0) {
19090Sstevel@tonic-gate 			c_len = 1;
19100Sstevel@tonic-gate 			goto not_print;
19110Sstevel@tonic-gate 		}
19120Sstevel@tonic-gate 
19130Sstevel@tonic-gate 		if ((p_col = wcwidth(pcode)) > 0) {
19140Sstevel@tonic-gate 			(void) putwchar(pcode);
19150Sstevel@tonic-gate 			cp += c_len;
19160Sstevel@tonic-gate 			curcol += p_col;
19170Sstevel@tonic-gate 			continue;
19180Sstevel@tonic-gate 		}
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate not_print:
19210Sstevel@tonic-gate 		for (i = 0; i < c_len; i++) {
19220Sstevel@tonic-gate 			if (qflg) {
19230Sstevel@tonic-gate 				c = '?';
19240Sstevel@tonic-gate 			} else {
19250Sstevel@tonic-gate 				curcol += 3;
19260Sstevel@tonic-gate 				(void) putc('\\', stdout);
19270Sstevel@tonic-gate 				c = '0' + ((*cp >> 6) & 07);
19280Sstevel@tonic-gate 				(void) putc(c, stdout);
19290Sstevel@tonic-gate 				c = '0' + ((*cp >> 3) & 07);
19300Sstevel@tonic-gate 				(void) putc(c, stdout);
19310Sstevel@tonic-gate 				c = '0' + (*cp & 07);
19320Sstevel@tonic-gate 			}
19330Sstevel@tonic-gate 			curcol++;
19340Sstevel@tonic-gate 			(void) putc(c, stdout);
19350Sstevel@tonic-gate 			cp++;
19360Sstevel@tonic-gate 		}
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate }
19390Sstevel@tonic-gate 
19400Sstevel@tonic-gate static int
19410Sstevel@tonic-gate strcol(unsigned char *s1)
19420Sstevel@tonic-gate {
19430Sstevel@tonic-gate 	int	w;
19440Sstevel@tonic-gate 	int	w_col;
19450Sstevel@tonic-gate 	int	len;
19460Sstevel@tonic-gate 	wchar_t	wc;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	w = 0;
19490Sstevel@tonic-gate 	while (*s1) {
19500Sstevel@tonic-gate 		if (isascii(*s1)) {
19510Sstevel@tonic-gate 			w++;
19520Sstevel@tonic-gate 			s1++;
19530Sstevel@tonic-gate 			continue;
19540Sstevel@tonic-gate 		}
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
19570Sstevel@tonic-gate 			w++;
19580Sstevel@tonic-gate 			s1++;
19590Sstevel@tonic-gate 			continue;
19600Sstevel@tonic-gate 		}
19610Sstevel@tonic-gate 
19620Sstevel@tonic-gate 		if ((w_col = wcwidth(wc)) < 0)
19630Sstevel@tonic-gate 			w_col = len;
19640Sstevel@tonic-gate 		s1 += len;
19650Sstevel@tonic-gate 		w += w_col;
19660Sstevel@tonic-gate 	}
19670Sstevel@tonic-gate 	return (w);
19680Sstevel@tonic-gate }
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate /*
19710Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
19720Sstevel@tonic-gate  * result in the caller-supplied buffer.
19730Sstevel@tonic-gate  *
19740Sstevel@tonic-gate  * The number provided is a size in bytes.  The number is first
19750Sstevel@tonic-gate  * converted to an integral multiple of 'scale' bytes.  This new
19760Sstevel@tonic-gate  * number is then scaled down until it is small enough to be in a good
19770Sstevel@tonic-gate  * human readable format, i.e.  in the range 0 thru scale-1.  If the
19780Sstevel@tonic-gate  * number used to derive the final number is not a multiple of scale, and
19790Sstevel@tonic-gate  * the final number has only a single significant digit, we compute
19800Sstevel@tonic-gate  * tenths of units to provide a second significant digit.
19810Sstevel@tonic-gate  *
19820Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
19830Sstevel@tonic-gate  * converted to "-1".
19840Sstevel@tonic-gate  *
19850Sstevel@tonic-gate  * A pointer to the caller-supplied buffer is returned.
19860Sstevel@tonic-gate  */
19870Sstevel@tonic-gate static char *
19880Sstevel@tonic-gate number_to_scaled_string(
19890Sstevel@tonic-gate 			numbuf_t buf,		/* put the result here */
19900Sstevel@tonic-gate 			unsigned long long number, /* convert this number */
19910Sstevel@tonic-gate 			long scale)
19920Sstevel@tonic-gate {
19930Sstevel@tonic-gate 	unsigned long long save;
19940Sstevel@tonic-gate 	/* Measurement: kilo, mega, giga, tera, peta, exa */
19950Sstevel@tonic-gate 	char *uom = "KMGTPE";
19960Sstevel@tonic-gate 
19970Sstevel@tonic-gate 	if ((long long)number == (long long)-1) {
19980Sstevel@tonic-gate 		(void) strlcpy(buf, "-1", sizeof (numbuf_t));
19990Sstevel@tonic-gate 		return (buf);
20000Sstevel@tonic-gate 	}
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate 	save = number;
20030Sstevel@tonic-gate 	number = number / scale;
20040Sstevel@tonic-gate 
20050Sstevel@tonic-gate 	/*
20060Sstevel@tonic-gate 	 * Now we have number as a count of scale units.
20070Sstevel@tonic-gate 	 * If no further scaling is necessary, we round up as appropriate.
20080Sstevel@tonic-gate 	 *
20090Sstevel@tonic-gate 	 * The largest value number could have had entering the routine is
20100Sstevel@tonic-gate 	 * 16 Exabytes, so running off the end of the uom array should
20110Sstevel@tonic-gate 	 * never happen.  We check for that, though, as a guard against
20120Sstevel@tonic-gate 	 * a breakdown elsewhere in the algorithm.
20130Sstevel@tonic-gate 	 */
20140Sstevel@tonic-gate 	if (number < (unsigned long long)scale) {
20150Sstevel@tonic-gate 		if ((save % scale) >= (unsigned long long)(scale / 2)) {
20160Sstevel@tonic-gate 			if (++number == (unsigned long long)scale) {
20170Sstevel@tonic-gate 				uom++;
20180Sstevel@tonic-gate 				number = 1;
20190Sstevel@tonic-gate 			}
20200Sstevel@tonic-gate 		}
20210Sstevel@tonic-gate 	} else {
20220Sstevel@tonic-gate 		while ((number >= (unsigned long long)scale) && (*uom != 'E')) {
20230Sstevel@tonic-gate 			uom++; /* next unit of measurement */
20240Sstevel@tonic-gate 			save = number;
20250Sstevel@tonic-gate 			/*
20260Sstevel@tonic-gate 			 * If we're over half way to the next unit of
20270Sstevel@tonic-gate 			 * 'scale' bytes (which means we should round
20280Sstevel@tonic-gate 			 * up), then adding half of 'scale' prior to
20290Sstevel@tonic-gate 			 * the division will push us into that next
20300Sstevel@tonic-gate 			 * unit of scale when we perform the division
20310Sstevel@tonic-gate 			 */
20320Sstevel@tonic-gate 			number = (number + (scale / 2)) / scale;
20330Sstevel@tonic-gate 		}
20340Sstevel@tonic-gate 	}
20350Sstevel@tonic-gate 
20360Sstevel@tonic-gate 	/* check if we should output a decimal place after the point */
20370Sstevel@tonic-gate 	if ((save / scale) < 10) {
20380Sstevel@tonic-gate 		/* snprintf() will round for us */
20390Sstevel@tonic-gate 		float fnum = (float)save / scale;
20400Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%2.1f%c",
20410Sstevel@tonic-gate 		    fnum, *uom);
20420Sstevel@tonic-gate 	} else {
20430Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%4llu%c",
20440Sstevel@tonic-gate 		    number, *uom);
20450Sstevel@tonic-gate 	}
20460Sstevel@tonic-gate 	return (buf);
20470Sstevel@tonic-gate }
20485331Samw 
20495331Samw /* Get extended system attributes and set the display */
20505331Samw 
20515331Samw int
20525331Samw get_sysxattr(char *fname, struct lbuf *rep)
20535331Samw {
20545331Samw 	boolean_t	value;
20555331Samw 	data_type_t	type;
20565331Samw 	int		error;
20575331Samw 	char		*name;
20585331Samw 	int		i;
20595331Samw 
20605331Samw 	if ((error = getattrat(AT_FDCWD, XATTR_VIEW_READWRITE, fname,
20615331Samw 	    &response)) != 0) {
20625331Samw 		perror("ls:getattrat");
20635331Samw 		return (error);
20645331Samw 	}
20655331Samw 
20665331Samw 	/*
20675331Samw 	 * Allocate 'sacnt' size array to hold extended timestamp
20685331Samw 	 * system attributes and initialize the array.
20695331Samw 	 */
20705331Samw 	rep->extm = xmalloc(sacnt * sizeof (struct attrtm), rep);
20715331Samw 	for (i = 0; i < sacnt; i++) {
20725331Samw 		rep->extm[i].stm = 0;
20735331Samw 		rep->extm[i].nstm = 0;
20745331Samw 		rep->extm[i].name = NULL;
20755331Samw 	}
20765331Samw 	while ((pair = nvlist_next_nvpair(response, pair)) != NULL) {
20775331Samw 		name = nvpair_name(pair);
20785331Samw 		type = nvpair_type(pair);
20795331Samw 		if (type == DATA_TYPE_BOOLEAN_VALUE) {
20805331Samw 			error = nvpair_value_boolean_value(pair, &value);
20815331Samw 			if (error) {
20825331Samw 				(void) fprintf(stderr,
20835331Samw 				    gettext("nvpair_value_boolean_value "
20845331Samw 				    "failed: error = %d\n"), error);
20855331Samw 				continue;
20865331Samw 			}
20875331Samw 			if (name != NULL)
20885331Samw 				set_sysattrb_display(name, value, rep);
20895331Samw 			continue;
20905331Samw 		} else if (type == DATA_TYPE_UINT64_ARRAY) {
20915331Samw 			if (name != NULL)
20925331Samw 				set_sysattrtm_display(name, rep);
20935331Samw 			continue;
20945331Samw 		}
20955331Samw 	}
20965331Samw 	nvlist_free(response);
20975331Samw 	return (0);
20985331Samw }
20995331Samw 
21005331Samw /* Set extended system attribute boolean display */
21015331Samw 
21025331Samw void
21035331Samw set_sysattrb_display(char *name, boolean_t val, struct lbuf *rep)
21045331Samw {
21055331Samw 	f_attr_t	fattr;
21065331Samw 	const char	*opt;
21075331Samw 	size_t		len;
21085331Samw 
21095331Samw 	fattr = name_to_attr(name);
21105331Samw 	if (fattr != F_ATTR_INVAL && fattr < sacnt) {
21115331Samw 		if (vopt) {
21125331Samw 			len = strlen(name);
21135331Samw 			if (val) {
21145331Samw 				rep->exttr[fattr].name = xmalloc(len + 1, rep);
21155331Samw 				(void) strlcpy(rep->exttr[fattr].name, name,
21165331Samw 				    len + 1);
21175331Samw 			} else {
21185331Samw 				rep->exttr[fattr].name = xmalloc(len + 3, rep);
21195331Samw 				(void) snprintf(rep->exttr[fattr].name, len + 3,
21205331Samw 				    "no%s", name);
21215331Samw 			}
21225331Samw 		} else {
21235331Samw 			opt = attr_to_option(fattr);
21245331Samw 			if (opt != NULL) {
21255331Samw 				len = strlen(opt);
21265331Samw 				rep->exttr[fattr].name = xmalloc(len + 1, rep);
21275331Samw 				if (val)
21285331Samw 					(void) strlcpy(rep->exttr[fattr].name,
21295331Samw 					    opt, len + 1);
21305331Samw 				else
21315331Samw 					(void) strlcpy(rep->exttr[fattr].name,
21325331Samw 					    "-", len + 1);
21335331Samw 			}
21345331Samw 		}
21355331Samw 	}
21365331Samw }
21375331Samw 
21385331Samw /* Set extended system attribute timestamp display */
21395331Samw 
21405331Samw void
21415331Samw set_sysattrtm_display(char *name, struct lbuf *rep)
21425331Samw {
21435331Samw 	uint_t		nelem;
21445331Samw 	uint64_t	*value;
21455331Samw 	int		i;
21465331Samw 	size_t		len;
21475331Samw 
21485331Samw 	if (nvpair_value_uint64_array(pair, &value, &nelem) == 0) {
21495331Samw 		if (*value != NULL) {
21505331Samw 			len = strlen(name);
21515331Samw 			i = 0;
21525331Samw 			while (rep->extm[i].stm != 0 && i < sacnt)
21535331Samw 				i++;
21545331Samw 			rep->extm[i].stm = value[0];
21555331Samw 			rep->extm[i].nstm = value[1];
21565331Samw 			rep->extm[i].name = xmalloc(len + 1, rep);
21575331Samw 			(void) strlcpy(rep->extm[i].name, name, len + 1);
21585331Samw 		}
21595331Samw 	}
21605331Samw }
21615331Samw 
21625331Samw void
21635331Samw format_time(const char *format, time_t sec)
21645331Samw {
21655331Samw 
21665331Samw 	(void) strftime(time_buf, sizeof (time_buf),
21675331Samw 	    dcgettext(NULL, format, LC_TIME),
21685331Samw 	    localtime(&sec));
21695331Samw }
21705331Samw 
21715331Samw void
21725331Samw format_etime(const char *format, time_t sec, time_t nsec)
21735331Samw {
21745331Samw 	char fmt_buf[FMTSIZE];
21755331Samw 
21765331Samw 	(void) snprintf(fmt_buf, FMTSIZE,
21775331Samw 	    format, nsec);
21785331Samw 	(void) strftime(time_buf, sizeof (time_buf),
21795331Samw 	    fmt_buf, localtime(&sec));
21805331Samw }
21815331Samw 
21825331Samw /* Format timestamp extended system attributes */
21835331Samw 
21845331Samw void
21855331Samw format_attrtime(struct lbuf *p)
21865331Samw {
21875331Samw 	int	tmattr = 0;
21885331Samw 	int i;
21895331Samw 
21905331Samw 	if (p->extm != NULL) {
21915331Samw 		for (i = 0; i < sacnt; i++) {
21925331Samw 			if (p->extm[i].name != NULL) {
21935331Samw 				tmattr = 1;
21945331Samw 				break;
21955331Samw 			}
21965331Samw 		}
21975331Samw 	}
21985331Samw 	if (tmattr) {
21995331Samw 		if (Eflg)
22005331Samw 			format_etime(FORMAT4, (time_t)p->extm[i].stm,
22015331Samw 			    (time_t)p->extm[i].nstm);
22025331Samw 		else  {
22035331Samw 			if ((p->lmtime.tv_sec < year) ||
22045331Samw 			    (p->lmtime.tv_sec > now))
22055331Samw 				format_time(FORMAT1,
22065331Samw 				    (time_t)p->extm[i].stm);
22075331Samw 			else
22085331Samw 				format_time(FORMAT2,
22095331Samw 				    (time_t)p->extm[i].stm);
22105331Samw 		}
22115331Samw 	}
22125331Samw }
22135331Samw 
22145331Samw void
22155331Samw print_time(struct lbuf *p)
22165331Samw {
22175331Samw 	int i = 0;
22185331Samw 
22195331Samw 	new_line();
22205331Samw 	if (Eflg) {
22215331Samw 		format_etime(FORMAT4, p->lat.tv_sec, p->lat.tv_nsec);
22225331Samw 		(void) printf("		timestamp: atime	%s\n",
22235331Samw 		    time_buf);
22245331Samw 		format_etime(FORMAT4, p->lct.tv_sec, p->lct.tv_nsec);
22255331Samw 		(void) printf("		timestamp: ctime	%s\n",
22265331Samw 		    time_buf);
22275331Samw 		format_etime(FORMAT4, p->lmt.tv_sec, p->lmt.tv_nsec);
22285331Samw 		(void) printf("		timestamp: mtime	%s\n",
22295331Samw 		    time_buf);
22305331Samw 		if (p->extm != NULL) {
22315331Samw 			while (p->extm[i].nstm != 0 && i < sacnt) {
22325331Samw 				format_etime(FORMAT4, p->extm[i].stm,
22335331Samw 				    p->extm[i].nstm);
22345331Samw 				if (p->extm[i].name != NULL) {
22355331Samw 					(void) printf("		timestamp:"
22365331Samw 					    " %s	%s\n",
22375331Samw 					    p->extm[i].name, time_buf);
22385331Samw 				}
22395331Samw 				i++;
22405331Samw 			}
22415331Samw 		}
22425331Samw 	} else {
22435331Samw 		format_time(FORMAT3, p->lat.tv_sec);
22445331Samw 		(void) printf("		timestamp: atime	%s\n",
22455331Samw 		    time_buf);
22465331Samw 		format_time(FORMAT3, p->lct.tv_sec);
22475331Samw 		(void) printf("		timestamp: ctime	%s\n",
22485331Samw 		    time_buf);
22495331Samw 		format_time(FORMAT3, p->lmt.tv_sec);
22505331Samw 		(void) printf("		timestamp: mtime	%s\n",
22515331Samw 		    time_buf);
22525331Samw 		if (p->extm != NULL) {
22535331Samw 			while (p->extm[i].stm != 0 && i < sacnt) {
22545331Samw 				format_time(FORMAT3, p->extm[i].stm);
22555331Samw 				if (p->extm[i].name != NULL) {
22565331Samw 					(void) printf("		timestamp:"
22575331Samw 					    " %s	%s\n",
22585331Samw 					    p->extm[i].name, time_buf);
22595331Samw 				}
22605331Samw 				i++;
22615331Samw 			}
22625331Samw 		}
22635331Samw 	}
22645331Samw }
22655331Samw 
22665331Samw /* Free extended system attribute lists */
22675331Samw 
22685331Samw void
22695331Samw free_sysattr(struct lbuf *p)
22705331Samw {
22715331Samw 	int i;
22725331Samw 
22735331Samw 	if (p->exttr != NULL) {
22745331Samw 		for (i = 0; i < sacnt; i++) {
22755331Samw 			if (p->exttr[i].name != NULL)
22765331Samw 				free(p->exttr[i].name);
22775331Samw 		}
22785331Samw 		free(p->exttr);
22795331Samw 	}
22805331Samw 	if (p->extm != NULL) {
22815331Samw 		for (i = 0; i < sacnt; i++) {
22825331Samw 			if (p->extm[i].name != NULL)
22835331Samw 				free(p->extm[i].name);
22845331Samw 		}
22855331Samw 		free(p->extm);
22865331Samw 	}
22875331Samw }
22885331Samw 
22895331Samw /* Allocate extended system attribute list */
22905331Samw 
22915331Samw void *
22925331Samw xmalloc(size_t size, struct lbuf *p)
22935331Samw {
22945331Samw 	if ((p = malloc(size)) == NULL) {
22955331Samw 		perror("ls");
22965331Samw 		free_sysattr(p);
22975331Samw 		nvlist_free(response);
22985331Samw 		exit(2);
22995331Samw 	}
23005331Samw 	return (p);
23015331Samw }
2302