refractored 04-find.c to seperate compilation units
parent
ee037ecf6f
commit
ff3df46f36
|
@ -3,40 +3,13 @@
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
int grep(char pattern[]);
|
|
||||||
|
|
||||||
/* grep with graceful buffer overflow */
|
|
||||||
/* parse and verify arg pattern, pass it to grep */
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
/* verify arg is given */
|
|
||||||
if (argc < 2) {
|
|
||||||
printf("error: excpected pattern\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *pattern = argv[1];
|
|
||||||
|
|
||||||
/* verify pattern */
|
|
||||||
char c;
|
|
||||||
int i;
|
|
||||||
for (i = 0; (c = pattern[i]) != '\0'; i++)
|
|
||||||
if (c == '\n') {
|
|
||||||
printf("error: newline in pattern\n");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
int found = grep(pattern);
|
|
||||||
printf("found = %d\n", found);
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
void println(void);
|
void println(void);
|
||||||
int flush_lnbuff(void);
|
int flush_lnbuff(void);
|
||||||
int getch_lnbuff(void);
|
int getch_lnbuff(void);
|
||||||
|
|
||||||
/* grep: prints lines containing pattern, returns number of matching lines */
|
/* find: prints lines containing pattern, returns number of matching lines
|
||||||
int grep(char *pattern)
|
* gracefully handle line overflow */
|
||||||
|
int find(char *pattern)
|
||||||
{
|
{
|
||||||
int found, i, c;
|
int found, i, c;
|
||||||
const int plen = strlen(pattern);
|
const int plen = strlen(pattern);
|
||||||
|
|
|
@ -3,39 +3,11 @@
|
||||||
|
|
||||||
#define MAXLINE 1000 /* maximum input line length */
|
#define MAXLINE 1000 /* maximum input line length */
|
||||||
|
|
||||||
int grep(char *pattern);
|
|
||||||
|
|
||||||
/* somewhat optimized book version of grep */
|
|
||||||
/* find all lines matching pattern */
|
|
||||||
int main(int argc, char *argv[])
|
|
||||||
{
|
|
||||||
/* verify arg is given */
|
|
||||||
if (argc < 2) {
|
|
||||||
printf("error: excpected pattern\n");
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *pattern = argv[1];
|
|
||||||
|
|
||||||
/* verify pattern */
|
|
||||||
char c;
|
|
||||||
int i;
|
|
||||||
for (i = 0; (c = pattern[i]) != '\0'; i++)
|
|
||||||
if (c == '\n') {
|
|
||||||
printf("error: newline in pattern\n");
|
|
||||||
return -2;
|
|
||||||
}
|
|
||||||
|
|
||||||
int found = grep(pattern);
|
|
||||||
printf("found = %d\n", found);
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
int getln(char line[], int lim);
|
int getln(char line[], int lim);
|
||||||
int strindex(char source[], char searchfor[], int slen);
|
int strindex(char source[], char searchfor[], int slen);
|
||||||
|
|
||||||
/* grep: prints lines containing pattern, returns number of matching lines */
|
/* find: prints lines containing pattern, returns number of matching lines */
|
||||||
int grep(char *pattern)
|
int find(char *pattern)
|
||||||
{
|
{
|
||||||
char line[MAXLINE];
|
char line[MAXLINE];
|
||||||
int found = 0;
|
int found = 0;
|
||||||
|
|
|
@ -0,0 +1,32 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
/* compile with 04-find-no-overflow.c or 04-find-opt.c
|
||||||
|
* gcc 04-find.c [04-find-no-overflow.c | 04-find-opt.c] */
|
||||||
|
|
||||||
|
int find(char *pattern);
|
||||||
|
|
||||||
|
/* find pattern given by first arg in lines and print them
|
||||||
|
* compile with 04-find-no-overflow.c or 04-find-opt.c */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
/* verify arg is given */
|
||||||
|
if (argc < 2) {
|
||||||
|
printf("error: excpected pattern\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *pattern = argv[1];
|
||||||
|
|
||||||
|
/* verify pattern */
|
||||||
|
char c;
|
||||||
|
int i;
|
||||||
|
for (i = 0; (c = pattern[i]) != '\0'; i++)
|
||||||
|
if (c == '\n') {
|
||||||
|
printf("error: newline in pattern\n");
|
||||||
|
return -2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int found = find(pattern);
|
||||||
|
printf("found = %d\n", found);
|
||||||
|
return found;
|
||||||
|
}
|
Loading…
Reference in New Issue