Posted By: znouza (- Armored Pilsener -) on 'CZprogram'
Title: Re: algoritmus pro prochazeni adresaru
Date: Thu Jan 13 22:15:25 2000
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/param.h>
void do_a_directory(char * pathname, int level)
{
DIR * directory;
struct dirent * entry;
char * newname;
struct stat statbuf;
long where;
int retval, i;
for( i = 0; i < level; i++)
printf(" ");
printf("%sn", pathname);
directory = opendir(pathname);
if(directory == NULL)
{
fprintf(stderr, "%s neni adresar !n", pathname);
perror("Reason");
return;
}
while( ( entry = readdir(directory)) != NULL)
{
/* pokud je to . nebo .., preskoc */
if( strcmp(entry -> d_name, ".") == 0 ||
strcmp(entry -> d_name, "..") == 0 )
continue;
newname = (char *) malloc(MAXPATHLEN);
strcpy(newname, pathname);
strcat(newname, "/");
strcat(newname, entry -> d_name);
/* kontrola - je to adresar ? */
retval = stat(newname, &statbuf);
if (retval == 0 && ( statbuf.st_mode & S_IFDIR))
{
where = telldir(directory);
closedir(directory);
do_a_directory(newname, level + 1);
directory = opendir(pathname);
if(directory == NULL)
{
fprintf(stderr, "Cannot open %sn", pathname);
return;
}
seekdir(directory, where);
}
free(newname);
}
closedir(directory);
}
void main(int argc, char * argv[])
{
if( argc < 2)
{
fprintf(stderr,"dtree adresarn");
exit(1);
}
do_a_directory(argv[1], 0);
}
----
pak staci u toho testu testovat na soubory a delat s tim, co chces..
_ _ /-------- znouza
( o)(o ) /__________
|| | |
|| |_| "The game of life is hard to play..."
** __| 3T