// readir.h
// BarSSoft, 1999
#ifndef __READIR_H__
#define __READIR_H__
#include
typedef struct
{
char* path;
char* fileName;
} FindData;
#define MAX_THREADS 20
extern int threads[MAX_THREADS];
extern int nextThread;
extern int showStat;
extern pthread_mutex_t lockThreads;
void* showDir(void*);
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <string.h>
#include <pthread.h>
#include "readir.h"
#include "deftype.h"
#include "filelist.h"
#include "stat.h"
extern int wildmat(const char*, const char*);
#if defined (BSD) && !_POSIX_SOURCE
#include <sys/dir.h>
typedef struct direct Dirent;
#else
#include <dirent.h>
typedef struct dirent Dirent;
#endif
int threads[MAX_THREADS];
int nextThread;
int showStat = 1;
pthread_t statusThreadID;
pthread_mutex_t lockThreads;
int fileNameCmp(const char* first, const char* second)
{
if (strchr(second, '*') > 0)
{
if (!strncmp(first, second,
(int)(strchr(second, '*') - second))
)
return 1;
}
else
if (!strcmp(first, second))
return 1;
return 0;
}
int readDir(const char *startDir, const char *fileName)
{
DIR *dir;
Dirent *dirEnt;
struct stat fileStat;
char s[200], ss[70];
int statRes = 0, nxt = 0, i;
pthread_t t;
FileInf *fileInf;
if (showStat)
{
strcpy(currPath, (char*)startDir);
pthread_cond_signal(&condStatusChanged);
}
dir = opendir((char*) startDir);
if (!dir)
{
perror("Error directory openning");
perror((char*) startDir);
return 0;
}
while (dirEnt = readdir(dir))
{
if (strcmp(dirEnt->d_name, ".") && strcmp(dirEnt->d_name, ".."))
{
strcpy(s, (char*) startDir);
if (strcmp((char*) startDir, "/"))
strcat(s, "/");
strcat(s, dirEnt->d_name);
statRes = lstat(s, &fileStat);
// if (!fileName || fileNameCmp(dirEnt->d_name, fileName))
if (!fileName || wildmat(dirEnt->d_name, fileName))
{
fileInf = (FileInf*)malloc(sizeof(FileInf));
fileInf->name = (char*)malloc(strlen(s) + 1);
strcpy(fileInf->name, s);
fileInf->size = fileStat.st_size;
fileInf->mtime = fileStat.st_mtime;
pthread_mutex_lock(&lockThreads);
if (threads[nextThread])
pthread_join(threads[nextThread], 0);
if (pthread_create(&t, 0, defineType, (void*) fileInf) == -1)
perror("Create defineType thread");
else
{
threads[nextThread++] = (int)t;
if (nextThread == MAX_THREADS)
nextThread = 0;
}
pthread_mutex_unlock(&lockThreads);
}
if (!statRes)
if ((fileStat.st_mode & S_IFMT) == S_IFDIR)
if (!readDir(s, fileName))
return 0;
}
}
closedir(dir);
return 1;
}
void threadsInit(void)
{
memset(threads, 0, MAX_THREADS);
nextThread = 0;
pthread_mutex_init(&lockThreads, 0);
pthread_mutex_init(&lockFileList, 0);
if (showStat)
pthread_cond_init(&condStatusChanged, 0);
finishStatusThread = 0;
if (showStat)
if (pthread_create(&statusThreadID, 0, showStatus, (void*) 0) == -1)
perror("Create Status thread");
}
void threadsWaitAndPrint(void)
{
int i = 0;
pthread_mutex_lock(&lockThreads);
for (i = 0; i < MAX_THREADS && threads[i] > 0; i++)
pthread_join((pthread_t)threads[i], 0);
pthread_mutex_unlock(&lockThreads);
finishStatusThread = 1;
if (showStat)
pthread_cond_signal(&condStatusChanged);
if (showStat)
pthread_join(statusThreadID, 0);
pthread_mutex_lock(&lockFileList);
fileList_Print();
fileList_Clear();
pthread_mutex_unlock(&lockFileList);
pthread_mutex_destroy(&lockThreads);
pthread_mutex_destroy(&lockFileList);
if (showStat)
pthread_cond_destroy(&condStatusChanged);
}
void* showDir(void* path)
{
int *rc,r;
threadsInit();
readDir((*(FindData*)path).path, (*(FindData*)path).fileName);
threadsWaitAndPrint();
}