ls.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/* Copyright (c) 1987, 1988 Microsoft Corporation */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* List files or directories
*/
#include <wchar.h>
#include <stdio.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#include <locale.h>
#include <curses.h>
#include <termios.h>
#include <stdlib.h>
#include <widec.h>
#include <locale.h>
#include <wctype.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include <fcntl.h>
#include <unistd.h>
#include <libgen.h>
#include <errno.h>
#include <libcmdutils.h>
#ifndef STANDALONE
#define TERMINFO
#endif
/*
* -DNOTERMINFO can be defined on the cc command line to prevent
* the use of terminfo. This should be done on systems not having
* the terminfo feature(pre 6.0 sytems ?).
* As a result, columnar listings assume 80 columns for output,
* unless told otherwise via the COLUMNS environment variable.
*/
#ifdef NOTERMINFO
#endif
#include <term.h>
#define BFSIZE 16
/* this bit equals 1 in lflags of structure lbuf if *namep is to be used */
#define ISARG 0100000
/*
* this flag has been added to manipulate the display of S instead of 'l' when
* the file is not a regular file and when group execution bit is off
*/
#define LS_NOTREG 010000
/*
* Date and time formats
*
* b --- abbreviated month name
* e --- day number
* Y --- year in the form ccyy
* H --- hour(24-hour version)
* M --- minute
* F --- yyyy-mm-dd
* T --- hh:mm:ss
* z --- time zone as hours displacement from UTC
* note that %F and %z are from the ISO C99 standard and are
* not present in older C libraries
*/
#define FORMAT1 " %b %e %Y "
#define FORMAT2 " %b %e %H:%M "
#define FORMAT3 " %b %e %T %Y "
#define FORMAT4 " %%F %%T.%.09ld %%z "
#define BUFSIZ 4096
#define NUMBER_WIDTH 40
#define FMTSIZE 50
struct ditem {
};
struct lbuf {
union {
char *namep; /* for name in ls-command; */
} ln;
char ltype; /* filetype */
char *flinkto; /* symbolic link contents */
char acl; /* indicate there are additional acl entries */
int cycle; /* cycle detected flag */
};
struct dchain {
char *dc_name; /* path name */
int cycle_detected; /* cycle detected visiting this directory */
};
/*
* A numbuf_t is used when converting a number to a string representation
*/
typedef char numbuf_t[NUMBER_WIDTH];
static char *curdir; /* the current directory */
static int nfiles = 0; /* number of flist entries in current use */
static int nargs = 0; /* number of flist entries used for arguments */
static int maxn = 0; /* number of flist entries with lbufs asigned */
static char *makename(char *, char *);
static void column(void);
static void selection(int *);
static void new_line(void);
static int strcol(unsigned char *);
static void pdirectory(char *, int, int, int, struct ditem *);
static void csi_pprintf(unsigned char *);
static void pprintf(char *, char *);
unsigned long long number,
long scale);
int, struct ditem *);
static int aflg;
static int atflg;
static int bflg;
static int cflg;
static int dflg;
static int eflg;
static int fflg;
static int gflg;
static int hflg;
static int iflg;
static int lflg;
static int mflg;
static int nflg;
static int oflg;
static int pflg;
static int qflg;
static int sflg;
static int tflg;
static int uflg;
static int xflg;
static int Aflg;
static int Cflg;
static int Eflg;
static int Fflg;
static int Hflg;
static int Lflg;
static int Rflg;
static int Sflg;
static long hscale;
static int err = 0; /* Contains return code */
/* statreq > 0 if any of sflg, (n)lflg, tflg, Sflg are on */
static int statreq;
static char *dotp = ".";
static int num_cols = 80;
static int colwidth;
static int filewidth;
static int fixedwidth;
static int nomocore;
static int curcol;
#define NOTWORKINGDIR(d, l) (((l) < 2) || \
#define NOTPARENTDIR(d, l) (((l) < 3) || \
int
{
int c;
int i;
int width;
int amino = 0;
int opterr = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
#ifdef STANDALONE
if (argv[0][0] == '\0')
#endif
if (isatty(1)) {
Cflg = 1;
mflg = 0;
}
"aAbcCdeEfFghHilLmnopqrRsStux1@")) != EOF)
switch (c) {
case 'a':
aflg++;
continue;
case 'A':
Aflg++;
continue;
case 'b':
bflg = 1;
qflg = 0;
continue;
case 'c':
uflg = 0;
cflg++;
continue;
case 'C':
Cflg = 1;
mflg = 0;
#ifdef XPG4
lflg = 0;
#endif
continue;
case 'd':
dflg++;
continue;
case 'e':
eflg++;
lflg++;
statreq++;
Eflg = 0;
continue;
case 'E':
Eflg++;
lflg++;
statreq++;
eflg = 0;
continue;
case 'f':
fflg++;
continue;
case 'F':
Fflg++;
statreq++;
continue;
case 'g':
gflg++;
lflg++;
statreq++;
continue;
case 'h':
hflg++;
hscale = 1024;
continue;
case 'H':
Hflg++;
/* -H and -L are mutually exclusive */
Lflg = 0;
continue;
case 'i':
iflg++;
continue;
case 'l':
lflg++;
statreq++;
Cflg = 0;
xflg = 0;
mflg = 0;
atflg = 0;
continue;
case 'L':
Lflg++;
/* -H and -L are mutually exclusive */
Hflg = 0;
continue;
case 'm':
Cflg = 0;
mflg = 1;
#ifdef XPG4
lflg = 0;
#endif
continue;
case 'n':
nflg++;
lflg++;
statreq++;
Cflg = 0;
xflg = 0;
mflg = 0;
atflg = 0;
continue;
case 'o':
oflg++;
lflg++;
statreq++;
continue;
case 'p':
pflg++;
statreq++;
continue;
case 'q':
qflg = 1;
bflg = 0;
continue;
case 'r':
rflg = -1;
continue;
case 'R':
Rflg++;
statreq++;
continue;
case 's':
sflg++;
statreq++;
continue;
case 'S':
tflg = 0;
Sflg++;
statreq++;
continue;
case 't':
Sflg = 0;
tflg++;
statreq++;
continue;
case 'u':
cflg = 0;
uflg++;
continue;
case 'x':
xflg = 1;
Cflg = 1;
mflg = 0;
#ifdef XPG4
lflg = 0;
#endif
continue;
case '1':
Cflg = 0;
continue;
case '@':
#if !defined(XPG4)
/*
* -l has precedence over -@
*/
if (lflg)
continue;
#endif
atflg++;
lflg++;
statreq++;
Cflg = 0;
xflg = 0;
mflg = 0;
continue;
case '?':
opterr++;
continue;
}
if (opterr) {
"usage: ls -aAbcCdeEfFghHilLmnopqrRsStux1@ [files]\n"));
exit(2);
}
if (fflg) {
aflg++;
lflg = 0;
sflg = 0;
tflg = 0;
Sflg = 0;
statreq = 0;
}
fixedwidth = 2;
fixedwidth++;
if (iflg)
fixedwidth += 11;
if (sflg)
fixedwidth += 5;
if (lflg) {
else
}
char *clptr;
#ifdef TERMINFO
else {
}
#endif
/* assume it is an error */
num_cols = 80;
}
/* allocate space for flist and the associated */
/* data structures (lbufs) */
perror("ls");
exit(2);
}
/*
* case when no names are given
* in ls-command and current
* directory is to be used
*/
}
/*
* If we are recursing, we need to make sure we don't
* get into an endless loop. To keep track of the inodes
* (actually, just the directories) visited, we
* maintain a directory ancestry list for a file
* hierarchy. As we go deeper into the hierarchy,
* a parent directory passes its directory list
* info (device id, inode number, and a pointer to
* its parent) to each of its children. As we
* process a child that is a directory, we save
* its own personal directory list info. We then
* check to see if the child has already been
* processed by comparing its device id and inode
* number from its own personal directory list info
* to that of each of its ancestors. If there is a
* match, then we know we've detected a cycle.
*/
if (Rflg) {
/*
* This is the first parent in this lineage
* (first in a directory hierarchy), so
* this parent's parent doesn't exist. We
* only initialize myinfo when we are
* recursing, otherwise it's not used.
*/
perror("ls");
exit(2);
} else {
}
}
}
if (nomocore)
exit(2);
err = 2;
optind++;
continue;
}
optind++;
nargs++; /* count good arguments stored in flist */
}
(int (*)(const void *, const void *))compar);
for (i = 0; i < nargs; i++) {
break;
}
for (; i < nargs; i++) {
if (nomocore)
exit(2);
/* -R: print subdirectories found */
/* Place direct subdirs on front in right order */
while (cdfirst) {
/* reverse cdfirst onto front of dfirst */
}
/* take off first dir on dfirst & print it */
if (nomocore)
exit(2);
}
}
return (err);
}
/*
* pdirectory: print the directory name, labelling it if title is
* nonzero, using lp as the place to start reading in the dir.
*/
static void
{
char *pname;
int j;
filewidth = 0;
if (title) {
if (!first)
new_line();
}
/*
* If there was a cycle detected, then notify and don't report
* further.
*/
if (cdetect) {
new_line();
}
"ls: cycle detected for %s\n"), name);
return;
}
if (nomocore)
return;
if (fflg == 0)
sizeof (struct lbuf *),
(int (*)(const void *, const void *))compar);
if (Rflg) {
perror("ls");
exit(2);
}
perror("ls");
exit(2);
}
}
}
}
new_line();
}
}
/*
* pem: print 'em. Print a list of files (e.g. a directory) bounded
* by slp and lp.
*/
static void
{
ncols = 1;
} else {
}
}
new_line();
return;
}
/* otherwise print -C columns */
if (tot_flag) {
slp--;
row = 1;
}
else
row = 0;
}
new_line();
}
}
/*
* print one output entry;
*/
static void
{
struct lbuf *p;
char *cp;
p = ap;
column();
if (iflg)
else
if (sflg)
if (lflg) {
curcol++;
/* ACL: additional access mode flag */
curcol++;
if (oflg)
if (!nflg) {
} else
if (gflg)
if (!nflg) {
} else
} else {
}
if (eflg) {
} else if (Eflg) {
/* fill in nanoseconds first */
} else {
} else {
}
}
}
/*
* prevent both "->" and trailing marks
* from appearing
*/
dmark = "/";
if (p->ltype == 'd')
dmark = "/";
else if (p->ltype == 'D')
dmark = ">";
else if (p->ltype == 'p')
dmark = "|";
else if (p->ltype == 'l')
dmark = "@";
else if (p->ltype == 's')
dmark = "=";
dmark = "*";
else
dmark = "";
}
}
else {
}
} else {
else {
}
}
}
/* print various r,w,x permissions */
static void
{
/* these arrays are declared static to allow initializations */
#ifdef XPG4
#else
#endif
int **mp;
}
static void
{
int n;
n = *pairp++;
while (n-->0) {
pairp++;
break;
} else {
pairp += 2;
}
}
curcol++;
}
/*
* column: get to the beginning of the next column.
*/
static void
column(void)
{
if (curcol == 0)
return;
if (mflg) {
curcol++;
curcol = 0;
return;
}
curcol++;
return;
}
if (Cflg == 0) {
curcol = 0;
return;
}
curcol = 0;
return;
}
do {
curcol++;
}
static void
new_line(void)
{
if (curcol) {
first = 0;
curcol = 0;
}
}
/*
* read each filename in directory dir and store its
* status in flist[nfiles]
*/
static void
{
int j;
int width;
err = 2;
return;
} else {
tblocks = 0;
for (;;) {
errno = 0;
break;
(Aflg == 0 ||
/*
* check for directory items '.', '..',
* and items without valid inode-number;
*/
continue;
}
if (nomocore)
return;
continue;
} else {
}
}
if (errno) {
gettext("ls: error reading directory %s: %s\n"),
}
}
}
/*
* Attaching a link to an inode's ancestors. Search
* through the ancestors to check for cycles (an inode which
* we have already tracked in this inodes ancestry). If a cycle
* is detected, set the exit code and record the fact so that
* it is reported at the right time when printing the directory.
* In addition, set the exit code. Note: If the -a flag was
* specified, we don't want to check for cycles for directories
* ending in '/.' or '/..' unless they were specified on the
* command line.
*/
static void
{
/*
* Add this inode's ancestry
* info and insert it into the
* ancestry list by pointing
* back to its parent. We save
* it (in rep) with the other info
* we're gathering for this inode.
*/
perror("ls");
exit(2);
}
/*
* If this node has the same device id and
* inode number of one of its ancestors,
* then we've detected a cycle.
*/
/*
* Cycle detected for this
* directory. Record the fact
* it is a cycle so we don't
* try to process this
* directory as we are
* walking through the
* list of directories.
*/
err = 2;
break;
}
}
}
}
}
/*
* get status of file and recomputes tblocks;
* argfl = 1 if file is a name in ls-command and = 0
* for filename in a directory whose name is an
* argument in the command;
* stores a pointer in flist[nfiles] and
* returns that pointer;
* returns NULL if failed;
*/
static struct lbuf *
{
int aclcnt;
int grouppermfound, maskfound;
if (nomocore)
return (NULL);
/*
* more space
*/
perror("ls");
nomocore = 1;
return (NULL);
}
}
/*
* nfiles is reset to nargs for each directory
* that is given as an argument maxn is checked
* to prevent the assignment of an lbuf to a flist entry
* that already has one assigned.
*/
} else {
}
int doacl;
if (lflg)
doacl = 1;
else
doacl = 0;
/*
* Avoid race between readdir and lstat.
* Print error message in case of dangling link.
*/
}
nfiles--;
return (NULL);
}
/*
* If -H was specified, and the file linked to was
* not a directory, then we need to get the info
* for the symlink itself.
*/
}
}
case S_IFDIR:
if (Rflg) {
}
break;
case S_IFBLK:
break;
case S_IFCHR:
break;
case S_IFIFO:
break;
case S_IFSOCK:
break;
case S_IFLNK:
/* symbolic links may not have ACLs, so elide acl() */
doacl = 0;
}
if (lflg) {
if (cc >= 0) {
/*
* follow the symbolic link
* to generate the appropriate
* Fflg marker for the object
*/
S_IFMT) {
case S_IFDIR:
break;
case S_IFSOCK:
break;
default:
~S_IFMT) &
S_IXOTH))
break;
}
}
}
break;
}
/*
* ls /sym behaves differently from ls /sym/
* when /sym is a symbolic link. This is fixed
* when explicit arguments are specified.
*/
#ifdef XPG6
/* Do not follow a symlink when -F is specified */
#else
/* Follow a symlink when -F is specified */
#endif /* XPG6 */
break;
if (Rflg) {
}
}
break;
case S_IFDOOR:
break;
case S_IFREG:
break;
case S_IFPORT:
break;
default:
break;
}
/* ACL: check acl entries count */
if (doacl) {
/* this file has a non-trivial acl */
/*
* For files with non-trivial acls, the
* effective group permissions are the
* intersection of the GROUP_OBJ value and
* the CLASS_OBJ (acl mask) value. Determine
* both the GROUP_OBJ and CLASS_OBJ for this
* file and insert the logical AND of those
* two values in the group permissions field
* of the lflags value for this file.
*/
perror("ls");
exit(2);
}
nfiles--;
err = 2;
return (NULL);
}
/*
* Until found in acl list, assume maximum
* permissions for both group and mask. (Just
* in case the acl lacks either value for
* some reason.)
*/
groupperm = 07;
mask = 07;
grouppermfound = 0;
maskfound = 0;
grouppermfound = 1;
continue;
}
maskfound = 1;
}
if (grouppermfound && maskfound)
break;
}
/* reset all the group bits */
/*
* Now set them to the logical AND of the
* GROUP_OBJ permissions and the acl mask.
*/
}
} else
/* mask ISARG and other file-type bits */
if (uflg)
else if (cflg)
else
}
return (rep);
}
/*
* dir and file are null-terminated strings.
*/
static char *
{
/*
* PATH_MAX is the maximum length of a path name.
* MAXNAMLEN is the maximum length of any path name component.
* Allocate space for both, plus the '/' in the middle
* and the null character at the end.
* dfile is static as this is returned by makename().
*/
while (*fp)
*dp++ = '/';
while (*fp)
*dp = '\0';
return (dfile);
}
#include <pwd.h>
#include <grp.h>
#include <utmpx.h>
struct cachenode { /* this struct must be zeroed before using */
long val; /* the uid or gid of this entry */
int initted; /* name has been filled in */
};
static struct cachenode *
{
while (c != NULL) {
/* found it */
return (c);
c = c->lesschild;
} else {
c = c->grtrchild;
}
}
/* not in the cache, make a new entry for it */
if (c == NULL) {
perror("ls");
exit(2);
}
*parent = c;
return (c);
}
/*
* get name from cache, or passwd file for a given uid;
* lastuid is set to uid.
*/
static char *
{
struct cachenode *c;
return (lastuname);
if (c->initted == 0) {
} else {
}
c->initted = 1;
}
return (lastuname);
}
/*
* get name from cache, or group file for a given gid;
* lastgid is set to gid.
*/
static char *
{
struct cachenode *c;
return (lastgname);
if (c->initted == 0) {
} else {
}
c->initted = 1;
}
return (lastgname);
}
/* return >0 if item pointed by pp2 should appear first */
static int
{
if (dflg == 0) {
/*
* compare two names in ls-command one of which is file
* and the other is a directory;
* this portion is not used for comparing files within
* a directory name of ls-command;
*/
return (1);
} else {
return (-1);
}
}
if (tflg) {
return (rflg);
return (-rflg);
/* times are equal to the sec, check nsec */
return (rflg);
return (-rflg);
/* if times are equal, fall through and sort by name */
} else if (Sflg) {
/*
* The size stored in lsize can be either the
* size or the major minor number (in the case of
* block and character special devices). If it's
* a major minor number, then the size is considered
* to be zero and we want to fall through and sort
* by name. In addition, if the size of p2 is equal
* to the size of p1 we want to fall through and
* sort by name.
*/
return (rflg);
return (-rflg);
}
/* Sizes are equal, fall through and sort by name. */
}
}
static void
{
csi_pprintf((unsigned char *)s1);
csi_pprintf((unsigned char *)s2);
}
static void
csi_pprintf(unsigned char *s)
{
unsigned char *cp;
char c;
int i;
int c_len;
int p_col;
curcol++;
}
return;
}
if (!isprint(c)) {
if (qflg) {
c = '?';
} else {
curcol += 3;
}
}
curcol++;
cp++;
continue;
}
c_len = 1;
goto not_print;
}
continue;
}
for (i = 0; i < c_len; i++) {
if (qflg) {
c = '?';
} else {
curcol += 3;
}
curcol++;
cp++;
}
}
}
static int
{
int w;
int w_col;
int len;
w = 0;
while (*s1) {
w++;
s1++;
continue;
}
w++;
s1++;
continue;
}
w += w_col;
}
return (w);
}
/*
* Convert an unsigned long long to a string representation and place the
* result in the caller-supplied buffer.
*
* The number provided is a size in bytes. The number is first
* converted to an integral multiple of 'scale' bytes. This new
* number is then scaled down until it is small enough to be in a good
* human readable format, i.e. in the range 0 thru scale-1. If the
* number used to derive the final number is not a multiple of scale, and
* the final number has only a single significant digit, we compute
* tenths of units to provide a second significant digit.
*
* The value "(unsigned long long)-1" is a special case and is always
* converted to "-1".
*
* A pointer to the caller-supplied buffer is returned.
*/
static char *
unsigned long long number, /* convert this number */
long scale)
{
unsigned long long save;
/* Measurement: kilo, mega, giga, tera, peta, exa */
char *uom = "KMGTPE";
if ((long long)number == (long long)-1) {
return (buf);
}
/*
* Now we have number as a count of scale units.
* If no further scaling is necessary, we round up as appropriate.
*
* The largest value number could have had entering the routine is
* 16 Exabytes, so running off the end of the uom array should
* never happen. We check for that, though, as a guard against
* a breakdown elsewhere in the algorithm.
*/
uom++;
number = 1;
}
}
} else {
uom++; /* next unit of measurement */
/*
* If we're over half way to the next unit of
* 'scale' bytes (which means we should round
* up), then adding half of 'scale' prior to
* the division will push us into that next
* unit of scale when we perform the division
*/
}
}
/* check if we should output a decimal place after the point */
/* snprintf() will round for us */
} else {
}
return (buf);
}