SCCS-vsn: bin/ls/ls.c 5.18
#endif /* not lint */
#ifndef lint
#endif /* not lint */
#ifndef lint
-static char sccsid[] = "@(#)ls.c 5.17 (Berkeley) %G%";
+static char sccsid[] = "@(#)ls.c 5.18 (Berkeley) %G%";
#endif /* not lint */
#include <sys/param.h>
#endif /* not lint */
#include <sys/param.h>
- register LS *dstats, *rstats;
+ register LS *dstatp, *rstatp;
+ LS *dstats, *rstats;
register int cnt, dircnt, regcnt;
struct stat sb;
LS *stats;
int num, (*statfcn)(), stat(), lstat();
char *names, top[MAXPATHLEN + 1];
register int cnt, dircnt, regcnt;
struct stat sb;
LS *stats;
int num, (*statfcn)(), stat(), lstat();
char *names, top[MAXPATHLEN + 1];
+ /*
+ * walk through the operands, building separate arrays of LS
+ * structures for directory and non-directory files.
+ */
dstats = rstats = NULL;
statfcn = f_ignorelink ? stat : lstat;
for (dircnt = regcnt = 0; *argv; ++argv) {
dstats = rstats = NULL;
statfcn = f_ignorelink ? stat : lstat;
for (dircnt = regcnt = 0; *argv; ++argv) {
}
if (!f_specialdir && !f_listdir && S_ISDIR(sb.st_mode)) {
if (!dstats)
}
if (!f_specialdir && !f_listdir && S_ISDIR(sb.st_mode)) {
if (!dstats)
- dstats = (LS *)emalloc((u_int)argc *
+ dstatp = dstats = (LS *)emalloc((u_int)argc *
- dstats[dircnt].name = *argv;
- dstats[dircnt].lstat = sb;
+ dstatp->name = *argv;
+ dstatp->lstat = sb;
+ ++dstatp;
++dircnt;
}
else {
if (!rstats)
++dircnt;
}
else {
if (!rstats)
- rstats = (LS *)emalloc((u_int)argc *
+ rstatp = rstats = (LS *)emalloc((u_int)argc *
- rstats[regcnt].name = *argv;
- rstats[regcnt].lstat = sb;
+ rstatp->name = *argv;
+ rstatp->lstat = sb;
+ ++rstatp;
+ /* display regular files */
+ /*
+ * for -f flag -- switch above treats all -f operands as
+ * regular files; this code uses buildstats() to read
+ * them as directories.
+ */
- for (cnt = 0; cnt < regcnt; ++cnt) {
+ for (cnt = regcnt; cnt--;) {
if (num = buildstats(rstats++, &stats, &names))
ls(stats, num);
(void)free((char *)stats);
if (num = buildstats(rstats++, &stats, &names))
ls(stats, num);
(void)free((char *)stats);
}
} else
ls(rstats, regcnt);
}
} else
ls(rstats, regcnt);
- if (dircnt)
- (void)putchar('\n');
+ /* display directories */
if (dircnt) {
register char *p;
if (dircnt) {
register char *p;
for (cnt = 0; cnt < dircnt; ++dstats) {
for (endofpath = path, p = dstats->name;
*endofpath = *p++; ++endofpath);
for (cnt = 0; cnt < dircnt; ++dstats) {
for (endofpath = path, p = dstats->name;
*endofpath = *p++; ++endofpath);
- ls_dir(dstats, cnt, regcnt || dircnt > 1);
+ ls_dir(dstats, regcnt, regcnt || dircnt > 1);
if (++cnt < dircnt && chdir(top)) {
(void)fprintf(stderr, "ls: %s: %s\n",
top, strerror(errno));
if (++cnt < dircnt && chdir(top)) {
(void)fprintf(stderr, "ls: %s: %s\n",
top, strerror(errno));
-#ifdef whybother
- (void)free((char *)rstats);
- (void)free((char *)dstats);
-#endif
+ /*
+ * this doesn't really belong here, but it's the only place that
+ * everybody goes through; the `tag' variable is so that we don't
+ * print the header for directories unless we're going to display
+ * more directories, or we've already displayed files or directories.
+ * The `newline' variable keeps us from inserting a newline before
+ * we've displayed anything at all.
+ */
if (newline)
(void)putchar('\n');
if (tag)
if (newline)
(void)putchar('\n');
if (tag)
return(0);
}
for (cnt = 0; entry = readdir(dirp);) {
return(0);
}
for (cnt = 0; entry = readdir(dirp);) {
+ /* this does -A and -a */
p = entry->d_name;
if (p[0] == '.') {
if (!f_listdot)
p = entry->d_name;
if (p[0] == '.') {
if (!f_listdot)