xref: /titanic_51/usr/src/cmd/ls/ls.c (revision da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52236845bSakaplan  * Common Development and Distribution License (the "License").
62236845bSakaplan  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*da6c28aaSamw  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*	Copyright (c) 1987, 1988 Microsoft Corporation	*/
307c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * List files or directories
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <sys/param.h>
397c478bd9Sstevel@tonic-gate #include <sys/types.h>
407c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/acl.h>
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate #include <wchar.h>
457c478bd9Sstevel@tonic-gate #include <stdio.h>
467c478bd9Sstevel@tonic-gate #include <ctype.h>
477c478bd9Sstevel@tonic-gate #include <dirent.h>
487c478bd9Sstevel@tonic-gate #include <string.h>
497c478bd9Sstevel@tonic-gate #include <locale.h>
507c478bd9Sstevel@tonic-gate #include <curses.h>
517c478bd9Sstevel@tonic-gate #include <termios.h>
527c478bd9Sstevel@tonic-gate #include <stdlib.h>
537c478bd9Sstevel@tonic-gate #include <widec.h>
547c478bd9Sstevel@tonic-gate #include <locale.h>
557c478bd9Sstevel@tonic-gate #include <wctype.h>
567c478bd9Sstevel@tonic-gate #include <pwd.h>
577c478bd9Sstevel@tonic-gate #include <grp.h>
587c478bd9Sstevel@tonic-gate #include <limits.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <libgen.h>
627c478bd9Sstevel@tonic-gate #include <errno.h>
63fa9e4066Sahrens #include <aclutils.h>
64*da6c28aaSamw #include <libnvpair.h>
65*da6c28aaSamw #include <libcmdutils.h>
66*da6c28aaSamw #include <attr.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #ifndef STANDALONE
697c478bd9Sstevel@tonic-gate #define	TERMINFO
707c478bd9Sstevel@tonic-gate #endif
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate  * -DNOTERMINFO can be defined on the cc command line to prevent
747c478bd9Sstevel@tonic-gate  * the use of terminfo.  This should be done on systems not having
75*da6c28aaSamw  * the terminfo feature(pre 6.0 systems ?).
767c478bd9Sstevel@tonic-gate  * As a result, columnar listings assume 80 columns for output,
777c478bd9Sstevel@tonic-gate  * unless told otherwise via the COLUMNS environment variable.
787c478bd9Sstevel@tonic-gate  */
797c478bd9Sstevel@tonic-gate #ifdef NOTERMINFO
807c478bd9Sstevel@tonic-gate #undef TERMINFO
817c478bd9Sstevel@tonic-gate #endif
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <term.h>
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #define	BFSIZE	16
867c478bd9Sstevel@tonic-gate /* this bit equals 1 in lflags of structure lbuf if *namep is to be used */
877c478bd9Sstevel@tonic-gate #define	ISARG	0100000
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /*
907c478bd9Sstevel@tonic-gate  * this flag has been added to manipulate the display of S instead of 'l' when
917c478bd9Sstevel@tonic-gate  * the file is not a regular file and when group execution bit is off
927c478bd9Sstevel@tonic-gate  */
937c478bd9Sstevel@tonic-gate #define	LS_NOTREG	010000
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Date and time formats
987c478bd9Sstevel@tonic-gate  *
997c478bd9Sstevel@tonic-gate  * b --- abbreviated month name
1007c478bd9Sstevel@tonic-gate  * e --- day number
1017c478bd9Sstevel@tonic-gate  * Y --- year in the form ccyy
1027c478bd9Sstevel@tonic-gate  * H --- hour(24-hour version)
1037c478bd9Sstevel@tonic-gate  * M --- minute
1047c478bd9Sstevel@tonic-gate  * F --- yyyy-mm-dd
1057c478bd9Sstevel@tonic-gate  * T --- hh:mm:ss
1067c478bd9Sstevel@tonic-gate  * z --- time zone as hours displacement from UTC
1077c478bd9Sstevel@tonic-gate  * note that %F and %z are from the ISO C99 standard and are
1087c478bd9Sstevel@tonic-gate  * not present in older C libraries
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate #define	FORMAT1	 " %b %e  %Y "
1117c478bd9Sstevel@tonic-gate #define	FORMAT2  " %b %e %H:%M "
1127c478bd9Sstevel@tonic-gate #define	FORMAT3  " %b %e %T %Y "
1137c478bd9Sstevel@tonic-gate #define	FORMAT4  " %%F %%T.%.09ld %%z "
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #undef BUFSIZ
1167c478bd9Sstevel@tonic-gate #define	BUFSIZ 4096
1177c478bd9Sstevel@tonic-gate #define	NUMBER_WIDTH 40
1187c478bd9Sstevel@tonic-gate #define	FMTSIZE 50
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate struct ditem {
1217c478bd9Sstevel@tonic-gate 	dev_t	dev;			/* directory items device number */
1227c478bd9Sstevel@tonic-gate 	ino_t	ino;			/* directory items inode number */
1237c478bd9Sstevel@tonic-gate 	struct ditem *parent;		/* dir items ptr to its parent's info */
1247c478bd9Sstevel@tonic-gate };
125*da6c28aaSamw /* Holds boolean extended system attributes */
126*da6c28aaSamw struct attrb {
127*da6c28aaSamw 	char		*name;
128*da6c28aaSamw };
129*da6c28aaSamw /* Holds timestamp extended system attributes */
130*da6c28aaSamw struct attrtm {
131*da6c28aaSamw 	char		*name;
132*da6c28aaSamw 	uint64_t	stm;
133*da6c28aaSamw 	uint64_t	nstm;
134*da6c28aaSamw };
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate struct	lbuf	{
1377c478bd9Sstevel@tonic-gate 	union	{
1387c478bd9Sstevel@tonic-gate 		char	lname[MAXNAMLEN]; /* used for filename in a directory */
1397c478bd9Sstevel@tonic-gate 		char	*namep;		/* for name in ls-command; */
1407c478bd9Sstevel@tonic-gate 	} ln;
1417c478bd9Sstevel@tonic-gate 	char	ltype;		/* filetype */
1427c478bd9Sstevel@tonic-gate 	ino_t	lnum;		/* inode number of file */
1437c478bd9Sstevel@tonic-gate 	mode_t	lflags; 	/* 0777 bits used as r,w,x permissions */
1447c478bd9Sstevel@tonic-gate 	nlink_t	lnl;		/* number of links to file */
1457c478bd9Sstevel@tonic-gate 	uid_t	luid;
1467c478bd9Sstevel@tonic-gate 	gid_t	lgid;
1477c478bd9Sstevel@tonic-gate 	off_t	lsize;		/* filesize or major/minor dev numbers */
1487c478bd9Sstevel@tonic-gate 	blkcnt_t	lblocks;	/* number of file blocks */
1497c478bd9Sstevel@tonic-gate 	timestruc_t	lmtime;
150*da6c28aaSamw 	timestruc_t	lat;
151*da6c28aaSamw 	timestruc_t	lct;
152*da6c28aaSamw 	timestruc_t	lmt;
1537c478bd9Sstevel@tonic-gate 	char	*flinkto;	/* symbolic link contents */
1547c478bd9Sstevel@tonic-gate 	char 	acl;		/* indicate there are additional acl entries */
1557c478bd9Sstevel@tonic-gate 	int	cycle;		/* cycle detected flag */
1567c478bd9Sstevel@tonic-gate 	struct ditem *ancinfo;	/* maintains ancestor info */
157fa9e4066Sahrens 	acl_t *aclp;		/* ACL if present */
158*da6c28aaSamw 	struct attrb *exttr;	/* boolean extended system attributes */
159*da6c28aaSamw 	struct attrtm *extm;	/* timestamp extended system attributes */
1607c478bd9Sstevel@tonic-gate };
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate struct dchain {
1637c478bd9Sstevel@tonic-gate 	char *dc_name;		/* path name */
1647c478bd9Sstevel@tonic-gate 	int cycle_detected;	/* cycle detected visiting this directory */
1657c478bd9Sstevel@tonic-gate 	struct ditem *myancinfo;	/* this directory's ancestry info */
1667c478bd9Sstevel@tonic-gate 	struct dchain *dc_next;	/* next directory in the chain */
1677c478bd9Sstevel@tonic-gate };
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * A numbuf_t is used when converting a number to a string representation
1717c478bd9Sstevel@tonic-gate  */
1727c478bd9Sstevel@tonic-gate typedef char numbuf_t[NUMBER_WIDTH];
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate static struct dchain *dfirst;	/* start of the dir chain */
1757c478bd9Sstevel@tonic-gate static struct dchain *cdfirst;	/* start of the current dir chain */
1767c478bd9Sstevel@tonic-gate static struct dchain *dtemp;	/* temporary - used for linking */
1777c478bd9Sstevel@tonic-gate static char *curdir;		/* the current directory */
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate static int	first = 1;	/* true if first line is not yet printed */
1807c478bd9Sstevel@tonic-gate static int	nfiles = 0;	/* number of flist entries in current use */
1817c478bd9Sstevel@tonic-gate static int	nargs = 0;	/* number of flist entries used for arguments */
1827c478bd9Sstevel@tonic-gate static int	maxfils = 0;	/* number of flist/lbuf entries allocated */
1837c478bd9Sstevel@tonic-gate static int	maxn = 0;	/* number of flist entries with lbufs asigned */
1847c478bd9Sstevel@tonic-gate static int	quantn = 64;	/* allocation growth quantum */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate static struct lbuf	*nxtlbf;	/* ptr to next lbuf to be assigned */
1877c478bd9Sstevel@tonic-gate static struct lbuf	**flist;	/* ptr to list of lbuf pointers */
1887c478bd9Sstevel@tonic-gate static struct lbuf	*gstat(char *, int, struct ditem *);
1897c478bd9Sstevel@tonic-gate static char		*getname(uid_t);
1907c478bd9Sstevel@tonic-gate static char		*getgroup(gid_t);
1917c478bd9Sstevel@tonic-gate static char		*makename(char *, char *);
1927c478bd9Sstevel@tonic-gate static void		pentry(struct lbuf *);
1937c478bd9Sstevel@tonic-gate static void		column(void);
1947c478bd9Sstevel@tonic-gate static void		pmode(mode_t aflag);
1957c478bd9Sstevel@tonic-gate static void		selection(int *);
1967c478bd9Sstevel@tonic-gate static void		new_line(void);
1977c478bd9Sstevel@tonic-gate static void		rddir(char *, struct ditem *);
1987c478bd9Sstevel@tonic-gate static int		strcol(unsigned char *);
1997c478bd9Sstevel@tonic-gate static void		pem(struct lbuf **, struct lbuf **, int);
2007c478bd9Sstevel@tonic-gate static void		pdirectory(char *, int, int, int, struct ditem *);
2017c478bd9Sstevel@tonic-gate static struct cachenode *findincache(struct cachenode **, long);
2027c478bd9Sstevel@tonic-gate static void		csi_pprintf(unsigned char *);
2037c478bd9Sstevel@tonic-gate static void		pprintf(char *, char *);
2047c478bd9Sstevel@tonic-gate static int		compar(struct lbuf **pp1, struct lbuf **pp2);
2057c478bd9Sstevel@tonic-gate static char 		*number_to_scaled_string(numbuf_t buf,
2067c478bd9Sstevel@tonic-gate 			    unsigned long long number,
2077c478bd9Sstevel@tonic-gate 			    long scale);
2087c478bd9Sstevel@tonic-gate static void		record_ancestry(char *, struct stat *, struct lbuf *,
2097c478bd9Sstevel@tonic-gate 			    int, struct ditem *);
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate static int		aflg;
2127c478bd9Sstevel@tonic-gate static int		atflg;
2137c478bd9Sstevel@tonic-gate static int		bflg;
2147c478bd9Sstevel@tonic-gate static int		cflg;
2157c478bd9Sstevel@tonic-gate static int		dflg;
2167c478bd9Sstevel@tonic-gate static int		eflg;
2177c478bd9Sstevel@tonic-gate static int		fflg;
2187c478bd9Sstevel@tonic-gate static int		gflg;
2197c478bd9Sstevel@tonic-gate static int		hflg;
2207c478bd9Sstevel@tonic-gate static int		iflg;
2217c478bd9Sstevel@tonic-gate static int		lflg;
2227c478bd9Sstevel@tonic-gate static int		mflg;
2237c478bd9Sstevel@tonic-gate static int		nflg;
2247c478bd9Sstevel@tonic-gate static int		oflg;
2257c478bd9Sstevel@tonic-gate static int		pflg;
2267c478bd9Sstevel@tonic-gate static int		qflg;
2277c478bd9Sstevel@tonic-gate static int		rflg = 1; /* init to 1 for special use in compar */
2287c478bd9Sstevel@tonic-gate static int		sflg;
2297c478bd9Sstevel@tonic-gate static int		tflg;
2307c478bd9Sstevel@tonic-gate static int		uflg;
2317c478bd9Sstevel@tonic-gate static int		xflg;
2327c478bd9Sstevel@tonic-gate static int		Aflg;
2337c478bd9Sstevel@tonic-gate static int		Cflg;
2347c478bd9Sstevel@tonic-gate static int		Eflg;
2357c478bd9Sstevel@tonic-gate static int		Fflg;
2367c478bd9Sstevel@tonic-gate static int		Hflg;
2377c478bd9Sstevel@tonic-gate static int		Lflg;
2387c478bd9Sstevel@tonic-gate static int		Rflg;
2397c478bd9Sstevel@tonic-gate static int		Sflg;
240fa9e4066Sahrens static int		vflg;
2415a5eeccaSmarks static int		Vflg;
242*da6c28aaSamw static int		saflg;		/* boolean extended system attr. */
243*da6c28aaSamw static int		sacnt;		/* number of extended system attr. */
244*da6c28aaSamw static int		copt;
245*da6c28aaSamw static int		vopt;
246*da6c28aaSamw static int		tmflg;		/* create time ext. system attr. */
247*da6c28aaSamw static int		ctm;
248*da6c28aaSamw static int		atm;
249*da6c28aaSamw static int		mtm;
250*da6c28aaSamw static int		crtm;
251*da6c28aaSamw static int		alltm;
2527c478bd9Sstevel@tonic-gate static long		hscale;
2537c478bd9Sstevel@tonic-gate static mode_t		flags;
2547c478bd9Sstevel@tonic-gate static int		err = 0;	/* Contains return code */
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate static uid_t		lastuid	= (uid_t)-1;
2577c478bd9Sstevel@tonic-gate static gid_t		lastgid = (gid_t)-1;
2587c478bd9Sstevel@tonic-gate static char		*lastuname = NULL;
2597c478bd9Sstevel@tonic-gate static char		*lastgname = NULL;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /* statreq > 0 if any of sflg, (n)lflg, tflg, Sflg are on */
2627c478bd9Sstevel@tonic-gate static int		statreq;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate static char		*dotp = ".";
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate static u_longlong_t 	tblocks; /* number of blocks of files in a directory */
2677c478bd9Sstevel@tonic-gate static time_t		year, now;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate static int		num_cols = 80;
2707c478bd9Sstevel@tonic-gate static int		colwidth;
2717c478bd9Sstevel@tonic-gate static int		filewidth;
2727c478bd9Sstevel@tonic-gate static int		fixedwidth;
2737c478bd9Sstevel@tonic-gate static int		nomocore;
2747c478bd9Sstevel@tonic-gate static int		curcol;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate static struct	winsize	win;
2777c478bd9Sstevel@tonic-gate 
278*da6c28aaSamw static char	time_buf[FMTSIZE];	/* array to hold day and time */
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate #define	NOTWORKINGDIR(d, l)	(((l) < 2) || \
2817c478bd9Sstevel@tonic-gate 				    (strcmp((d) + (l) - 2, "/.") != 0))
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate #define	NOTPARENTDIR(d, l)	(((l) < 3) || \
2847c478bd9Sstevel@tonic-gate 				    (strcmp((d) + (l) - 3, "/..") != 0))
285*da6c28aaSamw /* Extended system attributes support */
286*da6c28aaSamw static int get_sysxattr(char *, struct lbuf *);
287*da6c28aaSamw static void set_sysattrb_display(char *, boolean_t, struct lbuf *);
288*da6c28aaSamw static void set_sysattrtm_display(char *, struct lbuf *);
289*da6c28aaSamw static void format_time(const char *, time_t);
290*da6c28aaSamw static void format_etime(const char *, time_t, time_t);
291*da6c28aaSamw static void print_time(struct lbuf *);
292*da6c28aaSamw static void format_attrtime(struct lbuf *);
293*da6c28aaSamw static void *xmalloc(size_t, struct lbuf *);
294*da6c28aaSamw static void free_sysattr(struct lbuf *);
295*da6c28aaSamw static nvpair_t *pair;
296*da6c28aaSamw static nvlist_t	*response;
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate int
2997c478bd9Sstevel@tonic-gate main(int argc, char *argv[])
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	int		c;
3027c478bd9Sstevel@tonic-gate 	int		i;
3037c478bd9Sstevel@tonic-gate 	int		width;
3047c478bd9Sstevel@tonic-gate 	int		amino = 0;
3057c478bd9Sstevel@tonic-gate 	int		opterr = 0;
3067c478bd9Sstevel@tonic-gate 	struct lbuf	*ep;
3077c478bd9Sstevel@tonic-gate 	struct lbuf	lb;
3087c478bd9Sstevel@tonic-gate 	struct ditem	*myinfo;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
3117c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
3127c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't */
3137c478bd9Sstevel@tonic-gate #endif
3147c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
3157c478bd9Sstevel@tonic-gate #ifdef STANDALONE
3167c478bd9Sstevel@tonic-gate 	if (argv[0][0] == '\0')
3177c478bd9Sstevel@tonic-gate 		argc = getargv("ls", &argv, 0);
3187c478bd9Sstevel@tonic-gate #endif
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	lb.lmtime.tv_sec = time(NULL);
3217c478bd9Sstevel@tonic-gate 	lb.lmtime.tv_nsec = 0;
3227c478bd9Sstevel@tonic-gate 	year = lb.lmtime.tv_sec - 6L*30L*24L*60L*60L; /* 6 months ago */
3237c478bd9Sstevel@tonic-gate 	now = lb.lmtime.tv_sec + 60;
3247c478bd9Sstevel@tonic-gate 	if (isatty(1)) {
3257c478bd9Sstevel@tonic-gate 		Cflg = 1;
3267c478bd9Sstevel@tonic-gate 		mflg = 0;
3277c478bd9Sstevel@tonic-gate 	}
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv,
330*da6c28aaSamw 	    "aAbcCdeEfFghHilLmnopqrRsStux1@vV/:%:")) != EOF)
3317c478bd9Sstevel@tonic-gate 		switch (c) {
3327c478bd9Sstevel@tonic-gate 		case 'a':
3337c478bd9Sstevel@tonic-gate 			aflg++;
3347c478bd9Sstevel@tonic-gate 			continue;
3357c478bd9Sstevel@tonic-gate 		case 'A':
3367c478bd9Sstevel@tonic-gate 			Aflg++;
3377c478bd9Sstevel@tonic-gate 			continue;
3387c478bd9Sstevel@tonic-gate 		case 'b':
3397c478bd9Sstevel@tonic-gate 			bflg = 1;
3407c478bd9Sstevel@tonic-gate 			qflg = 0;
3417c478bd9Sstevel@tonic-gate 			continue;
3427c478bd9Sstevel@tonic-gate 		case 'c':
3437c478bd9Sstevel@tonic-gate 			uflg = 0;
344*da6c28aaSamw 			atm = 0;
345*da6c28aaSamw 			ctm = 0;
346*da6c28aaSamw 			mtm = 0;
347*da6c28aaSamw 			crtm = 0;
3487c478bd9Sstevel@tonic-gate 			cflg++;
3497c478bd9Sstevel@tonic-gate 			continue;
3507c478bd9Sstevel@tonic-gate 		case 'C':
3517c478bd9Sstevel@tonic-gate 			Cflg = 1;
3527c478bd9Sstevel@tonic-gate 			mflg = 0;
3537c478bd9Sstevel@tonic-gate #ifdef XPG4
3547c478bd9Sstevel@tonic-gate 			lflg = 0;
3557c478bd9Sstevel@tonic-gate #endif
3567c478bd9Sstevel@tonic-gate 			continue;
3577c478bd9Sstevel@tonic-gate 		case 'd':
3587c478bd9Sstevel@tonic-gate 			dflg++;
3597c478bd9Sstevel@tonic-gate 			continue;
3607c478bd9Sstevel@tonic-gate 		case 'e':
3617c478bd9Sstevel@tonic-gate 			eflg++;
3627c478bd9Sstevel@tonic-gate 			lflg++;
3637c478bd9Sstevel@tonic-gate 			statreq++;
3647c478bd9Sstevel@tonic-gate 			Eflg = 0;
3657c478bd9Sstevel@tonic-gate 			continue;
3667c478bd9Sstevel@tonic-gate 		case 'E':
3677c478bd9Sstevel@tonic-gate 			Eflg++;
3687c478bd9Sstevel@tonic-gate 			lflg++;
3697c478bd9Sstevel@tonic-gate 			statreq++;
3707c478bd9Sstevel@tonic-gate 			eflg = 0;
3717c478bd9Sstevel@tonic-gate 			continue;
3727c478bd9Sstevel@tonic-gate 		case 'f':
3737c478bd9Sstevel@tonic-gate 			fflg++;
3747c478bd9Sstevel@tonic-gate 			continue;
3757c478bd9Sstevel@tonic-gate 		case 'F':
3767c478bd9Sstevel@tonic-gate 			Fflg++;
3777c478bd9Sstevel@tonic-gate 			statreq++;
3787c478bd9Sstevel@tonic-gate 			continue;
3797c478bd9Sstevel@tonic-gate 		case 'g':
3807c478bd9Sstevel@tonic-gate 			gflg++;
3817c478bd9Sstevel@tonic-gate 			lflg++;
3827c478bd9Sstevel@tonic-gate 			statreq++;
3837c478bd9Sstevel@tonic-gate 			continue;
3847c478bd9Sstevel@tonic-gate 		case 'h':
3857c478bd9Sstevel@tonic-gate 			hflg++;
3867c478bd9Sstevel@tonic-gate 			hscale = 1024;
3877c478bd9Sstevel@tonic-gate 			continue;
3887c478bd9Sstevel@tonic-gate 		case 'H':
3897c478bd9Sstevel@tonic-gate 			Hflg++;
3907c478bd9Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
3917c478bd9Sstevel@tonic-gate 			Lflg = 0;
3927c478bd9Sstevel@tonic-gate 			continue;
3937c478bd9Sstevel@tonic-gate 		case 'i':
3947c478bd9Sstevel@tonic-gate 			iflg++;
3957c478bd9Sstevel@tonic-gate 			continue;
3967c478bd9Sstevel@tonic-gate 		case 'l':
3977c478bd9Sstevel@tonic-gate 			lflg++;
3987c478bd9Sstevel@tonic-gate 			statreq++;
3997c478bd9Sstevel@tonic-gate 			Cflg = 0;
4007c478bd9Sstevel@tonic-gate 			xflg = 0;
4017c478bd9Sstevel@tonic-gate 			mflg = 0;
4027c478bd9Sstevel@tonic-gate 			atflg = 0;
4037c478bd9Sstevel@tonic-gate 			continue;
4047c478bd9Sstevel@tonic-gate 		case 'L':
4057c478bd9Sstevel@tonic-gate 			Lflg++;
4067c478bd9Sstevel@tonic-gate 			/* -H and -L are mutually exclusive */
4077c478bd9Sstevel@tonic-gate 			Hflg = 0;
4087c478bd9Sstevel@tonic-gate 			continue;
4097c478bd9Sstevel@tonic-gate 		case 'm':
4107c478bd9Sstevel@tonic-gate 			Cflg = 0;
4117c478bd9Sstevel@tonic-gate 			mflg = 1;
4127c478bd9Sstevel@tonic-gate #ifdef XPG4
4137c478bd9Sstevel@tonic-gate 			lflg = 0;
4147c478bd9Sstevel@tonic-gate #endif
4157c478bd9Sstevel@tonic-gate 			continue;
4167c478bd9Sstevel@tonic-gate 		case 'n':
4177c478bd9Sstevel@tonic-gate 			nflg++;
4187c478bd9Sstevel@tonic-gate 			lflg++;
4197c478bd9Sstevel@tonic-gate 			statreq++;
4207c478bd9Sstevel@tonic-gate 			Cflg = 0;
4217c478bd9Sstevel@tonic-gate 			xflg = 0;
4227c478bd9Sstevel@tonic-gate 			mflg = 0;
4237c478bd9Sstevel@tonic-gate 			atflg = 0;
4247c478bd9Sstevel@tonic-gate 			continue;
4257c478bd9Sstevel@tonic-gate 		case 'o':
4267c478bd9Sstevel@tonic-gate 			oflg++;
4277c478bd9Sstevel@tonic-gate 			lflg++;
4287c478bd9Sstevel@tonic-gate 			statreq++;
4297c478bd9Sstevel@tonic-gate 			continue;
4307c478bd9Sstevel@tonic-gate 		case 'p':
4317c478bd9Sstevel@tonic-gate 			pflg++;
4327c478bd9Sstevel@tonic-gate 			statreq++;
4337c478bd9Sstevel@tonic-gate 			continue;
4347c478bd9Sstevel@tonic-gate 		case 'q':
4357c478bd9Sstevel@tonic-gate 			qflg = 1;
4367c478bd9Sstevel@tonic-gate 			bflg = 0;
4377c478bd9Sstevel@tonic-gate 			continue;
4387c478bd9Sstevel@tonic-gate 		case 'r':
4397c478bd9Sstevel@tonic-gate 			rflg = -1;
4407c478bd9Sstevel@tonic-gate 			continue;
4417c478bd9Sstevel@tonic-gate 		case 'R':
4427c478bd9Sstevel@tonic-gate 			Rflg++;
4437c478bd9Sstevel@tonic-gate 			statreq++;
4447c478bd9Sstevel@tonic-gate 			continue;
4457c478bd9Sstevel@tonic-gate 		case 's':
4467c478bd9Sstevel@tonic-gate 			sflg++;
4477c478bd9Sstevel@tonic-gate 			statreq++;
4487c478bd9Sstevel@tonic-gate 			continue;
4497c478bd9Sstevel@tonic-gate 		case 'S':
4507c478bd9Sstevel@tonic-gate 			tflg = 0;
4517c478bd9Sstevel@tonic-gate 			Sflg++;
4527c478bd9Sstevel@tonic-gate 			statreq++;
4537c478bd9Sstevel@tonic-gate 			continue;
4547c478bd9Sstevel@tonic-gate 		case 't':
4557c478bd9Sstevel@tonic-gate 			Sflg = 0;
4567c478bd9Sstevel@tonic-gate 			tflg++;
4577c478bd9Sstevel@tonic-gate 			statreq++;
4587c478bd9Sstevel@tonic-gate 			continue;
4597c478bd9Sstevel@tonic-gate 		case 'u':
4607c478bd9Sstevel@tonic-gate 			cflg = 0;
461*da6c28aaSamw 			atm = 0;
462*da6c28aaSamw 			ctm = 0;
463*da6c28aaSamw 			mtm = 0;
464*da6c28aaSamw 			crtm = 0;
4657c478bd9Sstevel@tonic-gate 			uflg++;
4667c478bd9Sstevel@tonic-gate 			continue;
4675a5eeccaSmarks 		case 'V':
4685a5eeccaSmarks 			Vflg++;
4695a5eeccaSmarks 			/*FALLTHROUGH*/
470fa9e4066Sahrens 		case 'v':
471fa9e4066Sahrens 			vflg++;
472fa9e4066Sahrens #if !defined(XPG4)
473fa9e4066Sahrens 			if (lflg)
474fa9e4066Sahrens 				continue;
475fa9e4066Sahrens #endif
476fa9e4066Sahrens 			lflg++;
477fa9e4066Sahrens 			statreq++;
478fa9e4066Sahrens 			Cflg = 0;
479fa9e4066Sahrens 			xflg = 0;
480fa9e4066Sahrens 			mflg = 0;
481fa9e4066Sahrens 			continue;
4827c478bd9Sstevel@tonic-gate 		case 'x':
4837c478bd9Sstevel@tonic-gate 			xflg = 1;
4847c478bd9Sstevel@tonic-gate 			Cflg = 1;
4857c478bd9Sstevel@tonic-gate 			mflg = 0;
4867c478bd9Sstevel@tonic-gate #ifdef XPG4
4877c478bd9Sstevel@tonic-gate 			lflg = 0;
4887c478bd9Sstevel@tonic-gate #endif
4897c478bd9Sstevel@tonic-gate 			continue;
4907c478bd9Sstevel@tonic-gate 		case '1':
4917c478bd9Sstevel@tonic-gate 			Cflg = 0;
4927c478bd9Sstevel@tonic-gate 			continue;
4937c478bd9Sstevel@tonic-gate 		case '@':
4947c478bd9Sstevel@tonic-gate #if !defined(XPG4)
4957c478bd9Sstevel@tonic-gate 			/*
4967c478bd9Sstevel@tonic-gate 			 * -l has precedence over -@
4977c478bd9Sstevel@tonic-gate 			 */
4987c478bd9Sstevel@tonic-gate 			if (lflg)
4997c478bd9Sstevel@tonic-gate 				continue;
5007c478bd9Sstevel@tonic-gate #endif
5017c478bd9Sstevel@tonic-gate 			atflg++;
5027c478bd9Sstevel@tonic-gate 			lflg++;
5037c478bd9Sstevel@tonic-gate 			statreq++;
5047c478bd9Sstevel@tonic-gate 			Cflg = 0;
5057c478bd9Sstevel@tonic-gate 			xflg = 0;
5067c478bd9Sstevel@tonic-gate 			mflg = 0;
5077c478bd9Sstevel@tonic-gate 			continue;
508*da6c28aaSamw 		case '/':
509*da6c28aaSamw 			saflg++;
510*da6c28aaSamw 			if (optarg != NULL) {
511*da6c28aaSamw 				if (strcmp(optarg, "c") == 0) {
512*da6c28aaSamw 					copt++;
513*da6c28aaSamw 					vopt = 0;
514*da6c28aaSamw 				} else if (strcmp(optarg, "v") == 0) {
515*da6c28aaSamw 					vopt++;
516*da6c28aaSamw 					copt = 0;
517*da6c28aaSamw 				} else
518*da6c28aaSamw 					opterr++;
519*da6c28aaSamw 			} else
520*da6c28aaSamw 				opterr++;
521*da6c28aaSamw 			lflg++;
522*da6c28aaSamw 			statreq++;
523*da6c28aaSamw 			Cflg = 0;
524*da6c28aaSamw 			xflg = 0;
525*da6c28aaSamw 			mflg = 0;
526*da6c28aaSamw 			continue;
527*da6c28aaSamw 		case '%':
528*da6c28aaSamw 			tmflg++;
529*da6c28aaSamw 			if (optarg != NULL) {
530*da6c28aaSamw 				if (strcmp(optarg, "ctime") == 0) {
531*da6c28aaSamw 					ctm++;
532*da6c28aaSamw 					atm = 0;
533*da6c28aaSamw 					mtm = 0;
534*da6c28aaSamw 					crtm = 0;
535*da6c28aaSamw 				} else if (strcmp(optarg, "atime") == 0) {
536*da6c28aaSamw 					atm++;
537*da6c28aaSamw 					ctm = 0;
538*da6c28aaSamw 					mtm = 0;
539*da6c28aaSamw 					crtm = 0;
540*da6c28aaSamw 					uflg = 0;
541*da6c28aaSamw 					cflg = 0;
542*da6c28aaSamw 				} else if (strcmp(optarg, "mtime") == 0) {
543*da6c28aaSamw 					mtm++;
544*da6c28aaSamw 					atm = 0;
545*da6c28aaSamw 					ctm = 0;
546*da6c28aaSamw 					crtm = 0;
547*da6c28aaSamw 					uflg = 0;
548*da6c28aaSamw 					cflg = 0;
549*da6c28aaSamw 				} else if (strcmp(optarg, "crtime") == 0) {
550*da6c28aaSamw 					crtm++;
551*da6c28aaSamw 					atm = 0;
552*da6c28aaSamw 					ctm = 0;
553*da6c28aaSamw 					mtm = 0;
554*da6c28aaSamw 					uflg = 0;
555*da6c28aaSamw 					cflg = 0;
556*da6c28aaSamw 				} else if (strcmp(optarg, "all") == 0) {
557*da6c28aaSamw 					alltm++;
558*da6c28aaSamw 					atm = 0;
559*da6c28aaSamw 					ctm = 0;
560*da6c28aaSamw 					mtm = 0;
561*da6c28aaSamw 					crtm = 0;
562*da6c28aaSamw 				} else
563*da6c28aaSamw 					opterr++;
564*da6c28aaSamw 			} else
565*da6c28aaSamw 				opterr++;
566*da6c28aaSamw 
567*da6c28aaSamw 			Sflg = 0;
568*da6c28aaSamw 			statreq++;
569*da6c28aaSamw 			mflg = 0;
570*da6c28aaSamw 			continue;
5717c478bd9Sstevel@tonic-gate 		case '?':
5727c478bd9Sstevel@tonic-gate 			opterr++;
5737c478bd9Sstevel@tonic-gate 			continue;
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate 	if (opterr) {
5767c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
577*da6c28aaSamw 		    "usage: ls -aAbcCdeEfFghHilLmnopqrRsStuxvV1@/%[c | v]"
578*da6c28aaSamw 		    "%%[atime | crtime | ctime | mtime | all]"
579*da6c28aaSamw 		    " [files]\n"));
5807c478bd9Sstevel@tonic-gate 		exit(2);
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	if (fflg) {
5847c478bd9Sstevel@tonic-gate 		aflg++;
5857c478bd9Sstevel@tonic-gate 		lflg = 0;
5867c478bd9Sstevel@tonic-gate 		sflg = 0;
5877c478bd9Sstevel@tonic-gate 		tflg = 0;
5887c478bd9Sstevel@tonic-gate 		Sflg = 0;
5897c478bd9Sstevel@tonic-gate 		statreq = 0;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	fixedwidth = 2;
5937c478bd9Sstevel@tonic-gate 	if (pflg || Fflg)
5947c478bd9Sstevel@tonic-gate 		fixedwidth++;
5957c478bd9Sstevel@tonic-gate 	if (iflg)
5967c478bd9Sstevel@tonic-gate 		fixedwidth += 11;
5977c478bd9Sstevel@tonic-gate 	if (sflg)
5987c478bd9Sstevel@tonic-gate 		fixedwidth += 5;
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (lflg) {
6017c478bd9Sstevel@tonic-gate 		if (!gflg && !oflg)
6027c478bd9Sstevel@tonic-gate 			gflg = oflg = 1;
6037c478bd9Sstevel@tonic-gate 		else
6047c478bd9Sstevel@tonic-gate 		if (gflg && oflg)
6057c478bd9Sstevel@tonic-gate 			gflg = oflg = 0;
6067c478bd9Sstevel@tonic-gate 		Cflg = mflg = 0;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (Cflg || mflg) {
6107c478bd9Sstevel@tonic-gate 		char *clptr;
6117c478bd9Sstevel@tonic-gate 		if ((clptr = getenv("COLUMNS")) != NULL)
6127c478bd9Sstevel@tonic-gate 			num_cols = atoi(clptr);
6137c478bd9Sstevel@tonic-gate #ifdef TERMINFO
6147c478bd9Sstevel@tonic-gate 		else {
6157c478bd9Sstevel@tonic-gate 			if (ioctl(1, TIOCGWINSZ, &win) != -1)
6167c478bd9Sstevel@tonic-gate 				num_cols = (win.ws_col == 0 ? 80 : win.ws_col);
6177c478bd9Sstevel@tonic-gate 		}
6187c478bd9Sstevel@tonic-gate #endif
6197c478bd9Sstevel@tonic-gate 		if (num_cols < 20 || num_cols > 1000)
6207c478bd9Sstevel@tonic-gate 			/* assume it is an error */
6217c478bd9Sstevel@tonic-gate 			num_cols = 80;
6227c478bd9Sstevel@tonic-gate 	}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	/* allocate space for flist and the associated	*/
6257c478bd9Sstevel@tonic-gate 	/* data structures (lbufs)			*/
6267c478bd9Sstevel@tonic-gate 	maxfils = quantn;
6277c478bd9Sstevel@tonic-gate 	if (((flist = malloc(maxfils * sizeof (struct lbuf *))) == NULL) ||
6287c478bd9Sstevel@tonic-gate 	    ((nxtlbf = malloc(quantn * sizeof (struct lbuf))) == NULL)) {
6297c478bd9Sstevel@tonic-gate 		perror("ls");
6307c478bd9Sstevel@tonic-gate 		exit(2);
6317c478bd9Sstevel@tonic-gate 	}
6327c478bd9Sstevel@tonic-gate 	if ((amino = (argc-optind)) == 0) {
6337c478bd9Sstevel@tonic-gate 					/*
6347c478bd9Sstevel@tonic-gate 					 * case when no names are given
6357c478bd9Sstevel@tonic-gate 					 * in ls-command and current
6367c478bd9Sstevel@tonic-gate 					 * directory is to be used
6377c478bd9Sstevel@tonic-gate 					 */
6387c478bd9Sstevel@tonic-gate 		argv[optind] = dotp;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 	for (i = 0; i < (amino ? amino : 1); i++) {
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		/*
6447c478bd9Sstevel@tonic-gate 		 * If we are recursing, we need to make sure we don't
6457c478bd9Sstevel@tonic-gate 		 * get into an endless loop.  To keep track of the inodes
6467c478bd9Sstevel@tonic-gate 		 * (actually, just the directories) visited, we
6477c478bd9Sstevel@tonic-gate 		 * maintain a directory ancestry list for a file
6487c478bd9Sstevel@tonic-gate 		 * hierarchy.  As we go deeper into the hierarchy,
6497c478bd9Sstevel@tonic-gate 		 * a parent directory passes its directory list
6507c478bd9Sstevel@tonic-gate 		 * info (device id, inode number, and a pointer to
6517c478bd9Sstevel@tonic-gate 		 * its parent) to each of its children.  As we
6527c478bd9Sstevel@tonic-gate 		 * process a child that is a directory, we save
6537c478bd9Sstevel@tonic-gate 		 * its own personal directory list info.  We then
6547c478bd9Sstevel@tonic-gate 		 * check to see if the child has already been
6557c478bd9Sstevel@tonic-gate 		 * processed by comparing its device id and inode
6567c478bd9Sstevel@tonic-gate 		 * number from its own personal directory list info
6577c478bd9Sstevel@tonic-gate 		 * to that of each of its ancestors.  If there is a
6587c478bd9Sstevel@tonic-gate 		 * match, then we know we've detected a cycle.
6597c478bd9Sstevel@tonic-gate 		 */
6607c478bd9Sstevel@tonic-gate 		if (Rflg) {
6617c478bd9Sstevel@tonic-gate 			/*
6627c478bd9Sstevel@tonic-gate 			 * This is the first parent in this lineage
6637c478bd9Sstevel@tonic-gate 			 * (first in a directory hierarchy), so
6647c478bd9Sstevel@tonic-gate 			 * this parent's parent doesn't exist.  We
6657c478bd9Sstevel@tonic-gate 			 * only initialize myinfo when we are
6667c478bd9Sstevel@tonic-gate 			 * recursing, otherwise it's not used.
6677c478bd9Sstevel@tonic-gate 			 */
6687c478bd9Sstevel@tonic-gate 			if ((myinfo = (struct ditem *)malloc(
6697c478bd9Sstevel@tonic-gate 			    sizeof (struct ditem))) == NULL) {
6707c478bd9Sstevel@tonic-gate 				perror("ls");
6717c478bd9Sstevel@tonic-gate 				exit(2);
6727c478bd9Sstevel@tonic-gate 			} else {
6737c478bd9Sstevel@tonic-gate 				myinfo->dev = 0;
6747c478bd9Sstevel@tonic-gate 				myinfo->ino = 0;
6757c478bd9Sstevel@tonic-gate 				myinfo->parent = NULL;
6767c478bd9Sstevel@tonic-gate 			}
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 		if (Cflg || mflg) {
6807c478bd9Sstevel@tonic-gate 			width = strcol((unsigned char *)argv[optind]);
6817c478bd9Sstevel@tonic-gate 			if (width > filewidth)
6827c478bd9Sstevel@tonic-gate 				filewidth = width;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 		if ((ep = gstat((*argv[optind] ? argv[optind] : dotp),
6857c478bd9Sstevel@tonic-gate 		    1, myinfo)) == NULL) {
6867c478bd9Sstevel@tonic-gate 			if (nomocore)
6877c478bd9Sstevel@tonic-gate 				exit(2);
6887c478bd9Sstevel@tonic-gate 			err = 2;
6897c478bd9Sstevel@tonic-gate 			optind++;
6907c478bd9Sstevel@tonic-gate 			continue;
6917c478bd9Sstevel@tonic-gate 		}
6927c478bd9Sstevel@tonic-gate 		ep->ln.namep = (*argv[optind] ? argv[optind] : dotp);
6937c478bd9Sstevel@tonic-gate 		ep->lflags |= ISARG;
6947c478bd9Sstevel@tonic-gate 		optind++;
6957c478bd9Sstevel@tonic-gate 		nargs++;	/* count good arguments stored in flist */
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	colwidth = fixedwidth + filewidth;
6987c478bd9Sstevel@tonic-gate 	qsort(flist, (unsigned)nargs, sizeof (struct lbuf *),
6997c478bd9Sstevel@tonic-gate 	    (int (*)(const void *, const void *))compar);
7007c478bd9Sstevel@tonic-gate 	for (i = 0; i < nargs; i++) {
7017c478bd9Sstevel@tonic-gate 		if (flist[i]->ltype == 'd' && dflg == 0 || fflg)
7027c478bd9Sstevel@tonic-gate 			break;
7037c478bd9Sstevel@tonic-gate 	}
7047c478bd9Sstevel@tonic-gate 	pem(&flist[0], &flist[i], 0);
7057c478bd9Sstevel@tonic-gate 	for (; i < nargs; i++) {
7067c478bd9Sstevel@tonic-gate 		pdirectory(flist[i]->ln.namep, Rflg ||
7077c478bd9Sstevel@tonic-gate 		    (amino > 1), nargs, 0, flist[i]->ancinfo);
7087c478bd9Sstevel@tonic-gate 		if (nomocore)
7097c478bd9Sstevel@tonic-gate 			exit(2);
7107c478bd9Sstevel@tonic-gate 		/* -R: print subdirectories found */
7117c478bd9Sstevel@tonic-gate 		while (dfirst || cdfirst) {
7127c478bd9Sstevel@tonic-gate 			/* Place direct subdirs on front in right order */
7137c478bd9Sstevel@tonic-gate 			while (cdfirst) {
7147c478bd9Sstevel@tonic-gate 				/* reverse cdfirst onto front of dfirst */
7157c478bd9Sstevel@tonic-gate 				dtemp = cdfirst;
7167c478bd9Sstevel@tonic-gate 				cdfirst = cdfirst -> dc_next;
7177c478bd9Sstevel@tonic-gate 				dtemp -> dc_next = dfirst;
7187c478bd9Sstevel@tonic-gate 				dfirst = dtemp;
7197c478bd9Sstevel@tonic-gate 			}
7207c478bd9Sstevel@tonic-gate 			/* take off first dir on dfirst & print it */
7217c478bd9Sstevel@tonic-gate 			dtemp = dfirst;
7227c478bd9Sstevel@tonic-gate 			dfirst = dfirst->dc_next;
7237c478bd9Sstevel@tonic-gate 			pdirectory(dtemp->dc_name, 1, nargs,
7247c478bd9Sstevel@tonic-gate 			    dtemp->cycle_detected, dtemp->myancinfo);
7257c478bd9Sstevel@tonic-gate 			if (nomocore)
7267c478bd9Sstevel@tonic-gate 				exit(2);
7277c478bd9Sstevel@tonic-gate 			free(dtemp->dc_name);
7287c478bd9Sstevel@tonic-gate 			free(dtemp);
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 	return (err);
7327c478bd9Sstevel@tonic-gate }
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate /*
7357c478bd9Sstevel@tonic-gate  * pdirectory: print the directory name, labelling it if title is
7367c478bd9Sstevel@tonic-gate  * nonzero, using lp as the place to start reading in the dir.
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate static void
7397c478bd9Sstevel@tonic-gate pdirectory(char *name, int title, int lp, int cdetect, struct ditem *myinfo)
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	struct dchain *dp;
7427c478bd9Sstevel@tonic-gate 	struct lbuf *ap;
7437c478bd9Sstevel@tonic-gate 	char *pname;
7447c478bd9Sstevel@tonic-gate 	int j;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	filewidth = 0;
7477c478bd9Sstevel@tonic-gate 	curdir = name;
7487c478bd9Sstevel@tonic-gate 	if (title) {
7497c478bd9Sstevel@tonic-gate 		if (!first)
7507c478bd9Sstevel@tonic-gate 			(void) putc('\n', stdout);
7517c478bd9Sstevel@tonic-gate 		pprintf(name, ":");
7527c478bd9Sstevel@tonic-gate 		new_line();
7537c478bd9Sstevel@tonic-gate 	}
7547c478bd9Sstevel@tonic-gate 	/*
7557c478bd9Sstevel@tonic-gate 	 * If there was a cycle detected, then notify and don't report
7567c478bd9Sstevel@tonic-gate 	 * further.
7577c478bd9Sstevel@tonic-gate 	 */
7587c478bd9Sstevel@tonic-gate 	if (cdetect) {
7597c478bd9Sstevel@tonic-gate 		if (lflg || sflg) {
7607c478bd9Sstevel@tonic-gate 			curcol += printf(gettext("total %d"), 0);
7617c478bd9Sstevel@tonic-gate 			new_line();
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(
7647c478bd9Sstevel@tonic-gate 		    "ls: cycle detected for %s\n"), name);
7657c478bd9Sstevel@tonic-gate 		return;
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	nfiles = lp;
7697c478bd9Sstevel@tonic-gate 	rddir(name, myinfo);
7707c478bd9Sstevel@tonic-gate 	if (nomocore)
7717c478bd9Sstevel@tonic-gate 		return;
7727c478bd9Sstevel@tonic-gate 	if (fflg == 0)
7737c478bd9Sstevel@tonic-gate 		qsort(&flist[lp], (unsigned)(nfiles - lp),
7747c478bd9Sstevel@tonic-gate 		    sizeof (struct lbuf *),
7757c478bd9Sstevel@tonic-gate 		    (int (*)(const void *, const void *))compar);
7767c478bd9Sstevel@tonic-gate 	if (Rflg) {
7777c478bd9Sstevel@tonic-gate 		for (j = nfiles - 1; j >= lp; j--) {
7787c478bd9Sstevel@tonic-gate 			ap = flist[j];
7797c478bd9Sstevel@tonic-gate 			if (ap->ltype == 'd' && strcmp(ap->ln.lname, ".") &&
7807c478bd9Sstevel@tonic-gate 			    strcmp(ap->ln.lname, "..")) {
7817c478bd9Sstevel@tonic-gate 				dp = malloc(sizeof (struct dchain));
7827c478bd9Sstevel@tonic-gate 				if (dp == NULL) {
7837c478bd9Sstevel@tonic-gate 					perror("ls");
7847c478bd9Sstevel@tonic-gate 					exit(2);
7857c478bd9Sstevel@tonic-gate 				}
7867c478bd9Sstevel@tonic-gate 				pname = makename(curdir, ap->ln.lname);
7877c478bd9Sstevel@tonic-gate 				if ((dp->dc_name = strdup(pname)) == NULL) {
7887c478bd9Sstevel@tonic-gate 					perror("ls");
7897c478bd9Sstevel@tonic-gate 					exit(2);
7907c478bd9Sstevel@tonic-gate 				}
7917c478bd9Sstevel@tonic-gate 				dp->cycle_detected = ap->cycle;
7927c478bd9Sstevel@tonic-gate 				dp->myancinfo = ap->ancinfo;
7937c478bd9Sstevel@tonic-gate 				dp->dc_next = dfirst;
7947c478bd9Sstevel@tonic-gate 				dfirst = dp;
7957c478bd9Sstevel@tonic-gate 			}
7967c478bd9Sstevel@tonic-gate 		}
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 	if (lflg || sflg) {
7997c478bd9Sstevel@tonic-gate 		curcol += printf(gettext("total %llu"), tblocks);
8007c478bd9Sstevel@tonic-gate 		new_line();
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate 	pem(&flist[lp], &flist[nfiles], lflg||sflg);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * pem: print 'em. Print a list of files (e.g. a directory) bounded
8077c478bd9Sstevel@tonic-gate  * by slp and lp.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate static void
8107c478bd9Sstevel@tonic-gate pem(struct lbuf **slp, struct lbuf **lp, int tot_flag)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	long row, nrows, i;
8137c478bd9Sstevel@tonic-gate 	int col, ncols;
8147c478bd9Sstevel@tonic-gate 	struct lbuf **ep;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	if (Cflg || mflg) {
8177c478bd9Sstevel@tonic-gate 		if (colwidth > num_cols) {
8187c478bd9Sstevel@tonic-gate 			ncols = 1;
8197c478bd9Sstevel@tonic-gate 		} else {
8207c478bd9Sstevel@tonic-gate 			ncols = num_cols / colwidth;
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	if (ncols == 1 || mflg || xflg || !Cflg) {
8257c478bd9Sstevel@tonic-gate 		for (ep = slp; ep < lp; ep++)
8267c478bd9Sstevel@tonic-gate 			pentry(*ep);
8277c478bd9Sstevel@tonic-gate 		new_line();
8287c478bd9Sstevel@tonic-gate 		return;
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 	/* otherwise print -C columns */
8317c478bd9Sstevel@tonic-gate 	if (tot_flag) {
8327c478bd9Sstevel@tonic-gate 		slp--;
8337c478bd9Sstevel@tonic-gate 		row = 1;
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	else
8367c478bd9Sstevel@tonic-gate 		row = 0;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	nrows = (lp - slp - 1) / ncols + 1;
8397c478bd9Sstevel@tonic-gate 	for (i = 0; i < nrows; i++, row++) {
8407c478bd9Sstevel@tonic-gate 		for (col = 0; col < ncols; col++) {
8417c478bd9Sstevel@tonic-gate 			ep = slp + (nrows * col) + row;
8427c478bd9Sstevel@tonic-gate 			if (ep < lp)
8437c478bd9Sstevel@tonic-gate 				pentry(*ep);
8447c478bd9Sstevel@tonic-gate 		}
8457c478bd9Sstevel@tonic-gate 		new_line();
8467c478bd9Sstevel@tonic-gate 	}
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate /*
8507c478bd9Sstevel@tonic-gate  * print one output entry;
8517c478bd9Sstevel@tonic-gate  * if uid/gid is not found in the appropriate
8527c478bd9Sstevel@tonic-gate  * file(passwd/group), then print uid/gid instead of
8537c478bd9Sstevel@tonic-gate  * user/group name;
8547c478bd9Sstevel@tonic-gate  */
8557c478bd9Sstevel@tonic-gate static void
8567c478bd9Sstevel@tonic-gate pentry(struct lbuf *ap)
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	struct lbuf *p;
8597c478bd9Sstevel@tonic-gate 	numbuf_t hbuf;
8607c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
8617c478bd9Sstevel@tonic-gate 	char *dmark = "";	/* Used if -p or -F option active */
8627c478bd9Sstevel@tonic-gate 	char *cp;
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	p = ap;
8657c478bd9Sstevel@tonic-gate 	column();
8667c478bd9Sstevel@tonic-gate 	if (iflg)
8677c478bd9Sstevel@tonic-gate 		if (mflg && !lflg)
8687c478bd9Sstevel@tonic-gate 			curcol += printf("%llu ", (long long)p->lnum);
8697c478bd9Sstevel@tonic-gate 		else
8707c478bd9Sstevel@tonic-gate 			curcol += printf("%10llu ", (long long)p->lnum);
8717c478bd9Sstevel@tonic-gate 	if (sflg)
8727c478bd9Sstevel@tonic-gate 		curcol += printf((mflg && !lflg) ? "%lld " :
8737c478bd9Sstevel@tonic-gate 		    (p->lblocks < 10000) ? "%4lld " : "%lld ",
8747c478bd9Sstevel@tonic-gate 		    (p->ltype != 'b' && p->ltype != 'c') ?
8757c478bd9Sstevel@tonic-gate 		    p->lblocks : 0LL);
8767c478bd9Sstevel@tonic-gate 	if (lflg) {
8777c478bd9Sstevel@tonic-gate 		(void) putchar(p->ltype);
8787c478bd9Sstevel@tonic-gate 		curcol++;
8797c478bd9Sstevel@tonic-gate 		pmode(p->lflags);
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate 		/* ACL: additional access mode flag */
8827c478bd9Sstevel@tonic-gate 		(void) putchar(p->acl);
8837c478bd9Sstevel@tonic-gate 		curcol++;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		curcol += printf("%3lu ", (ulong_t)p->lnl);
8867c478bd9Sstevel@tonic-gate 		if (oflg)
8877c478bd9Sstevel@tonic-gate 			if (!nflg) {
8887c478bd9Sstevel@tonic-gate 				cp = getname(p->luid);
8897c478bd9Sstevel@tonic-gate 				curcol += printf("%-8s ", cp);
8907c478bd9Sstevel@tonic-gate 			} else
8917c478bd9Sstevel@tonic-gate 				curcol += printf("%-8lu ", (ulong_t)p->luid);
8927c478bd9Sstevel@tonic-gate 		if (gflg)
8937c478bd9Sstevel@tonic-gate 			if (!nflg) {
8947c478bd9Sstevel@tonic-gate 				cp = getgroup(p->lgid);
8957c478bd9Sstevel@tonic-gate 				curcol += printf("%-8s ", cp);
8967c478bd9Sstevel@tonic-gate 			} else
8977c478bd9Sstevel@tonic-gate 				curcol += printf("%-8lu ", (ulong_t)p->lgid);
8987c478bd9Sstevel@tonic-gate 		if (p->ltype == 'b' || p->ltype == 'c') {
8997c478bd9Sstevel@tonic-gate 			curcol += printf("%3u, %2u",
9007c478bd9Sstevel@tonic-gate 			    (uint_t)major((dev_t)p->lsize),
9017c478bd9Sstevel@tonic-gate 			    (uint_t)minor((dev_t)p->lsize));
9027c478bd9Sstevel@tonic-gate 		} else if (hflg && (p->lsize >= hscale)) {
9037c478bd9Sstevel@tonic-gate 			curcol += printf("%7s",
9047c478bd9Sstevel@tonic-gate 			    number_to_scaled_string(hbuf, p->lsize, hscale));
9057c478bd9Sstevel@tonic-gate 		} else {
9067c478bd9Sstevel@tonic-gate 			curcol += printf((p->lsize < (off_t)10000000) ?
9077c478bd9Sstevel@tonic-gate 			    "%7lld" : "%lld", p->lsize);
9087c478bd9Sstevel@tonic-gate 		}
909*da6c28aaSamw 		if (eflg)
910*da6c28aaSamw 			format_time(FORMAT3, p->lmtime.tv_sec);
911*da6c28aaSamw 		else if (Eflg)
9127c478bd9Sstevel@tonic-gate 			/* fill in nanoseconds first */
913*da6c28aaSamw 			format_etime(FORMAT4, p->lmtime.tv_sec,
914*da6c28aaSamw 			    p->lmtime.tv_nsec);
915*da6c28aaSamw 		else {
9167c478bd9Sstevel@tonic-gate 			if ((p->lmtime.tv_sec < year) ||
917*da6c28aaSamw 			    (p->lmtime.tv_sec > now))
918*da6c28aaSamw 				format_time(FORMAT1, p->lmtime.tv_sec);
919*da6c28aaSamw 			else
920*da6c28aaSamw 				format_time(FORMAT2, p->lmtime.tv_sec);
9217c478bd9Sstevel@tonic-gate 		}
922*da6c28aaSamw 		/* format extended system attribute time */
923*da6c28aaSamw 		if (tmflg && crtm)
924*da6c28aaSamw 			format_attrtime(p);
9257c478bd9Sstevel@tonic-gate 
926*da6c28aaSamw 		curcol += printf("%s", time_buf);
927*da6c28aaSamw 
928*da6c28aaSamw 	}
9297c478bd9Sstevel@tonic-gate 	/*
9307c478bd9Sstevel@tonic-gate 	 * prevent both "->" and trailing marks
9317c478bd9Sstevel@tonic-gate 	 * from appearing
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 	if (pflg && p->ltype == 'd')
9357c478bd9Sstevel@tonic-gate 		dmark = "/";
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	if (Fflg && !(lflg && p->flinkto)) {
9387c478bd9Sstevel@tonic-gate 		if (p->ltype == 'd')
9397c478bd9Sstevel@tonic-gate 			dmark = "/";
9407c478bd9Sstevel@tonic-gate 		else if (p->ltype == 'D')
9417c478bd9Sstevel@tonic-gate 			dmark = ">";
9427c478bd9Sstevel@tonic-gate 		else if (p->ltype == 'p')
9437c478bd9Sstevel@tonic-gate 			dmark = "|";
9447c478bd9Sstevel@tonic-gate 		else if (p->ltype == 'l')
9457c478bd9Sstevel@tonic-gate 			dmark = "@";
9467c478bd9Sstevel@tonic-gate 		else if (p->ltype == 's')
9477c478bd9Sstevel@tonic-gate 			dmark = "=";
9487c478bd9Sstevel@tonic-gate 		else if (p->lflags & (S_IXUSR|S_IXGRP|S_IXOTH))
9497c478bd9Sstevel@tonic-gate 			dmark = "*";
9507c478bd9Sstevel@tonic-gate 		else
9517c478bd9Sstevel@tonic-gate 			dmark = "";
9527c478bd9Sstevel@tonic-gate 	}
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (lflg && p->flinkto) {
9557c478bd9Sstevel@tonic-gate 		(void) strncpy(buf, " -> ", 4);
9567c478bd9Sstevel@tonic-gate 		(void) strcpy(buf + 4, p->flinkto);
9577c478bd9Sstevel@tonic-gate 		dmark = buf;
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	if (p->lflags & ISARG) {
9617c478bd9Sstevel@tonic-gate 		if (qflg || bflg)
9627c478bd9Sstevel@tonic-gate 			pprintf(p->ln.namep, dmark);
9637c478bd9Sstevel@tonic-gate 		else {
9647c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", p->ln.namep, dmark);
9657c478bd9Sstevel@tonic-gate 			curcol += strcol((unsigned char *)p->ln.namep);
9667c478bd9Sstevel@tonic-gate 			curcol += strcol((unsigned char *)dmark);
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 	} else {
9697c478bd9Sstevel@tonic-gate 		if (qflg || bflg)
9707c478bd9Sstevel@tonic-gate 			pprintf(p->ln.lname, dmark);
9717c478bd9Sstevel@tonic-gate 		else {
9727c478bd9Sstevel@tonic-gate 			(void) printf("%s%s", p->ln.lname, dmark);
9737c478bd9Sstevel@tonic-gate 			curcol += strcol((unsigned char *)p->ln.lname);
9747c478bd9Sstevel@tonic-gate 			curcol += strcol((unsigned char *)dmark);
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 	}
977fa9e4066Sahrens 
978*da6c28aaSamw 	/* Display extended system attributes */
979*da6c28aaSamw 	if (saflg) {
980*da6c28aaSamw 		int i;
981*da6c28aaSamw 
982*da6c28aaSamw 		new_line();
983*da6c28aaSamw 		(void) printf("	\t{");
984*da6c28aaSamw 		if (p->exttr != NULL) {
985*da6c28aaSamw 			int k = 0;
986*da6c28aaSamw 			for (i = 0; i < sacnt; i++) {
987*da6c28aaSamw 				if (p->exttr[i].name != NULL)
988*da6c28aaSamw 					k++;
989*da6c28aaSamw 			}
990*da6c28aaSamw 			for (i = 0; i < sacnt; i++) {
991*da6c28aaSamw 				if (p->exttr[i].name != NULL) {
992*da6c28aaSamw 					(void) printf("%s", p->exttr[i].name);
993*da6c28aaSamw 					k--;
994*da6c28aaSamw 					if (vopt && (k != 0))
995*da6c28aaSamw 						(void) printf(",");
996*da6c28aaSamw 				}
997*da6c28aaSamw 			}
998*da6c28aaSamw 		}
999*da6c28aaSamw 		(void) printf("}\n");
1000*da6c28aaSamw 	}
1001*da6c28aaSamw 	/* Display file timestamps and extended system attribute timestamps */
1002*da6c28aaSamw 	if (tmflg && alltm) {
1003*da6c28aaSamw 		new_line();
1004*da6c28aaSamw 		print_time(p);
1005*da6c28aaSamw 		new_line();
1006*da6c28aaSamw 	}
1007fa9e4066Sahrens 	if (vflg) {
1008fa9e4066Sahrens 		new_line();
1009fa9e4066Sahrens 		if (p->aclp) {
10105a5eeccaSmarks 			acl_printacl(p->aclp, num_cols, Vflg);
1011fa9e4066Sahrens 		}
1012fa9e4066Sahrens 	}
1013*da6c28aaSamw 	/* Free extended system attribute lists */
1014*da6c28aaSamw 	if (saflg || tmflg)
1015*da6c28aaSamw 		free_sysattr(p);
10167c478bd9Sstevel@tonic-gate }
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate /* print various r,w,x permissions */
10197c478bd9Sstevel@tonic-gate static void
10207c478bd9Sstevel@tonic-gate pmode(mode_t aflag)
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate 	/* these arrays are declared static to allow initializations */
10237c478bd9Sstevel@tonic-gate 	static int	m0[] = { 1, S_IRUSR, 'r', '-' };
10247c478bd9Sstevel@tonic-gate 	static int	m1[] = { 1, S_IWUSR, 'w', '-' };
10257c478bd9Sstevel@tonic-gate 	static int	m2[] = { 3, S_ISUID|S_IXUSR, 's', S_IXUSR,
10267c478bd9Sstevel@tonic-gate 	    'x', S_ISUID, 'S', '-' };
10277c478bd9Sstevel@tonic-gate 	static int	m3[] = { 1, S_IRGRP, 'r', '-' };
10287c478bd9Sstevel@tonic-gate 	static int	m4[] = { 1, S_IWGRP, 'w', '-' };
10297c478bd9Sstevel@tonic-gate 	static int	m5[] = { 4, S_ISGID|S_IXGRP, 's', S_IXGRP,
10307c478bd9Sstevel@tonic-gate 				'x', S_ISGID|LS_NOTREG, 'S',
10317c478bd9Sstevel@tonic-gate #ifdef XPG4
10327c478bd9Sstevel@tonic-gate 		S_ISGID, 'L', '-'};
10337c478bd9Sstevel@tonic-gate #else
10347c478bd9Sstevel@tonic-gate 		S_ISGID, 'l', '-'};
10357c478bd9Sstevel@tonic-gate #endif
10367c478bd9Sstevel@tonic-gate 	static int	m6[] = { 1, S_IROTH, 'r', '-' };
10377c478bd9Sstevel@tonic-gate 	static int	m7[] = { 1, S_IWOTH, 'w', '-' };
10387c478bd9Sstevel@tonic-gate 	static int	m8[] = { 3, S_ISVTX|S_IXOTH, 't', S_IXOTH,
10397c478bd9Sstevel@tonic-gate 	    'x', S_ISVTX, 'T', '-'};
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate 	static int *m[] = { m0, m1, m2, m3, m4, m5, m6, m7, m8};
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	int **mp;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	flags = aflag;
10467c478bd9Sstevel@tonic-gate 	for (mp = &m[0]; mp < &m[sizeof (m) / sizeof (m[0])]; mp++)
10477c478bd9Sstevel@tonic-gate 		selection(*mp);
10487c478bd9Sstevel@tonic-gate }
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate static void
10517c478bd9Sstevel@tonic-gate selection(int *pairp)
10527c478bd9Sstevel@tonic-gate {
10537c478bd9Sstevel@tonic-gate 	int n;
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate 	n = *pairp++;
10567c478bd9Sstevel@tonic-gate 	while (n-->0) {
10577c478bd9Sstevel@tonic-gate 		if ((flags & *pairp) == *pairp) {
10587c478bd9Sstevel@tonic-gate 			pairp++;
10597c478bd9Sstevel@tonic-gate 			break;
10607c478bd9Sstevel@tonic-gate 		} else {
10617c478bd9Sstevel@tonic-gate 			pairp += 2;
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 	}
10647c478bd9Sstevel@tonic-gate 	(void) putchar(*pairp);
10657c478bd9Sstevel@tonic-gate 	curcol++;
10667c478bd9Sstevel@tonic-gate }
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate /*
10697c478bd9Sstevel@tonic-gate  * column: get to the beginning of the next column.
10707c478bd9Sstevel@tonic-gate  */
10717c478bd9Sstevel@tonic-gate static void
10727c478bd9Sstevel@tonic-gate column(void)
10737c478bd9Sstevel@tonic-gate {
10747c478bd9Sstevel@tonic-gate 	if (curcol == 0)
10757c478bd9Sstevel@tonic-gate 		return;
10767c478bd9Sstevel@tonic-gate 	if (mflg) {
10777c478bd9Sstevel@tonic-gate 		(void) putc(',', stdout);
10787c478bd9Sstevel@tonic-gate 		curcol++;
10797c478bd9Sstevel@tonic-gate 		if (curcol + colwidth + 2 > num_cols) {
10807c478bd9Sstevel@tonic-gate 			(void) putc('\n', stdout);
10817c478bd9Sstevel@tonic-gate 			curcol = 0;
10827c478bd9Sstevel@tonic-gate 			return;
10837c478bd9Sstevel@tonic-gate 		}
10847c478bd9Sstevel@tonic-gate 		(void) putc(' ', stdout);
10857c478bd9Sstevel@tonic-gate 		curcol++;
10867c478bd9Sstevel@tonic-gate 		return;
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 	if (Cflg == 0) {
10897c478bd9Sstevel@tonic-gate 		(void) putc('\n', stdout);
10907c478bd9Sstevel@tonic-gate 		curcol = 0;
10917c478bd9Sstevel@tonic-gate 		return;
10927c478bd9Sstevel@tonic-gate 	}
10937c478bd9Sstevel@tonic-gate 	if ((curcol / colwidth + 2) * colwidth > num_cols) {
10947c478bd9Sstevel@tonic-gate 		(void) putc('\n', stdout);
10957c478bd9Sstevel@tonic-gate 		curcol = 0;
10967c478bd9Sstevel@tonic-gate 		return;
10977c478bd9Sstevel@tonic-gate 	}
10987c478bd9Sstevel@tonic-gate 	do {
10997c478bd9Sstevel@tonic-gate 		(void) putc(' ', stdout);
11007c478bd9Sstevel@tonic-gate 		curcol++;
11017c478bd9Sstevel@tonic-gate 	} while (curcol % colwidth);
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
11047c478bd9Sstevel@tonic-gate static void
11057c478bd9Sstevel@tonic-gate new_line(void)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	if (curcol) {
11087c478bd9Sstevel@tonic-gate 		first = 0;
11097c478bd9Sstevel@tonic-gate 		(void) putc('\n', stdout);
11107c478bd9Sstevel@tonic-gate 		curcol = 0;
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate }
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate /*
11157c478bd9Sstevel@tonic-gate  * read each filename in directory dir and store its
11167c478bd9Sstevel@tonic-gate  * status in flist[nfiles]
11177c478bd9Sstevel@tonic-gate  * use makename() to form pathname dir/filename;
11187c478bd9Sstevel@tonic-gate  */
11197c478bd9Sstevel@tonic-gate static void
11207c478bd9Sstevel@tonic-gate rddir(char *dir, struct ditem *myinfo)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	struct dirent *dentry;
11237c478bd9Sstevel@tonic-gate 	DIR *dirf;
11247c478bd9Sstevel@tonic-gate 	int j;
11257c478bd9Sstevel@tonic-gate 	struct lbuf *ep;
11267c478bd9Sstevel@tonic-gate 	int width;
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 	if ((dirf = opendir(dir)) == NULL) {
11297c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
11307c478bd9Sstevel@tonic-gate 		perror(dir);
11317c478bd9Sstevel@tonic-gate 		err = 2;
11327c478bd9Sstevel@tonic-gate 		return;
11337c478bd9Sstevel@tonic-gate 	} else {
11347c478bd9Sstevel@tonic-gate 		tblocks = 0;
11357c478bd9Sstevel@tonic-gate 		for (;;) {
11367c478bd9Sstevel@tonic-gate 			errno = 0;
11377c478bd9Sstevel@tonic-gate 			if ((dentry = readdir(dirf)) == NULL)
11387c478bd9Sstevel@tonic-gate 				break;
11397c478bd9Sstevel@tonic-gate 			if (aflg == 0 && dentry->d_name[0] == '.' &&
11407c478bd9Sstevel@tonic-gate 			    (Aflg == 0 ||
11417c478bd9Sstevel@tonic-gate 			    dentry->d_name[1] == '\0' ||
11427c478bd9Sstevel@tonic-gate 			    dentry->d_name[1] == '.' &&
11437c478bd9Sstevel@tonic-gate 			    dentry->d_name[2] == '\0'))
11447c478bd9Sstevel@tonic-gate 				/*
11457c478bd9Sstevel@tonic-gate 				 * check for directory items '.', '..',
11467c478bd9Sstevel@tonic-gate 				 *  and items without valid inode-number;
11477c478bd9Sstevel@tonic-gate 				 */
11487c478bd9Sstevel@tonic-gate 				continue;
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate 			if (Cflg || mflg) {
11517c478bd9Sstevel@tonic-gate 				width = strcol((unsigned char *)dentry->d_name);
11527c478bd9Sstevel@tonic-gate 				if (width > filewidth)
11537c478bd9Sstevel@tonic-gate 					filewidth = width;
11547c478bd9Sstevel@tonic-gate 			}
11557c478bd9Sstevel@tonic-gate 			ep = gstat(makename(dir, dentry->d_name), 0, myinfo);
11567c478bd9Sstevel@tonic-gate 			if (ep == NULL) {
11577c478bd9Sstevel@tonic-gate 				if (nomocore)
1158*da6c28aaSamw 					exit(2);
11597c478bd9Sstevel@tonic-gate 				continue;
11607c478bd9Sstevel@tonic-gate 			} else {
11617c478bd9Sstevel@tonic-gate 				ep->lnum = dentry->d_ino;
11627c478bd9Sstevel@tonic-gate 				for (j = 0; dentry->d_name[j] != '\0'; j++)
11637c478bd9Sstevel@tonic-gate 					ep->ln.lname[j] = dentry->d_name[j];
11647c478bd9Sstevel@tonic-gate 				ep->ln.lname[j] = '\0';
11657c478bd9Sstevel@tonic-gate 			}
11667c478bd9Sstevel@tonic-gate 		}
11677c478bd9Sstevel@tonic-gate 		if (errno) {
11687c478bd9Sstevel@tonic-gate 			int sav_errno = errno;
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
11717c478bd9Sstevel@tonic-gate 			    gettext("ls: error reading directory %s: %s\n"),
11727c478bd9Sstevel@tonic-gate 			    dir, strerror(sav_errno));
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 		(void) closedir(dirf);
11757c478bd9Sstevel@tonic-gate 		colwidth = fixedwidth + filewidth;
11767c478bd9Sstevel@tonic-gate 	}
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate /*
11807c478bd9Sstevel@tonic-gate  * Attaching a link to an inode's ancestors.  Search
11817c478bd9Sstevel@tonic-gate  * through the ancestors to check for cycles (an inode which
11827c478bd9Sstevel@tonic-gate  * we have already tracked in this inodes ancestry).  If a cycle
11837c478bd9Sstevel@tonic-gate  * is detected, set the exit code and record the fact so that
11847c478bd9Sstevel@tonic-gate  * it is reported at the right time when printing the directory.
11857c478bd9Sstevel@tonic-gate  * In addition, set the exit code.  Note:  If the -a flag was
11867c478bd9Sstevel@tonic-gate  * specified, we don't want to check for cycles for directories
11877c478bd9Sstevel@tonic-gate  * ending in '/.' or '/..' unless they were specified on the
11887c478bd9Sstevel@tonic-gate  * command line.
11897c478bd9Sstevel@tonic-gate  */
11907c478bd9Sstevel@tonic-gate static void
11917c478bd9Sstevel@tonic-gate record_ancestry(char *file, struct stat *pstatb, struct lbuf *rep,
11927c478bd9Sstevel@tonic-gate     int argfl, struct ditem *myparent)
11937c478bd9Sstevel@tonic-gate {
11947c478bd9Sstevel@tonic-gate 	size_t		file_len;
11957c478bd9Sstevel@tonic-gate 	struct ditem	*myinfo;
11967c478bd9Sstevel@tonic-gate 	struct ditem	*tptr;
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate 	file_len = strlen(file);
11997c478bd9Sstevel@tonic-gate 	if (!aflg || argfl || (NOTWORKINGDIR(file, file_len) &&
12007c478bd9Sstevel@tonic-gate 	    NOTPARENTDIR(file, file_len))) {
12017c478bd9Sstevel@tonic-gate 		/*
12027c478bd9Sstevel@tonic-gate 		 * Add this inode's ancestry
12037c478bd9Sstevel@tonic-gate 		 * info and insert it into the
12047c478bd9Sstevel@tonic-gate 		 * ancestry list by pointing
12057c478bd9Sstevel@tonic-gate 		 * back to its parent.  We save
12067c478bd9Sstevel@tonic-gate 		 * it (in rep) with the other info
12077c478bd9Sstevel@tonic-gate 		 * we're gathering for this inode.
12087c478bd9Sstevel@tonic-gate 		 */
12097c478bd9Sstevel@tonic-gate 		if ((myinfo = malloc(
12107c478bd9Sstevel@tonic-gate 		    sizeof (struct ditem))) == NULL) {
12117c478bd9Sstevel@tonic-gate 			perror("ls");
12127c478bd9Sstevel@tonic-gate 			exit(2);
12137c478bd9Sstevel@tonic-gate 		}
12147c478bd9Sstevel@tonic-gate 		myinfo->dev = pstatb->st_dev;
12157c478bd9Sstevel@tonic-gate 		myinfo->ino = pstatb->st_ino;
12167c478bd9Sstevel@tonic-gate 		myinfo->parent = myparent;
12177c478bd9Sstevel@tonic-gate 		rep->ancinfo = myinfo;
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate 		/*
12207c478bd9Sstevel@tonic-gate 		 * If this node has the same device id and
12217c478bd9Sstevel@tonic-gate 		 * inode number of one of its ancestors,
12227c478bd9Sstevel@tonic-gate 		 * then we've detected a cycle.
12237c478bd9Sstevel@tonic-gate 		 */
12247c478bd9Sstevel@tonic-gate 		if (myparent != NULL) {
12257c478bd9Sstevel@tonic-gate 			for (tptr = myparent; tptr->parent != NULL;
12267c478bd9Sstevel@tonic-gate 			    tptr = tptr->parent) {
12277c478bd9Sstevel@tonic-gate 				if ((tptr->dev == pstatb->st_dev) &&
12287c478bd9Sstevel@tonic-gate 				    (tptr->ino == pstatb->st_ino)) {
12297c478bd9Sstevel@tonic-gate 					/*
12307c478bd9Sstevel@tonic-gate 					 * Cycle detected for this
12317c478bd9Sstevel@tonic-gate 					 * directory.  Record the fact
12327c478bd9Sstevel@tonic-gate 					 * it is a cycle so we don't
12337c478bd9Sstevel@tonic-gate 					 * try to process this
12347c478bd9Sstevel@tonic-gate 					 * directory as we are
12357c478bd9Sstevel@tonic-gate 					 * walking through the
12367c478bd9Sstevel@tonic-gate 					 * list of directories.
12377c478bd9Sstevel@tonic-gate 					 */
12387c478bd9Sstevel@tonic-gate 					rep->cycle = 1;
12397c478bd9Sstevel@tonic-gate 					err = 2;
12407c478bd9Sstevel@tonic-gate 					break;
12417c478bd9Sstevel@tonic-gate 				}
12427c478bd9Sstevel@tonic-gate 			}
12437c478bd9Sstevel@tonic-gate 		}
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate }
12467c478bd9Sstevel@tonic-gate 
12477c478bd9Sstevel@tonic-gate /*
12487c478bd9Sstevel@tonic-gate  * get status of file and recomputes tblocks;
12497c478bd9Sstevel@tonic-gate  * argfl = 1 if file is a name in ls-command and = 0
12507c478bd9Sstevel@tonic-gate  * for filename in a directory whose name is an
12517c478bd9Sstevel@tonic-gate  * argument in the command;
12527c478bd9Sstevel@tonic-gate  * stores a pointer in flist[nfiles] and
12537c478bd9Sstevel@tonic-gate  * returns that pointer;
12547c478bd9Sstevel@tonic-gate  * returns NULL if failed;
12557c478bd9Sstevel@tonic-gate  */
12567c478bd9Sstevel@tonic-gate static struct lbuf *
12577c478bd9Sstevel@tonic-gate gstat(char *file, int argfl, struct ditem *myparent)
12587c478bd9Sstevel@tonic-gate {
12597c478bd9Sstevel@tonic-gate 	struct stat statb, statb1;
12607c478bd9Sstevel@tonic-gate 	struct lbuf *rep;
12617c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
12627c478bd9Sstevel@tonic-gate 	ssize_t cc;
12637c478bd9Sstevel@tonic-gate 	int (*statf)() = ((Lflg) || (Hflg && argfl)) ? stat : lstat;
12647c478bd9Sstevel@tonic-gate 	int aclcnt;
1265fa9e4066Sahrens 	int error;
12667c478bd9Sstevel@tonic-gate 	aclent_t *tp;
12677c478bd9Sstevel@tonic-gate 	o_mode_t groupperm, mask;
12687c478bd9Sstevel@tonic-gate 	int grouppermfound, maskfound;
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate 	if (nomocore)
12717c478bd9Sstevel@tonic-gate 		return (NULL);
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 	if (nfiles >= maxfils) {
12747c478bd9Sstevel@tonic-gate 		/*
12757c478bd9Sstevel@tonic-gate 		 * all flist/lbuf pair assigned files, time to get some
12767c478bd9Sstevel@tonic-gate 		 * more space
12777c478bd9Sstevel@tonic-gate 		 */
12787c478bd9Sstevel@tonic-gate 		maxfils += quantn;
12797c478bd9Sstevel@tonic-gate 		if (((flist = realloc(flist,
12807c478bd9Sstevel@tonic-gate 		    maxfils * sizeof (struct lbuf *))) == NULL) ||
12817c478bd9Sstevel@tonic-gate 		    ((nxtlbf = malloc(quantn *
12827c478bd9Sstevel@tonic-gate 		    sizeof (struct lbuf))) == NULL)) {
12837c478bd9Sstevel@tonic-gate 			perror("ls");
12847c478bd9Sstevel@tonic-gate 			nomocore = 1;
12857c478bd9Sstevel@tonic-gate 			return (NULL);
12867c478bd9Sstevel@tonic-gate 		}
12877c478bd9Sstevel@tonic-gate 	}
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	/*
12907c478bd9Sstevel@tonic-gate 	 * nfiles is reset to nargs for each directory
12917c478bd9Sstevel@tonic-gate 	 * that is given as an argument maxn is checked
12927c478bd9Sstevel@tonic-gate 	 * to prevent the assignment of an lbuf to a flist entry
12937c478bd9Sstevel@tonic-gate 	 * that already has one assigned.
12947c478bd9Sstevel@tonic-gate 	 */
12957c478bd9Sstevel@tonic-gate 	if (nfiles >= maxn) {
12967c478bd9Sstevel@tonic-gate 		rep = nxtlbf++;
12977c478bd9Sstevel@tonic-gate 		flist[nfiles++] = rep;
12987c478bd9Sstevel@tonic-gate 		maxn = nfiles;
12997c478bd9Sstevel@tonic-gate 	} else {
13007c478bd9Sstevel@tonic-gate 		rep = flist[nfiles++];
13017c478bd9Sstevel@tonic-gate 	}
13027c478bd9Sstevel@tonic-gate 	rep->lflags = (mode_t)0;
13037c478bd9Sstevel@tonic-gate 	rep->flinkto = NULL;
13047c478bd9Sstevel@tonic-gate 	rep->cycle = 0;
13057c478bd9Sstevel@tonic-gate 	if (argfl || statreq) {
13067c478bd9Sstevel@tonic-gate 		int doacl;
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 		if (lflg)
13097c478bd9Sstevel@tonic-gate 			doacl = 1;
13107c478bd9Sstevel@tonic-gate 		else
13117c478bd9Sstevel@tonic-gate 			doacl = 0;
13127c478bd9Sstevel@tonic-gate 		if ((*statf)(file, &statb) < 0) {
13137c478bd9Sstevel@tonic-gate 			if (argfl || errno != ENOENT ||
13147c478bd9Sstevel@tonic-gate 			    (Lflg && lstat(file, &statb) == 0)) {
13157c478bd9Sstevel@tonic-gate 				/*
13167c478bd9Sstevel@tonic-gate 				 * Avoid race between readdir and lstat.
13177c478bd9Sstevel@tonic-gate 				 * Print error message in case of dangling link.
13187c478bd9Sstevel@tonic-gate 				 */
13197c478bd9Sstevel@tonic-gate 				perror(file);
13207c478bd9Sstevel@tonic-gate 			}
13217c478bd9Sstevel@tonic-gate 			nfiles--;
13227c478bd9Sstevel@tonic-gate 			return (NULL);
13237c478bd9Sstevel@tonic-gate 		}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 		/*
13267c478bd9Sstevel@tonic-gate 		 * If -H was specified, and the file linked to was
13277c478bd9Sstevel@tonic-gate 		 * not a directory, then we need to get the info
13287c478bd9Sstevel@tonic-gate 		 * for the symlink itself.
13297c478bd9Sstevel@tonic-gate 		 */
13307c478bd9Sstevel@tonic-gate 		if ((Hflg) && (argfl) &&
13317c478bd9Sstevel@tonic-gate 		    ((statb.st_mode & S_IFMT) != S_IFDIR)) {
13327c478bd9Sstevel@tonic-gate 			if (lstat(file, &statb) < 0) {
13337c478bd9Sstevel@tonic-gate 				perror(file);
13347c478bd9Sstevel@tonic-gate 			}
13357c478bd9Sstevel@tonic-gate 		}
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 		rep->lnum = statb.st_ino;
13387c478bd9Sstevel@tonic-gate 		rep->lsize = statb.st_size;
13397c478bd9Sstevel@tonic-gate 		rep->lblocks = statb.st_blocks;
13407c478bd9Sstevel@tonic-gate 		switch (statb.st_mode & S_IFMT) {
13417c478bd9Sstevel@tonic-gate 		case S_IFDIR:
13427c478bd9Sstevel@tonic-gate 			rep->ltype = 'd';
13437c478bd9Sstevel@tonic-gate 			if (Rflg) {
13447c478bd9Sstevel@tonic-gate 				record_ancestry(file, &statb, rep,
13457c478bd9Sstevel@tonic-gate 				    argfl, myparent);
13467c478bd9Sstevel@tonic-gate 			}
13477c478bd9Sstevel@tonic-gate 			break;
13487c478bd9Sstevel@tonic-gate 		case S_IFBLK:
13497c478bd9Sstevel@tonic-gate 			rep->ltype = 'b';
13507c478bd9Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
13517c478bd9Sstevel@tonic-gate 			break;
13527c478bd9Sstevel@tonic-gate 		case S_IFCHR:
13537c478bd9Sstevel@tonic-gate 			rep->ltype = 'c';
13547c478bd9Sstevel@tonic-gate 			rep->lsize = (off_t)statb.st_rdev;
13557c478bd9Sstevel@tonic-gate 			break;
13567c478bd9Sstevel@tonic-gate 		case S_IFIFO:
13577c478bd9Sstevel@tonic-gate 			rep->ltype = 'p';
13587c478bd9Sstevel@tonic-gate 			break;
13597c478bd9Sstevel@tonic-gate 		case S_IFSOCK:
13607c478bd9Sstevel@tonic-gate 			rep->ltype = 's';
13617c478bd9Sstevel@tonic-gate 			rep->lsize = 0;
13627c478bd9Sstevel@tonic-gate 			break;
13637c478bd9Sstevel@tonic-gate 		case S_IFLNK:
13647c478bd9Sstevel@tonic-gate 			/* symbolic links may not have ACLs, so elide acl() */
13657c478bd9Sstevel@tonic-gate 			if ((Lflg == 0) || (Hflg == 0) ||
13667c478bd9Sstevel@tonic-gate 			    ((Hflg) && (!argfl))) {
13677c478bd9Sstevel@tonic-gate 				doacl = 0;
13687c478bd9Sstevel@tonic-gate 			}
13697c478bd9Sstevel@tonic-gate 			rep->ltype = 'l';
13707c478bd9Sstevel@tonic-gate 			if (lflg) {
13717c478bd9Sstevel@tonic-gate 				cc = readlink(file, buf, BUFSIZ);
13727c478bd9Sstevel@tonic-gate 				if (cc >= 0) {
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 					/*
13757c478bd9Sstevel@tonic-gate 					 * follow the symbolic link
13767c478bd9Sstevel@tonic-gate 					 * to generate the appropriate
13777c478bd9Sstevel@tonic-gate 					 * Fflg marker for the object
13787c478bd9Sstevel@tonic-gate 					 * eg, /bin -> /sym/bin/
13797c478bd9Sstevel@tonic-gate 					 */
13807c478bd9Sstevel@tonic-gate 					if ((Fflg || pflg) &&
13817c478bd9Sstevel@tonic-gate 					    (stat(file, &statb1) >= 0)) {
13827c478bd9Sstevel@tonic-gate 						switch (statb1.st_mode &
13837c478bd9Sstevel@tonic-gate 						    S_IFMT) {
13847c478bd9Sstevel@tonic-gate 						case S_IFDIR:
13857c478bd9Sstevel@tonic-gate 							buf[cc++] = '/';
13867c478bd9Sstevel@tonic-gate 							break;
13877c478bd9Sstevel@tonic-gate 						case S_IFSOCK:
13887c478bd9Sstevel@tonic-gate 							buf[cc++] = '=';
13897c478bd9Sstevel@tonic-gate 							break;
13902236845bSakaplan 						case S_IFDOOR:
13912236845bSakaplan 							buf[cc++] = '>';
13922236845bSakaplan 							break;
13932236845bSakaplan 						case S_IFIFO:
13942236845bSakaplan 							buf[cc++] = '|';
13952236845bSakaplan 							break;
13967c478bd9Sstevel@tonic-gate 						default:
13977c478bd9Sstevel@tonic-gate 							if ((statb1.st_mode &
13987c478bd9Sstevel@tonic-gate 							    ~S_IFMT) &
13997c478bd9Sstevel@tonic-gate 							    (S_IXUSR|S_IXGRP|
14007c478bd9Sstevel@tonic-gate 							    S_IXOTH))
14017c478bd9Sstevel@tonic-gate 								buf[cc++] = '*';
14027c478bd9Sstevel@tonic-gate 							break;
14037c478bd9Sstevel@tonic-gate 						}
14047c478bd9Sstevel@tonic-gate 					}
14057c478bd9Sstevel@tonic-gate 					buf[cc] = '\0';
14067c478bd9Sstevel@tonic-gate 					rep->flinkto = strdup(buf);
14077c478bd9Sstevel@tonic-gate 				}
14087c478bd9Sstevel@tonic-gate 				break;
14097c478bd9Sstevel@tonic-gate 			}
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 			/*
14127c478bd9Sstevel@tonic-gate 			 * ls /sym behaves differently from ls /sym/
14137c478bd9Sstevel@tonic-gate 			 * when /sym is a symbolic link. This is fixed
14147c478bd9Sstevel@tonic-gate 			 * when explicit arguments are specified.
14157c478bd9Sstevel@tonic-gate 			 */
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate #ifdef XPG6
14187c478bd9Sstevel@tonic-gate 			/* Do not follow a symlink when -F is specified */
14197c478bd9Sstevel@tonic-gate 			if ((!argfl) || (argfl && Fflg) ||
14207c478bd9Sstevel@tonic-gate 			    (stat(file, &statb1) < 0))
14217c478bd9Sstevel@tonic-gate #else
14227c478bd9Sstevel@tonic-gate 			/* Follow a symlink when -F is specified */
14237c478bd9Sstevel@tonic-gate 			if (!argfl || stat(file, &statb1) < 0)
14247c478bd9Sstevel@tonic-gate #endif /* XPG6 */
14257c478bd9Sstevel@tonic-gate 				break;
14267c478bd9Sstevel@tonic-gate 			if ((statb1.st_mode & S_IFMT) == S_IFDIR) {
14277c478bd9Sstevel@tonic-gate 				statb = statb1;
14287c478bd9Sstevel@tonic-gate 				rep->ltype = 'd';
14297c478bd9Sstevel@tonic-gate 				rep->lsize = statb1.st_size;
14307c478bd9Sstevel@tonic-gate 				if (Rflg) {
14317c478bd9Sstevel@tonic-gate 					record_ancestry(file, &statb, rep,
14327c478bd9Sstevel@tonic-gate 					    argfl, myparent);
14337c478bd9Sstevel@tonic-gate 				}
14347c478bd9Sstevel@tonic-gate 			}
14357c478bd9Sstevel@tonic-gate 			break;
14367c478bd9Sstevel@tonic-gate 		case S_IFDOOR:
14377c478bd9Sstevel@tonic-gate 			rep->ltype = 'D';
14387c478bd9Sstevel@tonic-gate 			break;
14397c478bd9Sstevel@tonic-gate 		case S_IFREG:
14407c478bd9Sstevel@tonic-gate 			rep->ltype = '-';
14417c478bd9Sstevel@tonic-gate 			break;
14427c478bd9Sstevel@tonic-gate 		case S_IFPORT:
14437c478bd9Sstevel@tonic-gate 			rep->ltype = 'P';
14447c478bd9Sstevel@tonic-gate 			break;
14457c478bd9Sstevel@tonic-gate 		default:
14467c478bd9Sstevel@tonic-gate 			rep->ltype = '?';
14477c478bd9Sstevel@tonic-gate 			break;
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 		rep->lflags = statb.st_mode & ~S_IFMT;
14507c478bd9Sstevel@tonic-gate 
14517c478bd9Sstevel@tonic-gate 		if (!S_ISREG(statb.st_mode))
14527c478bd9Sstevel@tonic-gate 			rep->lflags |= LS_NOTREG;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 		/* ACL: check acl entries count */
14557c478bd9Sstevel@tonic-gate 		if (doacl) {
14567c478bd9Sstevel@tonic-gate 
1457fa9e4066Sahrens 			error = acl_get(file, 0, &rep->aclp);
1458fa9e4066Sahrens 			if (error) {
1459fa9e4066Sahrens 				(void) fprintf(stderr,
1460fa9e4066Sahrens 				    gettext("ls: can't read ACL on %s: %s\n"),
1461fa9e4066Sahrens 				    file, acl_strerror(error));
14627c478bd9Sstevel@tonic-gate 				return (NULL);
14637c478bd9Sstevel@tonic-gate 			}
14647c478bd9Sstevel@tonic-gate 
1465fa9e4066Sahrens 			rep->acl = ' ';
1466fa9e4066Sahrens 
1467fa9e4066Sahrens 			if (rep->aclp &&
1468fa9e4066Sahrens 			    ((acl_flags(rep->aclp) & ACL_IS_TRIVIAL) == 0)) {
1469fa9e4066Sahrens 				rep->acl = '+';
14707c478bd9Sstevel@tonic-gate 				/*
1471fa9e4066Sahrens 				 * Special handling for ufs aka aclent_t ACL's
1472fa9e4066Sahrens 				 */
1473fa9e4066Sahrens 				if (rep->aclp &&
1474fa9e4066Sahrens 				    acl_type(rep->aclp) == ACLENT_T) {
1475fa9e4066Sahrens 					/*
1476fa9e4066Sahrens 					 * For files with non-trivial acls, the
1477fa9e4066Sahrens 					 * effective group permissions are the
1478fa9e4066Sahrens 					 * intersection of the GROUP_OBJ value
1479fa9e4066Sahrens 					 * and the CLASS_OBJ (acl mask) value.
1480fa9e4066Sahrens 					 * Determine both the GROUP_OBJ and
1481fa9e4066Sahrens 					 * CLASS_OBJ for this file and insert
1482fa9e4066Sahrens 					 * the logical AND of those two values
1483fa9e4066Sahrens 					 * in the group permissions field
1484fa9e4066Sahrens 					 * of the lflags value for this file.
1485fa9e4066Sahrens 					 */
1486fa9e4066Sahrens 
1487fa9e4066Sahrens 					/*
1488fa9e4066Sahrens 					 * Until found in acl list, assume
1489fa9e4066Sahrens 					 * maximum permissions for both group
1490fa9e4066Sahrens 					 * a nd mask.  (Just in case the acl
1491fa9e4066Sahrens 					 * lacks either value for some reason.)
14927c478bd9Sstevel@tonic-gate 					 */
14937c478bd9Sstevel@tonic-gate 					groupperm = 07;
14947c478bd9Sstevel@tonic-gate 					mask = 07;
14957c478bd9Sstevel@tonic-gate 					grouppermfound = 0;
14967c478bd9Sstevel@tonic-gate 					maskfound = 0;
1497fa9e4066Sahrens 					aclcnt = acl_cnt(rep->aclp);
1498fa9e4066Sahrens 					for (tp =
1499fa9e4066Sahrens 					    (aclent_t *)acl_data(rep->aclp);
1500fa9e4066Sahrens 					    aclcnt--; tp++) {
15017c478bd9Sstevel@tonic-gate 						if (tp->a_type == GROUP_OBJ) {
15027c478bd9Sstevel@tonic-gate 							groupperm = tp->a_perm;
15037c478bd9Sstevel@tonic-gate 							grouppermfound = 1;
15047c478bd9Sstevel@tonic-gate 							continue;
15057c478bd9Sstevel@tonic-gate 						}
15067c478bd9Sstevel@tonic-gate 						if (tp->a_type == CLASS_OBJ) {
15077c478bd9Sstevel@tonic-gate 							mask = tp->a_perm;
15087c478bd9Sstevel@tonic-gate 							maskfound = 1;
15097c478bd9Sstevel@tonic-gate 						}
15107c478bd9Sstevel@tonic-gate 						if (grouppermfound && maskfound)
15117c478bd9Sstevel@tonic-gate 							break;
15127c478bd9Sstevel@tonic-gate 					}
15137c478bd9Sstevel@tonic-gate 
15147c478bd9Sstevel@tonic-gate 
15157c478bd9Sstevel@tonic-gate 					/* reset all the group bits */
15167c478bd9Sstevel@tonic-gate 					rep->lflags &= ~S_IRWXG;
15177c478bd9Sstevel@tonic-gate 
15187c478bd9Sstevel@tonic-gate 					/*
1519fa9e4066Sahrens 					 * Now set them to the logical AND of
1520fa9e4066Sahrens 					 * the GROUP_OBJ permissions and the
1521fa9e4066Sahrens 					 * acl mask.
15227c478bd9Sstevel@tonic-gate 					 */
15237c478bd9Sstevel@tonic-gate 
15247c478bd9Sstevel@tonic-gate 					rep->lflags |= (groupperm & mask) << 3;
1525fa9e4066Sahrens 
1526fa9e4066Sahrens 				}
15277c478bd9Sstevel@tonic-gate 			}
15287c478bd9Sstevel@tonic-gate 
15295a5eeccaSmarks 			if (!vflg && !Vflg && rep->aclp) {
15305a5eeccaSmarks 				acl_free(rep->aclp);
15315a5eeccaSmarks 				rep->aclp = NULL;
15325a5eeccaSmarks 			}
15335a5eeccaSmarks 
15347c478bd9Sstevel@tonic-gate 			if (atflg && pathconf(file, _PC_XATTR_EXISTS) == 1)
15357c478bd9Sstevel@tonic-gate 				rep->acl = '@';
1536*da6c28aaSamw 
15377c478bd9Sstevel@tonic-gate 		} else
15387c478bd9Sstevel@tonic-gate 			rep->acl = ' ';
15397c478bd9Sstevel@tonic-gate 
15407c478bd9Sstevel@tonic-gate 		/* mask ISARG and other file-type bits */
15417c478bd9Sstevel@tonic-gate 
15427c478bd9Sstevel@tonic-gate 		rep->luid = statb.st_uid;
15437c478bd9Sstevel@tonic-gate 		rep->lgid = statb.st_gid;
15447c478bd9Sstevel@tonic-gate 		rep->lnl = statb.st_nlink;
1545*da6c28aaSamw 		if (uflg || (tmflg && atm))
15467c478bd9Sstevel@tonic-gate 			rep->lmtime = statb.st_atim;
1547*da6c28aaSamw 		else if (cflg || (tmflg && ctm))
15487c478bd9Sstevel@tonic-gate 			rep->lmtime = statb.st_ctim;
15497c478bd9Sstevel@tonic-gate 		else
15507c478bd9Sstevel@tonic-gate 			rep->lmtime = statb.st_mtim;
1551*da6c28aaSamw 		rep->lat = statb.st_atim;
1552*da6c28aaSamw 		rep->lct = statb.st_ctim;
1553*da6c28aaSamw 		rep->lmt = statb.st_mtim;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 		if (rep->ltype != 'b' && rep->ltype != 'c')
15567c478bd9Sstevel@tonic-gate 			tblocks += rep->lblocks;
1557*da6c28aaSamw 
1558*da6c28aaSamw 		/* Get extended system attributes */
1559*da6c28aaSamw 
1560*da6c28aaSamw 		rep->exttr = NULL;
1561*da6c28aaSamw 		rep->extm = NULL;
1562*da6c28aaSamw 		if ((saflg || (tmflg && crtm) || (tmflg && alltm)) &&
1563*da6c28aaSamw 		    (sysattr_support(file, _PC_SATTR_EXISTS) == 1)) {
1564*da6c28aaSamw 			int i;
1565*da6c28aaSamw 
1566*da6c28aaSamw 			sacnt = attr_count();
1567*da6c28aaSamw 			/*
1568*da6c28aaSamw 			 * Allocate 'sacnt' size array to hold extended
1569*da6c28aaSamw 			 * system attribute name (verbose) or respective
1570*da6c28aaSamw 			 * symbol represenation (compact).
1571*da6c28aaSamw 			 */
1572*da6c28aaSamw 			rep->exttr = xmalloc(sacnt * sizeof (struct attrb),
1573*da6c28aaSamw 			    rep);
1574*da6c28aaSamw 
1575*da6c28aaSamw 			/* initialize boolean attribute list */
1576*da6c28aaSamw 			for (i = 0; i < sacnt; i++)
1577*da6c28aaSamw 				rep->exttr[i].name = NULL;
1578*da6c28aaSamw 			if (get_sysxattr(file, rep) != 0) {
1579*da6c28aaSamw 				(void) fprintf(stderr,
1580*da6c28aaSamw 				    gettext("ls:Failed to retrieve "
1581*da6c28aaSamw 				    "extended system attribute from "
1582*da6c28aaSamw 				    "%s\n"), file);
1583*da6c28aaSamw 				rep->exttr[0].name = xmalloc(2, rep);
1584*da6c28aaSamw 				(void) strlcpy(rep->exttr[0].name, "?", 2);
1585*da6c28aaSamw 			}
1586*da6c28aaSamw 		}
15877c478bd9Sstevel@tonic-gate 	}
15887c478bd9Sstevel@tonic-gate 	return (rep);
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate /*
15927c478bd9Sstevel@tonic-gate  * returns pathname of the form dir/file;
15937c478bd9Sstevel@tonic-gate  * dir and file are null-terminated strings.
15947c478bd9Sstevel@tonic-gate  */
15957c478bd9Sstevel@tonic-gate static char *
15967c478bd9Sstevel@tonic-gate makename(char *dir, char *file)
15977c478bd9Sstevel@tonic-gate {
15987c478bd9Sstevel@tonic-gate 	/*
15997c478bd9Sstevel@tonic-gate 	 * PATH_MAX is the maximum length of a path name.
16007c478bd9Sstevel@tonic-gate 	 * MAXNAMLEN is the maximum length of any path name component.
16017c478bd9Sstevel@tonic-gate 	 * Allocate space for both, plus the '/' in the middle
16027c478bd9Sstevel@tonic-gate 	 * and the null character at the end.
16037c478bd9Sstevel@tonic-gate 	 * dfile is static as this is returned by makename().
16047c478bd9Sstevel@tonic-gate 	 */
16057c478bd9Sstevel@tonic-gate 	static char dfile[PATH_MAX + 1 + MAXNAMLEN + 1];
16067c478bd9Sstevel@tonic-gate 	char *dp, *fp;
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	dp = dfile;
16097c478bd9Sstevel@tonic-gate 	fp = dir;
16107c478bd9Sstevel@tonic-gate 	while (*fp)
16117c478bd9Sstevel@tonic-gate 		*dp++ = *fp++;
16127c478bd9Sstevel@tonic-gate 	if (dp > dfile && *(dp - 1) != '/')
16137c478bd9Sstevel@tonic-gate 		*dp++ = '/';
16147c478bd9Sstevel@tonic-gate 	fp = file;
16157c478bd9Sstevel@tonic-gate 	while (*fp)
16167c478bd9Sstevel@tonic-gate 		*dp++ = *fp++;
16177c478bd9Sstevel@tonic-gate 	*dp = '\0';
16187c478bd9Sstevel@tonic-gate 	return (dfile);
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate #include <pwd.h>
16237c478bd9Sstevel@tonic-gate #include <grp.h>
16247c478bd9Sstevel@tonic-gate #include <utmpx.h>
16257c478bd9Sstevel@tonic-gate 
16267c478bd9Sstevel@tonic-gate struct	utmpx utmp;
16277c478bd9Sstevel@tonic-gate 
16287c478bd9Sstevel@tonic-gate #define	NMAX	(sizeof (utmp.ut_name))
16297c478bd9Sstevel@tonic-gate #define	SCPYN(a, b)	(void) strncpy(a, b, NMAX)
16307c478bd9Sstevel@tonic-gate 
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate struct cachenode {		/* this struct must be zeroed before using */
16337c478bd9Sstevel@tonic-gate 	struct cachenode *lesschild;	/* subtree whose entries < val */
16347c478bd9Sstevel@tonic-gate 	struct cachenode *grtrchild;	/* subtree whose entries > val */
16357c478bd9Sstevel@tonic-gate 	long val;			/* the uid or gid of this entry */
16367c478bd9Sstevel@tonic-gate 	int initted;			/* name has been filled in */
16377c478bd9Sstevel@tonic-gate 	char name[NMAX+1];		/* the string that val maps to */
16387c478bd9Sstevel@tonic-gate };
16397c478bd9Sstevel@tonic-gate static struct cachenode *names, *groups;
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate static struct cachenode *
16427c478bd9Sstevel@tonic-gate findincache(struct cachenode **head, long val)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate 	struct cachenode **parent = head;
16457c478bd9Sstevel@tonic-gate 	struct cachenode *c = *parent;
16467c478bd9Sstevel@tonic-gate 
16477c478bd9Sstevel@tonic-gate 	while (c != NULL) {
16487c478bd9Sstevel@tonic-gate 		if (val == c->val) {
16497c478bd9Sstevel@tonic-gate 			/* found it */
16507c478bd9Sstevel@tonic-gate 			return (c);
16517c478bd9Sstevel@tonic-gate 		} else if (val < c->val) {
16527c478bd9Sstevel@tonic-gate 			parent = &c->lesschild;
16537c478bd9Sstevel@tonic-gate 			c = c->lesschild;
16547c478bd9Sstevel@tonic-gate 		} else {
16557c478bd9Sstevel@tonic-gate 			parent = &c->grtrchild;
16567c478bd9Sstevel@tonic-gate 			c = c->grtrchild;
16577c478bd9Sstevel@tonic-gate 		}
16587c478bd9Sstevel@tonic-gate 	}
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	/* not in the cache, make a new entry for it */
16617c478bd9Sstevel@tonic-gate 	c = calloc(1, sizeof (struct cachenode));
16627c478bd9Sstevel@tonic-gate 	if (c == NULL) {
16637c478bd9Sstevel@tonic-gate 		perror("ls");
16647c478bd9Sstevel@tonic-gate 		exit(2);
16657c478bd9Sstevel@tonic-gate 	}
16667c478bd9Sstevel@tonic-gate 	*parent = c;
16677c478bd9Sstevel@tonic-gate 	c->val = val;
16687c478bd9Sstevel@tonic-gate 	return (c);
16697c478bd9Sstevel@tonic-gate }
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate /*
16727c478bd9Sstevel@tonic-gate  * get name from cache, or passwd file for a given uid;
16737c478bd9Sstevel@tonic-gate  * lastuid is set to uid.
16747c478bd9Sstevel@tonic-gate  */
16757c478bd9Sstevel@tonic-gate static char *
16767c478bd9Sstevel@tonic-gate getname(uid_t uid)
16777c478bd9Sstevel@tonic-gate {
16787c478bd9Sstevel@tonic-gate 	struct passwd *pwent;
16797c478bd9Sstevel@tonic-gate 	struct cachenode *c;
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	if ((uid == lastuid) && lastuname)
16827c478bd9Sstevel@tonic-gate 		return (lastuname);
16837c478bd9Sstevel@tonic-gate 
16847c478bd9Sstevel@tonic-gate 	c = findincache(&names, uid);
16857c478bd9Sstevel@tonic-gate 	if (c->initted == 0) {
16867c478bd9Sstevel@tonic-gate 		if ((pwent = getpwuid(uid)) != NULL) {
16877c478bd9Sstevel@tonic-gate 			SCPYN(&c->name[0], pwent->pw_name);
16887c478bd9Sstevel@tonic-gate 		} else {
16897c478bd9Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)uid);
16907c478bd9Sstevel@tonic-gate 		}
16917c478bd9Sstevel@tonic-gate 		c->initted = 1;
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 	lastuid = uid;
16947c478bd9Sstevel@tonic-gate 	lastuname = &c->name[0];
16957c478bd9Sstevel@tonic-gate 	return (lastuname);
16967c478bd9Sstevel@tonic-gate }
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate /*
16997c478bd9Sstevel@tonic-gate  * get name from cache, or group file for a given gid;
17007c478bd9Sstevel@tonic-gate  * lastgid is set to gid.
17017c478bd9Sstevel@tonic-gate  */
17027c478bd9Sstevel@tonic-gate static char *
17037c478bd9Sstevel@tonic-gate getgroup(gid_t gid)
17047c478bd9Sstevel@tonic-gate {
17057c478bd9Sstevel@tonic-gate 	struct group *grent;
17067c478bd9Sstevel@tonic-gate 	struct cachenode *c;
17077c478bd9Sstevel@tonic-gate 
17087c478bd9Sstevel@tonic-gate 	if ((gid == lastgid) && lastgname)
17097c478bd9Sstevel@tonic-gate 		return (lastgname);
17107c478bd9Sstevel@tonic-gate 
17117c478bd9Sstevel@tonic-gate 	c = findincache(&groups, gid);
17127c478bd9Sstevel@tonic-gate 	if (c->initted == 0) {
17137c478bd9Sstevel@tonic-gate 		if ((grent = getgrgid(gid)) != NULL) {
17147c478bd9Sstevel@tonic-gate 			SCPYN(&c->name[0], grent->gr_name);
17157c478bd9Sstevel@tonic-gate 		} else {
17167c478bd9Sstevel@tonic-gate 			(void) sprintf(&c->name[0], "%-8u", (int)gid);
17177c478bd9Sstevel@tonic-gate 		}
17187c478bd9Sstevel@tonic-gate 		c->initted = 1;
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 	lastgid = gid;
17217c478bd9Sstevel@tonic-gate 	lastgname = &c->name[0];
17227c478bd9Sstevel@tonic-gate 	return (lastgname);
17237c478bd9Sstevel@tonic-gate }
17247c478bd9Sstevel@tonic-gate 
17257c478bd9Sstevel@tonic-gate /* return >0 if item pointed by pp2 should appear first */
17267c478bd9Sstevel@tonic-gate static int
17277c478bd9Sstevel@tonic-gate compar(struct lbuf **pp1, struct lbuf **pp2)
17287c478bd9Sstevel@tonic-gate {
17297c478bd9Sstevel@tonic-gate 	struct lbuf *p1, *p2;
17307c478bd9Sstevel@tonic-gate 
17317c478bd9Sstevel@tonic-gate 	p1 = *pp1;
17327c478bd9Sstevel@tonic-gate 	p2 = *pp2;
17337c478bd9Sstevel@tonic-gate 	if (dflg == 0) {
17347c478bd9Sstevel@tonic-gate /*
17357c478bd9Sstevel@tonic-gate  * compare two names in ls-command one of which is file
17367c478bd9Sstevel@tonic-gate  * and the other is a directory;
17377c478bd9Sstevel@tonic-gate  * this portion is not used for comparing files within
17387c478bd9Sstevel@tonic-gate  * a directory name of ls-command;
17397c478bd9Sstevel@tonic-gate  */
17407c478bd9Sstevel@tonic-gate 		if (p1->lflags&ISARG && p1->ltype == 'd') {
17417c478bd9Sstevel@tonic-gate 			if (!(p2->lflags&ISARG && p2->ltype == 'd'))
17427c478bd9Sstevel@tonic-gate 				return (1);
17437c478bd9Sstevel@tonic-gate 		} else {
17447c478bd9Sstevel@tonic-gate 			if (p2->lflags&ISARG && p2->ltype == 'd')
17457c478bd9Sstevel@tonic-gate 				return (-1);
17467c478bd9Sstevel@tonic-gate 		}
17477c478bd9Sstevel@tonic-gate 	}
17487c478bd9Sstevel@tonic-gate 	if (tflg) {
17497c478bd9Sstevel@tonic-gate 		if (p2->lmtime.tv_sec > p1->lmtime.tv_sec)
17507c478bd9Sstevel@tonic-gate 			return (rflg);
17517c478bd9Sstevel@tonic-gate 		else if (p2->lmtime.tv_sec < p1->lmtime.tv_sec)
17527c478bd9Sstevel@tonic-gate 			return (-rflg);
17537c478bd9Sstevel@tonic-gate 		/* times are equal to the sec, check nsec */
17547c478bd9Sstevel@tonic-gate 		if (p2->lmtime.tv_nsec > p1->lmtime.tv_nsec)
17557c478bd9Sstevel@tonic-gate 			return (rflg);
17567c478bd9Sstevel@tonic-gate 		else if (p2->lmtime.tv_nsec < p1->lmtime.tv_nsec)
17577c478bd9Sstevel@tonic-gate 			return (-rflg);
17587c478bd9Sstevel@tonic-gate 		/* if times are equal, fall through and sort by name */
17597c478bd9Sstevel@tonic-gate 	} else if (Sflg) {
17607c478bd9Sstevel@tonic-gate 		/*
17617c478bd9Sstevel@tonic-gate 		 * The size stored in lsize can be either the
17627c478bd9Sstevel@tonic-gate 		 * size or the major minor number (in the case of
17637c478bd9Sstevel@tonic-gate 		 * block and character special devices).  If it's
17647c478bd9Sstevel@tonic-gate 		 * a major minor number, then the size is considered
17657c478bd9Sstevel@tonic-gate 		 * to be zero and we want to fall through and sort
17667c478bd9Sstevel@tonic-gate 		 * by name.  In addition, if the size of p2 is equal
17677c478bd9Sstevel@tonic-gate 		 * to the size of p1 we want to fall through and
17687c478bd9Sstevel@tonic-gate 		 * sort by name.
17697c478bd9Sstevel@tonic-gate 		 */
17707c478bd9Sstevel@tonic-gate 		off_t	p1size = (p1->ltype == 'b') ||
17717c478bd9Sstevel@tonic-gate 		    (p1->ltype == 'c') ? 0 : p1->lsize;
17727c478bd9Sstevel@tonic-gate 		off_t	p2size = (p2->ltype == 'b') ||
17737c478bd9Sstevel@tonic-gate 		    (p2->ltype == 'c') ? 0 : p2->lsize;
17747c478bd9Sstevel@tonic-gate 		if (p2size > p1size) {
17757c478bd9Sstevel@tonic-gate 			return (rflg);
17767c478bd9Sstevel@tonic-gate 		} else if (p2size < p1size) {
17777c478bd9Sstevel@tonic-gate 			return (-rflg);
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 		/* Sizes are equal, fall through and sort by name. */
17807c478bd9Sstevel@tonic-gate 	}
17817c478bd9Sstevel@tonic-gate 	return (rflg * strcoll(
17827c478bd9Sstevel@tonic-gate 	    p1->lflags & ISARG ? p1->ln.namep : p1->ln.lname,
17837c478bd9Sstevel@tonic-gate 	    p2->lflags&ISARG ? p2->ln.namep : p2->ln.lname));
17847c478bd9Sstevel@tonic-gate }
17857c478bd9Sstevel@tonic-gate 
17867c478bd9Sstevel@tonic-gate static void
17877c478bd9Sstevel@tonic-gate pprintf(char *s1, char *s2)
17887c478bd9Sstevel@tonic-gate {
17897c478bd9Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s1);
17907c478bd9Sstevel@tonic-gate 	csi_pprintf((unsigned char *)s2);
17917c478bd9Sstevel@tonic-gate }
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate static void
17947c478bd9Sstevel@tonic-gate csi_pprintf(unsigned char *s)
17957c478bd9Sstevel@tonic-gate {
17967c478bd9Sstevel@tonic-gate 	unsigned char *cp;
17977c478bd9Sstevel@tonic-gate 	char	c;
17987c478bd9Sstevel@tonic-gate 	int	i;
17997c478bd9Sstevel@tonic-gate 	int	c_len;
18007c478bd9Sstevel@tonic-gate 	int	p_col;
18017c478bd9Sstevel@tonic-gate 	wchar_t	pcode;
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate 	if (!qflg && !bflg) {
18047c478bd9Sstevel@tonic-gate 		for (cp = s; *cp != '\0'; cp++) {
18057c478bd9Sstevel@tonic-gate 			(void) putchar(*cp);
18067c478bd9Sstevel@tonic-gate 			curcol++;
18077c478bd9Sstevel@tonic-gate 		}
18087c478bd9Sstevel@tonic-gate 		return;
18097c478bd9Sstevel@tonic-gate 	}
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate 	for (cp = s; *cp; ) {
18127c478bd9Sstevel@tonic-gate 		if (isascii(c = *cp)) {
18137c478bd9Sstevel@tonic-gate 			if (!isprint(c)) {
18147c478bd9Sstevel@tonic-gate 				if (qflg) {
18157c478bd9Sstevel@tonic-gate 					c = '?';
18167c478bd9Sstevel@tonic-gate 				} else {
18177c478bd9Sstevel@tonic-gate 					curcol += 3;
18187c478bd9Sstevel@tonic-gate 					(void) putc('\\', stdout);
18197c478bd9Sstevel@tonic-gate 					c = '0' + ((*cp >> 6) & 07);
18207c478bd9Sstevel@tonic-gate 					(void) putc(c, stdout);
18217c478bd9Sstevel@tonic-gate 					c = '0' + ((*cp >> 3) & 07);
18227c478bd9Sstevel@tonic-gate 					(void) putc(c, stdout);
18237c478bd9Sstevel@tonic-gate 					c = '0' + (*cp & 07);
18247c478bd9Sstevel@tonic-gate 				}
18257c478bd9Sstevel@tonic-gate 			}
18267c478bd9Sstevel@tonic-gate 			curcol++;
18277c478bd9Sstevel@tonic-gate 			cp++;
18287c478bd9Sstevel@tonic-gate 			(void) putc(c, stdout);
18297c478bd9Sstevel@tonic-gate 			continue;
18307c478bd9Sstevel@tonic-gate 		}
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 		if ((c_len = mbtowc(&pcode, (char *)cp, MB_LEN_MAX)) <= 0) {
18337c478bd9Sstevel@tonic-gate 			c_len = 1;
18347c478bd9Sstevel@tonic-gate 			goto not_print;
18357c478bd9Sstevel@tonic-gate 		}
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate 		if ((p_col = wcwidth(pcode)) > 0) {
18387c478bd9Sstevel@tonic-gate 			(void) putwchar(pcode);
18397c478bd9Sstevel@tonic-gate 			cp += c_len;
18407c478bd9Sstevel@tonic-gate 			curcol += p_col;
18417c478bd9Sstevel@tonic-gate 			continue;
18427c478bd9Sstevel@tonic-gate 		}
18437c478bd9Sstevel@tonic-gate 
18447c478bd9Sstevel@tonic-gate not_print:
18457c478bd9Sstevel@tonic-gate 		for (i = 0; i < c_len; i++) {
18467c478bd9Sstevel@tonic-gate 			if (qflg) {
18477c478bd9Sstevel@tonic-gate 				c = '?';
18487c478bd9Sstevel@tonic-gate 			} else {
18497c478bd9Sstevel@tonic-gate 				curcol += 3;
18507c478bd9Sstevel@tonic-gate 				(void) putc('\\', stdout);
18517c478bd9Sstevel@tonic-gate 				c = '0' + ((*cp >> 6) & 07);
18527c478bd9Sstevel@tonic-gate 				(void) putc(c, stdout);
18537c478bd9Sstevel@tonic-gate 				c = '0' + ((*cp >> 3) & 07);
18547c478bd9Sstevel@tonic-gate 				(void) putc(c, stdout);
18557c478bd9Sstevel@tonic-gate 				c = '0' + (*cp & 07);
18567c478bd9Sstevel@tonic-gate 			}
18577c478bd9Sstevel@tonic-gate 			curcol++;
18587c478bd9Sstevel@tonic-gate 			(void) putc(c, stdout);
18597c478bd9Sstevel@tonic-gate 			cp++;
18607c478bd9Sstevel@tonic-gate 		}
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate static int
18657c478bd9Sstevel@tonic-gate strcol(unsigned char *s1)
18667c478bd9Sstevel@tonic-gate {
18677c478bd9Sstevel@tonic-gate 	int	w;
18687c478bd9Sstevel@tonic-gate 	int	w_col;
18697c478bd9Sstevel@tonic-gate 	int	len;
18707c478bd9Sstevel@tonic-gate 	wchar_t	wc;
18717c478bd9Sstevel@tonic-gate 
18727c478bd9Sstevel@tonic-gate 	w = 0;
18737c478bd9Sstevel@tonic-gate 	while (*s1) {
18747c478bd9Sstevel@tonic-gate 		if (isascii(*s1)) {
18757c478bd9Sstevel@tonic-gate 			w++;
18767c478bd9Sstevel@tonic-gate 			s1++;
18777c478bd9Sstevel@tonic-gate 			continue;
18787c478bd9Sstevel@tonic-gate 		}
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) {
18817c478bd9Sstevel@tonic-gate 			w++;
18827c478bd9Sstevel@tonic-gate 			s1++;
18837c478bd9Sstevel@tonic-gate 			continue;
18847c478bd9Sstevel@tonic-gate 		}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 		if ((w_col = wcwidth(wc)) < 0)
18877c478bd9Sstevel@tonic-gate 			w_col = len;
18887c478bd9Sstevel@tonic-gate 		s1 += len;
18897c478bd9Sstevel@tonic-gate 		w += w_col;
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate 	return (w);
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate  * Convert an unsigned long long to a string representation and place the
18967c478bd9Sstevel@tonic-gate  * result in the caller-supplied buffer.
18977c478bd9Sstevel@tonic-gate  *
18987c478bd9Sstevel@tonic-gate  * The number provided is a size in bytes.  The number is first
18997c478bd9Sstevel@tonic-gate  * converted to an integral multiple of 'scale' bytes.  This new
19007c478bd9Sstevel@tonic-gate  * number is then scaled down until it is small enough to be in a good
19017c478bd9Sstevel@tonic-gate  * human readable format, i.e.  in the range 0 thru scale-1.  If the
19027c478bd9Sstevel@tonic-gate  * number used to derive the final number is not a multiple of scale, and
19037c478bd9Sstevel@tonic-gate  * the final number has only a single significant digit, we compute
19047c478bd9Sstevel@tonic-gate  * tenths of units to provide a second significant digit.
19057c478bd9Sstevel@tonic-gate  *
19067c478bd9Sstevel@tonic-gate  * The value "(unsigned long long)-1" is a special case and is always
19077c478bd9Sstevel@tonic-gate  * converted to "-1".
19087c478bd9Sstevel@tonic-gate  *
19097c478bd9Sstevel@tonic-gate  * A pointer to the caller-supplied buffer is returned.
19107c478bd9Sstevel@tonic-gate  */
19117c478bd9Sstevel@tonic-gate static char *
19127c478bd9Sstevel@tonic-gate number_to_scaled_string(
19137c478bd9Sstevel@tonic-gate 			numbuf_t buf,		/* put the result here */
19147c478bd9Sstevel@tonic-gate 			unsigned long long number, /* convert this number */
19157c478bd9Sstevel@tonic-gate 			long scale)
19167c478bd9Sstevel@tonic-gate {
19177c478bd9Sstevel@tonic-gate 	unsigned long long save;
19187c478bd9Sstevel@tonic-gate 	/* Measurement: kilo, mega, giga, tera, peta, exa */
19197c478bd9Sstevel@tonic-gate 	char *uom = "KMGTPE";
19207c478bd9Sstevel@tonic-gate 
19217c478bd9Sstevel@tonic-gate 	if ((long long)number == (long long)-1) {
19227c478bd9Sstevel@tonic-gate 		(void) strlcpy(buf, "-1", sizeof (numbuf_t));
19237c478bd9Sstevel@tonic-gate 		return (buf);
19247c478bd9Sstevel@tonic-gate 	}
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 	save = number;
19277c478bd9Sstevel@tonic-gate 	number = number / scale;
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	/*
19307c478bd9Sstevel@tonic-gate 	 * Now we have number as a count of scale units.
19317c478bd9Sstevel@tonic-gate 	 * If no further scaling is necessary, we round up as appropriate.
19327c478bd9Sstevel@tonic-gate 	 *
19337c478bd9Sstevel@tonic-gate 	 * The largest value number could have had entering the routine is
19347c478bd9Sstevel@tonic-gate 	 * 16 Exabytes, so running off the end of the uom array should
19357c478bd9Sstevel@tonic-gate 	 * never happen.  We check for that, though, as a guard against
19367c478bd9Sstevel@tonic-gate 	 * a breakdown elsewhere in the algorithm.
19377c478bd9Sstevel@tonic-gate 	 */
19387c478bd9Sstevel@tonic-gate 	if (number < (unsigned long long)scale) {
19397c478bd9Sstevel@tonic-gate 		if ((save % scale) >= (unsigned long long)(scale / 2)) {
19407c478bd9Sstevel@tonic-gate 			if (++number == (unsigned long long)scale) {
19417c478bd9Sstevel@tonic-gate 				uom++;
19427c478bd9Sstevel@tonic-gate 				number = 1;
19437c478bd9Sstevel@tonic-gate 			}
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 	} else {
19467c478bd9Sstevel@tonic-gate 		while ((number >= (unsigned long long)scale) && (*uom != 'E')) {
19477c478bd9Sstevel@tonic-gate 			uom++; /* next unit of measurement */
19487c478bd9Sstevel@tonic-gate 			save = number;
19497c478bd9Sstevel@tonic-gate 			/*
19507c478bd9Sstevel@tonic-gate 			 * If we're over half way to the next unit of
19517c478bd9Sstevel@tonic-gate 			 * 'scale' bytes (which means we should round
19527c478bd9Sstevel@tonic-gate 			 * up), then adding half of 'scale' prior to
19537c478bd9Sstevel@tonic-gate 			 * the division will push us into that next
19547c478bd9Sstevel@tonic-gate 			 * unit of scale when we perform the division
19557c478bd9Sstevel@tonic-gate 			 */
19567c478bd9Sstevel@tonic-gate 			number = (number + (scale / 2)) / scale;
19577c478bd9Sstevel@tonic-gate 		}
19587c478bd9Sstevel@tonic-gate 	}
19597c478bd9Sstevel@tonic-gate 
19607c478bd9Sstevel@tonic-gate 	/* check if we should output a decimal place after the point */
19617c478bd9Sstevel@tonic-gate 	if ((save / scale) < 10) {
19627c478bd9Sstevel@tonic-gate 		/* snprintf() will round for us */
19637c478bd9Sstevel@tonic-gate 		float fnum = (float)save / scale;
19647c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%2.1f%c",
19657c478bd9Sstevel@tonic-gate 		    fnum, *uom);
19667c478bd9Sstevel@tonic-gate 	} else {
19677c478bd9Sstevel@tonic-gate 		(void) snprintf(buf, sizeof (numbuf_t), "%4llu%c",
19687c478bd9Sstevel@tonic-gate 		    number, *uom);
19697c478bd9Sstevel@tonic-gate 	}
19707c478bd9Sstevel@tonic-gate 	return (buf);
19717c478bd9Sstevel@tonic-gate }
1972*da6c28aaSamw 
1973*da6c28aaSamw /* Get extended system attributes and set the display */
1974*da6c28aaSamw 
1975*da6c28aaSamw int
1976*da6c28aaSamw get_sysxattr(char *fname, struct lbuf *rep)
1977*da6c28aaSamw {
1978*da6c28aaSamw 	boolean_t	value;
1979*da6c28aaSamw 	data_type_t	type;
1980*da6c28aaSamw 	int		error;
1981*da6c28aaSamw 	char		*name;
1982*da6c28aaSamw 	int		i;
1983*da6c28aaSamw 
1984*da6c28aaSamw 	if ((error = nvlist_alloc(&response, NV_UNIQUE_NAME, 0)) != 0) {
1985*da6c28aaSamw 		perror("ls:nvlist_alloc");
1986*da6c28aaSamw 		return (error);
1987*da6c28aaSamw 	}
1988*da6c28aaSamw 	if ((error = getattrat(AT_FDCWD, XATTR_VIEW_READWRITE, fname,
1989*da6c28aaSamw 	    &response)) != 0) {
1990*da6c28aaSamw 		perror("ls:getattrat");
1991*da6c28aaSamw 		nvlist_free(response);
1992*da6c28aaSamw 		return (error);
1993*da6c28aaSamw 	}
1994*da6c28aaSamw 
1995*da6c28aaSamw 	/*
1996*da6c28aaSamw 	 * Allocate 'sacnt' size array to hold extended timestamp
1997*da6c28aaSamw 	 * system attributes and initialize the array.
1998*da6c28aaSamw 	 */
1999*da6c28aaSamw 	rep->extm = xmalloc(sacnt * sizeof (struct attrtm), rep);
2000*da6c28aaSamw 	for (i = 0; i < sacnt; i++) {
2001*da6c28aaSamw 		rep->extm[i].stm = 0;
2002*da6c28aaSamw 		rep->extm[i].nstm = 0;
2003*da6c28aaSamw 		rep->extm[i].name = NULL;
2004*da6c28aaSamw 	}
2005*da6c28aaSamw 	while ((pair = nvlist_next_nvpair(response, pair)) != NULL) {
2006*da6c28aaSamw 		name = nvpair_name(pair);
2007*da6c28aaSamw 		type = nvpair_type(pair);
2008*da6c28aaSamw 		if (type == DATA_TYPE_BOOLEAN_VALUE) {
2009*da6c28aaSamw 			error = nvpair_value_boolean_value(pair, &value);
2010*da6c28aaSamw 			if (error) {
2011*da6c28aaSamw 				(void) fprintf(stderr,
2012*da6c28aaSamw 				    gettext("nvpair_value_boolean_value "
2013*da6c28aaSamw 				    "failed: error = %d\n"), error);
2014*da6c28aaSamw 				continue;
2015*da6c28aaSamw 			}
2016*da6c28aaSamw 			if (name != NULL)
2017*da6c28aaSamw 				set_sysattrb_display(name, value, rep);
2018*da6c28aaSamw 			continue;
2019*da6c28aaSamw 		} else if (type == DATA_TYPE_UINT64_ARRAY) {
2020*da6c28aaSamw 			if (name != NULL)
2021*da6c28aaSamw 				set_sysattrtm_display(name, rep);
2022*da6c28aaSamw 			continue;
2023*da6c28aaSamw 		}
2024*da6c28aaSamw 	}
2025*da6c28aaSamw 	nvlist_free(response);
2026*da6c28aaSamw 	return (0);
2027*da6c28aaSamw }
2028*da6c28aaSamw 
2029*da6c28aaSamw /* Set extended system attribute boolean display */
2030*da6c28aaSamw 
2031*da6c28aaSamw void
2032*da6c28aaSamw set_sysattrb_display(char *name, boolean_t val, struct lbuf *rep)
2033*da6c28aaSamw {
2034*da6c28aaSamw 	f_attr_t	fattr;
2035*da6c28aaSamw 	const char	*opt;
2036*da6c28aaSamw 	size_t		len;
2037*da6c28aaSamw 
2038*da6c28aaSamw 	fattr = name_to_attr(name);
2039*da6c28aaSamw 	if (fattr != F_ATTR_INVAL && fattr < sacnt) {
2040*da6c28aaSamw 		if (vopt) {
2041*da6c28aaSamw 			len = strlen(name);
2042*da6c28aaSamw 			if (val) {
2043*da6c28aaSamw 				rep->exttr[fattr].name = xmalloc(len + 1, rep);
2044*da6c28aaSamw 				(void) strlcpy(rep->exttr[fattr].name, name,
2045*da6c28aaSamw 				    len + 1);
2046*da6c28aaSamw 			} else {
2047*da6c28aaSamw 				rep->exttr[fattr].name = xmalloc(len + 3, rep);
2048*da6c28aaSamw 				(void) snprintf(rep->exttr[fattr].name, len + 3,
2049*da6c28aaSamw 				    "no%s", name);
2050*da6c28aaSamw 			}
2051*da6c28aaSamw 		} else {
2052*da6c28aaSamw 			opt = attr_to_option(fattr);
2053*da6c28aaSamw 			if (opt != NULL) {
2054*da6c28aaSamw 				len = strlen(opt);
2055*da6c28aaSamw 				rep->exttr[fattr].name = xmalloc(len + 1, rep);
2056*da6c28aaSamw 				if (val)
2057*da6c28aaSamw 					(void) strlcpy(rep->exttr[fattr].name,
2058*da6c28aaSamw 					    opt, len + 1);
2059*da6c28aaSamw 				else
2060*da6c28aaSamw 					(void) strlcpy(rep->exttr[fattr].name,
2061*da6c28aaSamw 					    "-", len + 1);
2062*da6c28aaSamw 			}
2063*da6c28aaSamw 		}
2064*da6c28aaSamw 	}
2065*da6c28aaSamw }
2066*da6c28aaSamw 
2067*da6c28aaSamw /* Set extended system attribute timestamp display */
2068*da6c28aaSamw 
2069*da6c28aaSamw void
2070*da6c28aaSamw set_sysattrtm_display(char *name, struct lbuf *rep)
2071*da6c28aaSamw {
2072*da6c28aaSamw 	uint_t		nelem;
2073*da6c28aaSamw 	uint64_t	*value;
2074*da6c28aaSamw 	int		i;
2075*da6c28aaSamw 	size_t		len;
2076*da6c28aaSamw 
2077*da6c28aaSamw 	if (nvpair_value_uint64_array(pair, &value, &nelem) == 0) {
2078*da6c28aaSamw 		if (*value != NULL) {
2079*da6c28aaSamw 			len = strlen(name);
2080*da6c28aaSamw 			i = 0;
2081*da6c28aaSamw 			while (rep->extm[i].stm != 0 && i < sacnt)
2082*da6c28aaSamw 				i++;
2083*da6c28aaSamw 			rep->extm[i].stm = value[0];
2084*da6c28aaSamw 			rep->extm[i].nstm = value[1];
2085*da6c28aaSamw 			rep->extm[i].name = xmalloc(len + 1, rep);
2086*da6c28aaSamw 			(void) strlcpy(rep->extm[i].name, name, len + 1);
2087*da6c28aaSamw 		}
2088*da6c28aaSamw 	}
2089*da6c28aaSamw }
2090*da6c28aaSamw 
2091*da6c28aaSamw void
2092*da6c28aaSamw format_time(const char *format, time_t sec)
2093*da6c28aaSamw {
2094*da6c28aaSamw 
2095*da6c28aaSamw 	(void) strftime(time_buf, sizeof (time_buf),
2096*da6c28aaSamw 	    dcgettext(NULL, format, LC_TIME),
2097*da6c28aaSamw 	    localtime(&sec));
2098*da6c28aaSamw }
2099*da6c28aaSamw 
2100*da6c28aaSamw void
2101*da6c28aaSamw format_etime(const char *format, time_t sec, time_t nsec)
2102*da6c28aaSamw {
2103*da6c28aaSamw 	char fmt_buf[FMTSIZE];
2104*da6c28aaSamw 
2105*da6c28aaSamw 	(void) snprintf(fmt_buf, FMTSIZE,
2106*da6c28aaSamw 	    format, nsec);
2107*da6c28aaSamw 	(void) strftime(time_buf, sizeof (time_buf),
2108*da6c28aaSamw 	    fmt_buf, localtime(&sec));
2109*da6c28aaSamw }
2110*da6c28aaSamw 
2111*da6c28aaSamw /* Format timestamp extended system attributes */
2112*da6c28aaSamw 
2113*da6c28aaSamw void
2114*da6c28aaSamw format_attrtime(struct lbuf *p)
2115*da6c28aaSamw {
2116*da6c28aaSamw 	int	tmattr = 0;
2117*da6c28aaSamw 	int i;
2118*da6c28aaSamw 
2119*da6c28aaSamw 	if (p->extm != NULL) {
2120*da6c28aaSamw 		for (i = 0; i < sacnt; i++) {
2121*da6c28aaSamw 			if (p->extm[i].name != NULL) {
2122*da6c28aaSamw 				tmattr = 1;
2123*da6c28aaSamw 				break;
2124*da6c28aaSamw 			}
2125*da6c28aaSamw 		}
2126*da6c28aaSamw 	}
2127*da6c28aaSamw 	if (tmattr) {
2128*da6c28aaSamw 		if (Eflg)
2129*da6c28aaSamw 			format_etime(FORMAT4, (time_t)p->extm[i].stm,
2130*da6c28aaSamw 			    (time_t)p->extm[i].nstm);
2131*da6c28aaSamw 		else  {
2132*da6c28aaSamw 			if ((p->lmtime.tv_sec < year) ||
2133*da6c28aaSamw 			    (p->lmtime.tv_sec > now))
2134*da6c28aaSamw 				format_time(FORMAT1,
2135*da6c28aaSamw 				    (time_t)p->extm[i].stm);
2136*da6c28aaSamw 			else
2137*da6c28aaSamw 				format_time(FORMAT2,
2138*da6c28aaSamw 				    (time_t)p->extm[i].stm);
2139*da6c28aaSamw 		}
2140*da6c28aaSamw 	}
2141*da6c28aaSamw }
2142*da6c28aaSamw 
2143*da6c28aaSamw void
2144*da6c28aaSamw print_time(struct lbuf *p)
2145*da6c28aaSamw {
2146*da6c28aaSamw 	int i = 0;
2147*da6c28aaSamw 
2148*da6c28aaSamw 	new_line();
2149*da6c28aaSamw 	if (Eflg) {
2150*da6c28aaSamw 		format_etime(FORMAT4, p->lat.tv_sec, p->lat.tv_nsec);
2151*da6c28aaSamw 		(void) printf("		timestamp: atime	%s\n",
2152*da6c28aaSamw 		    time_buf);
2153*da6c28aaSamw 		format_etime(FORMAT4, p->lct.tv_sec, p->lct.tv_nsec);
2154*da6c28aaSamw 		(void) printf("		timestamp: ctime	%s\n",
2155*da6c28aaSamw 		    time_buf);
2156*da6c28aaSamw 		format_etime(FORMAT4, p->lmt.tv_sec, p->lmt.tv_nsec);
2157*da6c28aaSamw 		(void) printf("		timestamp: mtime	%s\n",
2158*da6c28aaSamw 		    time_buf);
2159*da6c28aaSamw 		if (p->extm != NULL) {
2160*da6c28aaSamw 			while (p->extm[i].nstm != 0 && i < sacnt) {
2161*da6c28aaSamw 				format_etime(FORMAT4, p->extm[i].stm,
2162*da6c28aaSamw 				    p->extm[i].nstm);
2163*da6c28aaSamw 				if (p->extm[i].name != NULL) {
2164*da6c28aaSamw 					(void) printf("		timestamp:"
2165*da6c28aaSamw 					    " %s	%s\n",
2166*da6c28aaSamw 					    p->extm[i].name, time_buf);
2167*da6c28aaSamw 				}
2168*da6c28aaSamw 				i++;
2169*da6c28aaSamw 			}
2170*da6c28aaSamw 		}
2171*da6c28aaSamw 	} else {
2172*da6c28aaSamw 		format_time(FORMAT3, p->lat.tv_sec);
2173*da6c28aaSamw 		(void) printf("		timestamp: atime	%s\n",
2174*da6c28aaSamw 		    time_buf);
2175*da6c28aaSamw 		format_time(FORMAT3, p->lct.tv_sec);
2176*da6c28aaSamw 		(void) printf("		timestamp: ctime	%s\n",
2177*da6c28aaSamw 		    time_buf);
2178*da6c28aaSamw 		format_time(FORMAT3, p->lmt.tv_sec);
2179*da6c28aaSamw 		(void) printf("		timestamp: mtime	%s\n",
2180*da6c28aaSamw 		    time_buf);
2181*da6c28aaSamw 		if (p->extm != NULL) {
2182*da6c28aaSamw 			while (p->extm[i].stm != 0 && i < sacnt) {
2183*da6c28aaSamw 				format_time(FORMAT3, p->extm[i].stm);
2184*da6c28aaSamw 				if (p->extm[i].name != NULL) {
2185*da6c28aaSamw 					(void) printf("		timestamp:"
2186*da6c28aaSamw 					    " %s	%s\n",
2187*da6c28aaSamw 					    p->extm[i].name, time_buf);
2188*da6c28aaSamw 				}
2189*da6c28aaSamw 				i++;
2190*da6c28aaSamw 			}
2191*da6c28aaSamw 		}
2192*da6c28aaSamw 	}
2193*da6c28aaSamw }
2194*da6c28aaSamw 
2195*da6c28aaSamw /* Free extended system attribute lists */
2196*da6c28aaSamw 
2197*da6c28aaSamw void
2198*da6c28aaSamw free_sysattr(struct lbuf *p)
2199*da6c28aaSamw {
2200*da6c28aaSamw 	int i;
2201*da6c28aaSamw 
2202*da6c28aaSamw 	if (p->exttr != NULL) {
2203*da6c28aaSamw 		for (i = 0; i < sacnt; i++) {
2204*da6c28aaSamw 			if (p->exttr[i].name != NULL)
2205*da6c28aaSamw 				free(p->exttr[i].name);
2206*da6c28aaSamw 		}
2207*da6c28aaSamw 		free(p->exttr);
2208*da6c28aaSamw 	}
2209*da6c28aaSamw 	if (p->extm != NULL) {
2210*da6c28aaSamw 		for (i = 0; i < sacnt; i++) {
2211*da6c28aaSamw 			if (p->extm[i].name != NULL)
2212*da6c28aaSamw 				free(p->extm[i].name);
2213*da6c28aaSamw 		}
2214*da6c28aaSamw 		free(p->extm);
2215*da6c28aaSamw 	}
2216*da6c28aaSamw }
2217*da6c28aaSamw 
2218*da6c28aaSamw /* Allocate extended system attribute list */
2219*da6c28aaSamw 
2220*da6c28aaSamw void *
2221*da6c28aaSamw xmalloc(size_t size, struct lbuf *p)
2222*da6c28aaSamw {
2223*da6c28aaSamw 	if ((p = malloc(size)) == NULL) {
2224*da6c28aaSamw 		perror("ls");
2225*da6c28aaSamw 		free_sysattr(p);
2226*da6c28aaSamw 		nvlist_free(response);
2227*da6c28aaSamw 		exit(2);
2228*da6c28aaSamw 	}
2229*da6c28aaSamw 	return (p);
2230*da6c28aaSamw }
2231