* ls - list file or directory
* Modified by Bill Joy UCB May/August 1977
* This version of ls is designed for graphic terminals and to
* list directories with lots of files in them compactly.
* It supports three variants for listings:
* 3) Old one per line format.
* Columnar output is the default.
* If, however, the standard output is not a teletype, the default
* With columnar output, the items are sorted down the columns.
* We use columns only for a directory we are interpreting.
* Thus, in particular, we do not use columns for
* This version of ls also prints non-printing characters as '?' if
* the standard output is a teletype.
* Flags relating to these and other new features are:
* -m force stream output.
* -1 force one entry per line, e.g. to a teletype
* -q force non-printings to be '?'s, e.g. to a file
* -c force columnar output, e.g. into a file
* -n like -l, but user/group id's in decimal rather than
* looking in /etc/passwd to save time
int aflg
, dflg
, lflg
, sflg
, tflg
, uflg
, iflg
, fflg
, nflg
, Aflg
;
struct lbuf
*rlastp
&end
;
register struct lbuf
*ep
;
register struct lbuf
*slastp
;
qflg
= gtty(1, &obuf
.buff
) == 0;
* If the standard output is not a teletype,
* then we default to one-per-line format
* otherwise decide between stream and
* columnar based on our name.
for (cp
= argv
[0]; cp
[0] && cp
[1]; cp
++)
* Name ends in l => stream
* ... if doesn't end in l or s ==> columns sorted across
year
= lb
.lmtime
[0] - 245; /* 6 months ago */
while (--argc
> 0 && *argv
[1] == '-') {
while (*++*argv
) switch (**argv
) {
* c - force columnar output
* m - force stream output
* q - force ?'s in output
* 1 - force 1/line in output
* n - don't look in password file
for (i
=0; i
< argc
; i
++) {
if ((ep
= gstat(*++argv
, 1))==0)
qsort(&end
, lastp
- &end
, sizeof *lastp
, compar
);
for (ep
= &end
; ep
<slastp
; ep
++) {
if (ep
->lflags
&DIR && dflg
==0 || fflg
) {
printf("\n%s:\n", ep
->namep
);
qsort(slastp
,lastp
- slastp
,sizeof *lastp
,compar
);
printf("total %s", locv(0, tblocks
));
register struct lbuf
*slp
, *lp
;
int ncols
, nrows
, row
, col
;
register struct lbuf
*ep
;
if (ncols
== 1 || cflg
== 0) {
for (ep
= slp
; ep
< lp
; ep
++)
for (ep
= slp
; ep
< lp
; ep
++)
nrows
= (lp
- slp
- 1) / ncols
+ 1;
for (row
= 0; row
< nrows
; row
++) {
col
= row
== 0 && statreq
;
for (; col
< ncols
; col
++) {
ep
= slp
+ (nrows
* col
) + row
;
outcol
= (outcol
+ 8) &~ 7;
if (qflg
&& (c
< ' ' || c
>= 0177))
if (outcol
+ colwidth
+ 2 > 80) {
if ((outcol
/ colwidth
+ 2) * colwidth
> 80) {
i
= colwidth
- outcol
% colwidth
;
} while (outcol
% colwidth
);
while (outcol
% colwidth
) {
struct { char dminor
, dmajor
;};
printf("%3d ", p
->lnl
&0377);
t
= /* p->lgid<<8 | */ (p
->luid
&0377);
if (nflg
== 0 && getpw(t
, tbuf
)==0) {
while (*cp
&& *cp
!= ':') cp
++; *cp
= 0;
if (p
->lflags
& (BLK
|CHR
)) {
if (p
->lflags
&CHR
&& p
->lsize
== -1)
if (p
->lquot
[0] < 10000 && p
->lquot
[1] < 10000)
p
->lquot
[0], p
->lquot
[1]);
p
->lquot
[0], p
->lquot
[1]);
printf("%4d,%4d", p
->lsize
.dmajor
&0477,
printf("%9s", locv(p
->lsize0
&0377, p
->lsize
));
t
= nblock(p
->lsize0
&0377, p
->lsize
);
printf("%s ", locv(0, t
));
printf("%4s ", locv(0, t
));
printf("%-7.7s %-4.4s ", cp
+4, cp
+20); else
printf("%-12.12s ", cp
+4);
printf("%.14s", p
->lname
);
n
= ldiv(size0
&0377, size
, 512);
int m0
[] { 3, DIR, 'd', BLK
, 'b', CHR
, 'c', '-'};
int m1
[] { 1, ROWN
, 'r', '-' };
int m2
[] { 1, WOWN
, 'w', '-' };
int m3
[] { 2, SUID
, 's', XOWN
, 'x', '-' };
int m1a
[] { 1, RGRP
, 'r', '-' };
int m1b
[] { 1, WGRP
, 'w', '-' };
int m1c
[] { 2, SGID
, 's', XGRP
, 'x', '-' };
int m4
[] { 1, ROTH
, 'r', '-' };
int m5
[] { 1, WOTH
, 'w', '-' };
int m6
[] { 2, STXT
, 't', XOTH
, 'x', '-' };
int *m
[] { m0
, m1
, m2
, m3
, m1a
, m1b
, m1c
, m4
, m5
, m6
};
if (flags
&CHR
&& p
->lsize
== -1)
for (mp
= &m
[1]; mp
< &m
[10];)
while (--n
>=0 && (flags
&*ap
++)==0)
register struct lbuf
*ep
;
if (fopen(dir
, &inf
) < 0) {
printf("%s unreadable\n", dir
);
|| aflg
==0 && dentry
.dname
[0]=='.' && (
dentry
.dname
[1] == 0 || (dentry
.dname
[1] == '.'
&& dentry
.dname
[2] == 0)))
ep
= gstat(makename(dir
, dentry
.dname
), 0);
ep
->lnum
= dentry
.dinode
;
ep
->lname
[j
] = dentry
.dname
[j
];
register struct lbuf
*rep
;
if (stat(file
, &statb
)<0) {
printf("%s not found\n", file
);
if ((statb
.iflags
&IFMT
) == 060000) {
} else if ((statb
.iflags
&IFMT
)==040000) {
if (statb
.iflags
& RSTXT
)
rep
->lflags
= statb
.iflags
;
rep
->lsize0
= statb
.isize0
;
rep
->lsize
= statb
.isize
;
if (rep
->lflags
& (BLK
|CHR
) && lflg
) {
rep
->lsize
= statb
.iaddr
[0];
rep
->lquot
[0] = statb
.iaddr
[1];
rep
->lquot
[1] = statb
.iaddr
[2];
rep
->lmtime
[0] = statb
.imtime
[0];
rep
->lmtime
[1] = statb
.imtime
[1];
rep
->lmtime
[0] = statb
.iatime
[0];
rep
->lmtime
[1] = statb
.iatime
[1];
tblocks
=+ nblock(statb
.isize0
, statb
.isize
);
register struct lbuf
*p1
, *p2
;
if ((p1
->lflags
&(DIR|ISARG
)) == (DIR|ISARG
)) {
if ((p2
->lflags
&(DIR|ISARG
)) != (DIR|ISARG
))
if ((p2
->lflags
&(DIR|ISARG
)) == (DIR|ISARG
))
if (p2
->lmtime
[0] > p1
->lmtime
[0])
else if (p2
->lmtime
[0] < p1
->lmtime
[0])
else if (p2
->lmtime
[1] > p1
->lmtime
[1])
else if (p2
->lmtime
[1] < p1
->lmtime
[1])
if ((j
= *p1
.charp
++ - *p2
.charp
++) || p1
.charp
[-1]==0)