#include #include #include // Is there a function to find the terminal width? #define MAXLINE 80 // I'm too lazy to look it up. int getline(char s[], int lim); int search(char line[], char str[]); main(int argc, char *argv[]) { if (argv[1] == NULL) // Skip blank searches return; char line[MAXLINE+2], *str, i, j; str = argv[1]; while (getline(line, MAXLINE) >= 0) { if (search(line, str)) printf("%s\n", line); } } int getline(char s[], int lim) { int c, i = 0; while ((c=getchar()) != '\n' && c != EOF && i < lim) { s[i++] = c; } if (c == EOF) return -1; s[i] = '\0'; return i; } int search(char line[], char str[]) { if (strlen(line) < strlen(str)) return; // str too big! int i, j, match; for (i = strlen(line) - strlen(str); i >= 0; i--) { match = 1; // Searches from top down to check the for (j = strlen(str) - 1; j >= 0; j--) // strlen() less if (str[j] != line[i+j]) { match = 0; break; } if (match) return 1; } return 0; }