All pastes #564410 Raw Edit

SOE Lab4

public c v1 · immutable
·published 2007-06-13 16:11 UTC
rendered paste body
#define _POSIX_PTHREAD_SEMANTICS 1#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <sys/mman.h>#include <unistd.h>#include <dirent.h>#include <pthread.h>   char *nazwa;char **slowa;unsigned int ilosc; //ilosc slowunsigned int ilosc_watkow = 0;pthread_mutex_t ilosc_watkow_mutex = PTHREAD_MUTEX_INITIALIZER; void *dofile(void *arg){    char *file = (char*) arg;    int fd,res;    char *plik,*ptr;    off_t rozmiar;    unsigned int n;        if ((fd = open(file,O_RDWR))<0) {	perror("open");		free(file);	pthread_mutex_lock(&ilosc_watkow_mutex);	ilosc_watkow--;	pthread_mutex_unlock(&ilosc_watkow_mutex);			return -1;    };        rozmiar = lseek (fd,0,SEEK_END);    lseek (fd,0,SEEK_SET);        if (rozmiar == 0){	fprintf(stderr,"%s: pusty plik!\n",file);	close(fd);		free(file);	pthread_mutex_lock(&ilosc_watkow_mutex);	ilosc_watkow--;	pthread_mutex_unlock(&ilosc_watkow_mutex);		return -1;    };    plik = (char *) mmap(0,rozmiar,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);             if (plik == MAP_FAILED) {	perror("mmap");	close(fd);			free(file);	pthread_mutex_lock(&ilosc_watkow_mutex);	ilosc_watkow--;	pthread_mutex_unlock(&ilosc_watkow_mutex);		return -1;    };        for (n=0;n<ilosc;n++) {	ptr = plik;	while (ptr<(plik+rozmiar-strlen(slowa[n])))	    if (memcmp(ptr,slowa[n],strlen(slowa[n]))==0)	    {		memset(ptr,'*',strlen(slowa[n]));		ptr +=strlen(slowa[n]);	    } else		ptr++;    };        res = 0;        if (msync(plik,rozmiar, MS_SYNC)!=0) {	perror("msync");	res = -1;    };        if (munmap(plik,rozmiar)!=0){	perror("munmap");	res = -1;    };        close(fd);    	    free(file);    pthread_mutex_lock(&ilosc_watkow_mutex);    ilosc_watkow--;    pthread_mutex_unlock(&ilosc_watkow_mutex);	    return res;};int glob(char *where) {        fprintf(stderr,"wchodze %s\n",(where)?where:".");        if (where && (chdir(where) !=0)) {	perror("chdir");	return -1;    };        int ret = 0;        DIR *dir = opendir(".");        if (!dir){	perror("opendir");	ret = -1;	goto out;    };            struct dirent *de;        while ((de=readdir(dir))) {	if ((strcmp(de->d_name,".")==0) || (strcmp(de->d_name,"..")==0))	    continue;	    	struct stat st;		if (stat(de->d_name,&st)!=0)	    continue;		if (S_ISDIR(st.st_mode))	    glob(de->d_name);	else if(S_ISREG(st.st_mode)&&(strcmp(de->d_name,nazwa)==0)) {	    	    char *cwd = getcwd(0,256);	    	    if (!cwd) {		perror("getcwd");		continue;	    }	    		    char *fname = malloc(strlen(cwd)+strlen(de->d_name)+1+1);	    	    if (!fname) {		perror("malloc");		continue;	    }	    	    sprintf(fname,"%s/%s",cwd,de->d_name);	    free(cwd);	    	    pthread_mutex_lock(&ilosc_watkow_mutex);	    ilosc_watkow++;	    pthread_mutex_unlock(&ilosc_watkow_mutex);		    	    if (pthread_create(0,0,dofile,fname)!=0){		perror("pthread_create");		ret = -1;			free(fname);				pthread_mutex_lock(&ilosc_watkow_mutex);	        ilosc_watkow--;		pthread_mutex_unlock(&ilosc_watkow_mutex);			    }	    	    	}        };    closedir(dir);    out:    if (where) {	fprintf(stderr,"%s ..\n",where);	chdir("..");    };        return ret;};int main(int argc, char *argv[]){        if (argc < 3) {	fprintf(stderr,"Uzycie: %s plik slowo1 (...) slowoN\n",argv[0]);	return -1;    };        nazwa = argv[1];    ilosc = argc-2;    slowa = argv+2;        int ret = glob(0);        while (1) {	pthread_mutex_lock(&ilosc_watkow_mutex);	if (!ilosc_watkow)	    return ret;	pthread_mutex_unlock(&ilosc_watkow_mutex);		sleep (2);    };    };