8000 minix/commands/simple/ls.c at v3.1.0 · blackskygg/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
executable file
·
1150 lines (979 loc) · 25.4 KB

ls.c

File metadata and controls

executable file
·
1150 lines (979 loc) · 25.4 KB
if (field & L_EXTRA) {
p= cxsize(f);
n= strlen(p)+1;
if (doit) {
n= f1width[W_SIZE] - n;
while (n > 0) { putchar(' '); --n; }
printf("%s ", p);
} else {
maxise(&f1width[W_SIZE], n);
}
}
if (field & L_LONG) {
if (doit) {
printf("%*u ", f1width[W_NLINK], (unsigned) f->nlink);
} else {
maxise(&f1width[W_NLINK], numwidth(f->nlink));
width++;
}
if (!(field & L_GROUP)) {
if (doit) {
printf("%-*s ", f1width[W_UID],
uidname(f->uid));
} else {
maxise(&f1width[W_UID],
strlen(uidname(f->uid)));
width+= 2;
}
}
if (doit) {
printf("%-*s ", f1width[W_GID], gidname(f->gid));
} else {
maxise(&f1width[W_GID], strlen(gidname(f->gid)));
width+= 2;
}
switch (f->mode & S_IFMT) {
case S_IFBLK:
case S_IFCHR:
#ifdef S_IFMPB
case S_IFMPB:
#endif
#ifdef S_IFMPC
case S_IFMPC:
#endif
#if __minix
if (doit) {
printf("%*d, %3d ", f1width[W_SIZE] - 5,
major(f->rdev), minor(f->rdev));
} else {
maxise(&f1width[W_SIZE],
numwidth(major(f->rdev)) + 5);
width++;
}
#else /* !__minix */
if (doit) {
printf("%*lX ", f1width[W_SIZE],
(unsigned long) f->rdev);
} else {
maxise(&f1width[W_SIZE], numwidth(f->rdev));
width++;
}
#endif /* !__minix */
break;
default:
if (field & L_KMG) {
p= cxsize(f);
n= strlen(p)+1;
if (doit) {
n= f1width[W_SIZE] - n;
while (n > 0) { putchar(' '); --n; }
printf("%s ", p);
} else {
maxise(&f1width[W_SIZE], n);
}
} else {
if (doit) {
printf("%*lu ", f1width[W_SIZE],
(unsigned long) f->size);
} else {
maxise(&f1width[W_SIZE],
numwidth(f->size));
width++;
}
}
}
if (doit) {
printf("%s ", timestamp(f));
} else {
width+= (field & L_LONGTIME) ? 21 : 13;
}
}
n= strlen(f->name);
if (doit) {
printname(f->name);
if (mark(f, 1) != 0) n++;
#ifdef S_IFLNK
if ((field & L_LONG) && (f->mode & S_IFMT) == S_IFLNK) {
char *buf;
int r, didx;
buf= (char *) allocate(((size_t) f->size + 1)
* sizeof(buf[0]));
addpath(&didx, f->name);
r= readlink(path, buf, (int) f->size);
delpath(didx);
if (r > 0) buf[r] = 0; else r=1, strcpy(buf, "?");
printf(" -> ");
printname(buf);
free((void *) buf);
n+= 4 + r;
}
#endif
spaces(f1width[W_NAME] - n);
} else {
if (mark(f, 0) != 0) n++;
#ifdef S_IFLNK
if ((field & L_LONG) && (f->mode & S_IFMT) == S_IFLNK) {
n+= 4 + (int) f->size;
}
#endif
maxise(&f1width[W_NAME], n + NSEP);
for (n= 1; n < MAXFLDS; n++) width+= f1width[n];
maxise(&f1width[W_COL], width);
}
}
int countfiles(struct file *flist)
/* Return number of files in the list. */
{
int n= 0;
while (flist != nil) { n++; flist= flist->next; }
return n;
}
struct file *filecol[MAXCOLS]; /* filecol[i] is list of files for column i. */
int nfiles, nlines; /* # files to print, # of lines needed. */
void columnise(struct file *flist, int nplin)
/* Chop list of files up in columns. Note that 3 columns are used for 5 files
* even though nplin may be 4, filecol[3] will simply be nil.
*/
{
int i, j;
nlines= (nfiles + nplin - 1) / nplin; /* nlines needed for nfiles */
filecol[0]= flist;
for (i=1; i<nplin; i++) { /* Give nlines files to each column. */
for (j=0; j<nlines && flist != nil; j++) flist= flist->next;
filecol[i]= flist;
}
}
int print(struct file *flist, int nplin, int doit)
/* Try (doit == 0), or really print the list of files over nplin columns.
* Return true if it can be done in nplin columns or if nplin == 1.
*/
{
register struct file *f;
register int col, fld, totlen;
columnise(flist, nplin);
if (!doit) {
for (col= 0; col < nplin; col++) {
for (fld= 0; fld < MAXFLDS; fld++) {
fieldwidth[col][fld]= 0;
}
}
}
while (--nlines >= 0) {
totlen= 0;
for (col= 0; col < nplin; col++) {
if ((f= filecol[col]) != nil) {
filecol[col]= f->next;
print1(f, col, doit);
}
if (!doit && nplin > 1) {
/* See if this line is not too long. */
if (fieldwidth[col][W_COL] == UCHAR_MAX) {
return 0;
}
totlen+= fieldwidth[col][W_COL];
if (totlen > ncols+NSEP) return 0;
}
}
if (doit) terpri();
}
return 1;
}
enum depth { SURFACE, SURFACE1, SUBMERGED };
enum state { BOTTOM, SINKING, FLOATING };
void listfiles(struct file *flist, enum depth depth, enum state state)
/* Main workhorse of ls, it sorts and prints the list of files. Flags:
* depth: working with the command line / just one file / listing dir.
* state: How "recursive" do we have to be.
*/
{
struct file *dlist= nil, **afl= &flist, **adl= &dlist;
int nplin;
static int white = 1; /* Nothing printed yet. */
/* Flush everything previously printed, so new error output will
* not intermix with files listed earlier.
*/
fflush(stdout);
if (field != 0 || state != BOTTOM) { /* Need stat(2) info. */
while (*afl != nil) {
static struct stat st;
int r, didx;
addpath(&didx, (*afl)->name);
if ((r= status(path, &st)) < 0
#ifdef S_IFLNK
&& (status == lstat || lstat(path, &st) < 0)
#endif
) {
if (depth != SUBMERGED || errno != ENOENT)
report((*afl)->name);
delfile(popfile(afl));
} else {
setstat(*afl, &st);
afl= &(*afl)->next;
}
delpath(didx);
}
}
sort(&flist);
if (depth == SUBMERGED && (field & (L_BLOCKS | L_LONG))) {
0