home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
EFFO
/
forum5.lzh
/
SPRACHEN
/
C
/
TREE
/
tree.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-04-06
|
4KB
|
205 lines
/* tree.c
*
* Print out the directory-tree
*
* Uwe Simon April 1988
*
* Dies Programm ist PD-Software
*
* Es ist aus dem Program 'du' von Tom Leitner, Graz entstanden
*
*/
/* system includes */
#include <stdio.h>
#include <dir.h>
#include <modes.h>
#include <direct.h>
#include <time.h>
/* constants & macros */
#define RBF (1) /* random block file found opened */
#define PIPE (2) /* pipe file opened */
#define NET (4) /* network file opened */
#define TRUE 1 /* logical true */
#define FALSE 0 /* logical false */
/* global variables */
int files = 0; /* # of files found */
int dirs = 0;/* # of directories found
*/
int max_level = 1000;
char only_dirs = FALSE;
char root[256]; /* root directory to start from */
usage()
{
fprintf(stderr,"Syntax: tree {<opts>} [<path>]\n");
fprintf(stderr,"Function: prints the directory-tree\n");
fprintf(stderr,"Options:\n");
fprintf(stderr,"\t-d print only the directories\n");
fprintf(stderr,"\t-l=<num> print to level <num>\n");
exit(1);
}
/*--------------------------------- M A I N ----------------------------------*/
main(argc, argv)
int argc;
char *argv[];
{
time_t t;
char tstr[27];
get_parms(argc, argv);
time(&t);
strcpy(tstr,ctime(&t));
tstr[19]='\0';
printf("\t\tDirectory-tree of %s %s\n", root,&tstr[11]);
printfile(0,0,root);
}
/*-------------------- parses command line for parameters --------------------*/
get_parms(argc,argv)
int argc;
char *argv[];
{
char *s;
int pc = 0;
while (--argc>0) {
if ((*++argv)[0]=='-' ) {
for (s = argv[0]+1;*s != '\0';s++)
switch(toupper(*s)) {
case 'D':
only_dirs = TRUE;
break;
case 'L':
if(s[1]=='=') max_level=atoi(&s[2]);
else max_level=atoi(&s[1]);
s[1]='\0';
break;
case '?':
usage();
exit(0);
default:
fprintf(stderr,
"%s: unknown option '%c'\n",
_prgname(),*s);
exit(1);
}
}
else if (pc == 0) {
strcpy(root,argv[0]);
pc++;
}
else {
fprintf(stderr,"%s: only one parameter allowed\n",
_prgname());
exit(1);
}
}
if (pc == 0) strcpy(root,".");
}
/*------------------------------ main algorithm ------------------------------*/
int printfile(anz_files,level,filename ) /* returns number of files printet */
int level;
char *filename;
{
int fd, pl, dfiles;
DIR *dirp;
struct direct *dirrec;
static struct fildes stat;
if(level>=max_level) return;
/* try to open the file */
if( ((fd=open(filename,S_IFDIR|S_IREAD)) < 0) && /* regular file ? */
((fd=open(filename,S_IREAD)) < 0) ) /* directory ? */
fprintf(stderr,"Cannot open '%s'\n", filename);
/* successfully opened. check if it's on the right device */
else {
if(_gs_gfd(fd, &stat,sizeof(struct fildes))==-1)
fprintf(stderr,"wrong type: %s\n",filename);
else {
/* ok. we're on a valid device : now check if we hit a directory file */
if( stat.fd_att & S_IFDIR ) {
/* yes. we hit a directory file. close the FD and CHD into it. */
close(fd);
if( (dirp = opendir( filename )) && !chdir( filename) ) {
strcat(filename," ---------------------");
printline(anz_files?level:0,filename);
anz_files=0;
if(level<max_level-1) {
/* skip the "." and the ".." entries */
readdir(dirp);
readdir( dirp );
/* get file sizes of all files in this directory */
dfiles = 0;
while (dirrec = readdir (dirp))
if (dirrec->d_addr)
printfile(anz_files++,level+1,dirrec->d_name);
/* end of directory : close it and print the results */
}
closedir( dirp );
chdir("..");
if(!only_dirs) {
printline(anz_files++?level+1:0,"");
printf("\n");
}
}
/* ERROR occurred : can't CHD into this directory. */
else
fprintf(stderr, "Cannot chdir '%s'\n", filename);
}
else {
if(!only_dirs) {
printline(anz_files?level:0,filename);
printf("\n");
anz_files++;
}
close( fd );
}
}
}
if(!anz_files) printf("\n");
}
printline(level,path)
int level;
char *path;
{
int i;
for(i=0;i<level;i++) printf(" | ");
printf("%-15.14s",path);
}
/* E O F */