#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
#include <limits.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <dirent.h>
#define MAX_THREAD 1
#define BUFSIZE 4096
typedef struct {
char* filein;
char* palabra;
pthread_mutex_t * wcs;
char* myset;
sem_t semaforo;
}param;
void myRun(param lpParam);
void lock(pthread_mutex_t * cs);
void unlock(pthread_mutex_t * cs);
int elabora(sem_t semaforo, char* myset, char* fileOut);
int busquedaPalabra(char* path, char* palabra, sem_t sem, char* myset, pthread_mutex_t * wcs, sem_t semaforo);
void produceFileOut(char* fileOut, char* myset);
char *getPath(const char* filename);
int main (int argc, char* argv[]) {
char* path = argv[1];
char* palabra = argv[2];
char* fileOut = argv[3];
char* myset = " ";
pthread_mutex_t * wcs;
sem_t semaforo;
pthread_mutex_init(&wcs,NULL);
sem_init(&semaforo, MAX_THREAD, NULL);
elabora(semaforo, myset, fileOut);
sem_post(&semaforo);
pthread_mutex_destroy(&wcs);
return 0;
}
void lock(pthread_mutex_t * cs){
pthread_mutex_lock(&cs);
}
void unlock(pthread_mutex_t * cs){
pthread_mutex_unlock(&cs);
}
int elabora(sem_t semaforo, char* myset, char* fileOut) {
int i;
for(i=0; i < MAX_THREAD; i++ )
wait(&semaforo);
if(strlen(myset)==0)
printf("La cadena a buscar no se encuentra en el directorio especificado \n");
else
produceFileOut(fileOut, myset);
return 0;
}
int busquedaPalabra(char* path, char* palabra, sem_t sem, char* myset, pthread_mutex_t * wcs, sem_t semaforo) {
pthread_t hthread;
char *puntero;
char* dirname=getPath(path);
DIR *file1;
struct stat statbuf;
struct dirent *direntp;
int fd=NULL;
strcat(dirname,"\\*");
direntp = readdir(file1);
if(fd==NULL )
return -1;
stat(file1, &statbuf);
while(readdir(file1)!=0) {
if(statbuf.st_mode & S_IFDIR) {
if((strcmp(direntp->d_name,".") != 0) && (strcmp(direntp->d_name,"..") != 0)) {
char* subdir=dirname;
//size_t its=strlen(subdir);
//its--;
//puntero=(char *)malloc(its);
strcpy(puntero, subdir);
strcat(puntero, file1->cFileName);
busquedaPalabra(puntero,palabra,sem,myset,wcs,semaforo);
free(puntero);
}
}
else {
char* nameF=dirname;
size_t its=strlen(nameF);
its--;
puntero=(char*)malloc(its);
strcpy(puntero, nameF);
strcat(puntero, file1->cFileName);
wait(&semaforo);
param->filein=puntero;
param->palabra=palabra;
param->myset=myset;
param->wcs=wcs;
param->semaforo=&semaforo;
pthread_create(hthread, NULL, myRun, (param)(param));
pthread_kill(hthread);
}
}
closedir(fd);
return 0;
}
void produceFileOut(char* fileOut, char* myset) {
FILE *fout;
fout = fopen (fileOut, "wt");
fwrite(myset, strlen(myset)+1, 1, fout);
fclose(fout);
}
char *getPath(const char* filename) {
getcwd(filename,BUFSIZE);
}
void myRun(param lpParam) {
param *pa=&lpParam;
char* s;
FILE *fin;
char* ret;
fin = fopen(pa->filein, "wt");
s=strlen(pa->filein);
while(getline(pa->palabra, s, fin)) {
lock(&pa->wcs);
ret=strcat(pa->myset, pa->filein);
break;
}
unlock(&pa->wcs);
fclose(fin);
sem_destroy(&(pa->semaforo));
}
Gracias