[net.sources] FLST

holloway@drivax.UUCP (Bruce Holloway) (12/02/86)

Here is a better version of the "flst" program posted a couple of days
ago... unlike the other one, this one works on my system.

In brief, FLST prints a directory tree, optionally including the normal files,
too, to stdout.

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	flst.1
#	flst.c
# This archive created: Tue Dec  2 10:06:22 1986
# By:	Bruce Holloway (Digital Research, Inc., Monterey)
export PATH; PATH=/bin:/usr/bin:$PATH
echo shar: "extracting 'flst.1'" '(523 characters)'
if test -f 'flst.1'
then
	echo shar: "will not over-write existing file 'flst.1'"
else
cat << \SHAR_EOF > 'flst.1'
.TH flst 1 "Digital Research, Inc."
.SH NAME
flst - fast listing sub-tree
.SH SYNOPSIS
flst [-d] [directory(s)]
.SH DESCRIPTION
.I Flst
lists a tree file system or systems specified in the argument
with indentation reflecting the level
of nesting of the file in the file system. If no argument is
given
.I flst
takes the current directory as default.
.TP 5
.B  \-d
Only print out directory names, rather than all files.
.SH SEE ALSO
ls(1), lstree(1)
.SH DIAGNOSTICS
.br
It cannot list files nested more than twenty levels.
SHAR_EOF
fi
echo shar: "extracting 'flst.c'" '(2235 characters)'
if test -f 'flst.c'
then
	echo shar: "will not over-write existing file 'flst.c'"
else
cat << \SHAR_EOF > 'flst.c'
/*****************************************************************************
*	FLST.C							   Version 1.0
*
*	Print out the directory tree.
*
*	flst			print out all files
*	flst -d			print out only directories
******************************************************************************/

#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <sys/file.h>

extern int alphasort();			/* Routine to sort dir. names */
extern char *malloc();			/* Allocate memory */

char VERSION[] = "Version 1.0 by Bruce Holloway (holloway@drivax.UUCP)";

int level = 0;
int dironly = 0;

main(acnt,avar)
int acnt;
char **avar;
{
    char *s;

    if(acnt == 1) lets_do(".");
    else while(--acnt){
	++avar;
	if(**avar == '-'){
	    s = *avar + 1;
	    while(*s) switch(*s++){
		case 'd': ++dironly; break;
		}
	    }
	else lets_do(*avar);
	}
    return(0);
}

/* Ignore any files that begin with a dot. */

int selfile(d)
struct direct *d;
{
    return(d->d_name[0] != '.');
}

/* Each subdirectory contains some number of other subdirectories, and
   some number of regular files, whose names are numbers. Any files that
   are directories append their names to fname and get called recursively.
*/

lets_do(fname)
char *fname;
{
    char xname[128];
    char qname[64];
    struct direct **namelist, *d;
    int nument, i, j;
    int flen, isdir;
    struct stat stb;

    flen = strlen(fname);
    (void) strcpy(xname,fname);

    if((nument = scandir(xname, &namelist, selfile, alphasort)) == -1){
	fprintf(stderr,"%s: Not a directory, or not enough memory.\n",xname);
	return;
	}

    for(i = 0; i < nument; ++i){
	d = namelist[i];
	(void) strcpy(qname,d->d_name);

	(void) strcat(xname,"/");
	(void) strcat(xname,qname);

	if(stat(xname,&stb) == -1){
	    fprintf(stderr,"Error finding stats for \"%s\".\n",xname);
	    continue;
	    }

	isdir = ((stb.st_mode & S_IFMT) == S_IFDIR);

	if(!dironly || (dironly && isdir)){
	    for(j=0; j++ < level;) printf("    ");
	    printf("%s%s\n",qname,(isdir)?"/":"");
	    }

	if(isdir){
	    ++level;
	    lets_do(xname);
	    --level;
	    }

	xname[flen] = 0;
	}

    for(i = 0; i < nument; ++i) free((char *)(namelist[i]));
    free((char *)namelist);
}
SHAR_EOF
fi
exit 0
#	End of shell archive
-- 
....!ucbvax!hplabs!amdahl!drivax!holloway
"What do you mean, 'almost dead'?" "Well, when you stop breathing, and moving
around, and seeing things... that kind of almost dead."