-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminigrep5.c
executable file
·142 lines (100 loc) · 3.33 KB
/
minigrep5.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Engr117 lab8
"strings strings strings"
This is a very simple grep program.
Author: Bryce Lobdell
Login: lobdellb
Section: sec1
Date: 4/10/99
*/
/* Preprocessor directive.... */
#include <stdio.h>
/* This function inputs a whole line from stdin until a
linefeed is encounted. It is stored in buffer. scanf
with the %s flag will only read until it finds a space, and
that isn't acceptable for this assignment. This function
will return EOF when it recieves and end of file, and there
isn't any other data in the buffer. */
int Getline(char *Buffer)
{
/* Ascii is the inputed character (return value from getchar
Count is the current position in the buffer
Retval is the value that will be returned (either EOF or 0) */
int Ascii, Count=0, Retval=0;
/* Will loop until a new line or a EOF is reached */
while (((Ascii = getchar()) != '\n') && (Ascii != EOF))
{
*(Buffer + Count) = (char) Ascii; /* stores stuff in buffer */
Count ++;
}
if ((Ascii == EOF) && (Count == 0)) /*returns eof ONLY if buffer is empty */
Retval = EOF;
*(Buffer + Count) = '\0'; /* Terminates the string */
return Retval;
}
/* This function finds the length of a string.
Be carefull, because if string isn't terminated,
this will probably cause a seg fault. */
int Strlen(char *String)
{
int Count=0, Loc=-1;
/* loops until loc a \0 is reached (and loc is assigned a value) */
while (Loc == -1)
{
if (*(String + Count) == '\0')
Loc = Count;
Count ++;
}
return Loc;
}
/* The main function (you probably didn't need me to tell you that...
search is a pointer to the string to be searched for
buffer is where strings will be read into from stdin
length is the length of the line of text current in the buffer
count,count2 are counters for a "for" loop
CharMatch is a flag that is = 1 until a difference is found (between strings)
Word Match is a flag that is set if CharMatch is still set to one after
two strings are compared
len is the length of the search string */
int main(int Argc, char *Argv[])
{
char *Search, Buffer[256];
int Length, Count, Count2, WordMatch, CharMatch, Len;
Search = Argv[1];
Len = Strlen(Search);
if (Argc != 2) /* Error control */
printf("Wrong Number of Command Line Arguments.\n");
else
{
while (Getline(Buffer) != EOF) /* Loops until a EOF */
{
Length = Strlen(Buffer);
WordMatch = 0;
/* outer loop goes through buffer and looks for the
first character in the first string. When it finds
it, it goes into the inner loop. */
for (Count = 0; Count < Length; Count ++)
{
if (Buffer[Count] == *Search)
z{
zCharMatch = 1;
z/* The inner loop starts assuming that the
zstrings are the same (Same =1) and if it finds
zand characters that does match, it sets same=0. */
zfor (Count2 = 0; Count2 < Len; Count2 ++)
zzif (Buffer[Count + Count2] != *(Search + Count2))
zzzCharMatch = 0;
z}
if (CharMatch) /* if no characters mismatch, prints whole line */
z{
zCharMatch = 0;
zWordMatch = 1;
z}
}
/* If the search string appears: print the whole line */
if (WordMatch)
printf("%s\n", Buffer);
}
}
/* bih-duh, bih-duh, bih-duh, thats all folks! */
return 0;
}