blob: f4a296e6b8f614d2dc8f12c426e07842ad37701d (
plain)
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
|
// find string in
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char **argv){
char buffer[BUFSIZ] = { 0 };
char *buf, *start;
ssize_t len = 0;
int save_size = 0;
int cpy = 0;
unsigned long long pos = 0;
unsigned long long cpy_size = 0;
int i = 0;
// get max argument length
for(i = 1; i < argc; i++) {
int len = strlen(argv[i]);
if(len > save_size)
save_size = len;
}
cpy = save_size - 1;
cpy_size = BUFSIZ - cpy;
buf = buffer;
start = buf + cpy;
// copy our first cpy length into the first part of our buffer
len = fread(buffer, 1, cpy, stdin);
if(len == 0)
exit(EXIT_FAILURE);
// read into "start" (buf + cpy) from stdin
while((len = fread(start, 1, cpy_size, stdin)) != 0) {
for(i = 1; i < argc; i++) {
char *p = buffer;
while((p = strstr(p, argv[i])) != NULL) {
printf("%d %llu\n", i - 1, pos + (p - buffer));
p++;
}
}
memcpy(buffer, buffer + len, cpy);
pos = pos + len;
}
return 0;
}
|