1*2408Scf46844 /* Portions Copyright 2006 Stephen P. Potter */
2*2408Scf46844
30Sstevel@tonic-gate /*
42168Scf46844 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
50Sstevel@tonic-gate * Use is subject to license terms.
60Sstevel@tonic-gate */
70Sstevel@tonic-gate
80Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
90Sstevel@tonic-gate /* All Rights Reserved */
100Sstevel@tonic-gate
110Sstevel@tonic-gate /*
120Sstevel@tonic-gate * Copyright (c) 1980 Regents of the University of California.
130Sstevel@tonic-gate * All rights reserved. The Berkeley software License Agreement
140Sstevel@tonic-gate * specifies the terms and conditions for redistribution.
150Sstevel@tonic-gate */
160Sstevel@tonic-gate
170Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
180Sstevel@tonic-gate
190Sstevel@tonic-gate /*
200Sstevel@tonic-gate * ls
210Sstevel@tonic-gate *
220Sstevel@tonic-gate * 4.2bsd version for symbolic links, variable length
230Sstevel@tonic-gate * directory entries, block size in the inode, etc.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate #include <stdio.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <unistd.h>
290Sstevel@tonic-gate #include <string.h>
300Sstevel@tonic-gate #include <stddef.h>
310Sstevel@tonic-gate #include <dirent.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <time.h>
340Sstevel@tonic-gate #include <limits.h>
350Sstevel@tonic-gate #include <locale.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/param.h>
390Sstevel@tonic-gate #include <sys/stat.h>
400Sstevel@tonic-gate #include <sys/termios.h>
410Sstevel@tonic-gate #include <sys/mkdev.h>
420Sstevel@tonic-gate #include <sys/acl.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #define dbtokb(nb) ((nb) / (1024 / DEV_BSIZE))
450Sstevel@tonic-gate
460Sstevel@tonic-gate struct afile {
470Sstevel@tonic-gate char ftype; /* file type, e.g. 'd', 'c', 'f' */
480Sstevel@tonic-gate ino_t fnum; /* inode number of file */
490Sstevel@tonic-gate short fflags; /* mode&~S_IFMT, perhaps ISARG */
500Sstevel@tonic-gate nlink_t fnl; /* number of links */
510Sstevel@tonic-gate uid_t fuid; /* owner id */
520Sstevel@tonic-gate gid_t fgid; /* group id */
530Sstevel@tonic-gate off_t fsize; /* file size */
540Sstevel@tonic-gate blkcnt_t fblks; /* number of blocks used */
550Sstevel@tonic-gate time_t fmtime; /* time (modify or access or create) */
560Sstevel@tonic-gate char *fname; /* file name */
570Sstevel@tonic-gate char *flinkto; /* symbolic link value */
580Sstevel@tonic-gate char acl; /* acl access flag */
590Sstevel@tonic-gate };
600Sstevel@tonic-gate
610Sstevel@tonic-gate #define ISARG 0x8000 /* extra ``mode'' */
620Sstevel@tonic-gate
630Sstevel@tonic-gate static struct subdirs {
640Sstevel@tonic-gate char *sd_name;
650Sstevel@tonic-gate struct subdirs *sd_next;
660Sstevel@tonic-gate } *subdirs;
670Sstevel@tonic-gate
680Sstevel@tonic-gate static int aflg, dflg, gflg, lflg, sflg, tflg, uflg, iflg, fflg, cflg;
690Sstevel@tonic-gate static int rflg = 1;
700Sstevel@tonic-gate static int qflg, Aflg, Cflg, Fflg, Lflg, Rflg;
710Sstevel@tonic-gate
720Sstevel@tonic-gate static int usetabs;
730Sstevel@tonic-gate
740Sstevel@tonic-gate static time_t now, sixmonthsago, onehourfromnow;
750Sstevel@tonic-gate
760Sstevel@tonic-gate static char *dotp = ".";
770Sstevel@tonic-gate
780Sstevel@tonic-gate static struct winsize win;
790Sstevel@tonic-gate static int twidth;
800Sstevel@tonic-gate
810Sstevel@tonic-gate static struct afile *gstat(struct afile *, char *, int, off_t *);
820Sstevel@tonic-gate static int fcmp(const void *, const void *);
830Sstevel@tonic-gate static char *cat(char *, char *);
840Sstevel@tonic-gate static char *savestr(char *);
850Sstevel@tonic-gate static char *fmtentry(struct afile *);
860Sstevel@tonic-gate static char *getname(), *getgroup();
870Sstevel@tonic-gate static void formatd(char *, int);
880Sstevel@tonic-gate static void formatf(struct afile *, struct afile *);
890Sstevel@tonic-gate static off_t getdir(char *, struct afile **, struct afile **);
900Sstevel@tonic-gate
910Sstevel@tonic-gate int
main(int argc,char ** argv)920Sstevel@tonic-gate main(int argc, char **argv)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate int i;
950Sstevel@tonic-gate struct afile *fp0, *fplast;
960Sstevel@tonic-gate register struct afile *fp;
970Sstevel@tonic-gate struct termios trbuf;
980Sstevel@tonic-gate
990Sstevel@tonic-gate argc--, argv++;
1000Sstevel@tonic-gate if (getuid() == 0)
1010Sstevel@tonic-gate Aflg++;
1020Sstevel@tonic-gate (void) time(&now);
1030Sstevel@tonic-gate sixmonthsago = now - 6L*30L*24L*60L*60L;
1040Sstevel@tonic-gate onehourfromnow = now + 60L*60L;
1050Sstevel@tonic-gate now += 60;
1060Sstevel@tonic-gate twidth = 80;
1070Sstevel@tonic-gate if (isatty(1)) {
1080Sstevel@tonic-gate qflg = Cflg = 1;
1090Sstevel@tonic-gate (void) ioctl(1, TCGETS, &trbuf);
1100Sstevel@tonic-gate if (ioctl(1, TIOCGWINSZ, &win) != -1)
1110Sstevel@tonic-gate twidth = (win.ws_col == 0 ? 80 : win.ws_col);
1120Sstevel@tonic-gate if ((trbuf.c_oflag & TABDLY) != TAB3)
1130Sstevel@tonic-gate usetabs = 1;
1140Sstevel@tonic-gate } else
1150Sstevel@tonic-gate usetabs = 1;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); /* set local environment */
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate while (argc > 0 && **argv == '-') {
1200Sstevel@tonic-gate (*argv)++;
1210Sstevel@tonic-gate while (**argv) {
1220Sstevel@tonic-gate switch (*(*argv)++) {
1230Sstevel@tonic-gate case 'C':
1240Sstevel@tonic-gate Cflg = 1; break;
1250Sstevel@tonic-gate case 'q':
1260Sstevel@tonic-gate qflg = 1; break;
1270Sstevel@tonic-gate case '1':
1280Sstevel@tonic-gate Cflg = 0; break;
1290Sstevel@tonic-gate case 'a':
1300Sstevel@tonic-gate aflg++; break;
1310Sstevel@tonic-gate case 'A':
1320Sstevel@tonic-gate Aflg++; break;
1330Sstevel@tonic-gate case 'c':
1340Sstevel@tonic-gate cflg++; break;
1350Sstevel@tonic-gate case 's':
1360Sstevel@tonic-gate sflg++; break;
1370Sstevel@tonic-gate case 'd':
1380Sstevel@tonic-gate dflg++; break;
1390Sstevel@tonic-gate case 'g':
1400Sstevel@tonic-gate gflg++; break;
1410Sstevel@tonic-gate case 'l':
1420Sstevel@tonic-gate lflg++; break;
1430Sstevel@tonic-gate case 'r':
1440Sstevel@tonic-gate rflg = -1; break;
1450Sstevel@tonic-gate case 't':
1460Sstevel@tonic-gate tflg++; break;
1470Sstevel@tonic-gate case 'u':
1480Sstevel@tonic-gate uflg++; break;
1490Sstevel@tonic-gate case 'i':
1500Sstevel@tonic-gate iflg++; break;
1510Sstevel@tonic-gate case 'f':
1520Sstevel@tonic-gate fflg++; break;
1530Sstevel@tonic-gate case 'L':
1540Sstevel@tonic-gate Lflg++; break;
1550Sstevel@tonic-gate case 'F':
1560Sstevel@tonic-gate Fflg++; break;
1570Sstevel@tonic-gate case 'R':
1580Sstevel@tonic-gate Rflg++; break;
1590Sstevel@tonic-gate }
1600Sstevel@tonic-gate }
1610Sstevel@tonic-gate argc--, argv++;
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate if (fflg) {
1640Sstevel@tonic-gate aflg++; lflg = 0; sflg = 0; tflg = 0;
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate if (lflg)
1670Sstevel@tonic-gate Cflg = 0;
1680Sstevel@tonic-gate if (argc == 0) {
1690Sstevel@tonic-gate argc++;
1700Sstevel@tonic-gate argv = &dotp;
1710Sstevel@tonic-gate }
1720Sstevel@tonic-gate fp = (struct afile *)calloc(argc, sizeof (struct afile));
1730Sstevel@tonic-gate if (fp == 0) {
1740Sstevel@tonic-gate (void) fprintf(stderr, "ls: out of memory\n");
1750Sstevel@tonic-gate exit(1);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate fp0 = fp;
1780Sstevel@tonic-gate for (i = 0; i < argc; i++) {
1790Sstevel@tonic-gate if (gstat(fp, *argv, 1, (off_t *)0)) {
1800Sstevel@tonic-gate fp->fname = *argv;
1810Sstevel@tonic-gate fp->fflags |= ISARG;
1820Sstevel@tonic-gate fp++;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate argv++;
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate fplast = fp;
1870Sstevel@tonic-gate qsort(fp0, fplast - fp0, sizeof (struct afile), fcmp);
1880Sstevel@tonic-gate if (dflg) {
1890Sstevel@tonic-gate formatf(fp0, fplast);
1900Sstevel@tonic-gate exit(0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate if (fflg)
1930Sstevel@tonic-gate fp = fp0;
1940Sstevel@tonic-gate else {
1950Sstevel@tonic-gate for (fp = fp0; fp < fplast && fp->ftype != 'd'; fp++)
1960Sstevel@tonic-gate continue;
1970Sstevel@tonic-gate formatf(fp0, fp);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate if (fp < fplast) {
2000Sstevel@tonic-gate if (fp > fp0)
2010Sstevel@tonic-gate (void) printf("\n");
2020Sstevel@tonic-gate for (;;) {
2030Sstevel@tonic-gate formatd(fp->fname, argc > 1);
2040Sstevel@tonic-gate while (subdirs) {
2050Sstevel@tonic-gate struct subdirs *t;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate t = subdirs; subdirs = t->sd_next;
2080Sstevel@tonic-gate (void) printf("\n");
2090Sstevel@tonic-gate formatd(t->sd_name, 1);
2100Sstevel@tonic-gate free(t->sd_name);
2110Sstevel@tonic-gate free(t);
2120Sstevel@tonic-gate }
2130Sstevel@tonic-gate if (++fp == fplast)
2140Sstevel@tonic-gate break;
2150Sstevel@tonic-gate (void) printf("\n");
2160Sstevel@tonic-gate }
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate return (0);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate static void
formatd(char * name,int title)2220Sstevel@tonic-gate formatd(char *name, int title)
2230Sstevel@tonic-gate {
2240Sstevel@tonic-gate register struct afile *fp;
2250Sstevel@tonic-gate register struct subdirs *dp;
2260Sstevel@tonic-gate struct afile *dfp0, *dfplast;
2270Sstevel@tonic-gate off_t nkb;
2280Sstevel@tonic-gate
2290Sstevel@tonic-gate nkb = getdir(name, &dfp0, &dfplast);
2300Sstevel@tonic-gate if (dfp0 == 0)
2310Sstevel@tonic-gate return;
2320Sstevel@tonic-gate if (fflg == 0)
2330Sstevel@tonic-gate qsort(dfp0, dfplast - dfp0, sizeof (struct afile), fcmp);
2340Sstevel@tonic-gate if (title)
2350Sstevel@tonic-gate (void) printf("%s:\n", name);
2360Sstevel@tonic-gate if (lflg || sflg)
2370Sstevel@tonic-gate (void) printf("total %lld\n", nkb);
2380Sstevel@tonic-gate formatf(dfp0, dfplast);
2390Sstevel@tonic-gate if (Rflg)
2400Sstevel@tonic-gate for (fp = dfplast - 1; fp >= dfp0; fp--) {
2410Sstevel@tonic-gate if (fp->ftype != 'd' ||
2420Sstevel@tonic-gate strcmp(fp->fname, ".") == 0 ||
2430Sstevel@tonic-gate strcmp(fp->fname, "..") == 0)
2440Sstevel@tonic-gate continue;
2450Sstevel@tonic-gate dp = (struct subdirs *)malloc(sizeof (struct subdirs));
2460Sstevel@tonic-gate dp->sd_name = savestr(cat(name, fp->fname));
2470Sstevel@tonic-gate dp->sd_next = subdirs; subdirs = dp;
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate for (fp = dfp0; fp < dfplast; fp++) {
2500Sstevel@tonic-gate if ((fp->fflags&ISARG) == 0 && fp->fname)
2510Sstevel@tonic-gate free(fp->fname);
2520Sstevel@tonic-gate if (fp->flinkto)
2530Sstevel@tonic-gate free(fp->flinkto);
2540Sstevel@tonic-gate }
2550Sstevel@tonic-gate free(dfp0);
2560Sstevel@tonic-gate }
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate static off_t
getdir(char * dir,struct afile ** pfp0,struct afile ** pfplast)2590Sstevel@tonic-gate getdir(char *dir, struct afile **pfp0, struct afile **pfplast)
2600Sstevel@tonic-gate {
2610Sstevel@tonic-gate register struct afile *fp;
2620Sstevel@tonic-gate DIR *dirp;
2630Sstevel@tonic-gate register struct dirent *dp;
2640Sstevel@tonic-gate off_t nb;
2650Sstevel@tonic-gate size_t nent = 20;
2660Sstevel@tonic-gate
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate * This code (opendir, readdir, and the "for" loop) is arranged in
2690Sstevel@tonic-gate * this strange manner to handle the case where UNIX lets root open
2700Sstevel@tonic-gate * any directory for reading, but NFS does not let root read the
2710Sstevel@tonic-gate * openned directory.
2720Sstevel@tonic-gate */
2730Sstevel@tonic-gate *pfp0 = *pfplast = NULL;
2740Sstevel@tonic-gate if ((dirp = opendir(dir)) == NULL) {
2750Sstevel@tonic-gate (void) printf("%s unreadable\n", dir); /* not stderr! */
2760Sstevel@tonic-gate return (0);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate errno = 0;
2790Sstevel@tonic-gate if (((dp = readdir(dirp)) == NULL) && (errno != 0)) {
2800Sstevel@tonic-gate /* root reading across NFS can get to this error case */
2810Sstevel@tonic-gate (void) printf("%s unreadable\n", dir); /* not stderr! */
2820Sstevel@tonic-gate (void) closedir(dirp);
2830Sstevel@tonic-gate return (0);
2840Sstevel@tonic-gate }
2850Sstevel@tonic-gate fp = *pfp0 = (struct afile *)calloc(nent, sizeof (struct afile));
2860Sstevel@tonic-gate *pfplast = *pfp0 + nent;
2870Sstevel@tonic-gate for (nb = 0; dp != NULL; dp = readdir(dirp)) {
2880Sstevel@tonic-gate if (dp->d_ino == 0)
2890Sstevel@tonic-gate continue;
2900Sstevel@tonic-gate if (aflg == 0 && dp->d_name[0] == '.' &&
2910Sstevel@tonic-gate (Aflg == 0 || dp->d_name[1] == 0 ||
2920Sstevel@tonic-gate dp->d_name[1] == '.' && dp->d_name[2] == 0))
2930Sstevel@tonic-gate continue;
2940Sstevel@tonic-gate if (gstat(fp, cat(dir, dp->d_name), Fflg+Rflg, &nb) == 0)
2950Sstevel@tonic-gate continue;
2960Sstevel@tonic-gate fp->fnum = dp->d_ino;
2970Sstevel@tonic-gate fp->fname = savestr(dp->d_name);
2980Sstevel@tonic-gate fp++;
2990Sstevel@tonic-gate if (fp == *pfplast) {
3000Sstevel@tonic-gate *pfp0 = (struct afile *)realloc((char *)*pfp0,
3010Sstevel@tonic-gate 2 * nent * sizeof (struct afile));
3020Sstevel@tonic-gate if (*pfp0 == 0) {
3030Sstevel@tonic-gate (void) fprintf(stderr, "ls: out of memory\n");
3040Sstevel@tonic-gate exit(1);
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate fp = *pfp0 + nent;
3070Sstevel@tonic-gate *pfplast = fp + nent;
3080Sstevel@tonic-gate nent *= 2;
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate (void) closedir(dirp);
3120Sstevel@tonic-gate *pfplast = fp;
3130Sstevel@tonic-gate return (dbtokb(nb));
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate
3170Sstevel@tonic-gate static struct afile *
gstat(struct afile * fp,char * file,int statarg,off_t * pnb)3180Sstevel@tonic-gate gstat(struct afile *fp, char *file, int statarg, off_t *pnb)
3190Sstevel@tonic-gate {
3200Sstevel@tonic-gate static struct afile azerofile;
3210Sstevel@tonic-gate int (*statf)() = Lflg ? stat : lstat;
3220Sstevel@tonic-gate int cc;
3230Sstevel@tonic-gate char buf[PATH_MAX];
3240Sstevel@tonic-gate int aclcnt;
3250Sstevel@tonic-gate aclent_t *aclp;
3260Sstevel@tonic-gate aclent_t *tp;
3270Sstevel@tonic-gate o_mode_t groupperm, mask;
3280Sstevel@tonic-gate int grouppermfound, maskfound;
3290Sstevel@tonic-gate
3300Sstevel@tonic-gate *fp = azerofile;
3310Sstevel@tonic-gate fp->fflags = 0;
3320Sstevel@tonic-gate fp->fnum = 0;
3330Sstevel@tonic-gate fp->ftype = '-';
3340Sstevel@tonic-gate if (statarg || sflg || lflg || tflg) {
3350Sstevel@tonic-gate struct stat stb, stb1;
3360Sstevel@tonic-gate
3370Sstevel@tonic-gate if ((*statf)(file, &stb) < 0) {
3380Sstevel@tonic-gate if (statf == lstat || lstat(file, &stb) < 0) {
3390Sstevel@tonic-gate if (errno == ENOENT)
3400Sstevel@tonic-gate (void) fprintf(stderr,
3410Sstevel@tonic-gate "%s not found\n", file);
3420Sstevel@tonic-gate else {
3430Sstevel@tonic-gate (void) fprintf(stderr, "ls: ");
3440Sstevel@tonic-gate perror(file);
3450Sstevel@tonic-gate }
3460Sstevel@tonic-gate return (0);
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate fp->fblks = stb.st_blocks;
3500Sstevel@tonic-gate fp->fsize = stb.st_size;
3510Sstevel@tonic-gate switch (stb.st_mode & S_IFMT) {
3520Sstevel@tonic-gate case S_IFDIR:
3530Sstevel@tonic-gate fp->ftype = 'd'; break;
3540Sstevel@tonic-gate case S_IFDOOR:
3550Sstevel@tonic-gate fp->ftype = 'D'; break;
3560Sstevel@tonic-gate case S_IFBLK:
3570Sstevel@tonic-gate fp->ftype = 'b'; fp->fsize = (off_t)stb.st_rdev; break;
3580Sstevel@tonic-gate case S_IFCHR:
3590Sstevel@tonic-gate fp->ftype = 'c'; fp->fsize = (off_t)stb.st_rdev; break;
3600Sstevel@tonic-gate case S_IFSOCK:
3610Sstevel@tonic-gate fp->ftype = 's'; fp->fsize = 0LL; break;
3620Sstevel@tonic-gate case S_IFIFO:
3630Sstevel@tonic-gate fp->ftype = 'p'; fp->fsize = 0LL; break;
3640Sstevel@tonic-gate case S_IFLNK:
3650Sstevel@tonic-gate fp->ftype = 'l';
3660Sstevel@tonic-gate if (lflg) {
3670Sstevel@tonic-gate cc = readlink(file, buf, BUFSIZ);
3680Sstevel@tonic-gate if (cc >= 0) {
3690Sstevel@tonic-gate /*
3700Sstevel@tonic-gate * here we follow the symbolic
3710Sstevel@tonic-gate * link to generate the proper
3720Sstevel@tonic-gate * Fflg marker for the object,
3730Sstevel@tonic-gate * eg, /bin -> /pub/bin/
3740Sstevel@tonic-gate */
3750Sstevel@tonic-gate buf[cc] = 0;
3760Sstevel@tonic-gate if (Fflg && !stat(file, &stb1))
3770Sstevel@tonic-gate switch (stb1.st_mode & S_IFMT) {
3780Sstevel@tonic-gate case S_IFDIR:
3790Sstevel@tonic-gate buf[cc++] = '/';
3800Sstevel@tonic-gate break;
3810Sstevel@tonic-gate case S_IFDOOR:
3820Sstevel@tonic-gate buf[cc++] = '>';
3830Sstevel@tonic-gate break;
3840Sstevel@tonic-gate case S_IFIFO:
3850Sstevel@tonic-gate buf[cc++] = '|';
3860Sstevel@tonic-gate break;
3870Sstevel@tonic-gate case S_IFSOCK:
3880Sstevel@tonic-gate buf[cc++] = '=';
3890Sstevel@tonic-gate break;
3900Sstevel@tonic-gate default:
3910Sstevel@tonic-gate if ((stb1.st_mode & ~S_IFMT)
3920Sstevel@tonic-gate & 0111)
3930Sstevel@tonic-gate buf[cc++] = '*';
3940Sstevel@tonic-gate break;
3950Sstevel@tonic-gate }
3960Sstevel@tonic-gate buf[cc] = 0;
3970Sstevel@tonic-gate fp->flinkto = savestr(buf);
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate break;
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate /*
4020Sstevel@tonic-gate * this is a hack from UCB to avoid having
4030Sstevel@tonic-gate * ls /bin behave differently from ls /bin/
4040Sstevel@tonic-gate * when /bin is a symbolic link. We hack the
4050Sstevel@tonic-gate * hack to have that happen, but only for
4060Sstevel@tonic-gate * explicit arguments, by inspecting pnb.
4070Sstevel@tonic-gate */
4080Sstevel@tonic-gate if (pnb != (off_t *)0 || stat(file, &stb1) < 0)
4090Sstevel@tonic-gate break;
4100Sstevel@tonic-gate if ((stb1.st_mode & S_IFMT) == S_IFDIR) {
4110Sstevel@tonic-gate stb = stb1;
4120Sstevel@tonic-gate fp->ftype = 'd';
4130Sstevel@tonic-gate fp->fsize = stb.st_size;
4140Sstevel@tonic-gate fp->fblks = stb.st_blocks;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate break;
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate fp->fnum = stb.st_ino;
4190Sstevel@tonic-gate fp->fflags = stb.st_mode & ~S_IFMT;
4200Sstevel@tonic-gate fp->fnl = stb.st_nlink;
4210Sstevel@tonic-gate fp->fuid = stb.st_uid;
4220Sstevel@tonic-gate fp->fgid = stb.st_gid;
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate /* ACL: check acl entries count */
4250Sstevel@tonic-gate if ((aclcnt = acl(file, GETACLCNT, 0, NULL)) >
4260Sstevel@tonic-gate MIN_ACL_ENTRIES) {
4270Sstevel@tonic-gate
4280Sstevel@tonic-gate /* this file has a non-trivial acl */
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate fp->acl = '+';
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate * For files with non-trivial acls, the
4340Sstevel@tonic-gate * effective group permissions are the
4350Sstevel@tonic-gate * intersection of the GROUP_OBJ value and
4360Sstevel@tonic-gate * the CLASS_OBJ (acl mask) value. Determine
4370Sstevel@tonic-gate * both the GROUP_OBJ and CLASS_OBJ for this
4380Sstevel@tonic-gate * file and insert the logical AND of those
4390Sstevel@tonic-gate * two values in the group permissions field
4400Sstevel@tonic-gate * of the lflags value for this file.
4410Sstevel@tonic-gate */
4420Sstevel@tonic-gate
4430Sstevel@tonic-gate if ((aclp = (aclent_t *)malloc(
4440Sstevel@tonic-gate (sizeof (aclent_t)) * aclcnt)) == NULL) {
4450Sstevel@tonic-gate perror("ls");
4460Sstevel@tonic-gate exit(2);
4470Sstevel@tonic-gate }
4480Sstevel@tonic-gate
4490Sstevel@tonic-gate if (acl(file, GETACL, aclcnt, aclp) < 0) {
4500Sstevel@tonic-gate free(aclp);
4510Sstevel@tonic-gate (void) fprintf(stderr, "ls: ");
4520Sstevel@tonic-gate perror(file);
4530Sstevel@tonic-gate return (0);
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate /*
4570Sstevel@tonic-gate * Until found in acl list, assume maximum
4580Sstevel@tonic-gate * permissions for both group and mask. (Just
4590Sstevel@tonic-gate * in case the acl lacks either value for
4600Sstevel@tonic-gate * some reason.)
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate groupperm = 07;
4630Sstevel@tonic-gate mask = 07;
4640Sstevel@tonic-gate grouppermfound = 0;
4650Sstevel@tonic-gate maskfound = 0;
4660Sstevel@tonic-gate for (tp = aclp; aclcnt--; tp++) {
4670Sstevel@tonic-gate if (tp->a_type == GROUP_OBJ) {
4680Sstevel@tonic-gate groupperm = tp->a_perm;
4690Sstevel@tonic-gate grouppermfound = 1;
4700Sstevel@tonic-gate continue;
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate if (tp->a_type == CLASS_OBJ) {
4730Sstevel@tonic-gate mask = tp->a_perm;
4740Sstevel@tonic-gate maskfound = 1;
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate if (grouppermfound && maskfound)
4770Sstevel@tonic-gate break;
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate free(aclp);
4810Sstevel@tonic-gate
4820Sstevel@tonic-gate /* reset all the group bits */
4830Sstevel@tonic-gate fp->fflags &= ~S_IRWXG;
4840Sstevel@tonic-gate
4850Sstevel@tonic-gate /*
4860Sstevel@tonic-gate * Now set them to the logical AND of the
4870Sstevel@tonic-gate * GROUP_OBJ permissions and the acl mask.
4880Sstevel@tonic-gate */
4890Sstevel@tonic-gate
4900Sstevel@tonic-gate fp->fflags |= (groupperm & mask) << 3;
4910Sstevel@tonic-gate } else
4920Sstevel@tonic-gate fp->acl = ' ';
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate if (uflg)
4950Sstevel@tonic-gate fp->fmtime = stb.st_atime;
4960Sstevel@tonic-gate else if (cflg)
4970Sstevel@tonic-gate fp->fmtime = stb.st_ctime;
4980Sstevel@tonic-gate else
4990Sstevel@tonic-gate fp->fmtime = stb.st_mtime;
5000Sstevel@tonic-gate if (pnb)
5010Sstevel@tonic-gate *pnb += stb.st_blocks;
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate return (fp);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate static void
formatf(struct afile * fp0,struct afile * fplast)5070Sstevel@tonic-gate formatf(struct afile *fp0, struct afile *fplast)
5080Sstevel@tonic-gate {
5090Sstevel@tonic-gate register struct afile *fp;
5100Sstevel@tonic-gate int width = 0, w, nentry = fplast - fp0;
5110Sstevel@tonic-gate int i, j, columns, lines;
5120Sstevel@tonic-gate char *cp;
5130Sstevel@tonic-gate
5140Sstevel@tonic-gate if (fp0 == fplast)
5150Sstevel@tonic-gate return;
5160Sstevel@tonic-gate if (lflg || Cflg == 0)
5170Sstevel@tonic-gate columns = 1;
5180Sstevel@tonic-gate else {
5190Sstevel@tonic-gate for (fp = fp0; fp < fplast; fp++) {
5200Sstevel@tonic-gate int len = strlen(fmtentry(fp));
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate if (len > width)
5230Sstevel@tonic-gate width = len;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate if (usetabs)
5260Sstevel@tonic-gate width = (width + 8) &~ 7;
5270Sstevel@tonic-gate else
5280Sstevel@tonic-gate width += 2;
5290Sstevel@tonic-gate columns = twidth / width;
5300Sstevel@tonic-gate if (columns == 0)
5310Sstevel@tonic-gate columns = 1;
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate lines = (nentry + columns - 1) / columns;
5340Sstevel@tonic-gate for (i = 0; i < lines; i++) {
5350Sstevel@tonic-gate for (j = 0; j < columns; j++) {
5360Sstevel@tonic-gate fp = fp0 + j * lines + i;
5370Sstevel@tonic-gate cp = fmtentry(fp);
5380Sstevel@tonic-gate (void) printf("%s", cp);
5390Sstevel@tonic-gate if (fp + lines >= fplast) {
5400Sstevel@tonic-gate (void) printf("\n");
5410Sstevel@tonic-gate break;
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate w = strlen(cp);
5440Sstevel@tonic-gate while (w < width)
5450Sstevel@tonic-gate if (usetabs) {
5460Sstevel@tonic-gate w = (w + 8) &~ 7;
5470Sstevel@tonic-gate (void) putchar('\t');
5480Sstevel@tonic-gate } else {
5490Sstevel@tonic-gate w++;
5500Sstevel@tonic-gate (void) putchar(' ');
5510Sstevel@tonic-gate }
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate }
5550Sstevel@tonic-gate
5560Sstevel@tonic-gate static int
fcmp(const void * arg1,const void * arg2)5570Sstevel@tonic-gate fcmp(const void *arg1, const void *arg2)
5580Sstevel@tonic-gate {
5590Sstevel@tonic-gate const struct afile *f1 = arg1;
5600Sstevel@tonic-gate const struct afile *f2 = arg2;
5610Sstevel@tonic-gate
5620Sstevel@tonic-gate if (dflg == 0 && fflg == 0) {
5630Sstevel@tonic-gate if ((f1->fflags&ISARG) && f1->ftype == 'd') {
5640Sstevel@tonic-gate if ((f2->fflags&ISARG) == 0 || f2->ftype != 'd')
5650Sstevel@tonic-gate return (1);
5660Sstevel@tonic-gate } else {
5670Sstevel@tonic-gate if ((f2->fflags&ISARG) && f2->ftype == 'd')
5680Sstevel@tonic-gate return (-1);
5690Sstevel@tonic-gate }
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate if (tflg) {
5720Sstevel@tonic-gate if (f2->fmtime == f1->fmtime)
5730Sstevel@tonic-gate return (0);
5740Sstevel@tonic-gate if (f2->fmtime > f1->fmtime)
5750Sstevel@tonic-gate return (rflg);
5760Sstevel@tonic-gate return (-rflg);
5770Sstevel@tonic-gate }
5780Sstevel@tonic-gate return (rflg * strcmp(f1->fname, f2->fname));
5790Sstevel@tonic-gate }
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate static char *
cat(char * dir,char * file)5820Sstevel@tonic-gate cat(char *dir, char *file)
5830Sstevel@tonic-gate {
5840Sstevel@tonic-gate static char dfile[BUFSIZ];
5850Sstevel@tonic-gate
5860Sstevel@tonic-gate if (strlen(dir)+1+strlen(file)+1 > BUFSIZ) {
5870Sstevel@tonic-gate (void) fprintf(stderr, "ls: filename too long\n");
5880Sstevel@tonic-gate exit(1);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate if (strcmp(dir, "") == 0 || strcmp(dir, ".") == 0) {
5910Sstevel@tonic-gate (void) strcpy(dfile, file);
5920Sstevel@tonic-gate return (dfile);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate (void) strcpy(dfile, dir);
5950Sstevel@tonic-gate if (dir[strlen(dir) - 1] != '/' && *file != '/')
5960Sstevel@tonic-gate (void) strcat(dfile, "/");
5970Sstevel@tonic-gate (void) strcat(dfile, file);
5980Sstevel@tonic-gate return (dfile);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate
6010Sstevel@tonic-gate static char *
savestr(char * str)6020Sstevel@tonic-gate savestr(char *str)
6030Sstevel@tonic-gate {
6040Sstevel@tonic-gate char *cp = malloc(strlen(str) + 1);
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate if (cp == NULL) {
6070Sstevel@tonic-gate (void) fprintf(stderr, "ls: out of memory\n");
6080Sstevel@tonic-gate exit(1);
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate (void) strcpy(cp, str);
6110Sstevel@tonic-gate return (cp);
6120Sstevel@tonic-gate }
6130Sstevel@tonic-gate
6140Sstevel@tonic-gate static char *fmtinum(struct afile *);
6150Sstevel@tonic-gate static char *fmtsize(struct afile *);
6160Sstevel@tonic-gate static char *fmtlstuff(struct afile *);
6170Sstevel@tonic-gate static char *fmtmode(char *, int);
6180Sstevel@tonic-gate
6190Sstevel@tonic-gate static char *
fmtentry(struct afile * fp)6200Sstevel@tonic-gate fmtentry(struct afile *fp)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate static char fmtres[BUFSIZ];
6230Sstevel@tonic-gate register char *cp, *dp;
6240Sstevel@tonic-gate
6250Sstevel@tonic-gate (void) sprintf(fmtres, "%s%s%s",
6260Sstevel@tonic-gate iflg ? fmtinum(fp) : "",
6270Sstevel@tonic-gate sflg ? fmtsize(fp) : "",
6280Sstevel@tonic-gate lflg ? fmtlstuff(fp) : "");
6290Sstevel@tonic-gate dp = &fmtres[strlen(fmtres)];
6300Sstevel@tonic-gate for (cp = fp->fname; *cp; cp++)
6310Sstevel@tonic-gate if (qflg && !isprint((unsigned char)*cp))
6320Sstevel@tonic-gate *dp++ = '?';
6330Sstevel@tonic-gate else
6340Sstevel@tonic-gate *dp++ = *cp;
6350Sstevel@tonic-gate /* avoid both "->" and trailing marks */
6360Sstevel@tonic-gate if (Fflg && ! (lflg && fp->flinkto)) {
6370Sstevel@tonic-gate if (fp->ftype == 'd')
6380Sstevel@tonic-gate *dp++ = '/';
6390Sstevel@tonic-gate else if (fp->ftype == 'D')
6400Sstevel@tonic-gate *dp++ = '>';
6410Sstevel@tonic-gate else if (fp->ftype == 'p')
6420Sstevel@tonic-gate *dp++ = '|';
6430Sstevel@tonic-gate else if (fp->ftype == 'l')
6440Sstevel@tonic-gate *dp++ = '@';
6450Sstevel@tonic-gate else if (fp->ftype == 's')
6460Sstevel@tonic-gate *dp++ = '=';
6470Sstevel@tonic-gate else if (fp->fflags & 0111)
6480Sstevel@tonic-gate *dp++ = '*';
6490Sstevel@tonic-gate }
6500Sstevel@tonic-gate if (lflg && fp->flinkto) {
6510Sstevel@tonic-gate (void) strcpy(dp, " -> "); dp += 4;
6520Sstevel@tonic-gate for (cp = fp->flinkto; *cp; cp++)
6530Sstevel@tonic-gate if (qflg && !isprint((unsigned char) *cp))
6540Sstevel@tonic-gate *dp++ = '?';
6550Sstevel@tonic-gate else
6560Sstevel@tonic-gate *dp++ = *cp;
6570Sstevel@tonic-gate }
6580Sstevel@tonic-gate *dp++ = 0;
6590Sstevel@tonic-gate return (fmtres);
6600Sstevel@tonic-gate }
6610Sstevel@tonic-gate
6620Sstevel@tonic-gate static char *
fmtinum(struct afile * p)6630Sstevel@tonic-gate fmtinum(struct afile *p)
6640Sstevel@tonic-gate {
6650Sstevel@tonic-gate static char inumbuf[12];
6660Sstevel@tonic-gate
6670Sstevel@tonic-gate (void) sprintf(inumbuf, "%10llu ", p->fnum);
6680Sstevel@tonic-gate return (inumbuf);
6690Sstevel@tonic-gate }
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate static char *
fmtsize(struct afile * p)6720Sstevel@tonic-gate fmtsize(struct afile *p)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate static char sizebuf[32];
6750Sstevel@tonic-gate
6760Sstevel@tonic-gate (void) sprintf(sizebuf, (off_t)dbtokb(p->fblks) < 10000 ? "%4lld " : \
6770Sstevel@tonic-gate "%lld ", (off_t)dbtokb(p->fblks));
6780Sstevel@tonic-gate return (sizebuf);
6790Sstevel@tonic-gate }
6800Sstevel@tonic-gate
6810Sstevel@tonic-gate static char *
fmtlstuff(struct afile * p)6820Sstevel@tonic-gate fmtlstuff(struct afile *p)
6830Sstevel@tonic-gate {
6840Sstevel@tonic-gate static char lstuffbuf[256];
6850Sstevel@tonic-gate char gname[32], uname[32], fsize[32], ftime[32];
6860Sstevel@tonic-gate register char *lp = lstuffbuf;
6870Sstevel@tonic-gate
6880Sstevel@tonic-gate /* type mode uname gname fsize ftime */
6890Sstevel@tonic-gate /* get uname */
6900Sstevel@tonic-gate {
6910Sstevel@tonic-gate char *cp = getname(p->fuid);
6922168Scf46844 (void) sprintf(uname, "%-8s ", cp);
6930Sstevel@tonic-gate }
6940Sstevel@tonic-gate /* get gname */
6950Sstevel@tonic-gate if (gflg) {
6960Sstevel@tonic-gate char *cp = getgroup(p->fgid);
6972168Scf46844 (void) sprintf(gname, "%-8s ", cp);
6980Sstevel@tonic-gate }
6990Sstevel@tonic-gate /* get fsize */
7000Sstevel@tonic-gate if (p->ftype == 'b' || p->ftype == 'c')
7010Sstevel@tonic-gate (void) sprintf(fsize, "%3ld,%4ld",
7020Sstevel@tonic-gate major(p->fsize), minor(p->fsize));
7030Sstevel@tonic-gate else if (p->ftype == 's')
7040Sstevel@tonic-gate (void) sprintf(fsize, "%8d", 0);
7050Sstevel@tonic-gate else
7060Sstevel@tonic-gate (void) sprintf(fsize, p->fsize < 100000000 ? "%8lld" : \
7070Sstevel@tonic-gate "%lld", p->fsize);
7080Sstevel@tonic-gate /* get ftime */
7090Sstevel@tonic-gate {
7100Sstevel@tonic-gate char *cp = ctime(&p->fmtime);
7110Sstevel@tonic-gate if ((p->fmtime < sixmonthsago) || (p->fmtime > onehourfromnow))
7120Sstevel@tonic-gate (void) sprintf(ftime, " %-7.7s %-4.4s ", cp+4, cp+20);
7130Sstevel@tonic-gate else
7140Sstevel@tonic-gate (void) sprintf(ftime, " %-12.12s ", cp+4);
7150Sstevel@tonic-gate }
7160Sstevel@tonic-gate /* splat */
7170Sstevel@tonic-gate *lp++ = p->ftype;
7180Sstevel@tonic-gate lp = fmtmode(lp, p->fflags);
7190Sstevel@tonic-gate (void) sprintf(lp, "%c%3ld %s%s%s%s",
7200Sstevel@tonic-gate p->acl, p->fnl, uname, gflg ? gname : "", fsize, ftime);
7210Sstevel@tonic-gate return (lstuffbuf);
7220Sstevel@tonic-gate }
7230Sstevel@tonic-gate
7240Sstevel@tonic-gate static int m1[] =
7250Sstevel@tonic-gate { 1, S_IREAD>>0, 'r', '-' };
7260Sstevel@tonic-gate static int m2[] =
7270Sstevel@tonic-gate { 1, S_IWRITE>>0, 'w', '-' };
7280Sstevel@tonic-gate static int m3[] =
7290Sstevel@tonic-gate { 3, S_ISUID|(S_IEXEC>>0), 's', S_IEXEC>>0, 'x', S_ISUID, 'S', '-' };
7300Sstevel@tonic-gate static int m4[] =
7310Sstevel@tonic-gate { 1, S_IREAD>>3, 'r', '-' };
7320Sstevel@tonic-gate static int m5[] =
7330Sstevel@tonic-gate { 1, S_IWRITE>>3, 'w', '-' };
7340Sstevel@tonic-gate static int m6[] =
7350Sstevel@tonic-gate { 3, S_ISGID|(S_IEXEC>>3), 's', S_IEXEC>>3, 'x', S_ISGID, 'S', '-' };
7360Sstevel@tonic-gate static int m7[] =
7370Sstevel@tonic-gate { 1, S_IREAD>>6, 'r', '-' };
7380Sstevel@tonic-gate static int m8[] =
7390Sstevel@tonic-gate { 1, S_IWRITE>>6, 'w', '-' };
7400Sstevel@tonic-gate static int m9[] =
7410Sstevel@tonic-gate { 3, S_ISVTX|(S_IEXEC>>6), 't', S_ISVTX, 'T', S_IEXEC>>6, 'x', '-' };
7420Sstevel@tonic-gate
7430Sstevel@tonic-gate static int *m[] = { m1, m2, m3, m4, m5, m6, m7, m8, m9};
7440Sstevel@tonic-gate
7450Sstevel@tonic-gate static char *
fmtmode(char * lp,int flags)7460Sstevel@tonic-gate fmtmode(char *lp, int flags)
7470Sstevel@tonic-gate {
7480Sstevel@tonic-gate int **mp;
7490Sstevel@tonic-gate
7500Sstevel@tonic-gate for (mp = &m[0]; mp < &m[sizeof (m)/sizeof (m[0])]; ) {
7510Sstevel@tonic-gate register int *pairp = *mp++;
7520Sstevel@tonic-gate register int n = *pairp++;
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate while (n-- > 0) {
7550Sstevel@tonic-gate if ((flags&*pairp) == *pairp) {
7560Sstevel@tonic-gate pairp++;
7570Sstevel@tonic-gate break;
7580Sstevel@tonic-gate } else
7590Sstevel@tonic-gate pairp += 2;
7600Sstevel@tonic-gate }
7610Sstevel@tonic-gate *lp++ = *pairp;
7620Sstevel@tonic-gate }
7630Sstevel@tonic-gate return (lp);
7640Sstevel@tonic-gate }
7650Sstevel@tonic-gate
7660Sstevel@tonic-gate /* rest should be done with nameserver or database */
7670Sstevel@tonic-gate
7680Sstevel@tonic-gate #include <pwd.h>
7690Sstevel@tonic-gate #include <grp.h>
7700Sstevel@tonic-gate #include <utmpx.h>
7710Sstevel@tonic-gate
7720Sstevel@tonic-gate #define NMAX (sizeof (((struct utmpx *)0)->ut_name))
7730Sstevel@tonic-gate #define SCPYN(a, b) strncpy(a, b, NMAX)
7740Sstevel@tonic-gate
7750Sstevel@tonic-gate
7760Sstevel@tonic-gate static struct cachenode { /* this struct must be zeroed before using */
7770Sstevel@tonic-gate struct cachenode *lesschild; /* subtree whose entries < val */
7780Sstevel@tonic-gate struct cachenode *grtrchild; /* subtree whose entries > val */
7790Sstevel@tonic-gate int val; /* the uid or gid of this entry */
7800Sstevel@tonic-gate int initted; /* name has been filled in */
7810Sstevel@tonic-gate char name[NMAX+1]; /* the string that val maps to */
7820Sstevel@tonic-gate } *names, *groups;
7830Sstevel@tonic-gate
7840Sstevel@tonic-gate static struct cachenode *
findincache(struct cachenode ** head,id_t val)7850Sstevel@tonic-gate findincache(struct cachenode **head, id_t val)
7860Sstevel@tonic-gate {
7870Sstevel@tonic-gate register struct cachenode **parent = head;
7880Sstevel@tonic-gate register struct cachenode *c = *parent;
7890Sstevel@tonic-gate
7900Sstevel@tonic-gate while (c != NULL) {
7910Sstevel@tonic-gate if (val == c->val) {
7920Sstevel@tonic-gate /* found it */
7930Sstevel@tonic-gate return (c);
7940Sstevel@tonic-gate } else if (val < c->val) {
7950Sstevel@tonic-gate parent = &c->lesschild;
7960Sstevel@tonic-gate c = c->lesschild;
7970Sstevel@tonic-gate } else {
7980Sstevel@tonic-gate parent = &c->grtrchild;
7990Sstevel@tonic-gate c = c->grtrchild;
8000Sstevel@tonic-gate }
8010Sstevel@tonic-gate }
8020Sstevel@tonic-gate
8030Sstevel@tonic-gate /* not in the cache, make a new entry for it */
8040Sstevel@tonic-gate *parent = c = (struct cachenode *)calloc(1, sizeof (struct cachenode));
8050Sstevel@tonic-gate c->val = val;
8060Sstevel@tonic-gate return (c);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate
8090Sstevel@tonic-gate static char *
getname(uid_t uid)8100Sstevel@tonic-gate getname(uid_t uid)
8110Sstevel@tonic-gate {
8120Sstevel@tonic-gate struct cachenode *c;
8130Sstevel@tonic-gate struct passwd *pw;
8140Sstevel@tonic-gate
8150Sstevel@tonic-gate c = findincache(&names, uid);
8160Sstevel@tonic-gate if (c->initted == 0) {
8170Sstevel@tonic-gate if ((pw = getpwuid(uid)) != NULL) {
8180Sstevel@tonic-gate (void) SCPYN(&c->name[0], pw->pw_name);
8190Sstevel@tonic-gate } else {
8200Sstevel@tonic-gate (void) sprintf(&c->name[0], "%-8lu ", uid);
8210Sstevel@tonic-gate }
8220Sstevel@tonic-gate c->initted = 1;
8230Sstevel@tonic-gate }
8240Sstevel@tonic-gate return (&c->name[0]);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate static char *
getgroup(gid_t gid)8280Sstevel@tonic-gate getgroup(gid_t gid)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate struct cachenode *c;
8310Sstevel@tonic-gate struct group *gr;
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate c = findincache(&groups, gid);
8340Sstevel@tonic-gate if (c->initted == 0) {
8350Sstevel@tonic-gate if ((gr = getgrgid(gid)) != NULL) {
8360Sstevel@tonic-gate (void) SCPYN(&c->name[0], gr->gr_name);
8370Sstevel@tonic-gate } else {
8380Sstevel@tonic-gate (void) sprintf(&c->name[0], "%-8lu ", gid);
8390Sstevel@tonic-gate }
8400Sstevel@tonic-gate c->initted = 1;
8410Sstevel@tonic-gate }
8420Sstevel@tonic-gate return (&c->name[0]);
8430Sstevel@tonic-gate }
844