1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate /* 23*0Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*0Sstevel@tonic-gate * Use is subject to license terms. 25*0Sstevel@tonic-gate */ 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */ 31*0Sstevel@tonic-gate /* All Rights Reserved */ 32*0Sstevel@tonic-gate 33*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 34*0Sstevel@tonic-gate 35*0Sstevel@tonic-gate /* 36*0Sstevel@tonic-gate * List files or directories 37*0Sstevel@tonic-gate */ 38*0Sstevel@tonic-gate 39*0Sstevel@tonic-gate #include <sys/param.h> 40*0Sstevel@tonic-gate #include <sys/types.h> 41*0Sstevel@tonic-gate #include <sys/mkdev.h> 42*0Sstevel@tonic-gate #include <sys/stat.h> 43*0Sstevel@tonic-gate #include <sys/acl.h> 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate #include <wchar.h> 46*0Sstevel@tonic-gate #include <stdio.h> 47*0Sstevel@tonic-gate #include <ctype.h> 48*0Sstevel@tonic-gate #include <dirent.h> 49*0Sstevel@tonic-gate #include <string.h> 50*0Sstevel@tonic-gate #include <locale.h> 51*0Sstevel@tonic-gate #include <curses.h> 52*0Sstevel@tonic-gate #include <termios.h> 53*0Sstevel@tonic-gate #include <stdlib.h> 54*0Sstevel@tonic-gate #include <widec.h> 55*0Sstevel@tonic-gate #include <locale.h> 56*0Sstevel@tonic-gate #include <wctype.h> 57*0Sstevel@tonic-gate #include <pwd.h> 58*0Sstevel@tonic-gate #include <grp.h> 59*0Sstevel@tonic-gate #include <limits.h> 60*0Sstevel@tonic-gate #include <fcntl.h> 61*0Sstevel@tonic-gate #include <unistd.h> 62*0Sstevel@tonic-gate #include <libgen.h> 63*0Sstevel@tonic-gate #include <errno.h> 64*0Sstevel@tonic-gate #include <libcmdutils.h> 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gate #ifndef STANDALONE 67*0Sstevel@tonic-gate #define TERMINFO 68*0Sstevel@tonic-gate #endif 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate /* 71*0Sstevel@tonic-gate * -DNOTERMINFO can be defined on the cc command line to prevent 72*0Sstevel@tonic-gate * the use of terminfo. This should be done on systems not having 73*0Sstevel@tonic-gate * the terminfo feature(pre 6.0 sytems ?). 74*0Sstevel@tonic-gate * As a result, columnar listings assume 80 columns for output, 75*0Sstevel@tonic-gate * unless told otherwise via the COLUMNS environment variable. 76*0Sstevel@tonic-gate */ 77*0Sstevel@tonic-gate #ifdef NOTERMINFO 78*0Sstevel@tonic-gate #undef TERMINFO 79*0Sstevel@tonic-gate #endif 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gate #include <term.h> 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate #define BFSIZE 16 84*0Sstevel@tonic-gate /* this bit equals 1 in lflags of structure lbuf if *namep is to be used */ 85*0Sstevel@tonic-gate #define ISARG 0100000 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate /* 88*0Sstevel@tonic-gate * this flag has been added to manipulate the display of S instead of 'l' when 89*0Sstevel@tonic-gate * the file is not a regular file and when group execution bit is off 90*0Sstevel@tonic-gate */ 91*0Sstevel@tonic-gate #define LS_NOTREG 010000 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gate 94*0Sstevel@tonic-gate /* 95*0Sstevel@tonic-gate * Date and time formats 96*0Sstevel@tonic-gate * 97*0Sstevel@tonic-gate * b --- abbreviated month name 98*0Sstevel@tonic-gate * e --- day number 99*0Sstevel@tonic-gate * Y --- year in the form ccyy 100*0Sstevel@tonic-gate * H --- hour(24-hour version) 101*0Sstevel@tonic-gate * M --- minute 102*0Sstevel@tonic-gate * F --- yyyy-mm-dd 103*0Sstevel@tonic-gate * T --- hh:mm:ss 104*0Sstevel@tonic-gate * z --- time zone as hours displacement from UTC 105*0Sstevel@tonic-gate * note that %F and %z are from the ISO C99 standard and are 106*0Sstevel@tonic-gate * not present in older C libraries 107*0Sstevel@tonic-gate */ 108*0Sstevel@tonic-gate #define FORMAT1 " %b %e %Y " 109*0Sstevel@tonic-gate #define FORMAT2 " %b %e %H:%M " 110*0Sstevel@tonic-gate #define FORMAT3 " %b %e %T %Y " 111*0Sstevel@tonic-gate #define FORMAT4 " %%F %%T.%.09ld %%z " 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate #undef BUFSIZ 114*0Sstevel@tonic-gate #define BUFSIZ 4096 115*0Sstevel@tonic-gate #define NUMBER_WIDTH 40 116*0Sstevel@tonic-gate #define FMTSIZE 50 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate struct ditem { 119*0Sstevel@tonic-gate dev_t dev; /* directory items device number */ 120*0Sstevel@tonic-gate ino_t ino; /* directory items inode number */ 121*0Sstevel@tonic-gate struct ditem *parent; /* dir items ptr to its parent's info */ 122*0Sstevel@tonic-gate }; 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gate struct lbuf { 125*0Sstevel@tonic-gate union { 126*0Sstevel@tonic-gate char lname[MAXNAMLEN]; /* used for filename in a directory */ 127*0Sstevel@tonic-gate char *namep; /* for name in ls-command; */ 128*0Sstevel@tonic-gate } ln; 129*0Sstevel@tonic-gate char ltype; /* filetype */ 130*0Sstevel@tonic-gate ino_t lnum; /* inode number of file */ 131*0Sstevel@tonic-gate mode_t lflags; /* 0777 bits used as r,w,x permissions */ 132*0Sstevel@tonic-gate nlink_t lnl; /* number of links to file */ 133*0Sstevel@tonic-gate uid_t luid; 134*0Sstevel@tonic-gate gid_t lgid; 135*0Sstevel@tonic-gate off_t lsize; /* filesize or major/minor dev numbers */ 136*0Sstevel@tonic-gate blkcnt_t lblocks; /* number of file blocks */ 137*0Sstevel@tonic-gate timestruc_t lmtime; 138*0Sstevel@tonic-gate char *flinkto; /* symbolic link contents */ 139*0Sstevel@tonic-gate char acl; /* indicate there are additional acl entries */ 140*0Sstevel@tonic-gate int cycle; /* cycle detected flag */ 141*0Sstevel@tonic-gate struct ditem *ancinfo; /* maintains ancestor info */ 142*0Sstevel@tonic-gate }; 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gate struct dchain { 145*0Sstevel@tonic-gate char *dc_name; /* path name */ 146*0Sstevel@tonic-gate int cycle_detected; /* cycle detected visiting this directory */ 147*0Sstevel@tonic-gate struct ditem *myancinfo; /* this directory's ancestry info */ 148*0Sstevel@tonic-gate struct dchain *dc_next; /* next directory in the chain */ 149*0Sstevel@tonic-gate }; 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gate /* 152*0Sstevel@tonic-gate * A numbuf_t is used when converting a number to a string representation 153*0Sstevel@tonic-gate */ 154*0Sstevel@tonic-gate typedef char numbuf_t[NUMBER_WIDTH]; 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate static struct dchain *dfirst; /* start of the dir chain */ 157*0Sstevel@tonic-gate static struct dchain *cdfirst; /* start of the current dir chain */ 158*0Sstevel@tonic-gate static struct dchain *dtemp; /* temporary - used for linking */ 159*0Sstevel@tonic-gate static char *curdir; /* the current directory */ 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gate static int first = 1; /* true if first line is not yet printed */ 162*0Sstevel@tonic-gate static int nfiles = 0; /* number of flist entries in current use */ 163*0Sstevel@tonic-gate static int nargs = 0; /* number of flist entries used for arguments */ 164*0Sstevel@tonic-gate static int maxfils = 0; /* number of flist/lbuf entries allocated */ 165*0Sstevel@tonic-gate static int maxn = 0; /* number of flist entries with lbufs asigned */ 166*0Sstevel@tonic-gate static int quantn = 64; /* allocation growth quantum */ 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gate static struct lbuf *nxtlbf; /* ptr to next lbuf to be assigned */ 169*0Sstevel@tonic-gate static struct lbuf **flist; /* ptr to list of lbuf pointers */ 170*0Sstevel@tonic-gate static struct lbuf *gstat(char *, int, struct ditem *); 171*0Sstevel@tonic-gate static char *getname(uid_t); 172*0Sstevel@tonic-gate static char *getgroup(gid_t); 173*0Sstevel@tonic-gate static char *makename(char *, char *); 174*0Sstevel@tonic-gate static void pentry(struct lbuf *); 175*0Sstevel@tonic-gate static void column(void); 176*0Sstevel@tonic-gate static void pmode(mode_t aflag); 177*0Sstevel@tonic-gate static void selection(int *); 178*0Sstevel@tonic-gate static void new_line(void); 179*0Sstevel@tonic-gate static void rddir(char *, struct ditem *); 180*0Sstevel@tonic-gate static int strcol(unsigned char *); 181*0Sstevel@tonic-gate static void pem(struct lbuf **, struct lbuf **, int); 182*0Sstevel@tonic-gate static void pdirectory(char *, int, int, int, struct ditem *); 183*0Sstevel@tonic-gate static struct cachenode *findincache(struct cachenode **, long); 184*0Sstevel@tonic-gate static void csi_pprintf(unsigned char *); 185*0Sstevel@tonic-gate static void pprintf(char *, char *); 186*0Sstevel@tonic-gate static int compar(struct lbuf **pp1, struct lbuf **pp2); 187*0Sstevel@tonic-gate static char *number_to_scaled_string(numbuf_t buf, 188*0Sstevel@tonic-gate unsigned long long number, 189*0Sstevel@tonic-gate long scale); 190*0Sstevel@tonic-gate static void record_ancestry(char *, struct stat *, struct lbuf *, 191*0Sstevel@tonic-gate int, struct ditem *); 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate static int aflg; 194*0Sstevel@tonic-gate static int atflg; 195*0Sstevel@tonic-gate static int bflg; 196*0Sstevel@tonic-gate static int cflg; 197*0Sstevel@tonic-gate static int dflg; 198*0Sstevel@tonic-gate static int eflg; 199*0Sstevel@tonic-gate static int fflg; 200*0Sstevel@tonic-gate static int gflg; 201*0Sstevel@tonic-gate static int hflg; 202*0Sstevel@tonic-gate static int iflg; 203*0Sstevel@tonic-gate static int lflg; 204*0Sstevel@tonic-gate static int mflg; 205*0Sstevel@tonic-gate static int nflg; 206*0Sstevel@tonic-gate static int oflg; 207*0Sstevel@tonic-gate static int pflg; 208*0Sstevel@tonic-gate static int qflg; 209*0Sstevel@tonic-gate static int rflg = 1; /* init to 1 for special use in compar */ 210*0Sstevel@tonic-gate static int sflg; 211*0Sstevel@tonic-gate static int tflg; 212*0Sstevel@tonic-gate static int uflg; 213*0Sstevel@tonic-gate static int xflg; 214*0Sstevel@tonic-gate static int Aflg; 215*0Sstevel@tonic-gate static int Cflg; 216*0Sstevel@tonic-gate static int Eflg; 217*0Sstevel@tonic-gate static int Fflg; 218*0Sstevel@tonic-gate static int Hflg; 219*0Sstevel@tonic-gate static int Lflg; 220*0Sstevel@tonic-gate static int Rflg; 221*0Sstevel@tonic-gate static int Sflg; 222*0Sstevel@tonic-gate static long hscale; 223*0Sstevel@tonic-gate static mode_t flags; 224*0Sstevel@tonic-gate static int err = 0; /* Contains return code */ 225*0Sstevel@tonic-gate 226*0Sstevel@tonic-gate static uid_t lastuid = (uid_t)-1; 227*0Sstevel@tonic-gate static gid_t lastgid = (gid_t)-1; 228*0Sstevel@tonic-gate static char *lastuname = NULL; 229*0Sstevel@tonic-gate static char *lastgname = NULL; 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gate /* statreq > 0 if any of sflg, (n)lflg, tflg, Sflg are on */ 232*0Sstevel@tonic-gate static int statreq; 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gate static char *dotp = "."; 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate static u_longlong_t tblocks; /* number of blocks of files in a directory */ 237*0Sstevel@tonic-gate static time_t year, now; 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate static int num_cols = 80; 240*0Sstevel@tonic-gate static int colwidth; 241*0Sstevel@tonic-gate static int filewidth; 242*0Sstevel@tonic-gate static int fixedwidth; 243*0Sstevel@tonic-gate static int nomocore; 244*0Sstevel@tonic-gate static int curcol; 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gate static struct winsize win; 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate static char time_buf[50]; /* array to hold day and time */ 249*0Sstevel@tonic-gate 250*0Sstevel@tonic-gate #define NOTWORKINGDIR(d, l) (((l) < 2) || \ 251*0Sstevel@tonic-gate (strcmp((d) + (l) - 2, "/.") != 0)) 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate #define NOTPARENTDIR(d, l) (((l) < 3) || \ 254*0Sstevel@tonic-gate (strcmp((d) + (l) - 3, "/..") != 0)) 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate int 257*0Sstevel@tonic-gate main(int argc, char *argv[]) 258*0Sstevel@tonic-gate { 259*0Sstevel@tonic-gate int c; 260*0Sstevel@tonic-gate int i; 261*0Sstevel@tonic-gate int width; 262*0Sstevel@tonic-gate int amino = 0; 263*0Sstevel@tonic-gate int opterr = 0; 264*0Sstevel@tonic-gate struct lbuf *ep; 265*0Sstevel@tonic-gate struct lbuf lb; 266*0Sstevel@tonic-gate struct ditem *myinfo; 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 269*0Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 270*0Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 271*0Sstevel@tonic-gate #endif 272*0Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 273*0Sstevel@tonic-gate #ifdef STANDALONE 274*0Sstevel@tonic-gate if (argv[0][0] == '\0') 275*0Sstevel@tonic-gate argc = getargv("ls", &argv, 0); 276*0Sstevel@tonic-gate #endif 277*0Sstevel@tonic-gate 278*0Sstevel@tonic-gate lb.lmtime.tv_sec = time(NULL); 279*0Sstevel@tonic-gate lb.lmtime.tv_nsec = 0; 280*0Sstevel@tonic-gate year = lb.lmtime.tv_sec - 6L*30L*24L*60L*60L; /* 6 months ago */ 281*0Sstevel@tonic-gate now = lb.lmtime.tv_sec + 60; 282*0Sstevel@tonic-gate if (isatty(1)) { 283*0Sstevel@tonic-gate Cflg = 1; 284*0Sstevel@tonic-gate mflg = 0; 285*0Sstevel@tonic-gate } 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gate 288*0Sstevel@tonic-gate while ((c = getopt(argc, argv, 289*0Sstevel@tonic-gate "aAbcCdeEfFghHilLmnopqrRsStux1@")) != EOF) 290*0Sstevel@tonic-gate switch (c) { 291*0Sstevel@tonic-gate case 'a': 292*0Sstevel@tonic-gate aflg++; 293*0Sstevel@tonic-gate continue; 294*0Sstevel@tonic-gate case 'A': 295*0Sstevel@tonic-gate Aflg++; 296*0Sstevel@tonic-gate continue; 297*0Sstevel@tonic-gate case 'b': 298*0Sstevel@tonic-gate bflg = 1; 299*0Sstevel@tonic-gate qflg = 0; 300*0Sstevel@tonic-gate continue; 301*0Sstevel@tonic-gate case 'c': 302*0Sstevel@tonic-gate uflg = 0; 303*0Sstevel@tonic-gate cflg++; 304*0Sstevel@tonic-gate continue; 305*0Sstevel@tonic-gate case 'C': 306*0Sstevel@tonic-gate Cflg = 1; 307*0Sstevel@tonic-gate mflg = 0; 308*0Sstevel@tonic-gate #ifdef XPG4 309*0Sstevel@tonic-gate lflg = 0; 310*0Sstevel@tonic-gate #endif 311*0Sstevel@tonic-gate continue; 312*0Sstevel@tonic-gate case 'd': 313*0Sstevel@tonic-gate dflg++; 314*0Sstevel@tonic-gate continue; 315*0Sstevel@tonic-gate case 'e': 316*0Sstevel@tonic-gate eflg++; 317*0Sstevel@tonic-gate lflg++; 318*0Sstevel@tonic-gate statreq++; 319*0Sstevel@tonic-gate Eflg = 0; 320*0Sstevel@tonic-gate continue; 321*0Sstevel@tonic-gate case 'E': 322*0Sstevel@tonic-gate Eflg++; 323*0Sstevel@tonic-gate lflg++; 324*0Sstevel@tonic-gate statreq++; 325*0Sstevel@tonic-gate eflg = 0; 326*0Sstevel@tonic-gate continue; 327*0Sstevel@tonic-gate case 'f': 328*0Sstevel@tonic-gate fflg++; 329*0Sstevel@tonic-gate continue; 330*0Sstevel@tonic-gate case 'F': 331*0Sstevel@tonic-gate Fflg++; 332*0Sstevel@tonic-gate statreq++; 333*0Sstevel@tonic-gate continue; 334*0Sstevel@tonic-gate case 'g': 335*0Sstevel@tonic-gate gflg++; 336*0Sstevel@tonic-gate lflg++; 337*0Sstevel@tonic-gate statreq++; 338*0Sstevel@tonic-gate continue; 339*0Sstevel@tonic-gate case 'h': 340*0Sstevel@tonic-gate hflg++; 341*0Sstevel@tonic-gate hscale = 1024; 342*0Sstevel@tonic-gate continue; 343*0Sstevel@tonic-gate case 'H': 344*0Sstevel@tonic-gate Hflg++; 345*0Sstevel@tonic-gate /* -H and -L are mutually exclusive */ 346*0Sstevel@tonic-gate Lflg = 0; 347*0Sstevel@tonic-gate continue; 348*0Sstevel@tonic-gate case 'i': 349*0Sstevel@tonic-gate iflg++; 350*0Sstevel@tonic-gate continue; 351*0Sstevel@tonic-gate case 'l': 352*0Sstevel@tonic-gate lflg++; 353*0Sstevel@tonic-gate statreq++; 354*0Sstevel@tonic-gate Cflg = 0; 355*0Sstevel@tonic-gate xflg = 0; 356*0Sstevel@tonic-gate mflg = 0; 357*0Sstevel@tonic-gate atflg = 0; 358*0Sstevel@tonic-gate continue; 359*0Sstevel@tonic-gate case 'L': 360*0Sstevel@tonic-gate Lflg++; 361*0Sstevel@tonic-gate /* -H and -L are mutually exclusive */ 362*0Sstevel@tonic-gate Hflg = 0; 363*0Sstevel@tonic-gate continue; 364*0Sstevel@tonic-gate case 'm': 365*0Sstevel@tonic-gate Cflg = 0; 366*0Sstevel@tonic-gate mflg = 1; 367*0Sstevel@tonic-gate #ifdef XPG4 368*0Sstevel@tonic-gate lflg = 0; 369*0Sstevel@tonic-gate #endif 370*0Sstevel@tonic-gate continue; 371*0Sstevel@tonic-gate case 'n': 372*0Sstevel@tonic-gate nflg++; 373*0Sstevel@tonic-gate lflg++; 374*0Sstevel@tonic-gate statreq++; 375*0Sstevel@tonic-gate Cflg = 0; 376*0Sstevel@tonic-gate xflg = 0; 377*0Sstevel@tonic-gate mflg = 0; 378*0Sstevel@tonic-gate atflg = 0; 379*0Sstevel@tonic-gate continue; 380*0Sstevel@tonic-gate case 'o': 381*0Sstevel@tonic-gate oflg++; 382*0Sstevel@tonic-gate lflg++; 383*0Sstevel@tonic-gate statreq++; 384*0Sstevel@tonic-gate continue; 385*0Sstevel@tonic-gate case 'p': 386*0Sstevel@tonic-gate pflg++; 387*0Sstevel@tonic-gate statreq++; 388*0Sstevel@tonic-gate continue; 389*0Sstevel@tonic-gate case 'q': 390*0Sstevel@tonic-gate qflg = 1; 391*0Sstevel@tonic-gate bflg = 0; 392*0Sstevel@tonic-gate continue; 393*0Sstevel@tonic-gate case 'r': 394*0Sstevel@tonic-gate rflg = -1; 395*0Sstevel@tonic-gate continue; 396*0Sstevel@tonic-gate case 'R': 397*0Sstevel@tonic-gate Rflg++; 398*0Sstevel@tonic-gate statreq++; 399*0Sstevel@tonic-gate continue; 400*0Sstevel@tonic-gate case 's': 401*0Sstevel@tonic-gate sflg++; 402*0Sstevel@tonic-gate statreq++; 403*0Sstevel@tonic-gate continue; 404*0Sstevel@tonic-gate case 'S': 405*0Sstevel@tonic-gate tflg = 0; 406*0Sstevel@tonic-gate Sflg++; 407*0Sstevel@tonic-gate statreq++; 408*0Sstevel@tonic-gate continue; 409*0Sstevel@tonic-gate case 't': 410*0Sstevel@tonic-gate Sflg = 0; 411*0Sstevel@tonic-gate tflg++; 412*0Sstevel@tonic-gate statreq++; 413*0Sstevel@tonic-gate continue; 414*0Sstevel@tonic-gate case 'u': 415*0Sstevel@tonic-gate cflg = 0; 416*0Sstevel@tonic-gate uflg++; 417*0Sstevel@tonic-gate continue; 418*0Sstevel@tonic-gate case 'x': 419*0Sstevel@tonic-gate xflg = 1; 420*0Sstevel@tonic-gate Cflg = 1; 421*0Sstevel@tonic-gate mflg = 0; 422*0Sstevel@tonic-gate #ifdef XPG4 423*0Sstevel@tonic-gate lflg = 0; 424*0Sstevel@tonic-gate #endif 425*0Sstevel@tonic-gate continue; 426*0Sstevel@tonic-gate case '1': 427*0Sstevel@tonic-gate Cflg = 0; 428*0Sstevel@tonic-gate continue; 429*0Sstevel@tonic-gate case '@': 430*0Sstevel@tonic-gate #if !defined(XPG4) 431*0Sstevel@tonic-gate /* 432*0Sstevel@tonic-gate * -l has precedence over -@ 433*0Sstevel@tonic-gate */ 434*0Sstevel@tonic-gate if (lflg) 435*0Sstevel@tonic-gate continue; 436*0Sstevel@tonic-gate #endif 437*0Sstevel@tonic-gate atflg++; 438*0Sstevel@tonic-gate lflg++; 439*0Sstevel@tonic-gate statreq++; 440*0Sstevel@tonic-gate Cflg = 0; 441*0Sstevel@tonic-gate xflg = 0; 442*0Sstevel@tonic-gate mflg = 0; 443*0Sstevel@tonic-gate continue; 444*0Sstevel@tonic-gate case '?': 445*0Sstevel@tonic-gate opterr++; 446*0Sstevel@tonic-gate continue; 447*0Sstevel@tonic-gate } 448*0Sstevel@tonic-gate if (opterr) { 449*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 450*0Sstevel@tonic-gate "usage: ls -aAbcCdeEfFghHilLmnopqrRsStux1@ [files]\n")); 451*0Sstevel@tonic-gate exit(2); 452*0Sstevel@tonic-gate } 453*0Sstevel@tonic-gate 454*0Sstevel@tonic-gate if (fflg) { 455*0Sstevel@tonic-gate aflg++; 456*0Sstevel@tonic-gate lflg = 0; 457*0Sstevel@tonic-gate sflg = 0; 458*0Sstevel@tonic-gate tflg = 0; 459*0Sstevel@tonic-gate Sflg = 0; 460*0Sstevel@tonic-gate statreq = 0; 461*0Sstevel@tonic-gate } 462*0Sstevel@tonic-gate 463*0Sstevel@tonic-gate fixedwidth = 2; 464*0Sstevel@tonic-gate if (pflg || Fflg) 465*0Sstevel@tonic-gate fixedwidth++; 466*0Sstevel@tonic-gate if (iflg) 467*0Sstevel@tonic-gate fixedwidth += 11; 468*0Sstevel@tonic-gate if (sflg) 469*0Sstevel@tonic-gate fixedwidth += 5; 470*0Sstevel@tonic-gate 471*0Sstevel@tonic-gate if (lflg) { 472*0Sstevel@tonic-gate if (!gflg && !oflg) 473*0Sstevel@tonic-gate gflg = oflg = 1; 474*0Sstevel@tonic-gate else 475*0Sstevel@tonic-gate if (gflg && oflg) 476*0Sstevel@tonic-gate gflg = oflg = 0; 477*0Sstevel@tonic-gate Cflg = mflg = 0; 478*0Sstevel@tonic-gate } 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gate if (Cflg || mflg) { 481*0Sstevel@tonic-gate char *clptr; 482*0Sstevel@tonic-gate if ((clptr = getenv("COLUMNS")) != NULL) 483*0Sstevel@tonic-gate num_cols = atoi(clptr); 484*0Sstevel@tonic-gate #ifdef TERMINFO 485*0Sstevel@tonic-gate else { 486*0Sstevel@tonic-gate if (ioctl(1, TIOCGWINSZ, &win) != -1) 487*0Sstevel@tonic-gate num_cols = (win.ws_col == 0 ? 80 : win.ws_col); 488*0Sstevel@tonic-gate } 489*0Sstevel@tonic-gate #endif 490*0Sstevel@tonic-gate if (num_cols < 20 || num_cols > 1000) 491*0Sstevel@tonic-gate /* assume it is an error */ 492*0Sstevel@tonic-gate num_cols = 80; 493*0Sstevel@tonic-gate } 494*0Sstevel@tonic-gate 495*0Sstevel@tonic-gate /* allocate space for flist and the associated */ 496*0Sstevel@tonic-gate /* data structures (lbufs) */ 497*0Sstevel@tonic-gate maxfils = quantn; 498*0Sstevel@tonic-gate if (((flist = malloc(maxfils * sizeof (struct lbuf *))) == NULL) || 499*0Sstevel@tonic-gate ((nxtlbf = malloc(quantn * sizeof (struct lbuf))) == NULL)) { 500*0Sstevel@tonic-gate perror("ls"); 501*0Sstevel@tonic-gate exit(2); 502*0Sstevel@tonic-gate } 503*0Sstevel@tonic-gate if ((amino = (argc-optind)) == 0) { 504*0Sstevel@tonic-gate /* 505*0Sstevel@tonic-gate * case when no names are given 506*0Sstevel@tonic-gate * in ls-command and current 507*0Sstevel@tonic-gate * directory is to be used 508*0Sstevel@tonic-gate */ 509*0Sstevel@tonic-gate argv[optind] = dotp; 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gate for (i = 0; i < (amino ? amino : 1); i++) { 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate /* 515*0Sstevel@tonic-gate * If we are recursing, we need to make sure we don't 516*0Sstevel@tonic-gate * get into an endless loop. To keep track of the inodes 517*0Sstevel@tonic-gate * (actually, just the directories) visited, we 518*0Sstevel@tonic-gate * maintain a directory ancestry list for a file 519*0Sstevel@tonic-gate * hierarchy. As we go deeper into the hierarchy, 520*0Sstevel@tonic-gate * a parent directory passes its directory list 521*0Sstevel@tonic-gate * info (device id, inode number, and a pointer to 522*0Sstevel@tonic-gate * its parent) to each of its children. As we 523*0Sstevel@tonic-gate * process a child that is a directory, we save 524*0Sstevel@tonic-gate * its own personal directory list info. We then 525*0Sstevel@tonic-gate * check to see if the child has already been 526*0Sstevel@tonic-gate * processed by comparing its device id and inode 527*0Sstevel@tonic-gate * number from its own personal directory list info 528*0Sstevel@tonic-gate * to that of each of its ancestors. If there is a 529*0Sstevel@tonic-gate * match, then we know we've detected a cycle. 530*0Sstevel@tonic-gate */ 531*0Sstevel@tonic-gate if (Rflg) { 532*0Sstevel@tonic-gate /* 533*0Sstevel@tonic-gate * This is the first parent in this lineage 534*0Sstevel@tonic-gate * (first in a directory hierarchy), so 535*0Sstevel@tonic-gate * this parent's parent doesn't exist. We 536*0Sstevel@tonic-gate * only initialize myinfo when we are 537*0Sstevel@tonic-gate * recursing, otherwise it's not used. 538*0Sstevel@tonic-gate */ 539*0Sstevel@tonic-gate if ((myinfo = (struct ditem *)malloc( 540*0Sstevel@tonic-gate sizeof (struct ditem))) == NULL) { 541*0Sstevel@tonic-gate perror("ls"); 542*0Sstevel@tonic-gate exit(2); 543*0Sstevel@tonic-gate } else { 544*0Sstevel@tonic-gate myinfo->dev = 0; 545*0Sstevel@tonic-gate myinfo->ino = 0; 546*0Sstevel@tonic-gate myinfo->parent = NULL; 547*0Sstevel@tonic-gate } 548*0Sstevel@tonic-gate } 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate if (Cflg || mflg) { 551*0Sstevel@tonic-gate width = strcol((unsigned char *)argv[optind]); 552*0Sstevel@tonic-gate if (width > filewidth) 553*0Sstevel@tonic-gate filewidth = width; 554*0Sstevel@tonic-gate } 555*0Sstevel@tonic-gate if ((ep = gstat((*argv[optind] ? argv[optind] : dotp), 556*0Sstevel@tonic-gate 1, myinfo)) == NULL) { 557*0Sstevel@tonic-gate if (nomocore) 558*0Sstevel@tonic-gate exit(2); 559*0Sstevel@tonic-gate err = 2; 560*0Sstevel@tonic-gate optind++; 561*0Sstevel@tonic-gate continue; 562*0Sstevel@tonic-gate } 563*0Sstevel@tonic-gate ep->ln.namep = (*argv[optind] ? argv[optind] : dotp); 564*0Sstevel@tonic-gate ep->lflags |= ISARG; 565*0Sstevel@tonic-gate optind++; 566*0Sstevel@tonic-gate nargs++; /* count good arguments stored in flist */ 567*0Sstevel@tonic-gate } 568*0Sstevel@tonic-gate colwidth = fixedwidth + filewidth; 569*0Sstevel@tonic-gate qsort(flist, (unsigned)nargs, sizeof (struct lbuf *), 570*0Sstevel@tonic-gate (int (*)(const void *, const void *))compar); 571*0Sstevel@tonic-gate for (i = 0; i < nargs; i++) { 572*0Sstevel@tonic-gate if (flist[i]->ltype == 'd' && dflg == 0 || fflg) 573*0Sstevel@tonic-gate break; 574*0Sstevel@tonic-gate } 575*0Sstevel@tonic-gate pem(&flist[0], &flist[i], 0); 576*0Sstevel@tonic-gate for (; i < nargs; i++) { 577*0Sstevel@tonic-gate pdirectory(flist[i]->ln.namep, Rflg || 578*0Sstevel@tonic-gate (amino > 1), nargs, 0, flist[i]->ancinfo); 579*0Sstevel@tonic-gate if (nomocore) 580*0Sstevel@tonic-gate exit(2); 581*0Sstevel@tonic-gate /* -R: print subdirectories found */ 582*0Sstevel@tonic-gate while (dfirst || cdfirst) { 583*0Sstevel@tonic-gate /* Place direct subdirs on front in right order */ 584*0Sstevel@tonic-gate while (cdfirst) { 585*0Sstevel@tonic-gate /* reverse cdfirst onto front of dfirst */ 586*0Sstevel@tonic-gate dtemp = cdfirst; 587*0Sstevel@tonic-gate cdfirst = cdfirst -> dc_next; 588*0Sstevel@tonic-gate dtemp -> dc_next = dfirst; 589*0Sstevel@tonic-gate dfirst = dtemp; 590*0Sstevel@tonic-gate } 591*0Sstevel@tonic-gate /* take off first dir on dfirst & print it */ 592*0Sstevel@tonic-gate dtemp = dfirst; 593*0Sstevel@tonic-gate dfirst = dfirst->dc_next; 594*0Sstevel@tonic-gate pdirectory(dtemp->dc_name, 1, nargs, 595*0Sstevel@tonic-gate dtemp->cycle_detected, dtemp->myancinfo); 596*0Sstevel@tonic-gate if (nomocore) 597*0Sstevel@tonic-gate exit(2); 598*0Sstevel@tonic-gate free(dtemp->dc_name); 599*0Sstevel@tonic-gate free(dtemp); 600*0Sstevel@tonic-gate } 601*0Sstevel@tonic-gate } 602*0Sstevel@tonic-gate return (err); 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate 605*0Sstevel@tonic-gate /* 606*0Sstevel@tonic-gate * pdirectory: print the directory name, labelling it if title is 607*0Sstevel@tonic-gate * nonzero, using lp as the place to start reading in the dir. 608*0Sstevel@tonic-gate */ 609*0Sstevel@tonic-gate static void 610*0Sstevel@tonic-gate pdirectory(char *name, int title, int lp, int cdetect, struct ditem *myinfo) 611*0Sstevel@tonic-gate { 612*0Sstevel@tonic-gate struct dchain *dp; 613*0Sstevel@tonic-gate struct lbuf *ap; 614*0Sstevel@tonic-gate char *pname; 615*0Sstevel@tonic-gate int j; 616*0Sstevel@tonic-gate 617*0Sstevel@tonic-gate filewidth = 0; 618*0Sstevel@tonic-gate curdir = name; 619*0Sstevel@tonic-gate if (title) { 620*0Sstevel@tonic-gate if (!first) 621*0Sstevel@tonic-gate (void) putc('\n', stdout); 622*0Sstevel@tonic-gate pprintf(name, ":"); 623*0Sstevel@tonic-gate new_line(); 624*0Sstevel@tonic-gate } 625*0Sstevel@tonic-gate /* 626*0Sstevel@tonic-gate * If there was a cycle detected, then notify and don't report 627*0Sstevel@tonic-gate * further. 628*0Sstevel@tonic-gate */ 629*0Sstevel@tonic-gate if (cdetect) { 630*0Sstevel@tonic-gate if (lflg || sflg) { 631*0Sstevel@tonic-gate curcol += printf(gettext("total %d"), 0); 632*0Sstevel@tonic-gate new_line(); 633*0Sstevel@tonic-gate } 634*0Sstevel@tonic-gate (void) fprintf(stderr, gettext( 635*0Sstevel@tonic-gate "ls: cycle detected for %s\n"), name); 636*0Sstevel@tonic-gate return; 637*0Sstevel@tonic-gate } 638*0Sstevel@tonic-gate 639*0Sstevel@tonic-gate nfiles = lp; 640*0Sstevel@tonic-gate rddir(name, myinfo); 641*0Sstevel@tonic-gate if (nomocore) 642*0Sstevel@tonic-gate return; 643*0Sstevel@tonic-gate if (fflg == 0) 644*0Sstevel@tonic-gate qsort(&flist[lp], (unsigned)(nfiles - lp), 645*0Sstevel@tonic-gate sizeof (struct lbuf *), 646*0Sstevel@tonic-gate (int (*)(const void *, const void *))compar); 647*0Sstevel@tonic-gate if (Rflg) { 648*0Sstevel@tonic-gate for (j = nfiles - 1; j >= lp; j--) { 649*0Sstevel@tonic-gate ap = flist[j]; 650*0Sstevel@tonic-gate if (ap->ltype == 'd' && strcmp(ap->ln.lname, ".") && 651*0Sstevel@tonic-gate strcmp(ap->ln.lname, "..")) { 652*0Sstevel@tonic-gate dp = malloc(sizeof (struct dchain)); 653*0Sstevel@tonic-gate if (dp == NULL) { 654*0Sstevel@tonic-gate perror("ls"); 655*0Sstevel@tonic-gate exit(2); 656*0Sstevel@tonic-gate } 657*0Sstevel@tonic-gate pname = makename(curdir, ap->ln.lname); 658*0Sstevel@tonic-gate if ((dp->dc_name = strdup(pname)) == NULL) { 659*0Sstevel@tonic-gate perror("ls"); 660*0Sstevel@tonic-gate exit(2); 661*0Sstevel@tonic-gate } 662*0Sstevel@tonic-gate dp->cycle_detected = ap->cycle; 663*0Sstevel@tonic-gate dp->myancinfo = ap->ancinfo; 664*0Sstevel@tonic-gate dp->dc_next = dfirst; 665*0Sstevel@tonic-gate dfirst = dp; 666*0Sstevel@tonic-gate } 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate } 669*0Sstevel@tonic-gate if (lflg || sflg) { 670*0Sstevel@tonic-gate curcol += printf(gettext("total %llu"), tblocks); 671*0Sstevel@tonic-gate new_line(); 672*0Sstevel@tonic-gate } 673*0Sstevel@tonic-gate pem(&flist[lp], &flist[nfiles], lflg||sflg); 674*0Sstevel@tonic-gate } 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gate /* 677*0Sstevel@tonic-gate * pem: print 'em. Print a list of files (e.g. a directory) bounded 678*0Sstevel@tonic-gate * by slp and lp. 679*0Sstevel@tonic-gate */ 680*0Sstevel@tonic-gate static void 681*0Sstevel@tonic-gate pem(struct lbuf **slp, struct lbuf **lp, int tot_flag) 682*0Sstevel@tonic-gate { 683*0Sstevel@tonic-gate long row, nrows, i; 684*0Sstevel@tonic-gate int col, ncols; 685*0Sstevel@tonic-gate struct lbuf **ep; 686*0Sstevel@tonic-gate 687*0Sstevel@tonic-gate if (Cflg || mflg) { 688*0Sstevel@tonic-gate if (colwidth > num_cols) { 689*0Sstevel@tonic-gate ncols = 1; 690*0Sstevel@tonic-gate } else { 691*0Sstevel@tonic-gate ncols = num_cols / colwidth; 692*0Sstevel@tonic-gate } 693*0Sstevel@tonic-gate } 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gate if (ncols == 1 || mflg || xflg || !Cflg) { 696*0Sstevel@tonic-gate for (ep = slp; ep < lp; ep++) 697*0Sstevel@tonic-gate pentry(*ep); 698*0Sstevel@tonic-gate new_line(); 699*0Sstevel@tonic-gate return; 700*0Sstevel@tonic-gate } 701*0Sstevel@tonic-gate /* otherwise print -C columns */ 702*0Sstevel@tonic-gate if (tot_flag) { 703*0Sstevel@tonic-gate slp--; 704*0Sstevel@tonic-gate row = 1; 705*0Sstevel@tonic-gate } 706*0Sstevel@tonic-gate else 707*0Sstevel@tonic-gate row = 0; 708*0Sstevel@tonic-gate 709*0Sstevel@tonic-gate nrows = (lp - slp - 1) / ncols + 1; 710*0Sstevel@tonic-gate for (i = 0; i < nrows; i++, row++) { 711*0Sstevel@tonic-gate for (col = 0; col < ncols; col++) { 712*0Sstevel@tonic-gate ep = slp + (nrows * col) + row; 713*0Sstevel@tonic-gate if (ep < lp) 714*0Sstevel@tonic-gate pentry(*ep); 715*0Sstevel@tonic-gate } 716*0Sstevel@tonic-gate new_line(); 717*0Sstevel@tonic-gate } 718*0Sstevel@tonic-gate } 719*0Sstevel@tonic-gate 720*0Sstevel@tonic-gate /* 721*0Sstevel@tonic-gate * print one output entry; 722*0Sstevel@tonic-gate * if uid/gid is not found in the appropriate 723*0Sstevel@tonic-gate * file(passwd/group), then print uid/gid instead of 724*0Sstevel@tonic-gate * user/group name; 725*0Sstevel@tonic-gate */ 726*0Sstevel@tonic-gate static void 727*0Sstevel@tonic-gate pentry(struct lbuf *ap) 728*0Sstevel@tonic-gate { 729*0Sstevel@tonic-gate struct lbuf *p; 730*0Sstevel@tonic-gate numbuf_t hbuf; 731*0Sstevel@tonic-gate char buf[BUFSIZ]; 732*0Sstevel@tonic-gate char fmt_buf[FMTSIZE]; 733*0Sstevel@tonic-gate char *dmark = ""; /* Used if -p or -F option active */ 734*0Sstevel@tonic-gate char *cp; 735*0Sstevel@tonic-gate 736*0Sstevel@tonic-gate p = ap; 737*0Sstevel@tonic-gate column(); 738*0Sstevel@tonic-gate if (iflg) 739*0Sstevel@tonic-gate if (mflg && !lflg) 740*0Sstevel@tonic-gate curcol += printf("%llu ", (long long)p->lnum); 741*0Sstevel@tonic-gate else 742*0Sstevel@tonic-gate curcol += printf("%10llu ", (long long)p->lnum); 743*0Sstevel@tonic-gate if (sflg) 744*0Sstevel@tonic-gate curcol += printf((mflg && !lflg) ? "%lld " : 745*0Sstevel@tonic-gate (p->lblocks < 10000) ? "%4lld " : "%lld ", 746*0Sstevel@tonic-gate (p->ltype != 'b' && p->ltype != 'c') ? 747*0Sstevel@tonic-gate p->lblocks : 0LL); 748*0Sstevel@tonic-gate if (lflg) { 749*0Sstevel@tonic-gate (void) putchar(p->ltype); 750*0Sstevel@tonic-gate curcol++; 751*0Sstevel@tonic-gate pmode(p->lflags); 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gate /* ACL: additional access mode flag */ 754*0Sstevel@tonic-gate (void) putchar(p->acl); 755*0Sstevel@tonic-gate curcol++; 756*0Sstevel@tonic-gate 757*0Sstevel@tonic-gate curcol += printf("%3lu ", (ulong_t)p->lnl); 758*0Sstevel@tonic-gate if (oflg) 759*0Sstevel@tonic-gate if (!nflg) { 760*0Sstevel@tonic-gate cp = getname(p->luid); 761*0Sstevel@tonic-gate curcol += printf("%-8s ", cp); 762*0Sstevel@tonic-gate } else 763*0Sstevel@tonic-gate curcol += printf("%-8lu ", (ulong_t)p->luid); 764*0Sstevel@tonic-gate if (gflg) 765*0Sstevel@tonic-gate if (!nflg) { 766*0Sstevel@tonic-gate cp = getgroup(p->lgid); 767*0Sstevel@tonic-gate curcol += printf("%-8s ", cp); 768*0Sstevel@tonic-gate } else 769*0Sstevel@tonic-gate curcol += printf("%-8lu ", (ulong_t)p->lgid); 770*0Sstevel@tonic-gate if (p->ltype == 'b' || p->ltype == 'c') { 771*0Sstevel@tonic-gate curcol += printf("%3u, %2u", 772*0Sstevel@tonic-gate (uint_t)major((dev_t)p->lsize), 773*0Sstevel@tonic-gate (uint_t)minor((dev_t)p->lsize)); 774*0Sstevel@tonic-gate } else if (hflg && (p->lsize >= hscale)) { 775*0Sstevel@tonic-gate curcol += printf("%7s", 776*0Sstevel@tonic-gate number_to_scaled_string(hbuf, p->lsize, hscale)); 777*0Sstevel@tonic-gate } else { 778*0Sstevel@tonic-gate curcol += printf((p->lsize < (off_t)10000000) ? 779*0Sstevel@tonic-gate "%7lld" : "%lld", p->lsize); 780*0Sstevel@tonic-gate } 781*0Sstevel@tonic-gate if (eflg) { 782*0Sstevel@tonic-gate (void) strftime(time_buf, sizeof (time_buf), 783*0Sstevel@tonic-gate dcgettext(NULL, FORMAT3, LC_TIME), 784*0Sstevel@tonic-gate localtime(&p->lmtime.tv_sec)); 785*0Sstevel@tonic-gate } else if (Eflg) { 786*0Sstevel@tonic-gate /* fill in nanoseconds first */ 787*0Sstevel@tonic-gate (void) snprintf(fmt_buf, sizeof (fmt_buf), 788*0Sstevel@tonic-gate FORMAT4, p->lmtime.tv_nsec); 789*0Sstevel@tonic-gate (void) strftime(time_buf, sizeof (time_buf), 790*0Sstevel@tonic-gate fmt_buf, localtime(&p->lmtime.tv_sec)); 791*0Sstevel@tonic-gate } else { 792*0Sstevel@tonic-gate if ((p->lmtime.tv_sec < year) || 793*0Sstevel@tonic-gate (p->lmtime.tv_sec > now)) { 794*0Sstevel@tonic-gate (void) strftime(time_buf, sizeof (time_buf), 795*0Sstevel@tonic-gate dcgettext(NULL, FORMAT1, LC_TIME), 796*0Sstevel@tonic-gate localtime(&p->lmtime.tv_sec)); 797*0Sstevel@tonic-gate } else { 798*0Sstevel@tonic-gate (void) strftime(time_buf, sizeof (time_buf), 799*0Sstevel@tonic-gate dcgettext(NULL, FORMAT2, LC_TIME), 800*0Sstevel@tonic-gate localtime(&p->lmtime.tv_sec)); 801*0Sstevel@tonic-gate } 802*0Sstevel@tonic-gate } 803*0Sstevel@tonic-gate curcol += printf("%s", time_buf); 804*0Sstevel@tonic-gate } 805*0Sstevel@tonic-gate 806*0Sstevel@tonic-gate /* 807*0Sstevel@tonic-gate * prevent both "->" and trailing marks 808*0Sstevel@tonic-gate * from appearing 809*0Sstevel@tonic-gate */ 810*0Sstevel@tonic-gate 811*0Sstevel@tonic-gate if (pflg && p->ltype == 'd') 812*0Sstevel@tonic-gate dmark = "/"; 813*0Sstevel@tonic-gate 814*0Sstevel@tonic-gate if (Fflg && !(lflg && p->flinkto)) { 815*0Sstevel@tonic-gate if (p->ltype == 'd') 816*0Sstevel@tonic-gate dmark = "/"; 817*0Sstevel@tonic-gate else if (p->ltype == 'D') 818*0Sstevel@tonic-gate dmark = ">"; 819*0Sstevel@tonic-gate else if (p->ltype == 'p') 820*0Sstevel@tonic-gate dmark = "|"; 821*0Sstevel@tonic-gate else if (p->ltype == 'l') 822*0Sstevel@tonic-gate dmark = "@"; 823*0Sstevel@tonic-gate else if (p->ltype == 's') 824*0Sstevel@tonic-gate dmark = "="; 825*0Sstevel@tonic-gate else if (p->lflags & (S_IXUSR|S_IXGRP|S_IXOTH)) 826*0Sstevel@tonic-gate dmark = "*"; 827*0Sstevel@tonic-gate else 828*0Sstevel@tonic-gate dmark = ""; 829*0Sstevel@tonic-gate } 830*0Sstevel@tonic-gate 831*0Sstevel@tonic-gate if (lflg && p->flinkto) { 832*0Sstevel@tonic-gate (void) strncpy(buf, " -> ", 4); 833*0Sstevel@tonic-gate (void) strcpy(buf + 4, p->flinkto); 834*0Sstevel@tonic-gate dmark = buf; 835*0Sstevel@tonic-gate } 836*0Sstevel@tonic-gate 837*0Sstevel@tonic-gate if (p->lflags & ISARG) { 838*0Sstevel@tonic-gate if (qflg || bflg) 839*0Sstevel@tonic-gate pprintf(p->ln.namep, dmark); 840*0Sstevel@tonic-gate else { 841*0Sstevel@tonic-gate (void) printf("%s%s", p->ln.namep, dmark); 842*0Sstevel@tonic-gate curcol += strcol((unsigned char *)p->ln.namep); 843*0Sstevel@tonic-gate curcol += strcol((unsigned char *)dmark); 844*0Sstevel@tonic-gate } 845*0Sstevel@tonic-gate } else { 846*0Sstevel@tonic-gate if (qflg || bflg) 847*0Sstevel@tonic-gate pprintf(p->ln.lname, dmark); 848*0Sstevel@tonic-gate else { 849*0Sstevel@tonic-gate (void) printf("%s%s", p->ln.lname, dmark); 850*0Sstevel@tonic-gate curcol += strcol((unsigned char *)p->ln.lname); 851*0Sstevel@tonic-gate curcol += strcol((unsigned char *)dmark); 852*0Sstevel@tonic-gate } 853*0Sstevel@tonic-gate } 854*0Sstevel@tonic-gate } 855*0Sstevel@tonic-gate 856*0Sstevel@tonic-gate /* print various r,w,x permissions */ 857*0Sstevel@tonic-gate static void 858*0Sstevel@tonic-gate pmode(mode_t aflag) 859*0Sstevel@tonic-gate { 860*0Sstevel@tonic-gate /* these arrays are declared static to allow initializations */ 861*0Sstevel@tonic-gate static int m0[] = { 1, S_IRUSR, 'r', '-' }; 862*0Sstevel@tonic-gate static int m1[] = { 1, S_IWUSR, 'w', '-' }; 863*0Sstevel@tonic-gate static int m2[] = { 3, S_ISUID|S_IXUSR, 's', S_IXUSR, 864*0Sstevel@tonic-gate 'x', S_ISUID, 'S', '-' }; 865*0Sstevel@tonic-gate static int m3[] = { 1, S_IRGRP, 'r', '-' }; 866*0Sstevel@tonic-gate static int m4[] = { 1, S_IWGRP, 'w', '-' }; 867*0Sstevel@tonic-gate static int m5[] = { 4, S_ISGID|S_IXGRP, 's', S_IXGRP, 868*0Sstevel@tonic-gate 'x', S_ISGID|LS_NOTREG, 'S', 869*0Sstevel@tonic-gate #ifdef XPG4 870*0Sstevel@tonic-gate S_ISGID, 'L', '-'}; 871*0Sstevel@tonic-gate #else 872*0Sstevel@tonic-gate S_ISGID, 'l', '-'}; 873*0Sstevel@tonic-gate #endif 874*0Sstevel@tonic-gate static int m6[] = { 1, S_IROTH, 'r', '-' }; 875*0Sstevel@tonic-gate static int m7[] = { 1, S_IWOTH, 'w', '-' }; 876*0Sstevel@tonic-gate static int m8[] = { 3, S_ISVTX|S_IXOTH, 't', S_IXOTH, 877*0Sstevel@tonic-gate 'x', S_ISVTX, 'T', '-'}; 878*0Sstevel@tonic-gate 879*0Sstevel@tonic-gate static int *m[] = { m0, m1, m2, m3, m4, m5, m6, m7, m8}; 880*0Sstevel@tonic-gate 881*0Sstevel@tonic-gate int **mp; 882*0Sstevel@tonic-gate 883*0Sstevel@tonic-gate flags = aflag; 884*0Sstevel@tonic-gate for (mp = &m[0]; mp < &m[sizeof (m) / sizeof (m[0])]; mp++) 885*0Sstevel@tonic-gate selection(*mp); 886*0Sstevel@tonic-gate } 887*0Sstevel@tonic-gate 888*0Sstevel@tonic-gate static void 889*0Sstevel@tonic-gate selection(int *pairp) 890*0Sstevel@tonic-gate { 891*0Sstevel@tonic-gate int n; 892*0Sstevel@tonic-gate 893*0Sstevel@tonic-gate n = *pairp++; 894*0Sstevel@tonic-gate while (n-->0) { 895*0Sstevel@tonic-gate if ((flags & *pairp) == *pairp) { 896*0Sstevel@tonic-gate pairp++; 897*0Sstevel@tonic-gate break; 898*0Sstevel@tonic-gate } else { 899*0Sstevel@tonic-gate pairp += 2; 900*0Sstevel@tonic-gate } 901*0Sstevel@tonic-gate } 902*0Sstevel@tonic-gate (void) putchar(*pairp); 903*0Sstevel@tonic-gate curcol++; 904*0Sstevel@tonic-gate } 905*0Sstevel@tonic-gate 906*0Sstevel@tonic-gate /* 907*0Sstevel@tonic-gate * column: get to the beginning of the next column. 908*0Sstevel@tonic-gate */ 909*0Sstevel@tonic-gate static void 910*0Sstevel@tonic-gate column(void) 911*0Sstevel@tonic-gate { 912*0Sstevel@tonic-gate if (curcol == 0) 913*0Sstevel@tonic-gate return; 914*0Sstevel@tonic-gate if (mflg) { 915*0Sstevel@tonic-gate (void) putc(',', stdout); 916*0Sstevel@tonic-gate curcol++; 917*0Sstevel@tonic-gate if (curcol + colwidth + 2 > num_cols) { 918*0Sstevel@tonic-gate (void) putc('\n', stdout); 919*0Sstevel@tonic-gate curcol = 0; 920*0Sstevel@tonic-gate return; 921*0Sstevel@tonic-gate } 922*0Sstevel@tonic-gate (void) putc(' ', stdout); 923*0Sstevel@tonic-gate curcol++; 924*0Sstevel@tonic-gate return; 925*0Sstevel@tonic-gate } 926*0Sstevel@tonic-gate if (Cflg == 0) { 927*0Sstevel@tonic-gate (void) putc('\n', stdout); 928*0Sstevel@tonic-gate curcol = 0; 929*0Sstevel@tonic-gate return; 930*0Sstevel@tonic-gate } 931*0Sstevel@tonic-gate if ((curcol / colwidth + 2) * colwidth > num_cols) { 932*0Sstevel@tonic-gate (void) putc('\n', stdout); 933*0Sstevel@tonic-gate curcol = 0; 934*0Sstevel@tonic-gate return; 935*0Sstevel@tonic-gate } 936*0Sstevel@tonic-gate do { 937*0Sstevel@tonic-gate (void) putc(' ', stdout); 938*0Sstevel@tonic-gate curcol++; 939*0Sstevel@tonic-gate } while (curcol % colwidth); 940*0Sstevel@tonic-gate } 941*0Sstevel@tonic-gate 942*0Sstevel@tonic-gate static void 943*0Sstevel@tonic-gate new_line(void) 944*0Sstevel@tonic-gate { 945*0Sstevel@tonic-gate if (curcol) { 946*0Sstevel@tonic-gate first = 0; 947*0Sstevel@tonic-gate (void) putc('\n', stdout); 948*0Sstevel@tonic-gate curcol = 0; 949*0Sstevel@tonic-gate } 950*0Sstevel@tonic-gate } 951*0Sstevel@tonic-gate 952*0Sstevel@tonic-gate /* 953*0Sstevel@tonic-gate * read each filename in directory dir and store its 954*0Sstevel@tonic-gate * status in flist[nfiles] 955*0Sstevel@tonic-gate * use makename() to form pathname dir/filename; 956*0Sstevel@tonic-gate */ 957*0Sstevel@tonic-gate static void 958*0Sstevel@tonic-gate rddir(char *dir, struct ditem *myinfo) 959*0Sstevel@tonic-gate { 960*0Sstevel@tonic-gate struct dirent *dentry; 961*0Sstevel@tonic-gate DIR *dirf; 962*0Sstevel@tonic-gate int j; 963*0Sstevel@tonic-gate struct lbuf *ep; 964*0Sstevel@tonic-gate int width; 965*0Sstevel@tonic-gate 966*0Sstevel@tonic-gate if ((dirf = opendir(dir)) == NULL) { 967*0Sstevel@tonic-gate (void) fflush(stdout); 968*0Sstevel@tonic-gate perror(dir); 969*0Sstevel@tonic-gate err = 2; 970*0Sstevel@tonic-gate return; 971*0Sstevel@tonic-gate } else { 972*0Sstevel@tonic-gate tblocks = 0; 973*0Sstevel@tonic-gate for (;;) { 974*0Sstevel@tonic-gate errno = 0; 975*0Sstevel@tonic-gate if ((dentry = readdir(dirf)) == NULL) 976*0Sstevel@tonic-gate break; 977*0Sstevel@tonic-gate if (aflg == 0 && dentry->d_name[0] == '.' && 978*0Sstevel@tonic-gate (Aflg == 0 || 979*0Sstevel@tonic-gate dentry->d_name[1] == '\0' || 980*0Sstevel@tonic-gate dentry->d_name[1] == '.' && 981*0Sstevel@tonic-gate dentry->d_name[2] == '\0')) 982*0Sstevel@tonic-gate /* 983*0Sstevel@tonic-gate * check for directory items '.', '..', 984*0Sstevel@tonic-gate * and items without valid inode-number; 985*0Sstevel@tonic-gate */ 986*0Sstevel@tonic-gate continue; 987*0Sstevel@tonic-gate 988*0Sstevel@tonic-gate if (Cflg || mflg) { 989*0Sstevel@tonic-gate width = strcol((unsigned char *)dentry->d_name); 990*0Sstevel@tonic-gate if (width > filewidth) 991*0Sstevel@tonic-gate filewidth = width; 992*0Sstevel@tonic-gate } 993*0Sstevel@tonic-gate ep = gstat(makename(dir, dentry->d_name), 0, myinfo); 994*0Sstevel@tonic-gate if (ep == NULL) { 995*0Sstevel@tonic-gate if (nomocore) 996*0Sstevel@tonic-gate return; 997*0Sstevel@tonic-gate continue; 998*0Sstevel@tonic-gate } else { 999*0Sstevel@tonic-gate ep->lnum = dentry->d_ino; 1000*0Sstevel@tonic-gate for (j = 0; dentry->d_name[j] != '\0'; j++) 1001*0Sstevel@tonic-gate ep->ln.lname[j] = dentry->d_name[j]; 1002*0Sstevel@tonic-gate ep->ln.lname[j] = '\0'; 1003*0Sstevel@tonic-gate } 1004*0Sstevel@tonic-gate } 1005*0Sstevel@tonic-gate if (errno) { 1006*0Sstevel@tonic-gate int sav_errno = errno; 1007*0Sstevel@tonic-gate 1008*0Sstevel@tonic-gate (void) fprintf(stderr, 1009*0Sstevel@tonic-gate gettext("ls: error reading directory %s: %s\n"), 1010*0Sstevel@tonic-gate dir, strerror(sav_errno)); 1011*0Sstevel@tonic-gate } 1012*0Sstevel@tonic-gate (void) closedir(dirf); 1013*0Sstevel@tonic-gate colwidth = fixedwidth + filewidth; 1014*0Sstevel@tonic-gate } 1015*0Sstevel@tonic-gate } 1016*0Sstevel@tonic-gate 1017*0Sstevel@tonic-gate /* 1018*0Sstevel@tonic-gate * Attaching a link to an inode's ancestors. Search 1019*0Sstevel@tonic-gate * through the ancestors to check for cycles (an inode which 1020*0Sstevel@tonic-gate * we have already tracked in this inodes ancestry). If a cycle 1021*0Sstevel@tonic-gate * is detected, set the exit code and record the fact so that 1022*0Sstevel@tonic-gate * it is reported at the right time when printing the directory. 1023*0Sstevel@tonic-gate * In addition, set the exit code. Note: If the -a flag was 1024*0Sstevel@tonic-gate * specified, we don't want to check for cycles for directories 1025*0Sstevel@tonic-gate * ending in '/.' or '/..' unless they were specified on the 1026*0Sstevel@tonic-gate * command line. 1027*0Sstevel@tonic-gate */ 1028*0Sstevel@tonic-gate static void 1029*0Sstevel@tonic-gate record_ancestry(char *file, struct stat *pstatb, struct lbuf *rep, 1030*0Sstevel@tonic-gate int argfl, struct ditem *myparent) 1031*0Sstevel@tonic-gate { 1032*0Sstevel@tonic-gate size_t file_len; 1033*0Sstevel@tonic-gate struct ditem *myinfo; 1034*0Sstevel@tonic-gate struct ditem *tptr; 1035*0Sstevel@tonic-gate 1036*0Sstevel@tonic-gate file_len = strlen(file); 1037*0Sstevel@tonic-gate if (!aflg || argfl || (NOTWORKINGDIR(file, file_len) && 1038*0Sstevel@tonic-gate NOTPARENTDIR(file, file_len))) { 1039*0Sstevel@tonic-gate /* 1040*0Sstevel@tonic-gate * Add this inode's ancestry 1041*0Sstevel@tonic-gate * info and insert it into the 1042*0Sstevel@tonic-gate * ancestry list by pointing 1043*0Sstevel@tonic-gate * back to its parent. We save 1044*0Sstevel@tonic-gate * it (in rep) with the other info 1045*0Sstevel@tonic-gate * we're gathering for this inode. 1046*0Sstevel@tonic-gate */ 1047*0Sstevel@tonic-gate if ((myinfo = malloc( 1048*0Sstevel@tonic-gate sizeof (struct ditem))) == NULL) { 1049*0Sstevel@tonic-gate perror("ls"); 1050*0Sstevel@tonic-gate exit(2); 1051*0Sstevel@tonic-gate } 1052*0Sstevel@tonic-gate myinfo->dev = pstatb->st_dev; 1053*0Sstevel@tonic-gate myinfo->ino = pstatb->st_ino; 1054*0Sstevel@tonic-gate myinfo->parent = myparent; 1055*0Sstevel@tonic-gate rep->ancinfo = myinfo; 1056*0Sstevel@tonic-gate 1057*0Sstevel@tonic-gate /* 1058*0Sstevel@tonic-gate * If this node has the same device id and 1059*0Sstevel@tonic-gate * inode number of one of its ancestors, 1060*0Sstevel@tonic-gate * then we've detected a cycle. 1061*0Sstevel@tonic-gate */ 1062*0Sstevel@tonic-gate if (myparent != NULL) { 1063*0Sstevel@tonic-gate for (tptr = myparent; tptr->parent != NULL; 1064*0Sstevel@tonic-gate tptr = tptr->parent) { 1065*0Sstevel@tonic-gate if ((tptr->dev == pstatb->st_dev) && 1066*0Sstevel@tonic-gate (tptr->ino == pstatb->st_ino)) { 1067*0Sstevel@tonic-gate /* 1068*0Sstevel@tonic-gate * Cycle detected for this 1069*0Sstevel@tonic-gate * directory. Record the fact 1070*0Sstevel@tonic-gate * it is a cycle so we don't 1071*0Sstevel@tonic-gate * try to process this 1072*0Sstevel@tonic-gate * directory as we are 1073*0Sstevel@tonic-gate * walking through the 1074*0Sstevel@tonic-gate * list of directories. 1075*0Sstevel@tonic-gate */ 1076*0Sstevel@tonic-gate rep->cycle = 1; 1077*0Sstevel@tonic-gate err = 2; 1078*0Sstevel@tonic-gate break; 1079*0Sstevel@tonic-gate } 1080*0Sstevel@tonic-gate } 1081*0Sstevel@tonic-gate } 1082*0Sstevel@tonic-gate } 1083*0Sstevel@tonic-gate } 1084*0Sstevel@tonic-gate 1085*0Sstevel@tonic-gate /* 1086*0Sstevel@tonic-gate * get status of file and recomputes tblocks; 1087*0Sstevel@tonic-gate * argfl = 1 if file is a name in ls-command and = 0 1088*0Sstevel@tonic-gate * for filename in a directory whose name is an 1089*0Sstevel@tonic-gate * argument in the command; 1090*0Sstevel@tonic-gate * stores a pointer in flist[nfiles] and 1091*0Sstevel@tonic-gate * returns that pointer; 1092*0Sstevel@tonic-gate * returns NULL if failed; 1093*0Sstevel@tonic-gate */ 1094*0Sstevel@tonic-gate static struct lbuf * 1095*0Sstevel@tonic-gate gstat(char *file, int argfl, struct ditem *myparent) 1096*0Sstevel@tonic-gate { 1097*0Sstevel@tonic-gate struct stat statb, statb1; 1098*0Sstevel@tonic-gate struct lbuf *rep; 1099*0Sstevel@tonic-gate char buf[BUFSIZ]; 1100*0Sstevel@tonic-gate ssize_t cc; 1101*0Sstevel@tonic-gate int (*statf)() = ((Lflg) || (Hflg && argfl)) ? stat : lstat; 1102*0Sstevel@tonic-gate int aclcnt; 1103*0Sstevel@tonic-gate aclent_t *aclp; 1104*0Sstevel@tonic-gate aclent_t *tp; 1105*0Sstevel@tonic-gate o_mode_t groupperm, mask; 1106*0Sstevel@tonic-gate int grouppermfound, maskfound; 1107*0Sstevel@tonic-gate 1108*0Sstevel@tonic-gate if (nomocore) 1109*0Sstevel@tonic-gate return (NULL); 1110*0Sstevel@tonic-gate 1111*0Sstevel@tonic-gate if (nfiles >= maxfils) { 1112*0Sstevel@tonic-gate /* 1113*0Sstevel@tonic-gate * all flist/lbuf pair assigned files, time to get some 1114*0Sstevel@tonic-gate * more space 1115*0Sstevel@tonic-gate */ 1116*0Sstevel@tonic-gate maxfils += quantn; 1117*0Sstevel@tonic-gate if (((flist = realloc(flist, 1118*0Sstevel@tonic-gate maxfils * sizeof (struct lbuf *))) == NULL) || 1119*0Sstevel@tonic-gate ((nxtlbf = malloc(quantn * 1120*0Sstevel@tonic-gate sizeof (struct lbuf))) == NULL)) { 1121*0Sstevel@tonic-gate perror("ls"); 1122*0Sstevel@tonic-gate nomocore = 1; 1123*0Sstevel@tonic-gate return (NULL); 1124*0Sstevel@tonic-gate } 1125*0Sstevel@tonic-gate } 1126*0Sstevel@tonic-gate 1127*0Sstevel@tonic-gate /* 1128*0Sstevel@tonic-gate * nfiles is reset to nargs for each directory 1129*0Sstevel@tonic-gate * that is given as an argument maxn is checked 1130*0Sstevel@tonic-gate * to prevent the assignment of an lbuf to a flist entry 1131*0Sstevel@tonic-gate * that already has one assigned. 1132*0Sstevel@tonic-gate */ 1133*0Sstevel@tonic-gate if (nfiles >= maxn) { 1134*0Sstevel@tonic-gate rep = nxtlbf++; 1135*0Sstevel@tonic-gate flist[nfiles++] = rep; 1136*0Sstevel@tonic-gate maxn = nfiles; 1137*0Sstevel@tonic-gate } else { 1138*0Sstevel@tonic-gate rep = flist[nfiles++]; 1139*0Sstevel@tonic-gate } 1140*0Sstevel@tonic-gate rep->lflags = (mode_t)0; 1141*0Sstevel@tonic-gate rep->flinkto = NULL; 1142*0Sstevel@tonic-gate rep->cycle = 0; 1143*0Sstevel@tonic-gate if (argfl || statreq) { 1144*0Sstevel@tonic-gate int doacl; 1145*0Sstevel@tonic-gate 1146*0Sstevel@tonic-gate if (lflg) 1147*0Sstevel@tonic-gate doacl = 1; 1148*0Sstevel@tonic-gate else 1149*0Sstevel@tonic-gate doacl = 0; 1150*0Sstevel@tonic-gate if ((*statf)(file, &statb) < 0) { 1151*0Sstevel@tonic-gate if (argfl || errno != ENOENT || 1152*0Sstevel@tonic-gate (Lflg && lstat(file, &statb) == 0)) { 1153*0Sstevel@tonic-gate /* 1154*0Sstevel@tonic-gate * Avoid race between readdir and lstat. 1155*0Sstevel@tonic-gate * Print error message in case of dangling link. 1156*0Sstevel@tonic-gate */ 1157*0Sstevel@tonic-gate perror(file); 1158*0Sstevel@tonic-gate } 1159*0Sstevel@tonic-gate nfiles--; 1160*0Sstevel@tonic-gate return (NULL); 1161*0Sstevel@tonic-gate } 1162*0Sstevel@tonic-gate 1163*0Sstevel@tonic-gate /* 1164*0Sstevel@tonic-gate * If -H was specified, and the file linked to was 1165*0Sstevel@tonic-gate * not a directory, then we need to get the info 1166*0Sstevel@tonic-gate * for the symlink itself. 1167*0Sstevel@tonic-gate */ 1168*0Sstevel@tonic-gate if ((Hflg) && (argfl) && 1169*0Sstevel@tonic-gate ((statb.st_mode & S_IFMT) != S_IFDIR)) { 1170*0Sstevel@tonic-gate if (lstat(file, &statb) < 0) { 1171*0Sstevel@tonic-gate perror(file); 1172*0Sstevel@tonic-gate } 1173*0Sstevel@tonic-gate } 1174*0Sstevel@tonic-gate 1175*0Sstevel@tonic-gate rep->lnum = statb.st_ino; 1176*0Sstevel@tonic-gate rep->lsize = statb.st_size; 1177*0Sstevel@tonic-gate rep->lblocks = statb.st_blocks; 1178*0Sstevel@tonic-gate switch (statb.st_mode & S_IFMT) { 1179*0Sstevel@tonic-gate case S_IFDIR: 1180*0Sstevel@tonic-gate rep->ltype = 'd'; 1181*0Sstevel@tonic-gate if (Rflg) { 1182*0Sstevel@tonic-gate record_ancestry(file, &statb, rep, 1183*0Sstevel@tonic-gate argfl, myparent); 1184*0Sstevel@tonic-gate } 1185*0Sstevel@tonic-gate break; 1186*0Sstevel@tonic-gate case S_IFBLK: 1187*0Sstevel@tonic-gate rep->ltype = 'b'; 1188*0Sstevel@tonic-gate rep->lsize = (off_t)statb.st_rdev; 1189*0Sstevel@tonic-gate break; 1190*0Sstevel@tonic-gate case S_IFCHR: 1191*0Sstevel@tonic-gate rep->ltype = 'c'; 1192*0Sstevel@tonic-gate rep->lsize = (off_t)statb.st_rdev; 1193*0Sstevel@tonic-gate break; 1194*0Sstevel@tonic-gate case S_IFIFO: 1195*0Sstevel@tonic-gate rep->ltype = 'p'; 1196*0Sstevel@tonic-gate break; 1197*0Sstevel@tonic-gate case S_IFSOCK: 1198*0Sstevel@tonic-gate rep->ltype = 's'; 1199*0Sstevel@tonic-gate rep->lsize = 0; 1200*0Sstevel@tonic-gate break; 1201*0Sstevel@tonic-gate case S_IFLNK: 1202*0Sstevel@tonic-gate /* symbolic links may not have ACLs, so elide acl() */ 1203*0Sstevel@tonic-gate if ((Lflg == 0) || (Hflg == 0) || 1204*0Sstevel@tonic-gate ((Hflg) && (!argfl))) { 1205*0Sstevel@tonic-gate doacl = 0; 1206*0Sstevel@tonic-gate } 1207*0Sstevel@tonic-gate rep->ltype = 'l'; 1208*0Sstevel@tonic-gate if (lflg) { 1209*0Sstevel@tonic-gate cc = readlink(file, buf, BUFSIZ); 1210*0Sstevel@tonic-gate if (cc >= 0) { 1211*0Sstevel@tonic-gate 1212*0Sstevel@tonic-gate /* 1213*0Sstevel@tonic-gate * follow the symbolic link 1214*0Sstevel@tonic-gate * to generate the appropriate 1215*0Sstevel@tonic-gate * Fflg marker for the object 1216*0Sstevel@tonic-gate * eg, /bin -> /sym/bin/ 1217*0Sstevel@tonic-gate */ 1218*0Sstevel@tonic-gate if ((Fflg || pflg) && 1219*0Sstevel@tonic-gate (stat(file, &statb1) >= 0)) { 1220*0Sstevel@tonic-gate switch (statb1.st_mode & 1221*0Sstevel@tonic-gate S_IFMT) { 1222*0Sstevel@tonic-gate case S_IFDIR: 1223*0Sstevel@tonic-gate buf[cc++] = '/'; 1224*0Sstevel@tonic-gate break; 1225*0Sstevel@tonic-gate case S_IFSOCK: 1226*0Sstevel@tonic-gate buf[cc++] = '='; 1227*0Sstevel@tonic-gate break; 1228*0Sstevel@tonic-gate default: 1229*0Sstevel@tonic-gate if ((statb1.st_mode & 1230*0Sstevel@tonic-gate ~S_IFMT) & 1231*0Sstevel@tonic-gate (S_IXUSR|S_IXGRP| 1232*0Sstevel@tonic-gate S_IXOTH)) 1233*0Sstevel@tonic-gate buf[cc++] = '*'; 1234*0Sstevel@tonic-gate break; 1235*0Sstevel@tonic-gate } 1236*0Sstevel@tonic-gate } 1237*0Sstevel@tonic-gate buf[cc] = '\0'; 1238*0Sstevel@tonic-gate rep->flinkto = strdup(buf); 1239*0Sstevel@tonic-gate } 1240*0Sstevel@tonic-gate break; 1241*0Sstevel@tonic-gate } 1242*0Sstevel@tonic-gate 1243*0Sstevel@tonic-gate /* 1244*0Sstevel@tonic-gate * ls /sym behaves differently from ls /sym/ 1245*0Sstevel@tonic-gate * when /sym is a symbolic link. This is fixed 1246*0Sstevel@tonic-gate * when explicit arguments are specified. 1247*0Sstevel@tonic-gate */ 1248*0Sstevel@tonic-gate 1249*0Sstevel@tonic-gate #ifdef XPG6 1250*0Sstevel@tonic-gate /* Do not follow a symlink when -F is specified */ 1251*0Sstevel@tonic-gate if ((!argfl) || (argfl && Fflg) || 1252*0Sstevel@tonic-gate (stat(file, &statb1) < 0)) 1253*0Sstevel@tonic-gate #else 1254*0Sstevel@tonic-gate /* Follow a symlink when -F is specified */ 1255*0Sstevel@tonic-gate if (!argfl || stat(file, &statb1) < 0) 1256*0Sstevel@tonic-gate #endif /* XPG6 */ 1257*0Sstevel@tonic-gate break; 1258*0Sstevel@tonic-gate if ((statb1.st_mode & S_IFMT) == S_IFDIR) { 1259*0Sstevel@tonic-gate statb = statb1; 1260*0Sstevel@tonic-gate rep->ltype = 'd'; 1261*0Sstevel@tonic-gate rep->lsize = statb1.st_size; 1262*0Sstevel@tonic-gate if (Rflg) { 1263*0Sstevel@tonic-gate record_ancestry(file, &statb, rep, 1264*0Sstevel@tonic-gate argfl, myparent); 1265*0Sstevel@tonic-gate } 1266*0Sstevel@tonic-gate } 1267*0Sstevel@tonic-gate break; 1268*0Sstevel@tonic-gate case S_IFDOOR: 1269*0Sstevel@tonic-gate rep->ltype = 'D'; 1270*0Sstevel@tonic-gate break; 1271*0Sstevel@tonic-gate case S_IFREG: 1272*0Sstevel@tonic-gate rep->ltype = '-'; 1273*0Sstevel@tonic-gate break; 1274*0Sstevel@tonic-gate case S_IFPORT: 1275*0Sstevel@tonic-gate rep->ltype = 'P'; 1276*0Sstevel@tonic-gate break; 1277*0Sstevel@tonic-gate default: 1278*0Sstevel@tonic-gate rep->ltype = '?'; 1279*0Sstevel@tonic-gate break; 1280*0Sstevel@tonic-gate } 1281*0Sstevel@tonic-gate rep->lflags = statb.st_mode & ~S_IFMT; 1282*0Sstevel@tonic-gate 1283*0Sstevel@tonic-gate if (!S_ISREG(statb.st_mode)) 1284*0Sstevel@tonic-gate rep->lflags |= LS_NOTREG; 1285*0Sstevel@tonic-gate 1286*0Sstevel@tonic-gate /* ACL: check acl entries count */ 1287*0Sstevel@tonic-gate if (doacl) { 1288*0Sstevel@tonic-gate rep->acl = ' '; 1289*0Sstevel@tonic-gate if ((aclcnt = acl(file, GETACLCNT, 0, NULL)) > 1290*0Sstevel@tonic-gate MIN_ACL_ENTRIES) { 1291*0Sstevel@tonic-gate 1292*0Sstevel@tonic-gate /* this file has a non-trivial acl */ 1293*0Sstevel@tonic-gate 1294*0Sstevel@tonic-gate rep->acl = '+'; 1295*0Sstevel@tonic-gate 1296*0Sstevel@tonic-gate /* 1297*0Sstevel@tonic-gate * For files with non-trivial acls, the 1298*0Sstevel@tonic-gate * effective group permissions are the 1299*0Sstevel@tonic-gate * intersection of the GROUP_OBJ value and 1300*0Sstevel@tonic-gate * the CLASS_OBJ (acl mask) value. Determine 1301*0Sstevel@tonic-gate * both the GROUP_OBJ and CLASS_OBJ for this 1302*0Sstevel@tonic-gate * file and insert the logical AND of those 1303*0Sstevel@tonic-gate * two values in the group permissions field 1304*0Sstevel@tonic-gate * of the lflags value for this file. 1305*0Sstevel@tonic-gate */ 1306*0Sstevel@tonic-gate 1307*0Sstevel@tonic-gate if ((aclp = (aclent_t *)malloc( 1308*0Sstevel@tonic-gate (sizeof (aclent_t)) * aclcnt)) == NULL) { 1309*0Sstevel@tonic-gate perror("ls"); 1310*0Sstevel@tonic-gate exit(2); 1311*0Sstevel@tonic-gate } 1312*0Sstevel@tonic-gate 1313*0Sstevel@tonic-gate if (acl(file, GETACL, aclcnt, aclp) < 0) { 1314*0Sstevel@tonic-gate free(aclp); 1315*0Sstevel@tonic-gate (void) fprintf(stderr, "ls: "); 1316*0Sstevel@tonic-gate perror(file); 1317*0Sstevel@tonic-gate nfiles--; 1318*0Sstevel@tonic-gate err = 2; 1319*0Sstevel@tonic-gate return (NULL); 1320*0Sstevel@tonic-gate } 1321*0Sstevel@tonic-gate 1322*0Sstevel@tonic-gate /* 1323*0Sstevel@tonic-gate * Until found in acl list, assume maximum 1324*0Sstevel@tonic-gate * permissions for both group and mask. (Just 1325*0Sstevel@tonic-gate * in case the acl lacks either value for 1326*0Sstevel@tonic-gate * some reason.) 1327*0Sstevel@tonic-gate */ 1328*0Sstevel@tonic-gate groupperm = 07; 1329*0Sstevel@tonic-gate mask = 07; 1330*0Sstevel@tonic-gate grouppermfound = 0; 1331*0Sstevel@tonic-gate maskfound = 0; 1332*0Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) { 1333*0Sstevel@tonic-gate if (tp->a_type == GROUP_OBJ) { 1334*0Sstevel@tonic-gate groupperm = tp->a_perm; 1335*0Sstevel@tonic-gate grouppermfound = 1; 1336*0Sstevel@tonic-gate continue; 1337*0Sstevel@tonic-gate } 1338*0Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ) { 1339*0Sstevel@tonic-gate mask = tp->a_perm; 1340*0Sstevel@tonic-gate maskfound = 1; 1341*0Sstevel@tonic-gate } 1342*0Sstevel@tonic-gate if (grouppermfound && maskfound) 1343*0Sstevel@tonic-gate break; 1344*0Sstevel@tonic-gate } 1345*0Sstevel@tonic-gate 1346*0Sstevel@tonic-gate free(aclp); 1347*0Sstevel@tonic-gate 1348*0Sstevel@tonic-gate /* reset all the group bits */ 1349*0Sstevel@tonic-gate rep->lflags &= ~S_IRWXG; 1350*0Sstevel@tonic-gate 1351*0Sstevel@tonic-gate /* 1352*0Sstevel@tonic-gate * Now set them to the logical AND of the 1353*0Sstevel@tonic-gate * GROUP_OBJ permissions and the acl mask. 1354*0Sstevel@tonic-gate */ 1355*0Sstevel@tonic-gate 1356*0Sstevel@tonic-gate rep->lflags |= (groupperm & mask) << 3; 1357*0Sstevel@tonic-gate } 1358*0Sstevel@tonic-gate 1359*0Sstevel@tonic-gate if (atflg && pathconf(file, _PC_XATTR_EXISTS) == 1) 1360*0Sstevel@tonic-gate rep->acl = '@'; 1361*0Sstevel@tonic-gate } else 1362*0Sstevel@tonic-gate rep->acl = ' '; 1363*0Sstevel@tonic-gate 1364*0Sstevel@tonic-gate /* mask ISARG and other file-type bits */ 1365*0Sstevel@tonic-gate 1366*0Sstevel@tonic-gate rep->luid = statb.st_uid; 1367*0Sstevel@tonic-gate rep->lgid = statb.st_gid; 1368*0Sstevel@tonic-gate rep->lnl = statb.st_nlink; 1369*0Sstevel@tonic-gate if (uflg) 1370*0Sstevel@tonic-gate rep->lmtime = statb.st_atim; 1371*0Sstevel@tonic-gate else if (cflg) 1372*0Sstevel@tonic-gate rep->lmtime = statb.st_ctim; 1373*0Sstevel@tonic-gate else 1374*0Sstevel@tonic-gate rep->lmtime = statb.st_mtim; 1375*0Sstevel@tonic-gate 1376*0Sstevel@tonic-gate if (rep->ltype != 'b' && rep->ltype != 'c') 1377*0Sstevel@tonic-gate tblocks += rep->lblocks; 1378*0Sstevel@tonic-gate } 1379*0Sstevel@tonic-gate return (rep); 1380*0Sstevel@tonic-gate } 1381*0Sstevel@tonic-gate 1382*0Sstevel@tonic-gate /* 1383*0Sstevel@tonic-gate * returns pathname of the form dir/file; 1384*0Sstevel@tonic-gate * dir and file are null-terminated strings. 1385*0Sstevel@tonic-gate */ 1386*0Sstevel@tonic-gate static char * 1387*0Sstevel@tonic-gate makename(char *dir, char *file) 1388*0Sstevel@tonic-gate { 1389*0Sstevel@tonic-gate /* 1390*0Sstevel@tonic-gate * PATH_MAX is the maximum length of a path name. 1391*0Sstevel@tonic-gate * MAXNAMLEN is the maximum length of any path name component. 1392*0Sstevel@tonic-gate * Allocate space for both, plus the '/' in the middle 1393*0Sstevel@tonic-gate * and the null character at the end. 1394*0Sstevel@tonic-gate * dfile is static as this is returned by makename(). 1395*0Sstevel@tonic-gate */ 1396*0Sstevel@tonic-gate static char dfile[PATH_MAX + 1 + MAXNAMLEN + 1]; 1397*0Sstevel@tonic-gate char *dp, *fp; 1398*0Sstevel@tonic-gate 1399*0Sstevel@tonic-gate dp = dfile; 1400*0Sstevel@tonic-gate fp = dir; 1401*0Sstevel@tonic-gate while (*fp) 1402*0Sstevel@tonic-gate *dp++ = *fp++; 1403*0Sstevel@tonic-gate if (dp > dfile && *(dp - 1) != '/') 1404*0Sstevel@tonic-gate *dp++ = '/'; 1405*0Sstevel@tonic-gate fp = file; 1406*0Sstevel@tonic-gate while (*fp) 1407*0Sstevel@tonic-gate *dp++ = *fp++; 1408*0Sstevel@tonic-gate *dp = '\0'; 1409*0Sstevel@tonic-gate return (dfile); 1410*0Sstevel@tonic-gate } 1411*0Sstevel@tonic-gate 1412*0Sstevel@tonic-gate 1413*0Sstevel@tonic-gate #include <pwd.h> 1414*0Sstevel@tonic-gate #include <grp.h> 1415*0Sstevel@tonic-gate #include <utmpx.h> 1416*0Sstevel@tonic-gate 1417*0Sstevel@tonic-gate struct utmpx utmp; 1418*0Sstevel@tonic-gate 1419*0Sstevel@tonic-gate #define NMAX (sizeof (utmp.ut_name)) 1420*0Sstevel@tonic-gate #define SCPYN(a, b) (void) strncpy(a, b, NMAX) 1421*0Sstevel@tonic-gate 1422*0Sstevel@tonic-gate 1423*0Sstevel@tonic-gate struct cachenode { /* this struct must be zeroed before using */ 1424*0Sstevel@tonic-gate struct cachenode *lesschild; /* subtree whose entries < val */ 1425*0Sstevel@tonic-gate struct cachenode *grtrchild; /* subtree whose entries > val */ 1426*0Sstevel@tonic-gate long val; /* the uid or gid of this entry */ 1427*0Sstevel@tonic-gate int initted; /* name has been filled in */ 1428*0Sstevel@tonic-gate char name[NMAX+1]; /* the string that val maps to */ 1429*0Sstevel@tonic-gate }; 1430*0Sstevel@tonic-gate static struct cachenode *names, *groups; 1431*0Sstevel@tonic-gate 1432*0Sstevel@tonic-gate static struct cachenode * 1433*0Sstevel@tonic-gate findincache(struct cachenode **head, long val) 1434*0Sstevel@tonic-gate { 1435*0Sstevel@tonic-gate struct cachenode **parent = head; 1436*0Sstevel@tonic-gate struct cachenode *c = *parent; 1437*0Sstevel@tonic-gate 1438*0Sstevel@tonic-gate while (c != NULL) { 1439*0Sstevel@tonic-gate if (val == c->val) { 1440*0Sstevel@tonic-gate /* found it */ 1441*0Sstevel@tonic-gate return (c); 1442*0Sstevel@tonic-gate } else if (val < c->val) { 1443*0Sstevel@tonic-gate parent = &c->lesschild; 1444*0Sstevel@tonic-gate c = c->lesschild; 1445*0Sstevel@tonic-gate } else { 1446*0Sstevel@tonic-gate parent = &c->grtrchild; 1447*0Sstevel@tonic-gate c = c->grtrchild; 1448*0Sstevel@tonic-gate } 1449*0Sstevel@tonic-gate } 1450*0Sstevel@tonic-gate 1451*0Sstevel@tonic-gate /* not in the cache, make a new entry for it */ 1452*0Sstevel@tonic-gate c = calloc(1, sizeof (struct cachenode)); 1453*0Sstevel@tonic-gate if (c == NULL) { 1454*0Sstevel@tonic-gate perror("ls"); 1455*0Sstevel@tonic-gate exit(2); 1456*0Sstevel@tonic-gate } 1457*0Sstevel@tonic-gate *parent = c; 1458*0Sstevel@tonic-gate c->val = val; 1459*0Sstevel@tonic-gate return (c); 1460*0Sstevel@tonic-gate } 1461*0Sstevel@tonic-gate 1462*0Sstevel@tonic-gate /* 1463*0Sstevel@tonic-gate * get name from cache, or passwd file for a given uid; 1464*0Sstevel@tonic-gate * lastuid is set to uid. 1465*0Sstevel@tonic-gate */ 1466*0Sstevel@tonic-gate static char * 1467*0Sstevel@tonic-gate getname(uid_t uid) 1468*0Sstevel@tonic-gate { 1469*0Sstevel@tonic-gate struct passwd *pwent; 1470*0Sstevel@tonic-gate struct cachenode *c; 1471*0Sstevel@tonic-gate 1472*0Sstevel@tonic-gate if ((uid == lastuid) && lastuname) 1473*0Sstevel@tonic-gate return (lastuname); 1474*0Sstevel@tonic-gate 1475*0Sstevel@tonic-gate c = findincache(&names, uid); 1476*0Sstevel@tonic-gate if (c->initted == 0) { 1477*0Sstevel@tonic-gate if ((pwent = getpwuid(uid)) != NULL) { 1478*0Sstevel@tonic-gate SCPYN(&c->name[0], pwent->pw_name); 1479*0Sstevel@tonic-gate } else { 1480*0Sstevel@tonic-gate (void) sprintf(&c->name[0], "%-8u", (int)uid); 1481*0Sstevel@tonic-gate } 1482*0Sstevel@tonic-gate c->initted = 1; 1483*0Sstevel@tonic-gate } 1484*0Sstevel@tonic-gate lastuid = uid; 1485*0Sstevel@tonic-gate lastuname = &c->name[0]; 1486*0Sstevel@tonic-gate return (lastuname); 1487*0Sstevel@tonic-gate } 1488*0Sstevel@tonic-gate 1489*0Sstevel@tonic-gate /* 1490*0Sstevel@tonic-gate * get name from cache, or group file for a given gid; 1491*0Sstevel@tonic-gate * lastgid is set to gid. 1492*0Sstevel@tonic-gate */ 1493*0Sstevel@tonic-gate static char * 1494*0Sstevel@tonic-gate getgroup(gid_t gid) 1495*0Sstevel@tonic-gate { 1496*0Sstevel@tonic-gate struct group *grent; 1497*0Sstevel@tonic-gate struct cachenode *c; 1498*0Sstevel@tonic-gate 1499*0Sstevel@tonic-gate if ((gid == lastgid) && lastgname) 1500*0Sstevel@tonic-gate return (lastgname); 1501*0Sstevel@tonic-gate 1502*0Sstevel@tonic-gate c = findincache(&groups, gid); 1503*0Sstevel@tonic-gate if (c->initted == 0) { 1504*0Sstevel@tonic-gate if ((grent = getgrgid(gid)) != NULL) { 1505*0Sstevel@tonic-gate SCPYN(&c->name[0], grent->gr_name); 1506*0Sstevel@tonic-gate } else { 1507*0Sstevel@tonic-gate (void) sprintf(&c->name[0], "%-8u", (int)gid); 1508*0Sstevel@tonic-gate } 1509*0Sstevel@tonic-gate c->initted = 1; 1510*0Sstevel@tonic-gate } 1511*0Sstevel@tonic-gate lastgid = gid; 1512*0Sstevel@tonic-gate lastgname = &c->name[0]; 1513*0Sstevel@tonic-gate return (lastgname); 1514*0Sstevel@tonic-gate } 1515*0Sstevel@tonic-gate 1516*0Sstevel@tonic-gate /* return >0 if item pointed by pp2 should appear first */ 1517*0Sstevel@tonic-gate static int 1518*0Sstevel@tonic-gate compar(struct lbuf **pp1, struct lbuf **pp2) 1519*0Sstevel@tonic-gate { 1520*0Sstevel@tonic-gate struct lbuf *p1, *p2; 1521*0Sstevel@tonic-gate 1522*0Sstevel@tonic-gate p1 = *pp1; 1523*0Sstevel@tonic-gate p2 = *pp2; 1524*0Sstevel@tonic-gate if (dflg == 0) { 1525*0Sstevel@tonic-gate /* 1526*0Sstevel@tonic-gate * compare two names in ls-command one of which is file 1527*0Sstevel@tonic-gate * and the other is a directory; 1528*0Sstevel@tonic-gate * this portion is not used for comparing files within 1529*0Sstevel@tonic-gate * a directory name of ls-command; 1530*0Sstevel@tonic-gate */ 1531*0Sstevel@tonic-gate if (p1->lflags&ISARG && p1->ltype == 'd') { 1532*0Sstevel@tonic-gate if (!(p2->lflags&ISARG && p2->ltype == 'd')) 1533*0Sstevel@tonic-gate return (1); 1534*0Sstevel@tonic-gate } else { 1535*0Sstevel@tonic-gate if (p2->lflags&ISARG && p2->ltype == 'd') 1536*0Sstevel@tonic-gate return (-1); 1537*0Sstevel@tonic-gate } 1538*0Sstevel@tonic-gate } 1539*0Sstevel@tonic-gate if (tflg) { 1540*0Sstevel@tonic-gate if (p2->lmtime.tv_sec > p1->lmtime.tv_sec) 1541*0Sstevel@tonic-gate return (rflg); 1542*0Sstevel@tonic-gate else if (p2->lmtime.tv_sec < p1->lmtime.tv_sec) 1543*0Sstevel@tonic-gate return (-rflg); 1544*0Sstevel@tonic-gate /* times are equal to the sec, check nsec */ 1545*0Sstevel@tonic-gate if (p2->lmtime.tv_nsec > p1->lmtime.tv_nsec) 1546*0Sstevel@tonic-gate return (rflg); 1547*0Sstevel@tonic-gate else if (p2->lmtime.tv_nsec < p1->lmtime.tv_nsec) 1548*0Sstevel@tonic-gate return (-rflg); 1549*0Sstevel@tonic-gate /* if times are equal, fall through and sort by name */ 1550*0Sstevel@tonic-gate } else if (Sflg) { 1551*0Sstevel@tonic-gate /* 1552*0Sstevel@tonic-gate * The size stored in lsize can be either the 1553*0Sstevel@tonic-gate * size or the major minor number (in the case of 1554*0Sstevel@tonic-gate * block and character special devices). If it's 1555*0Sstevel@tonic-gate * a major minor number, then the size is considered 1556*0Sstevel@tonic-gate * to be zero and we want to fall through and sort 1557*0Sstevel@tonic-gate * by name. In addition, if the size of p2 is equal 1558*0Sstevel@tonic-gate * to the size of p1 we want to fall through and 1559*0Sstevel@tonic-gate * sort by name. 1560*0Sstevel@tonic-gate */ 1561*0Sstevel@tonic-gate off_t p1size = (p1->ltype == 'b') || 1562*0Sstevel@tonic-gate (p1->ltype == 'c') ? 0 : p1->lsize; 1563*0Sstevel@tonic-gate off_t p2size = (p2->ltype == 'b') || 1564*0Sstevel@tonic-gate (p2->ltype == 'c') ? 0 : p2->lsize; 1565*0Sstevel@tonic-gate if (p2size > p1size) { 1566*0Sstevel@tonic-gate return (rflg); 1567*0Sstevel@tonic-gate } else if (p2size < p1size) { 1568*0Sstevel@tonic-gate return (-rflg); 1569*0Sstevel@tonic-gate } 1570*0Sstevel@tonic-gate /* Sizes are equal, fall through and sort by name. */ 1571*0Sstevel@tonic-gate } 1572*0Sstevel@tonic-gate return (rflg * strcoll( 1573*0Sstevel@tonic-gate p1->lflags & ISARG ? p1->ln.namep : p1->ln.lname, 1574*0Sstevel@tonic-gate p2->lflags&ISARG ? p2->ln.namep : p2->ln.lname)); 1575*0Sstevel@tonic-gate } 1576*0Sstevel@tonic-gate 1577*0Sstevel@tonic-gate static void 1578*0Sstevel@tonic-gate pprintf(char *s1, char *s2) 1579*0Sstevel@tonic-gate { 1580*0Sstevel@tonic-gate csi_pprintf((unsigned char *)s1); 1581*0Sstevel@tonic-gate csi_pprintf((unsigned char *)s2); 1582*0Sstevel@tonic-gate } 1583*0Sstevel@tonic-gate 1584*0Sstevel@tonic-gate static void 1585*0Sstevel@tonic-gate csi_pprintf(unsigned char *s) 1586*0Sstevel@tonic-gate { 1587*0Sstevel@tonic-gate unsigned char *cp; 1588*0Sstevel@tonic-gate char c; 1589*0Sstevel@tonic-gate int i; 1590*0Sstevel@tonic-gate int c_len; 1591*0Sstevel@tonic-gate int p_col; 1592*0Sstevel@tonic-gate wchar_t pcode; 1593*0Sstevel@tonic-gate 1594*0Sstevel@tonic-gate if (!qflg && !bflg) { 1595*0Sstevel@tonic-gate for (cp = s; *cp != '\0'; cp++) { 1596*0Sstevel@tonic-gate (void) putchar(*cp); 1597*0Sstevel@tonic-gate curcol++; 1598*0Sstevel@tonic-gate } 1599*0Sstevel@tonic-gate return; 1600*0Sstevel@tonic-gate } 1601*0Sstevel@tonic-gate 1602*0Sstevel@tonic-gate for (cp = s; *cp; ) { 1603*0Sstevel@tonic-gate if (isascii(c = *cp)) { 1604*0Sstevel@tonic-gate if (!isprint(c)) { 1605*0Sstevel@tonic-gate if (qflg) { 1606*0Sstevel@tonic-gate c = '?'; 1607*0Sstevel@tonic-gate } else { 1608*0Sstevel@tonic-gate curcol += 3; 1609*0Sstevel@tonic-gate (void) putc('\\', stdout); 1610*0Sstevel@tonic-gate c = '0' + ((*cp >> 6) & 07); 1611*0Sstevel@tonic-gate (void) putc(c, stdout); 1612*0Sstevel@tonic-gate c = '0' + ((*cp >> 3) & 07); 1613*0Sstevel@tonic-gate (void) putc(c, stdout); 1614*0Sstevel@tonic-gate c = '0' + (*cp & 07); 1615*0Sstevel@tonic-gate } 1616*0Sstevel@tonic-gate } 1617*0Sstevel@tonic-gate curcol++; 1618*0Sstevel@tonic-gate cp++; 1619*0Sstevel@tonic-gate (void) putc(c, stdout); 1620*0Sstevel@tonic-gate continue; 1621*0Sstevel@tonic-gate } 1622*0Sstevel@tonic-gate 1623*0Sstevel@tonic-gate if ((c_len = mbtowc(&pcode, (char *)cp, MB_LEN_MAX)) <= 0) { 1624*0Sstevel@tonic-gate c_len = 1; 1625*0Sstevel@tonic-gate goto not_print; 1626*0Sstevel@tonic-gate } 1627*0Sstevel@tonic-gate 1628*0Sstevel@tonic-gate if ((p_col = wcwidth(pcode)) > 0) { 1629*0Sstevel@tonic-gate (void) putwchar(pcode); 1630*0Sstevel@tonic-gate cp += c_len; 1631*0Sstevel@tonic-gate curcol += p_col; 1632*0Sstevel@tonic-gate continue; 1633*0Sstevel@tonic-gate } 1634*0Sstevel@tonic-gate 1635*0Sstevel@tonic-gate not_print: 1636*0Sstevel@tonic-gate for (i = 0; i < c_len; i++) { 1637*0Sstevel@tonic-gate if (qflg) { 1638*0Sstevel@tonic-gate c = '?'; 1639*0Sstevel@tonic-gate } else { 1640*0Sstevel@tonic-gate curcol += 3; 1641*0Sstevel@tonic-gate (void) putc('\\', stdout); 1642*0Sstevel@tonic-gate c = '0' + ((*cp >> 6) & 07); 1643*0Sstevel@tonic-gate (void) putc(c, stdout); 1644*0Sstevel@tonic-gate c = '0' + ((*cp >> 3) & 07); 1645*0Sstevel@tonic-gate (void) putc(c, stdout); 1646*0Sstevel@tonic-gate c = '0' + (*cp & 07); 1647*0Sstevel@tonic-gate } 1648*0Sstevel@tonic-gate curcol++; 1649*0Sstevel@tonic-gate (void) putc(c, stdout); 1650*0Sstevel@tonic-gate cp++; 1651*0Sstevel@tonic-gate } 1652*0Sstevel@tonic-gate } 1653*0Sstevel@tonic-gate } 1654*0Sstevel@tonic-gate 1655*0Sstevel@tonic-gate static int 1656*0Sstevel@tonic-gate strcol(unsigned char *s1) 1657*0Sstevel@tonic-gate { 1658*0Sstevel@tonic-gate int w; 1659*0Sstevel@tonic-gate int w_col; 1660*0Sstevel@tonic-gate int len; 1661*0Sstevel@tonic-gate wchar_t wc; 1662*0Sstevel@tonic-gate 1663*0Sstevel@tonic-gate w = 0; 1664*0Sstevel@tonic-gate while (*s1) { 1665*0Sstevel@tonic-gate if (isascii(*s1)) { 1666*0Sstevel@tonic-gate w++; 1667*0Sstevel@tonic-gate s1++; 1668*0Sstevel@tonic-gate continue; 1669*0Sstevel@tonic-gate } 1670*0Sstevel@tonic-gate 1671*0Sstevel@tonic-gate if ((len = mbtowc(&wc, (char *)s1, MB_LEN_MAX)) <= 0) { 1672*0Sstevel@tonic-gate w++; 1673*0Sstevel@tonic-gate s1++; 1674*0Sstevel@tonic-gate continue; 1675*0Sstevel@tonic-gate } 1676*0Sstevel@tonic-gate 1677*0Sstevel@tonic-gate if ((w_col = wcwidth(wc)) < 0) 1678*0Sstevel@tonic-gate w_col = len; 1679*0Sstevel@tonic-gate s1 += len; 1680*0Sstevel@tonic-gate w += w_col; 1681*0Sstevel@tonic-gate } 1682*0Sstevel@tonic-gate return (w); 1683*0Sstevel@tonic-gate } 1684*0Sstevel@tonic-gate 1685*0Sstevel@tonic-gate /* 1686*0Sstevel@tonic-gate * Convert an unsigned long long to a string representation and place the 1687*0Sstevel@tonic-gate * result in the caller-supplied buffer. 1688*0Sstevel@tonic-gate * 1689*0Sstevel@tonic-gate * The number provided is a size in bytes. The number is first 1690*0Sstevel@tonic-gate * converted to an integral multiple of 'scale' bytes. This new 1691*0Sstevel@tonic-gate * number is then scaled down until it is small enough to be in a good 1692*0Sstevel@tonic-gate * human readable format, i.e. in the range 0 thru scale-1. If the 1693*0Sstevel@tonic-gate * number used to derive the final number is not a multiple of scale, and 1694*0Sstevel@tonic-gate * the final number has only a single significant digit, we compute 1695*0Sstevel@tonic-gate * tenths of units to provide a second significant digit. 1696*0Sstevel@tonic-gate * 1697*0Sstevel@tonic-gate * The value "(unsigned long long)-1" is a special case and is always 1698*0Sstevel@tonic-gate * converted to "-1". 1699*0Sstevel@tonic-gate * 1700*0Sstevel@tonic-gate * A pointer to the caller-supplied buffer is returned. 1701*0Sstevel@tonic-gate */ 1702*0Sstevel@tonic-gate static char * 1703*0Sstevel@tonic-gate number_to_scaled_string( 1704*0Sstevel@tonic-gate numbuf_t buf, /* put the result here */ 1705*0Sstevel@tonic-gate unsigned long long number, /* convert this number */ 1706*0Sstevel@tonic-gate long scale) 1707*0Sstevel@tonic-gate { 1708*0Sstevel@tonic-gate unsigned long long save; 1709*0Sstevel@tonic-gate /* Measurement: kilo, mega, giga, tera, peta, exa */ 1710*0Sstevel@tonic-gate char *uom = "KMGTPE"; 1711*0Sstevel@tonic-gate 1712*0Sstevel@tonic-gate if ((long long)number == (long long)-1) { 1713*0Sstevel@tonic-gate (void) strlcpy(buf, "-1", sizeof (numbuf_t)); 1714*0Sstevel@tonic-gate return (buf); 1715*0Sstevel@tonic-gate } 1716*0Sstevel@tonic-gate 1717*0Sstevel@tonic-gate save = number; 1718*0Sstevel@tonic-gate number = number / scale; 1719*0Sstevel@tonic-gate 1720*0Sstevel@tonic-gate /* 1721*0Sstevel@tonic-gate * Now we have number as a count of scale units. 1722*0Sstevel@tonic-gate * If no further scaling is necessary, we round up as appropriate. 1723*0Sstevel@tonic-gate * 1724*0Sstevel@tonic-gate * The largest value number could have had entering the routine is 1725*0Sstevel@tonic-gate * 16 Exabytes, so running off the end of the uom array should 1726*0Sstevel@tonic-gate * never happen. We check for that, though, as a guard against 1727*0Sstevel@tonic-gate * a breakdown elsewhere in the algorithm. 1728*0Sstevel@tonic-gate */ 1729*0Sstevel@tonic-gate if (number < (unsigned long long)scale) { 1730*0Sstevel@tonic-gate if ((save % scale) >= (unsigned long long)(scale / 2)) { 1731*0Sstevel@tonic-gate if (++number == (unsigned long long)scale) { 1732*0Sstevel@tonic-gate uom++; 1733*0Sstevel@tonic-gate number = 1; 1734*0Sstevel@tonic-gate } 1735*0Sstevel@tonic-gate } 1736*0Sstevel@tonic-gate } else { 1737*0Sstevel@tonic-gate while ((number >= (unsigned long long)scale) && (*uom != 'E')) { 1738*0Sstevel@tonic-gate uom++; /* next unit of measurement */ 1739*0Sstevel@tonic-gate save = number; 1740*0Sstevel@tonic-gate /* 1741*0Sstevel@tonic-gate * If we're over half way to the next unit of 1742*0Sstevel@tonic-gate * 'scale' bytes (which means we should round 1743*0Sstevel@tonic-gate * up), then adding half of 'scale' prior to 1744*0Sstevel@tonic-gate * the division will push us into that next 1745*0Sstevel@tonic-gate * unit of scale when we perform the division 1746*0Sstevel@tonic-gate */ 1747*0Sstevel@tonic-gate number = (number + (scale / 2)) / scale; 1748*0Sstevel@tonic-gate } 1749*0Sstevel@tonic-gate } 1750*0Sstevel@tonic-gate 1751*0Sstevel@tonic-gate /* check if we should output a decimal place after the point */ 1752*0Sstevel@tonic-gate if ((save / scale) < 10) { 1753*0Sstevel@tonic-gate /* snprintf() will round for us */ 1754*0Sstevel@tonic-gate float fnum = (float)save / scale; 1755*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (numbuf_t), "%2.1f%c", 1756*0Sstevel@tonic-gate fnum, *uom); 1757*0Sstevel@tonic-gate } else { 1758*0Sstevel@tonic-gate (void) snprintf(buf, sizeof (numbuf_t), "%4llu%c", 1759*0Sstevel@tonic-gate number, *uom); 1760*0Sstevel@tonic-gate } 1761*0Sstevel@tonic-gate return (buf); 1762*0Sstevel@tonic-gate } 1763