ch5-Find pattern
Chapter_5 Exercise_5-9 echo | Exercise_5-10 |
Note: See also ch4-pattern and Exercise_4-1 in Chapter_4, Sec. 4.1.
CONTENTS: Khayam.txt find.c findp.c
Khayam.txt K&R, p. 68 download
Ah Love! could you and I with Fate conspire
To grasp this sorry Scheme of Things entire,
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
find.c K&R, p. 116 download
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#include <string.h> // for strstr()
#define MAXLINE 1000
int getLine(char *line, int max); // getline() is declared by stdio.h
// print lines that match pattern from first argument
int main(int argc, char *argv[])
{
char line[MAXLINE];
int found = 0, len;
if (argc != 2)
{printf("Usage: ./find pattern\n");}
else
{
while ((len = getLine(line, MAXLINE)) > 0)
{
if (strstr(line, argv[1]) != NULL)
{
printf("%s", line);
if (line[len-1] != '\n')
{putchar('\n');} // line may not end with '\n'
found++;
}
}
}
return found;
}
// getLine(): read a line into s[], return length
int getLine(char *s, int lim)
{
int c = EOF; // initialize
char *p;
// getchar() is only executed if ((p-s) < (lim-1)):
for (p = s; (p-s) < (lim-1); p++)
{ // from 0 to lim-2; s[lim-2]='\n' or not, s[lim-1]='\0'
c = getchar();
if (c == EOF) // (p-s) < (lim-1), getchar() executed
{
*p = '\0'; // the null character ends a string
return p-s;
}
if (c == '\n') // (p-s) < (lim-1), getchar() executed
{
*p++ = c; // '\n'
*p = '\0'; // the null character ends a string
return p-s;
}
*p = c;
} // here c != EOF && c != '\n'
*p = '\0'; // max(p-s) == (lim-1)
return p-s;
}
/*
gcc find.c -o find
./find
Usage: ./find pattern
./find "ould " "Khayam.txt"
Usage: ./find pattern
./find "ould " < Khayam.txt
Ah Love! could you and I with Fate conspire
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
*/
findp.c K&R, p. 117 download
#include <stdio.h> // for getchar(), putchar(), printf(), EOF
#include <string.h> // for strstr()
#define MAXLINE 1000
int getLine(char *line, int max); // getline() is declared by stdio.h
// print lines that match pattern with optional arguments or parameters
int main(int argc, char *argv[])
{
char line[MAXLINE];
long lineno = 0; // line number
int c, except = 0, number = 0, found = 0, len; // length
while (--argc > 0 && (*++argv)[0] == '-') // optional arguments
{
while (c = *++argv[0]) // -x -n -xn -nx
{
switch(c)
{
case 'x' :
except = 1;
break;
case 'n' :
number = 1;
break;
default:
printf("Illegal option: '%c'\n", c);
printf("Usage: ./findp -x -n pattern\n");
argc = 0; // end program
found = -1; // signal error
return found; // exit main()
}
}
}
if (argc != 1) // pattern
{printf("Usage: ./findp -x -n pattern\n");}
else
{
while ((len = getLine(line, MAXLINE)) > 0)
{
lineno++;
if (strstr(line, *argv) != NULL)
{
if (!except)
{
if (number)
{printf("%ld: ", lineno);}
printf("%s", line);
if (line[len-1] != '\n')
{putchar('\n');} // line may not end with '\n'
}
found++;
}
else if (except)
{
if (number)
{printf("%ld: ", lineno);}
printf("%s", line);
if (line[len-1] != '\n')
{putchar('\n');} // line may not end with '\n'
}
}
}
return found;
}
// getLine(): read a line into s[], return length
int getLine(char *s, int lim)
{
int c = EOF; // initialize
char *p;
// getchar() is only executed if ((p-s) < (lim-1)):
for (p = s; (p-s) < (lim-1); p++)
{ // from 0 to lim-2; s[lim-2]='\n' or not, s[lim-1]='\0'
c = getchar();
if (c == EOF) // (p-s) < (lim-1), getchar() executed
{
*p = '\0'; // the null character ends a string
return p-s;
}
if (c == '\n') // (p-s) < (lim-1), getchar() executed
{
*p++ = c; // '\n'
*p = '\0'; // the null character ends a string
return p-s;
}
*p = c;
} // here c != EOF && c != '\n'
*p = '\0'; // max(p-s) == (lim-1)
return p-s;
}
/*
gcc findp.c -o findp
./findp
Usage: ./findp -x -n pattern
./findp "ould " "Khayam.txt"
Usage: ./findp -x -n pattern
./findp "ould " < Khayam.txt
Ah Love! could you and I with Fate conspire
Would not we shatter it to bits -- and then
Re-mould it nearer to the Heart's Desire!
./findp -n "ould " < Khayam.txt
1: Ah Love! could you and I with Fate conspire
3: Would not we shatter it to bits -- and then
4: Re-mould it nearer to the Heart's Desire!
./findp -xn "ould " < Khayam.txt
2: To grasp this sorry Scheme of Things entire,
./findp -x -n "ould " < Khayam.txt
2: To grasp this sorry Scheme of Things entire,
./findp -x "ould " < Khayam.txt
To grasp this sorry Scheme of Things entire,
./findp -m -x "ould " < Khayam.txt
Illegal option: 'm'
Usage: ./findp -x -n pattern
*/
Chapter_5 Exercise_5-9 echo | BACK_TO_TOP | Exercise_5-10 |
Comments
Post a Comment