-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.c
77 lines (73 loc) · 1.33 KB
/
string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include<pthread.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
char *filename=NULL;
void *Search(void *args)
{
char *str=(char*)args;
FILE *fp;
int line=0;
int result=0;
int column=0;
char temp[50];
fp=fopen(filename,"r");
if(fp==NULL)
{
printf("cannot open file");
exit(0);
return (void*)0;
}
else
{
while(fgets(temp,50,fp)!=NULL)
{
if(strstr(temp,str)!=NULL)
{
printf("A match found on line %d\n",line);
printf("A mathc found at column %d\n",column);
printf("string is%s\n",temp);
result++;
}
line++;
column++;
}
if(fp)
{
fclose(fp);
}
if(result==0)
{
printf("sorry could not find match ");
}
return (void*)0;
}
return(void*)1;
pthread_exit(NULL);
}
int main(int argc,char *argv[])
{
int numofthreads=atoi(argv[2]);
pthread_t threads[numofthreads];
filename=(char*)malloc(strlen(argv[1])*sizeof(char*));
stpcpy(filename,argv[1]);
int rc;
long t;
void *result=0;
for(t=0;t<numofthreads;t++)
{
rc=pthread_create(&threads[t],NULL,Search,(void*)argv[3]);
pthread_join(threads[t],&result);
if((long)result==1)
{
pthread_cancel(threads[t]);
}
break;
}
if(rc)
{
printf("Error return code from pthread_create() is%d\n",rc);
exit(-1);
}
pthread_exit(NULL);
}