xref: /onnv-gate/usr/src/cmd/ls/ls.c (revision 5331:3047ad28a67b)
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 /*
22*5331Samw  * Copyright 2007 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>
64*5331Samw #include <libnvpair.h>
65*5331Samw #include <libcmdutils.h>
66*5331Samw #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
75*5331Samw  * 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 };
125*5331Samw /* Holds boolean extended system attributes */
126*5331Samw struct attrb {
127*5331Samw 	char		*name;
128*5331Samw };
129*5331Samw /* Holds timestamp extended system attributes */
130*5331Samw struct attrtm {
131*5331Samw 	char		*name;
132*5331Samw 	uint64_t	stm;
133*5331Samw 	uint64_t	nstm;
134*5331Samw };
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;
150*5331Samw 	timestruc_t	lat;
151*5331Samw 	timestruc_t	lct;
152*5331Samw 	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 */
158*5331Samw 	struct attrb *exttr;	/* boolean extended system attributes */
159*5331Samw 	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;
242*5331Samw static int		saflg;		/* boolean extended system attr. */
243*5331Samw static int		sacnt;		/* number of extended system attr. */
244*5331Samw static int		copt;
245*5331Samw static int		vopt;
246*5331Samw static int		tmflg;		/* create time ext. system attr. */
247*5331Samw static int		ctm;
248*5331Samw static int		atm;
249*5331Samw static int		mtm;
250*5331Samw static int		crtm;
251*5331Samw 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 
278*5331Samw 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))
285*5331Samw /* Extended system attributes support */
286*5331Samw static int get_sysxattr(char *, struct lbuf *);
287*5331Samw static void set_sysattrb_display(char *, boolean_t, struct lbuf *);
288*5331Samw static void set_sysattrtm_display(char *, struct lbuf *);
289*5331Samw static void format_time(const char *, time_t);
290*5331Samw static void format_etime(const char *, time_t, time_t);
291*5331Samw static void print_time(struct lbuf *);
292*5331Samw static void format_attrtime(struct lbuf *);
293*5331Samw static void *xmalloc(size_t, struct lbuf *);
294*5331Samw static void free_sysattr(struct lbuf *);
295*5331Samw static nvpair_t *pair;
296*5331Samw 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,
330*5331Samw 	    "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;
344*5331Samw 			atm = 0;
345*5331Samw 			ctm = 0;
346*5331Samw 			mtm = 0;
347*5331Samw 			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;
461*5331Samw 			atm = 0;
462*5331Samw 			ctm = 0;
463*5331Samw 			mtm = 0;
464*5331Samw 			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;
508*5331Samw 		case '/':
509*5331Samw 			saflg++;
510*5331Samw 			if (optarg != NULL) {
511*5331Samw 				if (strcmp(optarg, "c") == 0) {
512*5331Samw 					copt++;
513*5331Samw 					vopt = 0;
514*5331Samw 				} else if (strcmp(optarg, "v") == 0) {
515*5331Samw 					vopt++;
516*5331Samw 					copt = 0;
517*5331Samw 				} else
518*5331Samw 					opterr++;
519*5331Samw 			} else
520*5331Samw 				opterr++;
521*5331Samw 			lflg++;
522*5331Samw 			statreq++;
523*5331Samw 			Cflg = 0;
524*5331Samw 			xflg = 0;
525*5331Samw 			mflg = 0;
526*5331Samw 			continue;
527*5331Samw 		case '%':
528*5331Samw 			tmflg++;
529*5331Samw 			if (optarg != NULL) {
530*5331Samw 				if (strcmp(optarg, "ctime") == 0) {
531*5331Samw 					ctm++;
532*5331Samw 					atm = 0;
533*5331Samw 					mtm = 0;
534*5331Samw 					crtm = 0;
535*5331Samw 				} else if (strcmp(optarg, "atime") == 0) {
536*5331Samw 					atm++;
537*5331Samw 					ctm = 0;
538*5331Samw 					mtm = 0;
539*5331Samw 					crtm = 0;
540*5331Samw 					uflg = 0;
541*5331Samw 					cflg = 0;
542*5331Samw 				} else if (strcmp(optarg, "mtime") == 0) {
543*5331Samw 					mtm++;
544*5331Samw 					atm = 0;
545*5331Samw 					ctm = 0;
546*5331Samw 					crtm = 0;
547*5331Samw 					uflg = 0;
548*5331Samw 					cflg = 0;
549*5331Samw 				} else if (strcmp(optarg, "crtime") == 0) {
550*5331Samw 					crtm++;
551*5331Samw 					atm = 0;
552*5331Samw 					ctm = 0;
553*5331Samw 					mtm = 0;
554*5331Samw 					uflg = 0;
555*5331Samw 					cflg = 0;
556*5331Samw 				} else if (strcmp(optarg, "all") == 0) {
557*5331Samw 					alltm++;
558*5331Samw 					atm = 0;
559*5331Samw 					ctm = 0;
560*5331Samw 					mtm = 0;
561*5331Samw 					crtm = 0;
562*5331Samw 				} else
563*5331Samw 					opterr++;
564*5331Samw 			} else
565*5331Samw 				opterr++;
566*5331Samw 
567*5331Samw 			Sflg = 0;
568*5331Samw 			statreq++;
569*5331Samw 			mflg = 0;
570*5331Samw 			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(
577*5331Samw 		    "usage: ls -aAbcCdeEfFghHilLmnopqrRsStuxvV1@/%[c | v]"
578*5331Samw 		    "%%[atime | crtime | ctime | mtime | all]"
579*5331Samw 		    " [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 " :
873*5331Samw 		    (p->lblocks < 10000) ? "%4lld " : "%lld ",
874*5331Samw 		    (p->ltype != 'b' && p->ltype != 'c') ?
875*5331Samw 		    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 		}
909*5331Samw 		if (eflg)
910*5331Samw 			format_time(FORMAT3, p->lmtime.tv_sec);
911*5331Samw 		else if (Eflg)
9120Sstevel@tonic-gate 			/* fill in nanoseconds first */
913*5331Samw 			format_etime(FORMAT4, p->lmtime.tv_sec,
914*5331Samw 			    p->lmtime.tv_nsec);
915*5331Samw 		else {
9160Sstevel@tonic-gate 			if ((p->lmtime.tv_sec < year) ||
917*5331Samw 			    (p->lmtime.tv_sec > now))
918*5331Samw 				format_time(FORMAT1, p->lmtime.tv_sec);
919*5331Samw 			else
920*5331Samw 				format_time(FORMAT2, p->lmtime.tv_sec);
9210Sstevel@tonic-gate 		}
922*5331Samw 		/* format extended system attribute time */
923*5331Samw 		if (tmflg && crtm)
924*5331Samw 			format_attrtime(p);
925*5331Samw 
9260Sstevel@tonic-gate 		curcol += printf("%s", time_buf);
927*5331Samw 
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 
978*5331Samw 	/* Display extended system attributes */
979*5331Samw 	if (saflg) {
980*5331Samw 		int i;
981*5331Samw 
982*5331Samw 		new_line();
983*5331Samw 		(void) printf("	\t{");
984*5331Samw 		if (p->exttr != NULL) {
985*5331Samw 			int k = 0;
986*5331Samw 			for (i = 0; i < sacnt; i++) {
987*5331Samw 				if (p->exttr[i].name != NULL)
988*5331Samw 					k++;
989*5331Samw 			}
990*5331Samw 			for (i = 0; i < sacnt; i++) {
991*5331Samw 				if (p->exttr[i].name != NULL) {
992*5331Samw 					(void) printf("%s", p->exttr[i].name);
993*5331Samw 					k--;
994*5331Samw 					if (vopt && (k != 0))
995*5331Samw 						(void) printf(",");
996*5331Samw 				}
997*5331Samw 			}
998*5331Samw 		}
999*5331Samw 		(void) printf("}\n");
1000*5331Samw 	}
1001*5331Samw 	/* Display file timestamps and extended system attribute timestamps */
1002*5331Samw 	if (tmflg && alltm) {
1003*5331Samw 		new_line();
1004*5331Samw 		print_time(p);
1005*5331Samw 		new_line();
1006*5331Samw 	}
1007789Sahrens 	if (vflg) {
1008789Sahrens 		new_line();
1009789Sahrens 		if (p->aclp) {
10101420Smarks 			acl_printacl(p->aclp, num_cols, Vflg);
1011789Sahrens 		}
1012789Sahrens 	}
1013*5331Samw 	/* Free extended system attribute lists */
1014*5331Samw 	if (saflg || tmflg)
1015*5331Samw 		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)
1158*5331Samw 					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 /*
12480Sstevel@tonic-gate  * get status of file and recomputes tblocks;
12490Sstevel@tonic-gate  * argfl = 1 if file is a name in ls-command and = 0
12500Sstevel@tonic-gate  * for filename in a directory whose name is an
12510Sstevel@tonic-gate  * argument in the command;
12520Sstevel@tonic-gate  * stores a pointer in flist[nfiles] and
12530Sstevel@tonic-gate  * returns that pointer;
12540Sstevel@tonic-gate  * returns NULL if failed;
12550Sstevel@tonic-gate  */
12560Sstevel@tonic-gate static struct lbuf *
12570Sstevel@tonic-gate gstat(char *file, int argfl, struct ditem *myparent)
12580Sstevel@tonic-gate {
12590Sstevel@tonic-gate 	struct stat statb, statb1;
12600Sstevel@tonic-gate 	struct lbuf *rep;
12610Sstevel@tonic-gate 	char buf[BUFSIZ];
12620Sstevel@tonic-gate 	ssize_t cc;
12630Sstevel@tonic-gate 	int (*statf)() = ((Lflg) || (Hflg && argfl)) ? stat : lstat;
12640Sstevel@tonic-gate 	int aclcnt;
1265789Sahrens 	int error;
12660Sstevel@tonic-gate 	aclent_t *tp;
12670Sstevel@tonic-gate 	o_mode_t groupperm, mask;
12680Sstevel@tonic-gate 	int grouppermfound, maskfound;
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if (nomocore)
12710Sstevel@tonic-gate 		return (NULL);
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 	if (nfiles >= maxfils) {
12740Sstevel@tonic-gate 		/*
12750Sstevel@tonic-gate 		 * all flist/lbuf pair assigned files, time to get some
12760Sstevel@tonic-gate 		 * more space
12770Sstevel@tonic-gate 		 */
12780Sstevel@tonic-gate 		maxfils += quantn;
12790Sstevel@tonic-gate 		if (((flist = realloc(flist,
12800Sstevel@tonic-gate 		    maxfils * sizeof (struct lbuf *))) == NULL) ||
12810Sstevel@tonic-gate 		    ((nxtlbf = malloc(quantn *
12820Sstevel@tonic-gate 		    sizeof (struct lbuf))) == NULL)) {
12830Sstevel@tonic-gate 			perror("ls");
12840Sstevel@tonic-gate 			nomocore = 1;
12850Sstevel@tonic-gate 			return (NULL);
12860Sstevel@tonic-gate 		}
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	/*
12900Sstevel@tonic-gate 	 * nfiles is reset to nargs for each directory
12910Sstevel@tonic-gate 	 * that is given as an argument maxn is checked
12920Sstevel@tonic-gate 	 * to prevent the assignment of an lbuf to a flist entry
12930Sstevel@tonic-gate 	 * that already has one assigned.
12940Sstevel@tonic-gate 	 */
12950Sstevel@tonic-gate 	if (nfiles >= maxn) {
12960Sstevel@tonic-gate 		rep = nxtlbf++;
12970Sstevel@tonic-gate 		flist[nfiles++] = rep;
12980Sstevel@tonic-gate 		maxn = nfiles;
12990Sstevel@tonic-gate 	} else {
13000Sstevel@tonic-gate 		rep = flist[nfiles++];
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 	rep->lflags = (mode_t)0;
13030Sstevel@tonic-gate 	rep->flinkto = NULL;
13040Sstevel@tonic-gate 	rep->cycle = 0;
13050Sstevel@tonic-gate 	if (argfl || statreq) {
13060Sstevel@tonic-gate 		int doacl;
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 		if (lflg)
13090Sstevel@tonic-gate 			doacl = 1;
13100Sstevel@tonic-gate 		else
13110Sstevel@tonic-gate 			doacl = 0;
13120Sstevel@tonic-gate 		if ((*statf)(file, &statb) < 0) {
13130Sstevel@tonic-gate 			if (argfl || errno != ENOENT ||
13140Sstevel@tonic-gate 			    (Lflg && lstat(file, &statb) == 0)) {
13150Sstevel@tonic-gate 				/*
13160Sstevel@tonic-gate 				 * Avoid race between readdir and lstat.
13170Sstevel@tonic-gate 				 * Print error message in case of dangling link.
13180Sstevel@tonic-gate 				 */
13190Sstevel@tonic-gate 				perror(file);
13200Sstevel@tonic-gate 			}
13210Sstevel@tonic-gate 			nfiles--;
13220Sstevel@tonic-gate 			return (NULL);
13230Sstevel@tonic-gate 		}
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 		/*
13260Sstevel@tonic-gate 		 * If -H was specified, and the file linked to was
13270Sstevel@tonic-gate 		 * not a directory, then we need to get the info
13280Sstevel@tonic-gate 		 * for the symlink itself.
13290Sstevel@tonic-gate 		 */
13300Sstevel@tonic-gate 		if ((Hflg) && (argfl) &&
13310Sstevel@tonic-gate 		    ((statb.st_mode & S_IFMT) != S_IFDIR)) {
13320Sstevel@tonic-gate 			if (lstat(file, &statb) < 0) {
13330Sstevel@tonic-gate 				perror(file);
13340Sstevel@tonic-gate 			}
13350Sstevel@tonic-gate 		}
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 		rep->lnum = statb.st_ino;
13380Sstevel@tonic-gate 		rep->lsize = statb.st_size;
13390Sstevel@tonic-gate 		rep->lblocks = statb.st_blocks;
13400Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
13410Sstevel@tonic-gate 		case S_IFDIR:
13420Sstevel@tonic-gate 			rep->ltype = 'd';
13430Sstevel@tonic-gate 			if (Rflg) {
13440Sstevel@tonic-gate 				record_ancestry(file, &statb, rep,
13450Sstevel@tonic-gate 				    argfl, myparent);
13460Sstevel@tonic-gate 			}
13470Sstevel@tonic-gate 			break;
13480Sstevel@tonic-gate 		case S_IFBLK:
13490Sstevel@tonic-gate 			rep->ltype = 'b';
13500Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
13510Sstevel@tonic-gate 			break;
13520Sstevel@tonic-gate 		case S_IFCHR:
13530Sstevel@tonic-gate 			rep->ltype = 'c';
13540Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
13550Sstevel@tonic-gate 			break;
13560Sstevel@tonic-gate 		case S_IFIFO:
13570Sstevel@tonic-gate 			rep->ltype = 'p';
13580Sstevel@tonic-gate 			break;
13590Sstevel@tonic-gate 		case S_IFSOCK:
13600Sstevel@tonic-gate 			rep->ltype = 's';
13610Sstevel@tonic-gate 			rep->lsize = 0;
13620Sstevel@tonic-gate 			break;
13630Sstevel@tonic-gate 		case S_IFLNK:
13640Sstevel@tonic-gate 			/* symbolic links may not have ACLs, so elide acl() */
13650Sstevel@tonic-gate 			if ((Lflg == 0) || (Hflg == 0) ||
13660Sstevel@tonic-gate 			    ((Hflg) && (!argfl))) {
13670Sstevel@tonic-gate 				doacl = 0;
13680Sstevel@tonic-gate 			}
13690Sstevel@tonic-gate 			rep->ltype = 'l';
13700Sstevel@tonic-gate 			if (lflg) {
13710Sstevel@tonic-gate 				cc = readlink(file, buf, BUFSIZ);
13720Sstevel@tonic-gate 				if (cc >= 0) {
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 					/*
13750Sstevel@tonic-gate 					 * follow the symbolic link
13760Sstevel@tonic-gate 					 * to generate the appropriate
13770Sstevel@tonic-gate 					 * Fflg marker for the object
13780Sstevel@tonic-gate 					 * eg, /bin -> /sym/bin/
13790Sstevel@tonic-gate 					 */
13800Sstevel@tonic-gate 					if ((Fflg || pflg) &&
13810Sstevel@tonic-gate 					    (stat(file, &statb1) >= 0)) {
13820Sstevel@tonic-gate 						switch (statb1.st_mode &
13830Sstevel@tonic-gate 						    S_IFMT) {
13840Sstevel@tonic-gate 						case S_IFDIR:
13850Sstevel@tonic-gate 							buf[cc++] = '/';
13860Sstevel@tonic-gate 							break;
13870Sstevel@tonic-gate 						case S_IFSOCK:
13880Sstevel@tonic-gate 							buf[cc++] = '=';
13890Sstevel@tonic-gate 							break;
13901447Sakaplan 						case S_IFDOOR:
13911447Sakaplan 							buf[cc++] = '>';
13921447Sakaplan 							break;
13931447Sakaplan 						case S_IFIFO:
13941447Sakaplan 							buf[cc++] = '|';
13951447Sakaplan 							break;
13960Sstevel@tonic-gate 						default:
13970Sstevel@tonic-gate 							if ((statb1.st_mode &
13980Sstevel@tonic-gate 							    ~S_IFMT) &
13990Sstevel@tonic-gate 							    (S_IXUSR|S_IXGRP|
14000Sstevel@tonic-gate 							    S_IXOTH))
14010Sstevel@tonic-gate 								buf[cc++] = '*';
14020Sstevel@tonic-gate 							break;
14030Sstevel@tonic-gate 						}
14040Sstevel@tonic-gate 					}
14050Sstevel@tonic-gate 					buf[cc] = '\0';
14060Sstevel@tonic-gate 					rep->flinkto = strdup(buf);
14070Sstevel@tonic-gate 				}
14080Sstevel@tonic-gate 				break;
14090Sstevel@tonic-gate 			}
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate 			/*
14120Sstevel@tonic-gate 			 * ls /sym behaves differently from ls /sym/
14130Sstevel@tonic-gate 			 * when /sym is a symbolic link. This is fixed
14140Sstevel@tonic-gate 			 * when explicit arguments are specified.
14150Sstevel@tonic-gate 			 */
14160Sstevel@tonic-gate 
14170Sstevel@tonic-gate #ifdef XPG6
14180Sstevel@tonic-gate 			/* Do not follow a symlink when -F is specified */
14190Sstevel@tonic-gate 			if ((!argfl) || (argfl && Fflg) ||
14200Sstevel@tonic-gate 			    (stat(file, &statb1) < 0))
14210Sstevel@tonic-gate #else
14220Sstevel@tonic-gate 			/* Follow a symlink when -F is specified */
14230Sstevel@tonic-gate 			if (!argfl || stat(file, &statb1) < 0)
14240Sstevel@tonic-gate #endif /* XPG6 */
14250Sstevel@tonic-gate 				break;
14260Sstevel@tonic-gate 			if ((statb1.st_mode & S_IFMT) == S_IFDIR) {
14270Sstevel@tonic-gate 				statb = statb1;
14280Sstevel@tonic-gate 				rep->ltype = 'd';
14290Sstevel@tonic-gate 				rep->lsize = statb1.st_size;
14300Sstevel@tonic-gate 				if (Rflg) {
14310Sstevel@tonic-gate 					record_ancestry(file, &statb, rep,
14320Sstevel@tonic-gate 					    argfl, myparent);
14330Sstevel@tonic-gate 				}
14340Sstevel@tonic-gate 			}
14350Sstevel@tonic-gate 			break;
14360Sstevel@tonic-gate 		case S_IFDOOR:
14370Sstevel@tonic-gate 			rep->ltype = 'D';
14380Sstevel@tonic-gate 			break;
14390Sstevel@tonic-gate 		case S_IFREG:
14400Sstevel@tonic-gate 			rep->ltype = '-';
14410Sstevel@tonic-gate 			break;
14420Sstevel@tonic-gate 		case S_IFPORT:
14430Sstevel@tonic-gate 			rep->ltype = 'P';
14440Sstevel@tonic-gate 			break;
14450Sstevel@tonic-gate 		default:
14460Sstevel@tonic-gate 			rep->ltype = '?';
14470Sstevel@tonic-gate 			break;
14480Sstevel@tonic-gate 		}
14490Sstevel@tonic-gate 		rep->lflags = statb.st_mode & ~S_IFMT;
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 		if (!S_ISREG(statb.st_mode))
14520Sstevel@tonic-gate 			rep->lflags |= LS_NOTREG;
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 		/* ACL: check acl entries count */
14550Sstevel@tonic-gate 		if (doacl) {
14560Sstevel@tonic-gate 
1457789Sahrens 			error = acl_get(file, 0, &rep->aclp);
1458789Sahrens 			if (error) {
1459789Sahrens 				(void) fprintf(stderr,
1460789Sahrens 				    gettext("ls: can't read ACL on %s: %s\n"),
1461789Sahrens 				    file, acl_strerror(error));
1462789Sahrens 				return (NULL);
1463789Sahrens 			}
14640Sstevel@tonic-gate 
1465789Sahrens 			rep->acl = ' ';
14660Sstevel@tonic-gate 
1467789Sahrens 			if (rep->aclp &&
1468789Sahrens 			    ((acl_flags(rep->aclp) & ACL_IS_TRIVIAL) == 0)) {
1469789Sahrens 				rep->acl = '+';
14700Sstevel@tonic-gate 				/*
1471789Sahrens 				 * Special handling for ufs aka aclent_t ACL's
14720Sstevel@tonic-gate 				 */
1473789Sahrens 				if (rep->aclp &&
1474789Sahrens 				    acl_type(rep->aclp) == ACLENT_T) {
1475789Sahrens 					/*
1476789Sahrens 					 * For files with non-trivial acls, the
1477789Sahrens 					 * effective group permissions are the
1478789Sahrens 					 * intersection of the GROUP_OBJ value
1479789Sahrens 					 * and the CLASS_OBJ (acl mask) value.
1480789Sahrens 					 * Determine both the GROUP_OBJ and
1481789Sahrens 					 * CLASS_OBJ for this file and insert
1482789Sahrens 					 * the logical AND of those two values
1483789Sahrens 					 * in the group permissions field
1484789Sahrens 					 * of the lflags value for this file.
1485789Sahrens 					 */
1486789Sahrens 
1487789Sahrens 					/*
1488789Sahrens 					 * Until found in acl list, assume
1489789Sahrens 					 * maximum permissions for both group
1490789Sahrens 					 * a nd mask.  (Just in case the acl
1491789Sahrens 					 * lacks either value for some reason.)
1492789Sahrens 					 */
1493789Sahrens 					groupperm = 07;
1494789Sahrens 					mask = 07;
1495789Sahrens 					grouppermfound = 0;
1496789Sahrens 					maskfound = 0;
1497789Sahrens 					aclcnt = acl_cnt(rep->aclp);
1498789Sahrens 					for (tp =
1499789Sahrens 					    (aclent_t *)acl_data(rep->aclp);
1500789Sahrens 					    aclcnt--; tp++) {
1501789Sahrens 						if (tp->a_type == GROUP_OBJ) {
1502789Sahrens 							groupperm = tp->a_perm;
1503789Sahrens 							grouppermfound = 1;
1504789Sahrens 							continue;
1505789Sahrens 						}
1506789Sahrens 						if (tp->a_type == CLASS_OBJ) {
1507789Sahrens 							mask = tp->a_perm;
1508789Sahrens 							maskfound = 1;
1509789Sahrens 						}
1510789Sahrens 						if (grouppermfound && maskfound)
1511789Sahrens 							break;
15120Sstevel@tonic-gate 					}
1513789Sahrens 
15140Sstevel@tonic-gate 
1515789Sahrens 					/* reset all the group bits */
1516789Sahrens 					rep->lflags &= ~S_IRWXG;
15170Sstevel@tonic-gate 
1518789Sahrens 					/*
1519789Sahrens 					 * Now set them to the logical AND of
1520789Sahrens 					 * the GROUP_OBJ permissions and the
1521789Sahrens 					 * acl mask.
1522789Sahrens 					 */
15230Sstevel@tonic-gate 
1524789Sahrens 					rep->lflags |= (groupperm & mask) << 3;
15250Sstevel@tonic-gate 
1526789Sahrens 				}
15270Sstevel@tonic-gate 			}
15280Sstevel@tonic-gate 
15291420Smarks 			if (!vflg && !Vflg && rep->aclp) {
15301420Smarks 				acl_free(rep->aclp);
15311420Smarks 				rep->aclp = NULL;
15321420Smarks 			}
15331420Smarks 
15340Sstevel@tonic-gate 			if (atflg && pathconf(file, _PC_XATTR_EXISTS) == 1)
15350Sstevel@tonic-gate 				rep->acl = '@';
1536*5331Samw 
15370Sstevel@tonic-gate 		} else
15380Sstevel@tonic-gate 			rep->acl = ' ';
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 		/* mask ISARG and other file-type bits */
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 		rep->luid = statb.st_uid;
15430Sstevel@tonic-gate 		rep->lgid = statb.st_gid;
15440Sstevel@tonic-gate 		rep->lnl = statb.st_nlink;
1545*5331Samw 		if (uflg || (tmflg && atm))
15460Sstevel@tonic-gate 			rep->lmtime = statb.st_atim;
1547*5331Samw 		else if (cflg || (tmflg && ctm))
15480Sstevel@tonic-gate 			rep->lmtime = statb.st_ctim;
15490Sstevel@tonic-gate 		else
15500Sstevel@tonic-gate 			rep->lmtime = statb.st_mtim;
1551*5331Samw 		rep->lat = statb.st_atim;
1552*5331Samw 		rep->lct = statb.st_ctim;
1553*5331Samw 		rep->lmt = statb.st_mtim;
15540Sstevel@tonic-gate 
15550Sstevel@tonic-gate 		if (rep->ltype != 'b' && rep->ltype != 'c')
15560Sstevel@tonic-gate 			tblocks += rep->lblocks;
1557*5331Samw 
1558*5331Samw 		/* Get extended system attributes */
1559*5331Samw 
1560*5331Samw 		rep->exttr = NULL;
1561*5331Samw 		rep->extm = NULL;
1562*5331Samw 		if ((saflg || (tmflg && crtm) || (tmflg && alltm)) &&
1563*5331Samw 		    (sysattr_support(file, _PC_SATTR_EXISTS) == 1)) {
1564*5331Samw 			int i;
1565*5331Samw 
1566*5331Samw 			sacnt = attr_count();
1567*5331Samw 			/*
1568*5331Samw 			 * Allocate 'sacnt' size array to hold extended
1569*5331Samw 			 * system attribute name (verbose) or respective
1570*5331Samw 			 * symbol represenation (compact).
1571*5331Samw 			 */
1572*5331Samw 			rep->exttr = xmalloc(sacnt * sizeof (struct attrb),
1573*5331Samw 			    rep);
1574*5331Samw 
1575*5331Samw 			/* initialize boolean attribute list */
1576*5331Samw 			for (i = 0; i < sacnt; i++)
1577*5331Samw 				rep->exttr[i].name = NULL;
1578*5331Samw 			if (get_sysxattr(file, rep) != 0) {
1579*5331Samw 				(void) fprintf(stderr,
1580*5331Samw 				    gettext("ls:Failed to retrieve "
1581*5331Samw 				    "extended system attribute from "
1582*5331Samw 				    "%s\n"), file);
1583*5331Samw 				rep->exttr[0].name = xmalloc(2, rep);
1584*5331Samw 				(void) strlcpy(rep->exttr[0].name, "?", 2);
1585*5331Samw 			}
1586*5331Samw 		}
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 	return (rep);
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate /*
15920Sstevel@tonic-gate  * returns pathname of the form dir/file;
15930Sstevel@tonic-gate  * dir and file are null-terminated strings.
15940Sstevel@tonic-gate  */
15950Sstevel@tonic-gate static char *
15960Sstevel@tonic-gate makename(char *dir, char *file)
15970Sstevel@tonic-gate {
15980Sstevel@tonic-gate 	/*
15990Sstevel@tonic-gate 	 * PATH_MAX is the maximum length of a path name.
16000Sstevel@tonic-gate 	 * MAXNAMLEN is the maximum length of any path name component.
16010Sstevel@tonic-gate 	 * Allocate space for both, plus the '/' in the middle
16020Sstevel@tonic-gate 	 * and the null character at the end.
16030Sstevel@tonic-gate 	 * dfile is static as this is returned by makename().
16040Sstevel@tonic-gate 	 */
16050Sstevel@tonic-gate 	static char dfile[PATH_MAX + 1 + MAXNAMLEN + 1];
16060Sstevel@tonic-gate 	char *dp, *fp;
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	dp = dfile;
16090Sstevel@tonic-gate 	fp = dir;
16100Sstevel@tonic-gate 	while (*fp)
16110Sstevel@tonic-gate 		*dp++ = *fp++;
16120Sstevel@tonic-gate 	if (dp > dfile && *(dp - 1) != '/')
16130Sstevel@tonic-gate 		*dp++ = '/';
16140Sstevel@tonic-gate 	fp = file;
16150Sstevel@tonic-gate 	while (*fp)
16160Sstevel@tonic-gate 		*dp++ = *fp++;
16170Sstevel@tonic-gate 	*dp = '\0';
16180Sstevel@tonic-gate 	return (dfile);
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 
16220Sstevel@tonic-gate #include <pwd.h>
16230Sstevel@tonic-gate #include <grp.h>
16240Sstevel@tonic-gate #include <utmpx.h>
16250Sstevel@tonic-gate 
16260Sstevel@tonic-gate struct	utmpx utmp;
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate #define	NMAX	(sizeof (utmp.ut_name))
16290Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 
16320Sstevel@tonic-gate struct cachenode {		/* this struct must be zeroed before using */
16330Sstevel@tonic-gate 	struct cachenode *lesschild;	/* subtree whose entries < val */
16340Sstevel@tonic-gate 	struct cachenode *grtrchild;	/* subtree whose entries > val */
16350Sstevel@tonic-gate 	long val;			/* the uid or gid of this entry */
16360Sstevel@tonic-gate 	int initted;			/* name has been filled in */
16370Sstevel@tonic-gate 	char name[NMAX+1];		/* the string that val maps to */
16380Sstevel@tonic-gate };
16390Sstevel@tonic-gate static struct cachenode *names, *groups;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate static struct cachenode *
16420Sstevel@tonic-gate findincache(struct cachenode **head, long val)
16430Sstevel@tonic-gate {
16440Sstevel@tonic-gate 	struct cachenode **parent = head;
16450Sstevel@tonic-gate 	struct cachenode *c = *parent;
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	while (c != NULL) {
16480Sstevel@tonic-gate 		if (val == c->val) {
16490Sstevel@tonic-gate 			/* found it */
16500Sstevel@tonic-gate 			return (c);
16510Sstevel@tonic-gate 		} else if (val < c->val) {
16520Sstevel@tonic-gate 			parent = &c->lesschild;
16530Sstevel@tonic-gate 			c = c->lesschild;
16540Sstevel@tonic-gate 		} else {
16550Sstevel@tonic-gate 			parent = &c->grtrchild;
16560Sstevel@tonic-gate 			c = c->grtrchild;
16570Sstevel@tonic-gate 		}
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 
16600Sstevel@tonic-gate 	/* not in the cache, make a new entry for it */
16610Sstevel@tonic-gate 	c = calloc(1, sizeof (struct cachenode));
16620Sstevel@tonic-gate 	if (c == NULL) {
16630Sstevel@tonic-gate 		perror("ls");
16640Sstevel@tonic-gate 		exit(2);
16650Sstevel@tonic-gate 	}
16660Sstevel@tonic-gate 	*parent = c;
16670Sstevel@tonic-gate 	c->val = val;
16680Sstevel@tonic-gate 	return (c);
16690Sstevel@tonic-gate }
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate /*
16720Sstevel@tonic-gate  * get name from cache, or passwd file for a given uid;
16730Sstevel@tonic-gate  * lastuid is set to uid.
16740Sstevel@tonic-gate  */
16750Sstevel@tonic-gate static char *
16760Sstevel@tonic-gate getname(uid_t uid)
16770Sstevel@tonic-gate {
16780Sstevel@tonic-gate 	struct passwd *pwent;
16790Sstevel@tonic-gate 	struct cachenode *c;
16800Sstevel@tonic-gate 
16810Sstevel@tonic-gate 	if ((uid == lastuid) && lastuname)
16820Sstevel@tonic-gate 		return (lastuname);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	c = findincache(&names, uid);
16850Sstevel@tonic-gate 	if (c->initted == 0) {
16860Sstevel@tonic-gate 		if ((pwent = getpwuid(uid)) != NULL) {
16870Sstevel@tonic-gate 			SCPYN(&c->name[0], pwent->pw_name);
16880Sstevel@tonic-gate 		} else {
16890Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)uid);
16900Sstevel@tonic-gate 		}
16910Sstevel@tonic-gate 		c->initted = 1;
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 	lastuid = uid;
16940Sstevel@tonic-gate 	lastuname = &c->name[0];
16950Sstevel@tonic-gate 	return (lastuname);
16960Sstevel@tonic-gate }
16970Sstevel@tonic-gate 
16980Sstevel@tonic-gate /*
16990Sstevel@tonic-gate  * get name from cache, or group file for a given gid;
17000Sstevel@tonic-gate  * lastgid is set to gid.
17010Sstevel@tonic-gate  */
17020Sstevel@tonic-gate static char *
17030Sstevel@tonic-gate getgroup(gid_t gid)
17040Sstevel@tonic-gate {
17050Sstevel@tonic-gate 	struct group *grent;
17060Sstevel@tonic-gate 	struct cachenode *c;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	if ((gid == lastgid) && lastgname)
17090Sstevel@tonic-gate 		return (lastgname);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	c = findincache(&groups, gid);
17120Sstevel@tonic-gate 	if (c->initted == 0) {
17130Sstevel@tonic-gate 		if ((grent = getgrgid(gid)) != NULL) {
17140Sstevel@tonic-gate 			SCPYN(&c->name[0], grent->gr_name);
17150Sstevel@tonic-gate 		} else {
17160Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)gid);
17170Sstevel@tonic-gate 		}
17180Sstevel@tonic-gate 		c->initted = 1;
17190Sstevel@tonic-gate 	}
17200Sstevel@tonic-gate 	lastgid = gid;
17210Sstevel@tonic-gate 	lastgname = &c->name[0];
17220Sstevel@tonic-gate 	return (lastgname);
17230Sstevel@tonic-gate }
17240Sstevel@tonic-gate 
17250Sstevel@tonic-gate /* return >0 if item pointed by pp2 should appear first */
17260Sstevel@tonic-gate static int
17270Sstevel@tonic-gate compar(struct lbuf **pp1, struct lbuf **pp2)
17280Sstevel@tonic-gate {
17290Sstevel@tonic-gate 	struct lbuf *p1, *p2;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 	p1 = *pp1;
17320Sstevel@tonic-gate 	p2 = *pp2;
17330Sstevel@tonic-gate 	if (dflg == 0) {
17340Sstevel@tonic-gate /*
17350Sstevel@tonic-gate  * compare two names in ls-command one of which is file
17360Sstevel@tonic-gate  * and the other is a directory;
17370Sstevel@tonic-gate  * this portion is not used for comparing files within
17380Sstevel@tonic-gate  * a directory name of ls-command;
17390Sstevel@tonic-gate  */
17400Sstevel@tonic-gate 		if (p1->lflags&ISARG && p1->ltype == 'd') {
17410Sstevel@tonic-gate 			if (!(p2->lflags&ISARG && p2->ltype == 'd'))
17420Sstevel@tonic-gate 				return (1);
17430Sstevel@tonic-gate 		} else {
17440Sstevel@tonic-gate 			if (p2->lflags&ISARG && p2->ltype == 'd')
17450Sstevel@tonic-gate 				return (-1);
17460Sstevel@tonic-gate 		}
17470Sstevel@tonic-gate 	}
17480Sstevel@tonic-gate 	if (tflg) {
17490Sstevel@tonic-gate 		if (p2->lmtime.tv_sec > p1->lmtime.tv_sec)
17500Sstevel@tonic-gate 			return (rflg);
17510Sstevel@tonic-gate 		else if (p2->lmtime.tv_sec < p1->lmtime.tv_sec)
17520Sstevel@tonic-gate 			return (-rflg);
17530Sstevel@tonic-gate 		/* times are equal to the sec, check nsec */
17540Sstevel@tonic-gate 		if (p2->lmtime.tv_nsec > p1->lmtime.tv_nsec)
17550Sstevel@tonic-gate 			return (rflg);
17560Sstevel@tonic-gate 		else if (p2->lmtime.tv_nsec < p1->lmtime.tv_nsec)
17570Sstevel@tonic-gate 			return (-rflg);
17580Sstevel@tonic-gate 		/* if times are equal, fall through and sort by name */
17590Sstevel@tonic-gate 	} else if (Sflg) {
17600Sstevel@tonic-gate 		/*
17610Sstevel@tonic-gate 		 * The size stored in lsize can be either the
17620Sstevel@tonic-gate 		 * size or the major minor number (in the case of
17630Sstevel@tonic-gate 		 * block and character special devices).  If it's
17640Sstevel@tonic-gate 		 * a major minor number, then the size is considered
17650Sstevel@tonic-gate 		 * to be zero and we want to fall through and sort
17660Sstevel@tonic-gate 		 * by name.  In addition, if the size of p2 is equal
17670Sstevel@tonic-gate 		 * to the size of p1 we want to fall through and
17680Sstevel@tonic-gate 		 * sort by name.
17690Sstevel@tonic-gate 		 */
17700Sstevel@tonic-gate 		off_t	p1size = (p1->ltype == 'b') ||
1771*5331Samw 		    (p1->ltype == 'c') ? 0 : p1->lsize;
17720Sstevel@tonic-gate 		off_t	p2size = (p2->ltype == 'b') ||
1773*5331Samw 		    (p2->ltype == 'c') ? 0 : p2->lsize;
17740Sstevel@tonic-gate 		if (p2size > p1size) {
17750Sstevel@tonic-gate 			return (rflg);
17760Sstevel@tonic-gate 		} else if (p2size < p1size) {
17770Sstevel@tonic-gate 			return (-rflg);
17780Sstevel@tonic-gate 		}
17790Sstevel@tonic-gate 		/* Sizes are equal, fall through and sort by name. */
17800Sstevel@tonic-gate 	}
17810Sstevel@tonic-gate 	return (rflg * strcoll(
17820Sstevel@tonic-gate 	    p1->lflags & ISARG ? p1->ln.namep : p1->ln.lname,
17830Sstevel@tonic-gate 	    p2->lflags&ISARG ? p2->ln.namep : p2->ln.lname));
17840Sstevel@tonic-gate }
17850Sstevel@tonic-gate 
17860Sstevel@tonic-gate static void
17870Sstevel@tonic-gate pprintf(char *s1, char *s2)
17880Sstevel@tonic-gate {
17890Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s1);
17900Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s2);
17910Sstevel@tonic-gate }
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate static void
17940Sstevel@tonic-gate csi_pprintf(unsigned char *s)
17950Sstevel@tonic-gate {
17960Sstevel@tonic-gate 	unsigned char *cp;
17970Sstevel@tonic-gate 	char	c;
17980Sstevel@tonic-gate 	int	i;
17990Sstevel@tonic-gate 	int	c_len;
18000Sstevel@tonic-gate 	int	p_col;
18010Sstevel@tonic-gate 	wchar_t	pcode;
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	if (!qflg && !bflg) {
18040Sstevel@tonic-gate 		for (cp = s; *cp != '\0'; cp++) {
18050Sstevel@tonic-gate 			(void) putchar(*cp);
18060Sstevel@tonic-gate 			curcol++;
18070Sstevel@tonic-gate 		}
18080Sstevel@tonic-gate 		return;
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	for (cp = s; *cp; ) {
18120Sstevel@tonic-gate 		if (isascii(c = *cp)) {
18130Sstevel@tonic-gate 			if (!isprint(c)) {
18140Sstevel@tonic-gate 				if (qflg) {
18150Sstevel@tonic-gate 					c = '?';
18160Sstevel@tonic-gate 				} else {
18170Sstevel@tonic-gate 					curcol += 3;
18180Sstevel@tonic-gate 					(void) putc('\\', stdout);
18190Sstevel@tonic-gate 					c = '0' + ((*cp >> 6) & 07);
18200Sstevel@tonic-gate 					(void) putc(c, stdout);
18210Sstevel@tonic-gate 					c = '0' + ((*cp >> 3) & 07);
18220Sstevel@tonic-gate 					(void) putc(c, stdout);
18230Sstevel@tonic-gate 					c = '0' + (*cp & 07);
18240Sstevel@tonic-gate 				}
18250Sstevel@tonic-gate 			}
18260Sstevel@tonic-gate 			curcol++;
18270Sstevel@tonic-gate 			cp++;
18280Sstevel@tonic-gate 			(void) putc(c, stdout);
18290Sstevel@tonic-gate 			continue;
18300Sstevel@tonic-gate 		}
18310Sstevel@tonic-gate 
18320Sstevel@tonic-gate 		if ((c_len = mbtowc(&pcode, (char *)cp, MB_LEN_MAX)) <= 0) {
18330Sstevel@tonic-gate 			c_len = 1;
18340Sstevel@tonic-gate 			goto not_print;
18350Sstevel@tonic-gate 		}
18360Sstevel@tonic-gate 
18370Sstevel@tonic-gate 		if ((p_col = wcwidth(pcode)) > 0) {
18380Sstevel@tonic-gate 			(void) putwchar(pcode);
18390Sstevel@tonic-gate 			cp += c_len;
18400Sstevel@tonic-gate 			curcol += p_col;
18410Sstevel@tonic-gate 			continue;
18420Sstevel@tonic-gate 		}
18430Sstevel@tonic-gate 
18440Sstevel@tonic-gate not_print:
18450Sstevel@tonic-gate 		for (i = 0; i < c_len; i++) {
18460Sstevel@tonic-gate 			if (qflg) {
18470Sstevel@tonic-gate 				c = '?';
18480Sstevel@tonic-gate 			} else {
18490Sstevel@tonic-gate 				curcol += 3;
18500Sstevel@tonic-gate 				(void) putc('\\', stdout);
18510Sstevel@tonic-gate 				c = '0' + ((*cp >> 6) & 07);
18520Sstevel@tonic-gate 				(void) putc(c, stdout);
18530Sstevel@tonic-gate 				c = '0' + ((*cp >> 3) & 07);
18540Sstevel@tonic-gate 				(void) putc(c, stdout);
18550Sstevel@tonic-gate 				c = '0' + (*cp & 07);
18560Sstevel@tonic-gate 			}
18570Sstevel@tonic-gate 			curcol++;
18580Sstevel@tonic-gate 			(void) putc(c, stdout);
18590Sstevel@tonic-gate 			cp++;
18600Sstevel@tonic-gate 		}
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate }
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate static int
18650Sstevel@tonic-gate strcol(unsigned char *s1)
18660Sstevel@tonic-gate {
18670Sstevel@tonic-gate 	int	w;
18680Sstevel@tonic-gate 	int	w_col;
18690Sstevel@tonic-gate 	int	len;
18700Sstevel@tonic-gate 	wchar_t	wc;
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 	w = 0;
18730Sstevel@tonic-gate 	while (*s1) {
18740Sstevel@tonic-gate 		if (isascii(*s1)) {
18750Sstevel@tonic-gate 			w++;
18760Sstevel@tonic-gate 			s1++;
18770Sstevel@tonic-gate 			continue;
18780Sstevel@tonic-gate 		}
18790Sstevel@tonic-gate 
18800Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
18810Sstevel@tonic-gate 			w++;
18820Sstevel@tonic-gate 			s1++;
18830Sstevel@tonic-gate 			continue;
18840Sstevel@tonic-gate 		}
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 		if ((w_col = wcwidth(wc)) < 0)
18870Sstevel@tonic-gate 			w_col = len;
18880Sstevel@tonic-gate 		s1 += len;
18890Sstevel@tonic-gate 		w += w_col;
18900Sstevel@tonic-gate 	}
18910Sstevel@tonic-gate 	return (w);
18920Sstevel@tonic-gate }
18930Sstevel@tonic-gate 
18940Sstevel@tonic-gate /*
18950Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
18960Sstevel@tonic-gate  * result in the caller-supplied buffer.
18970Sstevel@tonic-gate  *
18980Sstevel@tonic-gate  * The number provided is a size in bytes.  The number is first
18990Sstevel@tonic-gate  * converted to an integral multiple of 'scale' bytes.  This new
19000Sstevel@tonic-gate  * number is then scaled down until it is small enough to be in a good
19010Sstevel@tonic-gate  * human readable format, i.e.  in the range 0 thru scale-1.  If the
19020Sstevel@tonic-gate  * number used to derive the final number is not a multiple of scale, and
19030Sstevel@tonic-gate  * the final number has only a single significant digit, we compute
19040Sstevel@tonic-gate  * tenths of units to provide a second significant digit.
19050Sstevel@tonic-gate  *
19060Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
19070Sstevel@tonic-gate  * converted to "-1".
19080Sstevel@tonic-gate  *
19090Sstevel@tonic-gate  * A pointer to the caller-supplied buffer is returned.
19100Sstevel@tonic-gate  */
19110Sstevel@tonic-gate static char *
19120Sstevel@tonic-gate number_to_scaled_string(
19130Sstevel@tonic-gate 			numbuf_t buf,		/* put the result here */
19140Sstevel@tonic-gate 			unsigned long long number, /* convert this number */
19150Sstevel@tonic-gate 			long scale)
19160Sstevel@tonic-gate {
19170Sstevel@tonic-gate 	unsigned long long save;
19180Sstevel@tonic-gate 	/* Measurement: kilo, mega, giga, tera, peta, exa */
19190Sstevel@tonic-gate 	char *uom = "KMGTPE";
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 	if ((long long)number == (long long)-1) {
19220Sstevel@tonic-gate 		(void) strlcpy(buf, "-1", sizeof (numbuf_t));
19230Sstevel@tonic-gate 		return (buf);
19240Sstevel@tonic-gate 	}
19250Sstevel@tonic-gate 
19260Sstevel@tonic-gate 	save = number;
19270Sstevel@tonic-gate 	number = number / scale;
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 	/*
19300Sstevel@tonic-gate 	 * Now we have number as a count of scale units.
19310Sstevel@tonic-gate 	 * If no further scaling is necessary, we round up as appropriate.
19320Sstevel@tonic-gate 	 *
19330Sstevel@tonic-gate 	 * The largest value number could have had entering the routine is
19340Sstevel@tonic-gate 	 * 16 Exabytes, so running off the end of the uom array should
19350Sstevel@tonic-gate 	 * never happen.  We check for that, though, as a guard against
19360Sstevel@tonic-gate 	 * a breakdown elsewhere in the algorithm.
19370Sstevel@tonic-gate 	 */
19380Sstevel@tonic-gate 	if (number < (unsigned long long)scale) {
19390Sstevel@tonic-gate 		if ((save % scale) >= (unsigned long long)(scale / 2)) {
19400Sstevel@tonic-gate 			if (++number == (unsigned long long)scale) {
19410Sstevel@tonic-gate 				uom++;
19420Sstevel@tonic-gate 				number = 1;
19430Sstevel@tonic-gate 			}
19440Sstevel@tonic-gate 		}
19450Sstevel@tonic-gate 	} else {
19460Sstevel@tonic-gate 		while ((number >= (unsigned long long)scale) && (*uom != 'E')) {
19470Sstevel@tonic-gate 			uom++; /* next unit of measurement */
19480Sstevel@tonic-gate 			save = number;
19490Sstevel@tonic-gate 			/*
19500Sstevel@tonic-gate 			 * If we're over half way to the next unit of
19510Sstevel@tonic-gate 			 * 'scale' bytes (which means we should round
19520Sstevel@tonic-gate 			 * up), then adding half of 'scale' prior to
19530Sstevel@tonic-gate 			 * the division will push us into that next
19540Sstevel@tonic-gate 			 * unit of scale when we perform the division
19550Sstevel@tonic-gate 			 */
19560Sstevel@tonic-gate 			number = (number + (scale / 2)) / scale;
19570Sstevel@tonic-gate 		}
19580Sstevel@tonic-gate 	}
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 	/* check if we should output a decimal place after the point */
19610Sstevel@tonic-gate 	if ((save / scale) < 10) {
19620Sstevel@tonic-gate 		/* snprintf() will round for us */
19630Sstevel@tonic-gate 		float fnum = (float)save / scale;
19640Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%2.1f%c",
19650Sstevel@tonic-gate 		    fnum, *uom);
19660Sstevel@tonic-gate 	} else {
19670Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%4llu%c",
19680Sstevel@tonic-gate 		    number, *uom);
19690Sstevel@tonic-gate 	}
19700Sstevel@tonic-gate 	return (buf);
19710Sstevel@tonic-gate }
1972*5331Samw 
1973*5331Samw /* Get extended system attributes and set the display */
1974*5331Samw 
1975*5331Samw int
1976*5331Samw get_sysxattr(char *fname, struct lbuf *rep)
1977*5331Samw {
1978*5331Samw 	boolean_t	value;
1979*5331Samw 	data_type_t	type;
1980*5331Samw 	int		error;
1981*5331Samw 	char		*name;
1982*5331Samw 	int		i;
1983*5331Samw 
1984*5331Samw 	if ((error = nvlist_alloc(&response, NV_UNIQUE_NAME, 0)) != 0) {
1985*5331Samw 		perror("ls:nvlist_alloc");
1986*5331Samw 		return (error);
1987*5331Samw 	}
1988*5331Samw 	if ((error = getattrat(AT_FDCWD, XATTR_VIEW_READWRITE, fname,
1989*5331Samw 	    &response)) != 0) {
1990*5331Samw 		perror("ls:getattrat");
1991*5331Samw 		nvlist_free(response);
1992*5331Samw 		return (error);
1993*5331Samw 	}
1994*5331Samw 
1995*5331Samw 	/*
1996*5331Samw 	 * Allocate 'sacnt' size array to hold extended timestamp
1997*5331Samw 	 * system attributes and initialize the array.
1998*5331Samw 	 */
1999*5331Samw 	rep->extm = xmalloc(sacnt * sizeof (struct attrtm), rep);
2000*5331Samw 	for (i = 0; i < sacnt; i++) {
2001*5331Samw 		rep->extm[i].stm = 0;
2002*5331Samw 		rep->extm[i].nstm = 0;
2003*5331Samw 		rep->extm[i].name = NULL;
2004*5331Samw 	}
2005*5331Samw 	while ((pair = nvlist_next_nvpair(response, pair)) != NULL) {
2006*5331Samw 		name = nvpair_name(pair);
2007*5331Samw 		type = nvpair_type(pair);
2008*5331Samw 		if (type == DATA_TYPE_BOOLEAN_VALUE) {
2009*5331Samw 			error = nvpair_value_boolean_value(pair, &value);
2010*5331Samw 			if (error) {
2011*5331Samw 				(void) fprintf(stderr,
2012*5331Samw 				    gettext("nvpair_value_boolean_value "
2013*5331Samw 				    "failed: error = %d\n"), error);
2014*5331Samw 				continue;
2015*5331Samw 			}
2016*5331Samw 			if (name != NULL)
2017*5331Samw 				set_sysattrb_display(name, value, rep);
2018*5331Samw 			continue;
2019*5331Samw 		} else if (type == DATA_TYPE_UINT64_ARRAY) {
2020*5331Samw 			if (name != NULL)
2021*5331Samw 				set_sysattrtm_display(name, rep);
2022*5331Samw 			continue;
2023*5331Samw 		}
2024*5331Samw 	}
2025*5331Samw 	nvlist_free(response);
2026*5331Samw 	return (0);
2027*5331Samw }
2028*5331Samw 
2029*5331Samw /* Set extended system attribute boolean display */
2030*5331Samw 
2031*5331Samw void
2032*5331Samw set_sysattrb_display(char *name, boolean_t val, struct lbuf *rep)
2033*5331Samw {
2034*5331Samw 	f_attr_t	fattr;
2035*5331Samw 	const char	*opt;
2036*5331Samw 	size_t		len;
2037*5331Samw 
2038*5331Samw 	fattr = name_to_attr(name);
2039*5331Samw 	if (fattr != F_ATTR_INVAL && fattr < sacnt) {
2040*5331Samw 		if (vopt) {
2041*5331Samw 			len = strlen(name);
2042*5331Samw 			if (val) {
2043*5331Samw 				rep->exttr[fattr].name = xmalloc(len + 1, rep);
2044*5331Samw 				(void) strlcpy(rep->exttr[fattr].name, name,
2045*5331Samw 				    len + 1);
2046*5331Samw 			} else {
2047*5331Samw 				rep->exttr[fattr].name = xmalloc(len + 3, rep);
2048*5331Samw 				(void) snprintf(rep->exttr[fattr].name, len + 3,
2049*5331Samw 				    "no%s", name);
2050*5331Samw 			}
2051*5331Samw 		} else {
2052*5331Samw 			opt = attr_to_option(fattr);
2053*5331Samw 			if (opt != NULL) {
2054*5331Samw 				len = strlen(opt);
2055*5331Samw 				rep->exttr[fattr].name = xmalloc(len + 1, rep);
2056*5331Samw 				if (val)
2057*5331Samw 					(void) strlcpy(rep->exttr[fattr].name,
2058*5331Samw 					    opt, len + 1);
2059*5331Samw 				else
2060*5331Samw 					(void) strlcpy(rep->exttr[fattr].name,
2061*5331Samw 					    "-", len + 1);
2062*5331Samw 			}
2063*5331Samw 		}
2064*5331Samw 	}
2065*5331Samw }
2066*5331Samw 
2067*5331Samw /* Set extended system attribute timestamp display */
2068*5331Samw 
2069*5331Samw void
2070*5331Samw set_sysattrtm_display(char *name, struct lbuf *rep)
2071*5331Samw {
2072*5331Samw 	uint_t		nelem;
2073*5331Samw 	uint64_t	*value;
2074*5331Samw 	int		i;
2075*5331Samw 	size_t		len;
2076*5331Samw 
2077*5331Samw 	if (nvpair_value_uint64_array(pair, &value, &nelem) == 0) {
2078*5331Samw 		if (*value != NULL) {
2079*5331Samw 			len = strlen(name);
2080*5331Samw 			i = 0;
2081*5331Samw 			while (rep->extm[i].stm != 0 && i < sacnt)
2082*5331Samw 				i++;
2083*5331Samw 			rep->extm[i].stm = value[0];
2084*5331Samw 			rep->extm[i].nstm = value[1];
2085*5331Samw 			rep->extm[i].name = xmalloc(len + 1, rep);
2086*5331Samw 			(void) strlcpy(rep->extm[i].name, name, len + 1);
2087*5331Samw 		}
2088*5331Samw 	}
2089*5331Samw }
2090*5331Samw 
2091*5331Samw void
2092*5331Samw format_time(const char *format, time_t sec)
2093*5331Samw {
2094*5331Samw 
2095*5331Samw 	(void) strftime(time_buf, sizeof (time_buf),
2096*5331Samw 	    dcgettext(NULL, format, LC_TIME),
2097*5331Samw 	    localtime(&sec));
2098*5331Samw }
2099*5331Samw 
2100*5331Samw void
2101*5331Samw format_etime(const char *format, time_t sec, time_t nsec)
2102*5331Samw {
2103*5331Samw 	char fmt_buf[FMTSIZE];
2104*5331Samw 
2105*5331Samw 	(void) snprintf(fmt_buf, FMTSIZE,
2106*5331Samw 	    format, nsec);
2107*5331Samw 	(void) strftime(time_buf, sizeof (time_buf),
2108*5331Samw 	    fmt_buf, localtime(&sec));
2109*5331Samw }
2110*5331Samw 
2111*5331Samw /* Format timestamp extended system attributes */
2112*5331Samw 
2113*5331Samw void
2114*5331Samw format_attrtime(struct lbuf *p)
2115*5331Samw {
2116*5331Samw 	int	tmattr = 0;
2117*5331Samw 	int i;
2118*5331Samw 
2119*5331Samw 	if (p->extm != NULL) {
2120*5331Samw 		for (i = 0; i < sacnt; i++) {
2121*5331Samw 			if (p->extm[i].name != NULL) {
2122*5331Samw 				tmattr = 1;
2123*5331Samw 				break;
2124*5331Samw 			}
2125*5331Samw 		}
2126*5331Samw 	}
2127*5331Samw 	if (tmattr) {
2128*5331Samw 		if (Eflg)
2129*5331Samw 			format_etime(FORMAT4, (time_t)p->extm[i].stm,
2130*5331Samw 			    (time_t)p->extm[i].nstm);
2131*5331Samw 		else  {
2132*5331Samw 			if ((p->lmtime.tv_sec < year) ||
2133*5331Samw 			    (p->lmtime.tv_sec > now))
2134*5331Samw 				format_time(FORMAT1,
2135*5331Samw 				    (time_t)p->extm[i].stm);
2136*5331Samw 			else
2137*5331Samw 				format_time(FORMAT2,
2138*5331Samw 				    (time_t)p->extm[i].stm);
2139*5331Samw 		}
2140*5331Samw 	}
2141*5331Samw }
2142*5331Samw 
2143*5331Samw void
2144*5331Samw print_time(struct lbuf *p)
2145*5331Samw {
2146*5331Samw 	int i = 0;
2147*5331Samw 
2148*5331Samw 	new_line();
2149*5331Samw 	if (Eflg) {
2150*5331Samw 		format_etime(FORMAT4, p->lat.tv_sec, p->lat.tv_nsec);
2151*5331Samw 		(void) printf("		timestamp: atime	%s\n",
2152*5331Samw 		    time_buf);
2153*5331Samw 		format_etime(FORMAT4, p->lct.tv_sec, p->lct.tv_nsec);
2154*5331Samw 		(void) printf("		timestamp: ctime	%s\n",
2155*5331Samw 		    time_buf);
2156*5331Samw 		format_etime(FORMAT4, p->lmt.tv_sec, p->lmt.tv_nsec);
2157*5331Samw 		(void) printf("		timestamp: mtime	%s\n",
2158*5331Samw 		    time_buf);
2159*5331Samw 		if (p->extm != NULL) {
2160*5331Samw 			while (p->extm[i].nstm != 0 && i < sacnt) {
2161*5331Samw 				format_etime(FORMAT4, p->extm[i].stm,
2162*5331Samw 				    p->extm[i].nstm);
2163*5331Samw 				if (p->extm[i].name != NULL) {
2164*5331Samw 					(void) printf("		timestamp:"
2165*5331Samw 					    " %s	%s\n",
2166*5331Samw 					    p->extm[i].name, time_buf);
2167*5331Samw 				}
2168*5331Samw 				i++;
2169*5331Samw 			}
2170*5331Samw 		}
2171*5331Samw 	} else {
2172*5331Samw 		format_time(FORMAT3, p->lat.tv_sec);
2173*5331Samw 		(void) printf("		timestamp: atime	%s\n",
2174*5331Samw 		    time_buf);
2175*5331Samw 		format_time(FORMAT3, p->lct.tv_sec);
2176*5331Samw 		(void) printf("		timestamp: ctime	%s\n",
2177*5331Samw 		    time_buf);
2178*5331Samw 		format_time(FORMAT3, p->lmt.tv_sec);
2179*5331Samw 		(void) printf("		timestamp: mtime	%s\n",
2180*5331Samw 		    time_buf);
2181*5331Samw 		if (p->extm != NULL) {
2182*5331Samw 			while (p->extm[i].stm != 0 && i < sacnt) {
2183*5331Samw 				format_time(FORMAT3, p->extm[i].stm);
2184*5331Samw 				if (p->extm[i].name != NULL) {
2185*5331Samw 					(void) printf("		timestamp:"
2186*5331Samw 					    " %s	%s\n",
2187*5331Samw 					    p->extm[i].name, time_buf);
2188*5331Samw 				}
2189*5331Samw 				i++;
2190*5331Samw 			}
2191*5331Samw 		}
2192*5331Samw 	}
2193*5331Samw }
2194*5331Samw 
2195*5331Samw /* Free extended system attribute lists */
2196*5331Samw 
2197*5331Samw void
2198*5331Samw free_sysattr(struct lbuf *p)
2199*5331Samw {
2200*5331Samw 	int i;
2201*5331Samw 
2202*5331Samw 	if (p->exttr != NULL) {
2203*5331Samw 		for (i = 0; i < sacnt; i++) {
2204*5331Samw 			if (p->exttr[i].name != NULL)
2205*5331Samw 				free(p->exttr[i].name);
2206*5331Samw 		}
2207*5331Samw 		free(p->exttr);
2208*5331Samw 	}
2209*5331Samw 	if (p->extm != NULL) {
2210*5331Samw 		for (i = 0; i < sacnt; i++) {
2211*5331Samw 			if (p->extm[i].name != NULL)
2212*5331Samw 				free(p->extm[i].name);
2213*5331Samw 		}
2214*5331Samw 		free(p->extm);
2215*5331Samw 	}
2216*5331Samw }
2217*5331Samw 
2218*5331Samw /* Allocate extended system attribute list */
2219*5331Samw 
2220*5331Samw void *
2221*5331Samw xmalloc(size_t size, struct lbuf *p)
2222*5331Samw {
2223*5331Samw 	if ((p = malloc(size)) == NULL) {
2224*5331Samw 		perror("ls");
2225*5331Samw 		free_sysattr(p);
2226*5331Samw 		nvlist_free(response);
2227*5331Samw 		exit(2);
2228*5331Samw 	}
2229*5331Samw 	return (p);
2230*5331Samw }
2231