This repository has been archived by the owner on Oct 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grasp.c
92 lines (81 loc) · 2.04 KB
/
grasp.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define LINE_LENGTH 500
int count = 0;
int no_case = 0;
int number_scanned = 0;
void get_temp_line(char *line, char tmp[]);
void process(char *fname, char *pattern);
int main(int argc, char **argv)
{
int c;
char *pattern = NULL;
while ((c = getopt(argc, argv, "ic")) != -1)
{
switch (c)
{
case 'i':
no_case = 1;
break;
case 'c':
count = 1;
break;
case '?':
if (optopt == 'c')
fprintf(stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
return EXIT_FAILURE;
}
}
pattern = argv[optind];
if (no_case == 1)
{
for (char *p = pattern; *p; ++p)
*p = tolower(*p);
}
// process each file
for (int index = optind + 1; index < argc; index++)
{
process(argv[index], pattern);
}
// scan count report
if (count == 1)
printf("\nTotal lines scanned: %d\n", number_scanned);
return EXIT_SUCCESS;
}
void process(char *fname, char *pattern)
{
FILE *file = fopen(fname, "r");
char line[LINE_LENGTH];
if (!file)
{
fprintf(stderr, "Error opening file: %s\n", fname);
}
else
{
while (fgets(line, LINE_LENGTH, file))
{
char tmp[LINE_LENGTH];
get_temp_line(line, tmp);
if (strstr(tmp, pattern) != NULL)
printf("%s: %s", fname, line);
number_scanned++;
}
fclose(file);
if (ferror(file))
fprintf(stderr, "Error reading file: %s.\n", fname);
}
}
void get_temp_line(char *line, char tmp[])
{
strncpy(tmp, line, strlen(line));
if (no_case)
for (char *p = tmp; *p; ++p)
*p = tolower(*p);
}