-/*
- * list the files in a directory
- */
-extern int errno;
-
-lsdir(argc, argv)
- int argc; /* count of file names passed */
- char **argv; /* array of file names */
-{
- static char curpath[MAXPATHLEN + 1];
- register char *p;
- LS *stats;
- int curname, entries, i, needstat, neednewline;
- char *emalloc();
-
- stats = (LS *)emalloc((u_int)argc * (sizeof(LS)));
-
- needstat = f_firsttime || f_longform || f_recursive || f_timesort ||
- f_size || f_inode || f_type;
- neednewline = !f_firsttime;
-
- for (entries = curname = 0; curname < argc; ++curname) {
- if (!f_firsttime) {
- /*
- * check for .xxx files. Note can't get listalways
- * without listall being set.
- */
- p = argv[curname];
- if (p[0] == '.') {
- if (!f_listall)
- continue;
- /* and now for listalways: . and .. */
- if ((!p[1] || p[1] == '.' && !p[2]) &&
- !f_listalways)
- continue;
- }
- }
- if (needstat) {
- if (statfcn(argv[curname], &stats[entries].lstat)) {
- (void)fprintf(stderr, "ls: %s: %s\n",
- argv[curname], strerror(errno));
- if (errno == ENOENT)
- continue;
- exit(1);
- }
- if (!S_ISDIR(stats[entries].lstat.st_mode))
- neednewline = 1;
- }
- stats[entries++].name = argv[curname];
- }
- if (!entries)
- return;
-
- if (entries > 1 && !f_specialdir)
- qsort((char *)stats, entries, sizeof(LS), sortfcn);
-
- prindir(stats, entries);
-
- if ((f_firsttime && !f_listdir) || f_recursive) {
- for (i = 0; i < entries; ++i) {
- /* recurse on directories */
- if (S_ISDIR(stats[i].lstat.st_mode)) {
- /* don't recurse on . or .. */
- p = stats[i].name;
- if (p[0] == '.' && (!p[1] ||
- p[1] == '.' && !p[2]) &&
- !f_firsttime)
- continue;
- if (chdir(p)) {
- (void)fprintf(stderr, "ls: %s: %s\n",
- p, strerror(errno));
- break;
- }
- if (entries > 1 ||
- !f_firsttime || f_recursive) {
- /* add current name to path */
- p = rindex(curpath, '\0');
- if (p != curpath && p[-1] != '/')
- *p++ = '/';
- (void)strcpy(p, stats[i].name);
- if (entries > 1 || !f_firsttime) {
- if (neednewline)
- (void)putchar('\n');
- (void)printf("%s:\n",
- curpath);
- }
- }
- recursedir(&stats[i]);
- if (entries > 1 || !f_firsttime)
- *p = '\0';
- if (chdir("..")) {
- (void)fprintf(stderr, "ls: ..: %s\n",
- strerror(errno));
- exit(1);
- }
- }
- }
- f_firsttime = 0;
- }
- free((char *)stats);
-}
-
-/*
- * set up the call to lsdir (lsdir(count, argv-type array))
- * mutually recursive with lsdir