1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * Copyright 1997 Sun Microsystems, Inc. All rights reserved. 3*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 4*7c478bd9Sstevel@tonic-gate */ 5*7c478bd9Sstevel@tonic-gate 6*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 7*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 8*7c478bd9Sstevel@tonic-gate 9*7c478bd9Sstevel@tonic-gate /* 10*7c478bd9Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California. 11*7c478bd9Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement 12*7c478bd9Sstevel@tonic-gate * specifies the terms and conditions for redistribution. 13*7c478bd9Sstevel@tonic-gate */ 14*7c478bd9Sstevel@tonic-gate 15*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 16*7c478bd9Sstevel@tonic-gate 17*7c478bd9Sstevel@tonic-gate /* 18*7c478bd9Sstevel@tonic-gate * ls 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * 4.2bsd version for symbolic links, variable length 21*7c478bd9Sstevel@tonic-gate * directory entries, block size in the inode, etc. 22*7c478bd9Sstevel@tonic-gate */ 23*7c478bd9Sstevel@tonic-gate 24*7c478bd9Sstevel@tonic-gate #include <stdio.h> 25*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 26*7c478bd9Sstevel@tonic-gate #include <unistd.h> 27*7c478bd9Sstevel@tonic-gate #include <string.h> 28*7c478bd9Sstevel@tonic-gate #include <stddef.h> 29*7c478bd9Sstevel@tonic-gate #include <dirent.h> 30*7c478bd9Sstevel@tonic-gate #include <ctype.h> 31*7c478bd9Sstevel@tonic-gate #include <time.h> 32*7c478bd9Sstevel@tonic-gate #include <limits.h> 33*7c478bd9Sstevel@tonic-gate #include <locale.h> 34*7c478bd9Sstevel@tonic-gate #include <errno.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 36*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 38*7c478bd9Sstevel@tonic-gate #include <sys/termios.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/acl.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #define dbtokb(nb) ((nb) / (1024 / DEV_BSIZE)) 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate struct afile { 45*7c478bd9Sstevel@tonic-gate char ftype; /* file type, e.g. 'd', 'c', 'f' */ 46*7c478bd9Sstevel@tonic-gate ino_t fnum; /* inode number of file */ 47*7c478bd9Sstevel@tonic-gate short fflags; /* mode&~S_IFMT, perhaps ISARG */ 48*7c478bd9Sstevel@tonic-gate nlink_t fnl; /* number of links */ 49*7c478bd9Sstevel@tonic-gate uid_t fuid; /* owner id */ 50*7c478bd9Sstevel@tonic-gate gid_t fgid; /* group id */ 51*7c478bd9Sstevel@tonic-gate off_t fsize; /* file size */ 52*7c478bd9Sstevel@tonic-gate blkcnt_t fblks; /* number of blocks used */ 53*7c478bd9Sstevel@tonic-gate time_t fmtime; /* time (modify or access or create) */ 54*7c478bd9Sstevel@tonic-gate char *fname; /* file name */ 55*7c478bd9Sstevel@tonic-gate char *flinkto; /* symbolic link value */ 56*7c478bd9Sstevel@tonic-gate char acl; /* acl access flag */ 57*7c478bd9Sstevel@tonic-gate }; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate #define ISARG 0x8000 /* extra ``mode'' */ 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate static struct subdirs { 62*7c478bd9Sstevel@tonic-gate char *sd_name; 63*7c478bd9Sstevel@tonic-gate struct subdirs *sd_next; 64*7c478bd9Sstevel@tonic-gate } *subdirs; 65*7c478bd9Sstevel@tonic-gate 66*7c478bd9Sstevel@tonic-gate static int aflg, dflg, gflg, lflg, sflg, tflg, uflg, iflg, fflg, cflg; 67*7c478bd9Sstevel@tonic-gate static int rflg = 1; 68*7c478bd9Sstevel@tonic-gate static int qflg, Aflg, Cflg, Fflg, Lflg, Rflg; 69*7c478bd9Sstevel@tonic-gate 70*7c478bd9Sstevel@tonic-gate static int usetabs; 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate static time_t now, sixmonthsago, onehourfromnow; 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate static char *dotp = "."; 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate static struct winsize win; 77*7c478bd9Sstevel@tonic-gate static int twidth; 78*7c478bd9Sstevel@tonic-gate 79*7c478bd9Sstevel@tonic-gate static struct afile *gstat(struct afile *, char *, int, off_t *); 80*7c478bd9Sstevel@tonic-gate static int fcmp(const void *, const void *); 81*7c478bd9Sstevel@tonic-gate static char *cat(char *, char *); 82*7c478bd9Sstevel@tonic-gate static char *savestr(char *); 83*7c478bd9Sstevel@tonic-gate static char *fmtentry(struct afile *); 84*7c478bd9Sstevel@tonic-gate static char *getname(), *getgroup(); 85*7c478bd9Sstevel@tonic-gate static void formatd(char *, int); 86*7c478bd9Sstevel@tonic-gate static void formatf(struct afile *, struct afile *); 87*7c478bd9Sstevel@tonic-gate static off_t getdir(char *, struct afile **, struct afile **); 88*7c478bd9Sstevel@tonic-gate 89*7c478bd9Sstevel@tonic-gate int 90*7c478bd9Sstevel@tonic-gate main(int argc, char **argv) 91*7c478bd9Sstevel@tonic-gate { 92*7c478bd9Sstevel@tonic-gate int i; 93*7c478bd9Sstevel@tonic-gate struct afile *fp0, *fplast; 94*7c478bd9Sstevel@tonic-gate register struct afile *fp; 95*7c478bd9Sstevel@tonic-gate struct termios trbuf; 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate argc--, argv++; 98*7c478bd9Sstevel@tonic-gate if (getuid() == 0) 99*7c478bd9Sstevel@tonic-gate Aflg++; 100*7c478bd9Sstevel@tonic-gate (void) time(&now); 101*7c478bd9Sstevel@tonic-gate sixmonthsago = now - 6L*30L*24L*60L*60L; 102*7c478bd9Sstevel@tonic-gate onehourfromnow = now + 60L*60L; 103*7c478bd9Sstevel@tonic-gate now += 60; 104*7c478bd9Sstevel@tonic-gate twidth = 80; 105*7c478bd9Sstevel@tonic-gate if (isatty(1)) { 106*7c478bd9Sstevel@tonic-gate qflg = Cflg = 1; 107*7c478bd9Sstevel@tonic-gate (void) ioctl(1, TCGETS, &trbuf); 108*7c478bd9Sstevel@tonic-gate if (ioctl(1, TIOCGWINSZ, &win) != -1) 109*7c478bd9Sstevel@tonic-gate twidth = (win.ws_col == 0 ? 80 : win.ws_col); 110*7c478bd9Sstevel@tonic-gate if ((trbuf.c_oflag & TABDLY) != TAB3) 111*7c478bd9Sstevel@tonic-gate usetabs = 1; 112*7c478bd9Sstevel@tonic-gate } else 113*7c478bd9Sstevel@tonic-gate usetabs = 1; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); /* set local environment */ 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate while (argc > 0 && **argv == '-') { 118*7c478bd9Sstevel@tonic-gate (*argv)++; 119*7c478bd9Sstevel@tonic-gate while (**argv) { 120*7c478bd9Sstevel@tonic-gate switch (*(*argv)++) { 121*7c478bd9Sstevel@tonic-gate case 'C': 122*7c478bd9Sstevel@tonic-gate Cflg = 1; break; 123*7c478bd9Sstevel@tonic-gate case 'q': 124*7c478bd9Sstevel@tonic-gate qflg = 1; break; 125*7c478bd9Sstevel@tonic-gate case '1': 126*7c478bd9Sstevel@tonic-gate Cflg = 0; break; 127*7c478bd9Sstevel@tonic-gate case 'a': 128*7c478bd9Sstevel@tonic-gate aflg++; break; 129*7c478bd9Sstevel@tonic-gate case 'A': 130*7c478bd9Sstevel@tonic-gate Aflg++; break; 131*7c478bd9Sstevel@tonic-gate case 'c': 132*7c478bd9Sstevel@tonic-gate cflg++; break; 133*7c478bd9Sstevel@tonic-gate case 's': 134*7c478bd9Sstevel@tonic-gate sflg++; break; 135*7c478bd9Sstevel@tonic-gate case 'd': 136*7c478bd9Sstevel@tonic-gate dflg++; break; 137*7c478bd9Sstevel@tonic-gate case 'g': 138*7c478bd9Sstevel@tonic-gate gflg++; break; 139*7c478bd9Sstevel@tonic-gate case 'l': 140*7c478bd9Sstevel@tonic-gate lflg++; break; 141*7c478bd9Sstevel@tonic-gate case 'r': 142*7c478bd9Sstevel@tonic-gate rflg = -1; break; 143*7c478bd9Sstevel@tonic-gate case 't': 144*7c478bd9Sstevel@tonic-gate tflg++; break; 145*7c478bd9Sstevel@tonic-gate case 'u': 146*7c478bd9Sstevel@tonic-gate uflg++; break; 147*7c478bd9Sstevel@tonic-gate case 'i': 148*7c478bd9Sstevel@tonic-gate iflg++; break; 149*7c478bd9Sstevel@tonic-gate case 'f': 150*7c478bd9Sstevel@tonic-gate fflg++; break; 151*7c478bd9Sstevel@tonic-gate case 'L': 152*7c478bd9Sstevel@tonic-gate Lflg++; break; 153*7c478bd9Sstevel@tonic-gate case 'F': 154*7c478bd9Sstevel@tonic-gate Fflg++; break; 155*7c478bd9Sstevel@tonic-gate case 'R': 156*7c478bd9Sstevel@tonic-gate Rflg++; break; 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate argc--, argv++; 160*7c478bd9Sstevel@tonic-gate } 161*7c478bd9Sstevel@tonic-gate if (fflg) { 162*7c478bd9Sstevel@tonic-gate aflg++; lflg = 0; sflg = 0; tflg = 0; 163*7c478bd9Sstevel@tonic-gate } 164*7c478bd9Sstevel@tonic-gate if (lflg) 165*7c478bd9Sstevel@tonic-gate Cflg = 0; 166*7c478bd9Sstevel@tonic-gate if (argc == 0) { 167*7c478bd9Sstevel@tonic-gate argc++; 168*7c478bd9Sstevel@tonic-gate argv = &dotp; 169*7c478bd9Sstevel@tonic-gate } 170*7c478bd9Sstevel@tonic-gate fp = (struct afile *)calloc(argc, sizeof (struct afile)); 171*7c478bd9Sstevel@tonic-gate if (fp == 0) { 172*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ls: out of memory\n"); 173*7c478bd9Sstevel@tonic-gate exit(1); 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate fp0 = fp; 176*7c478bd9Sstevel@tonic-gate for (i = 0; i < argc; i++) { 177*7c478bd9Sstevel@tonic-gate if (gstat(fp, *argv, 1, (off_t *)0)) { 178*7c478bd9Sstevel@tonic-gate fp->fname = *argv; 179*7c478bd9Sstevel@tonic-gate fp->fflags |= ISARG; 180*7c478bd9Sstevel@tonic-gate fp++; 181*7c478bd9Sstevel@tonic-gate } 182*7c478bd9Sstevel@tonic-gate argv++; 183*7c478bd9Sstevel@tonic-gate } 184*7c478bd9Sstevel@tonic-gate fplast = fp; 185*7c478bd9Sstevel@tonic-gate qsort(fp0, fplast - fp0, sizeof (struct afile), fcmp); 186*7c478bd9Sstevel@tonic-gate if (dflg) { 187*7c478bd9Sstevel@tonic-gate formatf(fp0, fplast); 188*7c478bd9Sstevel@tonic-gate exit(0); 189*7c478bd9Sstevel@tonic-gate } 190*7c478bd9Sstevel@tonic-gate if (fflg) 191*7c478bd9Sstevel@tonic-gate fp = fp0; 192*7c478bd9Sstevel@tonic-gate else { 193*7c478bd9Sstevel@tonic-gate for (fp = fp0; fp < fplast && fp->ftype != 'd'; fp++) 194*7c478bd9Sstevel@tonic-gate continue; 195*7c478bd9Sstevel@tonic-gate formatf(fp0, fp); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate if (fp < fplast) { 198*7c478bd9Sstevel@tonic-gate if (fp > fp0) 199*7c478bd9Sstevel@tonic-gate (void) printf("\n"); 200*7c478bd9Sstevel@tonic-gate for (;;) { 201*7c478bd9Sstevel@tonic-gate formatd(fp->fname, argc > 1); 202*7c478bd9Sstevel@tonic-gate while (subdirs) { 203*7c478bd9Sstevel@tonic-gate struct subdirs *t; 204*7c478bd9Sstevel@tonic-gate 205*7c478bd9Sstevel@tonic-gate t = subdirs; subdirs = t->sd_next; 206*7c478bd9Sstevel@tonic-gate (void) printf("\n"); 207*7c478bd9Sstevel@tonic-gate formatd(t->sd_name, 1); 208*7c478bd9Sstevel@tonic-gate free(t->sd_name); 209*7c478bd9Sstevel@tonic-gate free(t); 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate if (++fp == fplast) 212*7c478bd9Sstevel@tonic-gate break; 213*7c478bd9Sstevel@tonic-gate (void) printf("\n"); 214*7c478bd9Sstevel@tonic-gate } 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate return (0); 217*7c478bd9Sstevel@tonic-gate } 218*7c478bd9Sstevel@tonic-gate 219*7c478bd9Sstevel@tonic-gate static void 220*7c478bd9Sstevel@tonic-gate formatd(char *name, int title) 221*7c478bd9Sstevel@tonic-gate { 222*7c478bd9Sstevel@tonic-gate register struct afile *fp; 223*7c478bd9Sstevel@tonic-gate register struct subdirs *dp; 224*7c478bd9Sstevel@tonic-gate struct afile *dfp0, *dfplast; 225*7c478bd9Sstevel@tonic-gate off_t nkb; 226*7c478bd9Sstevel@tonic-gate 227*7c478bd9Sstevel@tonic-gate nkb = getdir(name, &dfp0, &dfplast); 228*7c478bd9Sstevel@tonic-gate if (dfp0 == 0) 229*7c478bd9Sstevel@tonic-gate return; 230*7c478bd9Sstevel@tonic-gate if (fflg == 0) 231*7c478bd9Sstevel@tonic-gate qsort(dfp0, dfplast - dfp0, sizeof (struct afile), fcmp); 232*7c478bd9Sstevel@tonic-gate if (title) 233*7c478bd9Sstevel@tonic-gate (void) printf("%s:\n", name); 234*7c478bd9Sstevel@tonic-gate if (lflg || sflg) 235*7c478bd9Sstevel@tonic-gate (void) printf("total %lld\n", nkb); 236*7c478bd9Sstevel@tonic-gate formatf(dfp0, dfplast); 237*7c478bd9Sstevel@tonic-gate if (Rflg) 238*7c478bd9Sstevel@tonic-gate for (fp = dfplast - 1; fp >= dfp0; fp--) { 239*7c478bd9Sstevel@tonic-gate if (fp->ftype != 'd' || 240*7c478bd9Sstevel@tonic-gate strcmp(fp->fname, ".") == 0 || 241*7c478bd9Sstevel@tonic-gate strcmp(fp->fname, "..") == 0) 242*7c478bd9Sstevel@tonic-gate continue; 243*7c478bd9Sstevel@tonic-gate dp = (struct subdirs *)malloc(sizeof (struct subdirs)); 244*7c478bd9Sstevel@tonic-gate dp->sd_name = savestr(cat(name, fp->fname)); 245*7c478bd9Sstevel@tonic-gate dp->sd_next = subdirs; subdirs = dp; 246*7c478bd9Sstevel@tonic-gate } 247*7c478bd9Sstevel@tonic-gate for (fp = dfp0; fp < dfplast; fp++) { 248*7c478bd9Sstevel@tonic-gate if ((fp->fflags&ISARG) == 0 && fp->fname) 249*7c478bd9Sstevel@tonic-gate free(fp->fname); 250*7c478bd9Sstevel@tonic-gate if (fp->flinkto) 251*7c478bd9Sstevel@tonic-gate free(fp->flinkto); 252*7c478bd9Sstevel@tonic-gate } 253*7c478bd9Sstevel@tonic-gate free(dfp0); 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate static off_t 257*7c478bd9Sstevel@tonic-gate getdir(char *dir, struct afile **pfp0, struct afile **pfplast) 258*7c478bd9Sstevel@tonic-gate { 259*7c478bd9Sstevel@tonic-gate register struct afile *fp; 260*7c478bd9Sstevel@tonic-gate DIR *dirp; 261*7c478bd9Sstevel@tonic-gate register struct dirent *dp; 262*7c478bd9Sstevel@tonic-gate off_t nb; 263*7c478bd9Sstevel@tonic-gate size_t nent = 20; 264*7c478bd9Sstevel@tonic-gate 265*7c478bd9Sstevel@tonic-gate /* 266*7c478bd9Sstevel@tonic-gate * This code (opendir, readdir, and the "for" loop) is arranged in 267*7c478bd9Sstevel@tonic-gate * this strange manner to handle the case where UNIX lets root open 268*7c478bd9Sstevel@tonic-gate * any directory for reading, but NFS does not let root read the 269*7c478bd9Sstevel@tonic-gate * openned directory. 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate *pfp0 = *pfplast = NULL; 272*7c478bd9Sstevel@tonic-gate if ((dirp = opendir(dir)) == NULL) { 273*7c478bd9Sstevel@tonic-gate (void) printf("%s unreadable\n", dir); /* not stderr! */ 274*7c478bd9Sstevel@tonic-gate return (0); 275*7c478bd9Sstevel@tonic-gate } 276*7c478bd9Sstevel@tonic-gate errno = 0; 277*7c478bd9Sstevel@tonic-gate if (((dp = readdir(dirp)) == NULL) && (errno != 0)) { 278*7c478bd9Sstevel@tonic-gate /* root reading across NFS can get to this error case */ 279*7c478bd9Sstevel@tonic-gate (void) printf("%s unreadable\n", dir); /* not stderr! */ 280*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 281*7c478bd9Sstevel@tonic-gate return (0); 282*7c478bd9Sstevel@tonic-gate } 283*7c478bd9Sstevel@tonic-gate fp = *pfp0 = (struct afile *)calloc(nent, sizeof (struct afile)); 284*7c478bd9Sstevel@tonic-gate *pfplast = *pfp0 + nent; 285*7c478bd9Sstevel@tonic-gate for (nb = 0; dp != NULL; dp = readdir(dirp)) { 286*7c478bd9Sstevel@tonic-gate if (dp->d_ino == 0) 287*7c478bd9Sstevel@tonic-gate continue; 288*7c478bd9Sstevel@tonic-gate if (aflg == 0 && dp->d_name[0] == '.' && 289*7c478bd9Sstevel@tonic-gate (Aflg == 0 || dp->d_name[1] == 0 || 290*7c478bd9Sstevel@tonic-gate dp->d_name[1] == '.' && dp->d_name[2] == 0)) 291*7c478bd9Sstevel@tonic-gate continue; 292*7c478bd9Sstevel@tonic-gate if (gstat(fp, cat(dir, dp->d_name), Fflg+Rflg, &nb) == 0) 293*7c478bd9Sstevel@tonic-gate continue; 294*7c478bd9Sstevel@tonic-gate fp->fnum = dp->d_ino; 295*7c478bd9Sstevel@tonic-gate fp->fname = savestr(dp->d_name); 296*7c478bd9Sstevel@tonic-gate fp++; 297*7c478bd9Sstevel@tonic-gate if (fp == *pfplast) { 298*7c478bd9Sstevel@tonic-gate *pfp0 = (struct afile *)realloc((char *)*pfp0, 299*7c478bd9Sstevel@tonic-gate 2 * nent * sizeof (struct afile)); 300*7c478bd9Sstevel@tonic-gate if (*pfp0 == 0) { 301*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ls: out of memory\n"); 302*7c478bd9Sstevel@tonic-gate exit(1); 303*7c478bd9Sstevel@tonic-gate } 304*7c478bd9Sstevel@tonic-gate fp = *pfp0 + nent; 305*7c478bd9Sstevel@tonic-gate *pfplast = fp + nent; 306*7c478bd9Sstevel@tonic-gate nent *= 2; 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate } 309*7c478bd9Sstevel@tonic-gate (void) closedir(dirp); 310*7c478bd9Sstevel@tonic-gate *pfplast = fp; 311*7c478bd9Sstevel@tonic-gate return (dbtokb(nb)); 312*7c478bd9Sstevel@tonic-gate } 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate static struct afile * 316*7c478bd9Sstevel@tonic-gate gstat(struct afile *fp, char *file, int statarg, off_t *pnb) 317*7c478bd9Sstevel@tonic-gate { 318*7c478bd9Sstevel@tonic-gate static struct afile azerofile; 319*7c478bd9Sstevel@tonic-gate int (*statf)() = Lflg ? stat : lstat; 320*7c478bd9Sstevel@tonic-gate int cc; 321*7c478bd9Sstevel@tonic-gate char buf[PATH_MAX]; 322*7c478bd9Sstevel@tonic-gate int aclcnt; 323*7c478bd9Sstevel@tonic-gate aclent_t *aclp; 324*7c478bd9Sstevel@tonic-gate aclent_t *tp; 325*7c478bd9Sstevel@tonic-gate o_mode_t groupperm, mask; 326*7c478bd9Sstevel@tonic-gate int grouppermfound, maskfound; 327*7c478bd9Sstevel@tonic-gate 328*7c478bd9Sstevel@tonic-gate *fp = azerofile; 329*7c478bd9Sstevel@tonic-gate fp->fflags = 0; 330*7c478bd9Sstevel@tonic-gate fp->fnum = 0; 331*7c478bd9Sstevel@tonic-gate fp->ftype = '-'; 332*7c478bd9Sstevel@tonic-gate if (statarg || sflg || lflg || tflg) { 333*7c478bd9Sstevel@tonic-gate struct stat stb, stb1; 334*7c478bd9Sstevel@tonic-gate 335*7c478bd9Sstevel@tonic-gate if ((*statf)(file, &stb) < 0) { 336*7c478bd9Sstevel@tonic-gate if (statf == lstat || lstat(file, &stb) < 0) { 337*7c478bd9Sstevel@tonic-gate if (errno == ENOENT) 338*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, 339*7c478bd9Sstevel@tonic-gate "%s not found\n", file); 340*7c478bd9Sstevel@tonic-gate else { 341*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ls: "); 342*7c478bd9Sstevel@tonic-gate perror(file); 343*7c478bd9Sstevel@tonic-gate } 344*7c478bd9Sstevel@tonic-gate return (0); 345*7c478bd9Sstevel@tonic-gate } 346*7c478bd9Sstevel@tonic-gate } 347*7c478bd9Sstevel@tonic-gate fp->fblks = stb.st_blocks; 348*7c478bd9Sstevel@tonic-gate fp->fsize = stb.st_size; 349*7c478bd9Sstevel@tonic-gate switch (stb.st_mode & S_IFMT) { 350*7c478bd9Sstevel@tonic-gate case S_IFDIR: 351*7c478bd9Sstevel@tonic-gate fp->ftype = 'd'; break; 352*7c478bd9Sstevel@tonic-gate case S_IFDOOR: 353*7c478bd9Sstevel@tonic-gate fp->ftype = 'D'; break; 354*7c478bd9Sstevel@tonic-gate case S_IFBLK: 355*7c478bd9Sstevel@tonic-gate fp->ftype = 'b'; fp->fsize = (off_t)stb.st_rdev; break; 356*7c478bd9Sstevel@tonic-gate case S_IFCHR: 357*7c478bd9Sstevel@tonic-gate fp->ftype = 'c'; fp->fsize = (off_t)stb.st_rdev; break; 358*7c478bd9Sstevel@tonic-gate case S_IFSOCK: 359*7c478bd9Sstevel@tonic-gate fp->ftype = 's'; fp->fsize = 0LL; break; 360*7c478bd9Sstevel@tonic-gate case S_IFIFO: 361*7c478bd9Sstevel@tonic-gate fp->ftype = 'p'; fp->fsize = 0LL; break; 362*7c478bd9Sstevel@tonic-gate case S_IFLNK: 363*7c478bd9Sstevel@tonic-gate fp->ftype = 'l'; 364*7c478bd9Sstevel@tonic-gate if (lflg) { 365*7c478bd9Sstevel@tonic-gate cc = readlink(file, buf, BUFSIZ); 366*7c478bd9Sstevel@tonic-gate if (cc >= 0) { 367*7c478bd9Sstevel@tonic-gate /* 368*7c478bd9Sstevel@tonic-gate * here we follow the symbolic 369*7c478bd9Sstevel@tonic-gate * link to generate the proper 370*7c478bd9Sstevel@tonic-gate * Fflg marker for the object, 371*7c478bd9Sstevel@tonic-gate * eg, /bin -> /pub/bin/ 372*7c478bd9Sstevel@tonic-gate */ 373*7c478bd9Sstevel@tonic-gate buf[cc] = 0; 374*7c478bd9Sstevel@tonic-gate if (Fflg && !stat(file, &stb1)) 375*7c478bd9Sstevel@tonic-gate switch (stb1.st_mode & S_IFMT) { 376*7c478bd9Sstevel@tonic-gate case S_IFDIR: 377*7c478bd9Sstevel@tonic-gate buf[cc++] = '/'; 378*7c478bd9Sstevel@tonic-gate break; 379*7c478bd9Sstevel@tonic-gate case S_IFDOOR: 380*7c478bd9Sstevel@tonic-gate buf[cc++] = '>'; 381*7c478bd9Sstevel@tonic-gate break; 382*7c478bd9Sstevel@tonic-gate case S_IFIFO: 383*7c478bd9Sstevel@tonic-gate buf[cc++] = '|'; 384*7c478bd9Sstevel@tonic-gate break; 385*7c478bd9Sstevel@tonic-gate case S_IFSOCK: 386*7c478bd9Sstevel@tonic-gate buf[cc++] = '='; 387*7c478bd9Sstevel@tonic-gate break; 388*7c478bd9Sstevel@tonic-gate default: 389*7c478bd9Sstevel@tonic-gate if ((stb1.st_mode & ~S_IFMT) 390*7c478bd9Sstevel@tonic-gate & 0111) 391*7c478bd9Sstevel@tonic-gate buf[cc++] = '*'; 392*7c478bd9Sstevel@tonic-gate break; 393*7c478bd9Sstevel@tonic-gate } 394*7c478bd9Sstevel@tonic-gate buf[cc] = 0; 395*7c478bd9Sstevel@tonic-gate fp->flinkto = savestr(buf); 396*7c478bd9Sstevel@tonic-gate } 397*7c478bd9Sstevel@tonic-gate break; 398*7c478bd9Sstevel@tonic-gate } 399*7c478bd9Sstevel@tonic-gate /* 400*7c478bd9Sstevel@tonic-gate * this is a hack from UCB to avoid having 401*7c478bd9Sstevel@tonic-gate * ls /bin behave differently from ls /bin/ 402*7c478bd9Sstevel@tonic-gate * when /bin is a symbolic link. We hack the 403*7c478bd9Sstevel@tonic-gate * hack to have that happen, but only for 404*7c478bd9Sstevel@tonic-gate * explicit arguments, by inspecting pnb. 405*7c478bd9Sstevel@tonic-gate */ 406*7c478bd9Sstevel@tonic-gate if (pnb != (off_t *)0 || stat(file, &stb1) < 0) 407*7c478bd9Sstevel@tonic-gate break; 408*7c478bd9Sstevel@tonic-gate if ((stb1.st_mode & S_IFMT) == S_IFDIR) { 409*7c478bd9Sstevel@tonic-gate stb = stb1; 410*7c478bd9Sstevel@tonic-gate fp->ftype = 'd'; 411*7c478bd9Sstevel@tonic-gate fp->fsize = stb.st_size; 412*7c478bd9Sstevel@tonic-gate fp->fblks = stb.st_blocks; 413*7c478bd9Sstevel@tonic-gate } 414*7c478bd9Sstevel@tonic-gate break; 415*7c478bd9Sstevel@tonic-gate } 416*7c478bd9Sstevel@tonic-gate fp->fnum = stb.st_ino; 417*7c478bd9Sstevel@tonic-gate fp->fflags = stb.st_mode & ~S_IFMT; 418*7c478bd9Sstevel@tonic-gate fp->fnl = stb.st_nlink; 419*7c478bd9Sstevel@tonic-gate fp->fuid = stb.st_uid; 420*7c478bd9Sstevel@tonic-gate fp->fgid = stb.st_gid; 421*7c478bd9Sstevel@tonic-gate 422*7c478bd9Sstevel@tonic-gate /* ACL: check acl entries count */ 423*7c478bd9Sstevel@tonic-gate if ((aclcnt = acl(file, GETACLCNT, 0, NULL)) > 424*7c478bd9Sstevel@tonic-gate MIN_ACL_ENTRIES) { 425*7c478bd9Sstevel@tonic-gate 426*7c478bd9Sstevel@tonic-gate /* this file has a non-trivial acl */ 427*7c478bd9Sstevel@tonic-gate 428*7c478bd9Sstevel@tonic-gate fp->acl = '+'; 429*7c478bd9Sstevel@tonic-gate 430*7c478bd9Sstevel@tonic-gate /* 431*7c478bd9Sstevel@tonic-gate * For files with non-trivial acls, the 432*7c478bd9Sstevel@tonic-gate * effective group permissions are the 433*7c478bd9Sstevel@tonic-gate * intersection of the GROUP_OBJ value and 434*7c478bd9Sstevel@tonic-gate * the CLASS_OBJ (acl mask) value. Determine 435*7c478bd9Sstevel@tonic-gate * both the GROUP_OBJ and CLASS_OBJ for this 436*7c478bd9Sstevel@tonic-gate * file and insert the logical AND of those 437*7c478bd9Sstevel@tonic-gate * two values in the group permissions field 438*7c478bd9Sstevel@tonic-gate * of the lflags value for this file. 439*7c478bd9Sstevel@tonic-gate */ 440*7c478bd9Sstevel@tonic-gate 441*7c478bd9Sstevel@tonic-gate if ((aclp = (aclent_t *)malloc( 442*7c478bd9Sstevel@tonic-gate (sizeof (aclent_t)) * aclcnt)) == NULL) { 443*7c478bd9Sstevel@tonic-gate perror("ls"); 444*7c478bd9Sstevel@tonic-gate exit(2); 445*7c478bd9Sstevel@tonic-gate } 446*7c478bd9Sstevel@tonic-gate 447*7c478bd9Sstevel@tonic-gate if (acl(file, GETACL, aclcnt, aclp) < 0) { 448*7c478bd9Sstevel@tonic-gate free(aclp); 449*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ls: "); 450*7c478bd9Sstevel@tonic-gate perror(file); 451*7c478bd9Sstevel@tonic-gate return (0); 452*7c478bd9Sstevel@tonic-gate } 453*7c478bd9Sstevel@tonic-gate 454*7c478bd9Sstevel@tonic-gate /* 455*7c478bd9Sstevel@tonic-gate * Until found in acl list, assume maximum 456*7c478bd9Sstevel@tonic-gate * permissions for both group and mask. (Just 457*7c478bd9Sstevel@tonic-gate * in case the acl lacks either value for 458*7c478bd9Sstevel@tonic-gate * some reason.) 459*7c478bd9Sstevel@tonic-gate */ 460*7c478bd9Sstevel@tonic-gate groupperm = 07; 461*7c478bd9Sstevel@tonic-gate mask = 07; 462*7c478bd9Sstevel@tonic-gate grouppermfound = 0; 463*7c478bd9Sstevel@tonic-gate maskfound = 0; 464*7c478bd9Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) { 465*7c478bd9Sstevel@tonic-gate if (tp->a_type == GROUP_OBJ) { 466*7c478bd9Sstevel@tonic-gate groupperm = tp->a_perm; 467*7c478bd9Sstevel@tonic-gate grouppermfound = 1; 468*7c478bd9Sstevel@tonic-gate continue; 469*7c478bd9Sstevel@tonic-gate } 470*7c478bd9Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ) { 471*7c478bd9Sstevel@tonic-gate mask = tp->a_perm; 472*7c478bd9Sstevel@tonic-gate maskfound = 1; 473*7c478bd9Sstevel@tonic-gate } 474*7c478bd9Sstevel@tonic-gate if (grouppermfound && maskfound) 475*7c478bd9Sstevel@tonic-gate break; 476*7c478bd9Sstevel@tonic-gate } 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate free(aclp); 479*7c478bd9Sstevel@tonic-gate 480*7c478bd9Sstevel@tonic-gate /* reset all the group bits */ 481*7c478bd9Sstevel@tonic-gate fp->fflags &= ~S_IRWXG; 482*7c478bd9Sstevel@tonic-gate 483*7c478bd9Sstevel@tonic-gate /* 484*7c478bd9Sstevel@tonic-gate * Now set them to the logical AND of the 485*7c478bd9Sstevel@tonic-gate * GROUP_OBJ permissions and the acl mask. 486*7c478bd9Sstevel@tonic-gate */ 487*7c478bd9Sstevel@tonic-gate 488*7c478bd9Sstevel@tonic-gate fp->fflags |= (groupperm & mask) << 3; 489*7c478bd9Sstevel@tonic-gate } else 490*7c478bd9Sstevel@tonic-gate fp->acl = ' '; 491*7c478bd9Sstevel@tonic-gate 492*7c478bd9Sstevel@tonic-gate if (uflg) 493*7c478bd9Sstevel@tonic-gate fp->fmtime = stb.st_atime; 494*7c478bd9Sstevel@tonic-gate else if (cflg) 495*7c478bd9Sstevel@tonic-gate fp->fmtime = stb.st_ctime; 496*7c478bd9Sstevel@tonic-gate else 497*7c478bd9Sstevel@tonic-gate fp->fmtime = stb.st_mtime; 498*7c478bd9Sstevel@tonic-gate if (pnb) 499*7c478bd9Sstevel@tonic-gate *pnb += stb.st_blocks; 500*7c478bd9Sstevel@tonic-gate } 501*7c478bd9Sstevel@tonic-gate return (fp); 502*7c478bd9Sstevel@tonic-gate } 503*7c478bd9Sstevel@tonic-gate 504*7c478bd9Sstevel@tonic-gate static void 505*7c478bd9Sstevel@tonic-gate formatf(struct afile *fp0, struct afile *fplast) 506*7c478bd9Sstevel@tonic-gate { 507*7c478bd9Sstevel@tonic-gate register struct afile *fp; 508*7c478bd9Sstevel@tonic-gate int width = 0, w, nentry = fplast - fp0; 509*7c478bd9Sstevel@tonic-gate int i, j, columns, lines; 510*7c478bd9Sstevel@tonic-gate char *cp; 511*7c478bd9Sstevel@tonic-gate 512*7c478bd9Sstevel@tonic-gate if (fp0 == fplast) 513*7c478bd9Sstevel@tonic-gate return; 514*7c478bd9Sstevel@tonic-gate if (lflg || Cflg == 0) 515*7c478bd9Sstevel@tonic-gate columns = 1; 516*7c478bd9Sstevel@tonic-gate else { 517*7c478bd9Sstevel@tonic-gate for (fp = fp0; fp < fplast; fp++) { 518*7c478bd9Sstevel@tonic-gate int len = strlen(fmtentry(fp)); 519*7c478bd9Sstevel@tonic-gate 520*7c478bd9Sstevel@tonic-gate if (len > width) 521*7c478bd9Sstevel@tonic-gate width = len; 522*7c478bd9Sstevel@tonic-gate } 523*7c478bd9Sstevel@tonic-gate if (usetabs) 524*7c478bd9Sstevel@tonic-gate width = (width + 8) &~ 7; 525*7c478bd9Sstevel@tonic-gate else 526*7c478bd9Sstevel@tonic-gate width += 2; 527*7c478bd9Sstevel@tonic-gate columns = twidth / width; 528*7c478bd9Sstevel@tonic-gate if (columns == 0) 529*7c478bd9Sstevel@tonic-gate columns = 1; 530*7c478bd9Sstevel@tonic-gate } 531*7c478bd9Sstevel@tonic-gate lines = (nentry + columns - 1) / columns; 532*7c478bd9Sstevel@tonic-gate for (i = 0; i < lines; i++) { 533*7c478bd9Sstevel@tonic-gate for (j = 0; j < columns; j++) { 534*7c478bd9Sstevel@tonic-gate fp = fp0 + j * lines + i; 535*7c478bd9Sstevel@tonic-gate cp = fmtentry(fp); 536*7c478bd9Sstevel@tonic-gate (void) printf("%s", cp); 537*7c478bd9Sstevel@tonic-gate if (fp + lines >= fplast) { 538*7c478bd9Sstevel@tonic-gate (void) printf("\n"); 539*7c478bd9Sstevel@tonic-gate break; 540*7c478bd9Sstevel@tonic-gate } 541*7c478bd9Sstevel@tonic-gate w = strlen(cp); 542*7c478bd9Sstevel@tonic-gate while (w < width) 543*7c478bd9Sstevel@tonic-gate if (usetabs) { 544*7c478bd9Sstevel@tonic-gate w = (w + 8) &~ 7; 545*7c478bd9Sstevel@tonic-gate (void) putchar('\t'); 546*7c478bd9Sstevel@tonic-gate } else { 547*7c478bd9Sstevel@tonic-gate w++; 548*7c478bd9Sstevel@tonic-gate (void) putchar(' '); 549*7c478bd9Sstevel@tonic-gate } 550*7c478bd9Sstevel@tonic-gate } 551*7c478bd9Sstevel@tonic-gate } 552*7c478bd9Sstevel@tonic-gate } 553*7c478bd9Sstevel@tonic-gate 554*7c478bd9Sstevel@tonic-gate static int 555*7c478bd9Sstevel@tonic-gate fcmp(const void *arg1, const void *arg2) 556*7c478bd9Sstevel@tonic-gate { 557*7c478bd9Sstevel@tonic-gate const struct afile *f1 = arg1; 558*7c478bd9Sstevel@tonic-gate const struct afile *f2 = arg2; 559*7c478bd9Sstevel@tonic-gate 560*7c478bd9Sstevel@tonic-gate if (dflg == 0 && fflg == 0) { 561*7c478bd9Sstevel@tonic-gate if ((f1->fflags&ISARG) && f1->ftype == 'd') { 562*7c478bd9Sstevel@tonic-gate if ((f2->fflags&ISARG) == 0 || f2->ftype != 'd') 563*7c478bd9Sstevel@tonic-gate return (1); 564*7c478bd9Sstevel@tonic-gate } else { 565*7c478bd9Sstevel@tonic-gate if ((f2->fflags&ISARG) && f2->ftype == 'd') 566*7c478bd9Sstevel@tonic-gate return (-1); 567*7c478bd9Sstevel@tonic-gate } 568*7c478bd9Sstevel@tonic-gate } 569*7c478bd9Sstevel@tonic-gate if (tflg) { 570*7c478bd9Sstevel@tonic-gate if (f2->fmtime == f1->fmtime) 571*7c478bd9Sstevel@tonic-gate return (0); 572*7c478bd9Sstevel@tonic-gate if (f2->fmtime > f1->fmtime) 573*7c478bd9Sstevel@tonic-gate return (rflg); 574*7c478bd9Sstevel@tonic-gate return (-rflg); 575*7c478bd9Sstevel@tonic-gate } 576*7c478bd9Sstevel@tonic-gate return (rflg * strcmp(f1->fname, f2->fname)); 577*7c478bd9Sstevel@tonic-gate } 578*7c478bd9Sstevel@tonic-gate 579*7c478bd9Sstevel@tonic-gate static char * 580*7c478bd9Sstevel@tonic-gate cat(char *dir, char *file) 581*7c478bd9Sstevel@tonic-gate { 582*7c478bd9Sstevel@tonic-gate static char dfile[BUFSIZ]; 583*7c478bd9Sstevel@tonic-gate 584*7c478bd9Sstevel@tonic-gate if (strlen(dir)+1+strlen(file)+1 > BUFSIZ) { 585*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ls: filename too long\n"); 586*7c478bd9Sstevel@tonic-gate exit(1); 587*7c478bd9Sstevel@tonic-gate } 588*7c478bd9Sstevel@tonic-gate if (strcmp(dir, "") == 0 || strcmp(dir, ".") == 0) { 589*7c478bd9Sstevel@tonic-gate (void) strcpy(dfile, file); 590*7c478bd9Sstevel@tonic-gate return (dfile); 591*7c478bd9Sstevel@tonic-gate } 592*7c478bd9Sstevel@tonic-gate (void) strcpy(dfile, dir); 593*7c478bd9Sstevel@tonic-gate if (dir[strlen(dir) - 1] != '/' && *file != '/') 594*7c478bd9Sstevel@tonic-gate (void) strcat(dfile, "/"); 595*7c478bd9Sstevel@tonic-gate (void) strcat(dfile, file); 596*7c478bd9Sstevel@tonic-gate return (dfile); 597*7c478bd9Sstevel@tonic-gate } 598*7c478bd9Sstevel@tonic-gate 599*7c478bd9Sstevel@tonic-gate static char * 600*7c478bd9Sstevel@tonic-gate savestr(char *str) 601*7c478bd9Sstevel@tonic-gate { 602*7c478bd9Sstevel@tonic-gate char *cp = malloc(strlen(str) + 1); 603*7c478bd9Sstevel@tonic-gate 604*7c478bd9Sstevel@tonic-gate if (cp == NULL) { 605*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "ls: out of memory\n"); 606*7c478bd9Sstevel@tonic-gate exit(1); 607*7c478bd9Sstevel@tonic-gate } 608*7c478bd9Sstevel@tonic-gate (void) strcpy(cp, str); 609*7c478bd9Sstevel@tonic-gate return (cp); 610*7c478bd9Sstevel@tonic-gate } 611*7c478bd9Sstevel@tonic-gate 612*7c478bd9Sstevel@tonic-gate static char *fmtinum(struct afile *); 613*7c478bd9Sstevel@tonic-gate static char *fmtsize(struct afile *); 614*7c478bd9Sstevel@tonic-gate static char *fmtlstuff(struct afile *); 615*7c478bd9Sstevel@tonic-gate static char *fmtmode(char *, int); 616*7c478bd9Sstevel@tonic-gate 617*7c478bd9Sstevel@tonic-gate static char * 618*7c478bd9Sstevel@tonic-gate fmtentry(struct afile *fp) 619*7c478bd9Sstevel@tonic-gate { 620*7c478bd9Sstevel@tonic-gate static char fmtres[BUFSIZ]; 621*7c478bd9Sstevel@tonic-gate register char *cp, *dp; 622*7c478bd9Sstevel@tonic-gate 623*7c478bd9Sstevel@tonic-gate (void) sprintf(fmtres, "%s%s%s", 624*7c478bd9Sstevel@tonic-gate iflg ? fmtinum(fp) : "", 625*7c478bd9Sstevel@tonic-gate sflg ? fmtsize(fp) : "", 626*7c478bd9Sstevel@tonic-gate lflg ? fmtlstuff(fp) : ""); 627*7c478bd9Sstevel@tonic-gate dp = &fmtres[strlen(fmtres)]; 628*7c478bd9Sstevel@tonic-gate for (cp = fp->fname; *cp; cp++) 629*7c478bd9Sstevel@tonic-gate if (qflg && !isprint((unsigned char)*cp)) 630*7c478bd9Sstevel@tonic-gate *dp++ = '?'; 631*7c478bd9Sstevel@tonic-gate else 632*7c478bd9Sstevel@tonic-gate *dp++ = *cp; 633*7c478bd9Sstevel@tonic-gate /* avoid both "->" and trailing marks */ 634*7c478bd9Sstevel@tonic-gate if (Fflg && ! (lflg && fp->flinkto)) { 635*7c478bd9Sstevel@tonic-gate if (fp->ftype == 'd') 636*7c478bd9Sstevel@tonic-gate *dp++ = '/'; 637*7c478bd9Sstevel@tonic-gate else if (fp->ftype == 'D') 638*7c478bd9Sstevel@tonic-gate *dp++ = '>'; 639*7c478bd9Sstevel@tonic-gate else if (fp->ftype == 'p') 640*7c478bd9Sstevel@tonic-gate *dp++ = '|'; 641*7c478bd9Sstevel@tonic-gate else if (fp->ftype == 'l') 642*7c478bd9Sstevel@tonic-gate *dp++ = '@'; 643*7c478bd9Sstevel@tonic-gate else if (fp->ftype == 's') 644*7c478bd9Sstevel@tonic-gate *dp++ = '='; 645*7c478bd9Sstevel@tonic-gate else if (fp->fflags & 0111) 646*7c478bd9Sstevel@tonic-gate *dp++ = '*'; 647*7c478bd9Sstevel@tonic-gate } 648*7c478bd9Sstevel@tonic-gate if (lflg && fp->flinkto) { 649*7c478bd9Sstevel@tonic-gate (void) strcpy(dp, " -> "); dp += 4; 650*7c478bd9Sstevel@tonic-gate for (cp = fp->flinkto; *cp; cp++) 651*7c478bd9Sstevel@tonic-gate if (qflg && !isprint((unsigned char) *cp)) 652*7c478bd9Sstevel@tonic-gate *dp++ = '?'; 653*7c478bd9Sstevel@tonic-gate else 654*7c478bd9Sstevel@tonic-gate *dp++ = *cp; 655*7c478bd9Sstevel@tonic-gate } 656*7c478bd9Sstevel@tonic-gate *dp++ = 0; 657*7c478bd9Sstevel@tonic-gate return (fmtres); 658*7c478bd9Sstevel@tonic-gate } 659*7c478bd9Sstevel@tonic-gate 660*7c478bd9Sstevel@tonic-gate static char * 661*7c478bd9Sstevel@tonic-gate fmtinum(struct afile *p) 662*7c478bd9Sstevel@tonic-gate { 663*7c478bd9Sstevel@tonic-gate static char inumbuf[12]; 664*7c478bd9Sstevel@tonic-gate 665*7c478bd9Sstevel@tonic-gate (void) sprintf(inumbuf, "%10llu ", p->fnum); 666*7c478bd9Sstevel@tonic-gate return (inumbuf); 667*7c478bd9Sstevel@tonic-gate } 668*7c478bd9Sstevel@tonic-gate 669*7c478bd9Sstevel@tonic-gate static char * 670*7c478bd9Sstevel@tonic-gate fmtsize(struct afile *p) 671*7c478bd9Sstevel@tonic-gate { 672*7c478bd9Sstevel@tonic-gate static char sizebuf[32]; 673*7c478bd9Sstevel@tonic-gate 674*7c478bd9Sstevel@tonic-gate (void) sprintf(sizebuf, (off_t)dbtokb(p->fblks) < 10000 ? "%4lld " : \ 675*7c478bd9Sstevel@tonic-gate "%lld ", (off_t)dbtokb(p->fblks)); 676*7c478bd9Sstevel@tonic-gate return (sizebuf); 677*7c478bd9Sstevel@tonic-gate } 678*7c478bd9Sstevel@tonic-gate 679*7c478bd9Sstevel@tonic-gate static char * 680*7c478bd9Sstevel@tonic-gate fmtlstuff(struct afile *p) 681*7c478bd9Sstevel@tonic-gate { 682*7c478bd9Sstevel@tonic-gate static char lstuffbuf[256]; 683*7c478bd9Sstevel@tonic-gate char gname[32], uname[32], fsize[32], ftime[32]; 684*7c478bd9Sstevel@tonic-gate register char *lp = lstuffbuf; 685*7c478bd9Sstevel@tonic-gate 686*7c478bd9Sstevel@tonic-gate /* type mode uname gname fsize ftime */ 687*7c478bd9Sstevel@tonic-gate /* get uname */ 688*7c478bd9Sstevel@tonic-gate { 689*7c478bd9Sstevel@tonic-gate char *cp = getname(p->fuid); 690*7c478bd9Sstevel@tonic-gate (void) sprintf(uname, "%-9s", cp); 691*7c478bd9Sstevel@tonic-gate } 692*7c478bd9Sstevel@tonic-gate /* get gname */ 693*7c478bd9Sstevel@tonic-gate if (gflg) { 694*7c478bd9Sstevel@tonic-gate char *cp = getgroup(p->fgid); 695*7c478bd9Sstevel@tonic-gate (void) sprintf(gname, "%-9s", cp); 696*7c478bd9Sstevel@tonic-gate } 697*7c478bd9Sstevel@tonic-gate /* get fsize */ 698*7c478bd9Sstevel@tonic-gate if (p->ftype == 'b' || p->ftype == 'c') 699*7c478bd9Sstevel@tonic-gate (void) sprintf(fsize, "%3ld,%4ld", 700*7c478bd9Sstevel@tonic-gate major(p->fsize), minor(p->fsize)); 701*7c478bd9Sstevel@tonic-gate else if (p->ftype == 's') 702*7c478bd9Sstevel@tonic-gate (void) sprintf(fsize, "%8d", 0); 703*7c478bd9Sstevel@tonic-gate else 704*7c478bd9Sstevel@tonic-gate (void) sprintf(fsize, p->fsize < 100000000 ? "%8lld" : \ 705*7c478bd9Sstevel@tonic-gate "%lld", p->fsize); 706*7c478bd9Sstevel@tonic-gate /* get ftime */ 707*7c478bd9Sstevel@tonic-gate { 708*7c478bd9Sstevel@tonic-gate char *cp = ctime(&p->fmtime); 709*7c478bd9Sstevel@tonic-gate if ((p->fmtime < sixmonthsago) || (p->fmtime > onehourfromnow)) 710*7c478bd9Sstevel@tonic-gate (void) sprintf(ftime, " %-7.7s %-4.4s ", cp+4, cp+20); 711*7c478bd9Sstevel@tonic-gate else 712*7c478bd9Sstevel@tonic-gate (void) sprintf(ftime, " %-12.12s ", cp+4); 713*7c478bd9Sstevel@tonic-gate } 714*7c478bd9Sstevel@tonic-gate /* splat */ 715*7c478bd9Sstevel@tonic-gate *lp++ = p->ftype; 716*7c478bd9Sstevel@tonic-gate lp = fmtmode(lp, p->fflags); 717*7c478bd9Sstevel@tonic-gate (void) sprintf(lp, "%c%3ld %s%s%s%s", 718*7c478bd9Sstevel@tonic-gate p->acl, p->fnl, uname, gflg ? gname : "", fsize, ftime); 719*7c478bd9Sstevel@tonic-gate return (lstuffbuf); 720*7c478bd9Sstevel@tonic-gate } 721*7c478bd9Sstevel@tonic-gate 722*7c478bd9Sstevel@tonic-gate static int m1[] = 723*7c478bd9Sstevel@tonic-gate { 1, S_IREAD>>0, 'r', '-' }; 724*7c478bd9Sstevel@tonic-gate static int m2[] = 725*7c478bd9Sstevel@tonic-gate { 1, S_IWRITE>>0, 'w', '-' }; 726*7c478bd9Sstevel@tonic-gate static int m3[] = 727*7c478bd9Sstevel@tonic-gate { 3, S_ISUID|(S_IEXEC>>0), 's', S_IEXEC>>0, 'x', S_ISUID, 'S', '-' }; 728*7c478bd9Sstevel@tonic-gate static int m4[] = 729*7c478bd9Sstevel@tonic-gate { 1, S_IREAD>>3, 'r', '-' }; 730*7c478bd9Sstevel@tonic-gate static int m5[] = 731*7c478bd9Sstevel@tonic-gate { 1, S_IWRITE>>3, 'w', '-' }; 732*7c478bd9Sstevel@tonic-gate static int m6[] = 733*7c478bd9Sstevel@tonic-gate { 3, S_ISGID|(S_IEXEC>>3), 's', S_IEXEC>>3, 'x', S_ISGID, 'S', '-' }; 734*7c478bd9Sstevel@tonic-gate static int m7[] = 735*7c478bd9Sstevel@tonic-gate { 1, S_IREAD>>6, 'r', '-' }; 736*7c478bd9Sstevel@tonic-gate static int m8[] = 737*7c478bd9Sstevel@tonic-gate { 1, S_IWRITE>>6, 'w', '-' }; 738*7c478bd9Sstevel@tonic-gate static int m9[] = 739*7c478bd9Sstevel@tonic-gate { 3, S_ISVTX|(S_IEXEC>>6), 't', S_ISVTX, 'T', S_IEXEC>>6, 'x', '-' }; 740*7c478bd9Sstevel@tonic-gate 741*7c478bd9Sstevel@tonic-gate static int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9}; 742*7c478bd9Sstevel@tonic-gate 743*7c478bd9Sstevel@tonic-gate static char * 744*7c478bd9Sstevel@tonic-gate fmtmode(char *lp, int flags) 745*7c478bd9Sstevel@tonic-gate { 746*7c478bd9Sstevel@tonic-gate int **mp; 747*7c478bd9Sstevel@tonic-gate 748*7c478bd9Sstevel@tonic-gate for (mp = &m[0]; mp < &m[sizeof (m)/sizeof (m[0])]; ) { 749*7c478bd9Sstevel@tonic-gate register int *pairp = *mp++; 750*7c478bd9Sstevel@tonic-gate register int n = *pairp++; 751*7c478bd9Sstevel@tonic-gate 752*7c478bd9Sstevel@tonic-gate while (n-- > 0) { 753*7c478bd9Sstevel@tonic-gate if ((flags&*pairp) == *pairp) { 754*7c478bd9Sstevel@tonic-gate pairp++; 755*7c478bd9Sstevel@tonic-gate break; 756*7c478bd9Sstevel@tonic-gate } else 757*7c478bd9Sstevel@tonic-gate pairp += 2; 758*7c478bd9Sstevel@tonic-gate } 759*7c478bd9Sstevel@tonic-gate *lp++ = *pairp; 760*7c478bd9Sstevel@tonic-gate } 761*7c478bd9Sstevel@tonic-gate return (lp); 762*7c478bd9Sstevel@tonic-gate } 763*7c478bd9Sstevel@tonic-gate 764*7c478bd9Sstevel@tonic-gate /* rest should be done with nameserver or database */ 765*7c478bd9Sstevel@tonic-gate 766*7c478bd9Sstevel@tonic-gate #include <pwd.h> 767*7c478bd9Sstevel@tonic-gate #include <grp.h> 768*7c478bd9Sstevel@tonic-gate #include <utmpx.h> 769*7c478bd9Sstevel@tonic-gate 770*7c478bd9Sstevel@tonic-gate #define NMAX (sizeof (((struct utmpx *)0)->ut_name)) 771*7c478bd9Sstevel@tonic-gate #define SCPYN(a, b) strncpy(a, b, NMAX) 772*7c478bd9Sstevel@tonic-gate 773*7c478bd9Sstevel@tonic-gate 774*7c478bd9Sstevel@tonic-gate static struct cachenode { /* this struct must be zeroed before using */ 775*7c478bd9Sstevel@tonic-gate struct cachenode *lesschild; /* subtree whose entries < val */ 776*7c478bd9Sstevel@tonic-gate struct cachenode *grtrchild; /* subtree whose entries > val */ 777*7c478bd9Sstevel@tonic-gate int val; /* the uid or gid of this entry */ 778*7c478bd9Sstevel@tonic-gate int initted; /* name has been filled in */ 779*7c478bd9Sstevel@tonic-gate char name[NMAX+1]; /* the string that val maps to */ 780*7c478bd9Sstevel@tonic-gate } *names, *groups; 781*7c478bd9Sstevel@tonic-gate 782*7c478bd9Sstevel@tonic-gate static struct cachenode * 783*7c478bd9Sstevel@tonic-gate findincache(struct cachenode **head, id_t val) 784*7c478bd9Sstevel@tonic-gate { 785*7c478bd9Sstevel@tonic-gate register struct cachenode **parent = head; 786*7c478bd9Sstevel@tonic-gate register struct cachenode *c = *parent; 787*7c478bd9Sstevel@tonic-gate 788*7c478bd9Sstevel@tonic-gate while (c != NULL) { 789*7c478bd9Sstevel@tonic-gate if (val == c->val) { 790*7c478bd9Sstevel@tonic-gate /* found it */ 791*7c478bd9Sstevel@tonic-gate return (c); 792*7c478bd9Sstevel@tonic-gate } else if (val < c->val) { 793*7c478bd9Sstevel@tonic-gate parent = &c->lesschild; 794*7c478bd9Sstevel@tonic-gate c = c->lesschild; 795*7c478bd9Sstevel@tonic-gate } else { 796*7c478bd9Sstevel@tonic-gate parent = &c->grtrchild; 797*7c478bd9Sstevel@tonic-gate c = c->grtrchild; 798*7c478bd9Sstevel@tonic-gate } 799*7c478bd9Sstevel@tonic-gate } 800*7c478bd9Sstevel@tonic-gate 801*7c478bd9Sstevel@tonic-gate /* not in the cache, make a new entry for it */ 802*7c478bd9Sstevel@tonic-gate *parent = c = (struct cachenode *)calloc(1, sizeof (struct cachenode)); 803*7c478bd9Sstevel@tonic-gate c->val = val; 804*7c478bd9Sstevel@tonic-gate return (c); 805*7c478bd9Sstevel@tonic-gate } 806*7c478bd9Sstevel@tonic-gate 807*7c478bd9Sstevel@tonic-gate static char * 808*7c478bd9Sstevel@tonic-gate getname(uid_t uid) 809*7c478bd9Sstevel@tonic-gate { 810*7c478bd9Sstevel@tonic-gate struct cachenode *c; 811*7c478bd9Sstevel@tonic-gate struct passwd *pw; 812*7c478bd9Sstevel@tonic-gate 813*7c478bd9Sstevel@tonic-gate c = findincache(&names, uid); 814*7c478bd9Sstevel@tonic-gate if (c->initted == 0) { 815*7c478bd9Sstevel@tonic-gate if ((pw = getpwuid(uid)) != NULL) { 816*7c478bd9Sstevel@tonic-gate (void) SCPYN(&c->name[0], pw->pw_name); 817*7c478bd9Sstevel@tonic-gate } else { 818*7c478bd9Sstevel@tonic-gate (void) sprintf(&c->name[0], "%-8lu ", uid); 819*7c478bd9Sstevel@tonic-gate } 820*7c478bd9Sstevel@tonic-gate c->initted = 1; 821*7c478bd9Sstevel@tonic-gate } 822*7c478bd9Sstevel@tonic-gate return (&c->name[0]); 823*7c478bd9Sstevel@tonic-gate } 824*7c478bd9Sstevel@tonic-gate 825*7c478bd9Sstevel@tonic-gate static char * 826*7c478bd9Sstevel@tonic-gate getgroup(gid_t gid) 827*7c478bd9Sstevel@tonic-gate { 828*7c478bd9Sstevel@tonic-gate struct cachenode *c; 829*7c478bd9Sstevel@tonic-gate struct group *gr; 830*7c478bd9Sstevel@tonic-gate 831*7c478bd9Sstevel@tonic-gate c = findincache(&groups, gid); 832*7c478bd9Sstevel@tonic-gate if (c->initted == 0) { 833*7c478bd9Sstevel@tonic-gate if ((gr = getgrgid(gid)) != NULL) { 834*7c478bd9Sstevel@tonic-gate (void) SCPYN(&c->name[0], gr->gr_name); 835*7c478bd9Sstevel@tonic-gate } else { 836*7c478bd9Sstevel@tonic-gate (void) sprintf(&c->name[0], "%-8lu ", gid); 837*7c478bd9Sstevel@tonic-gate } 838*7c478bd9Sstevel@tonic-gate c->initted = 1; 839*7c478bd9Sstevel@tonic-gate } 840*7c478bd9Sstevel@tonic-gate return (&c->name[0]); 841*7c478bd9Sstevel@tonic-gate } 842