matmatmat
October 20th, 2009, 06:08 PM
I have this program:
#include <stdio.h>
#include "useful_functions.h"
#define MAXLINE 1000
int main(int argc, char *argv[]){
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0, found = 0, ignore = 0;
while (--argc > 0 && (*++argv)[0] == '-'){
while (c = *++argv[0]){
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
case 'i':
ignore = 1;
break;
default:
printf("Illegal option\n");
argc = 0;
found = -1;
}
}
}
FILE *fp;
*++argv;
fp = fopen(*argv, "r");
*argv--;
if (fp == NULL){
while (getline(line, MAXLINE, stdin) > 0){
lineno++;
char oline[MAXLINE];
if (ignore){
copy(oline, line);
strlower(line);
strlower(*argv);
}
if (match(line, *argv)){
if (number){
printf("%ld: ", lineno);
}
if (ignore){
printf("%s", oline);
}else{
printf("%s", line);
}
found++;
}
}
}else{
while(getline(line,MAXLINE, fp) > 0){
lineno++;
char oline[MAXLINE];
if (ignore){
copy(oline, line);
strlower(line);
strlower(*argv);
}
if (match(line, *argv)){
if (number){
printf("%ld: ", lineno);
}
if (ignore){
printf("%s", oline);
}else{
printf("%s", line);
}
found++;
}
}
}
return found;
}
the functions defined in the header file are:
//strcpy clone
void copy(char to[], char from[]){
int i = 0;
while ((to[i] = from[i]) != '\0'){
++i;
}
}
//tolower clone
char lower(char c){
if (c >= 'A' && c <= 'Z'){
return c + 'a' - 'A';
}else{
return c;
}
}
void strlower(char string[]){
int i;
for(i=0;string[i] != '\0';i++){
string[i] = lower(string[i]);
}
}
int getline(char s[], int lim, FILE *f){
int c, i;
for (i=0; i<lim-1 && (c=getc(f))!=EOF && c!='\n'; ++i){
s[i] = c;
}
if (c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int match(const char *string, char *pattern) {
int status;
regex_t re;
if(regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
return 0;
}
status = regexec(&re, string, (size_t)0, NULL, 0);
regfree(&re);
if(status != 0) {
return 0;
}
return 1;
}
when run like this:
ls | /.mgrep .c$
I expect it to print out all files that end with '.c', but it prints nothing
#include <stdio.h>
#include "useful_functions.h"
#define MAXLINE 1000
int main(int argc, char *argv[]){
char line[MAXLINE];
long lineno = 0;
int c, except = 0, number = 0, found = 0, ignore = 0;
while (--argc > 0 && (*++argv)[0] == '-'){
while (c = *++argv[0]){
switch (c) {
case 'x':
except = 1;
break;
case 'n':
number = 1;
break;
case 'i':
ignore = 1;
break;
default:
printf("Illegal option\n");
argc = 0;
found = -1;
}
}
}
FILE *fp;
*++argv;
fp = fopen(*argv, "r");
*argv--;
if (fp == NULL){
while (getline(line, MAXLINE, stdin) > 0){
lineno++;
char oline[MAXLINE];
if (ignore){
copy(oline, line);
strlower(line);
strlower(*argv);
}
if (match(line, *argv)){
if (number){
printf("%ld: ", lineno);
}
if (ignore){
printf("%s", oline);
}else{
printf("%s", line);
}
found++;
}
}
}else{
while(getline(line,MAXLINE, fp) > 0){
lineno++;
char oline[MAXLINE];
if (ignore){
copy(oline, line);
strlower(line);
strlower(*argv);
}
if (match(line, *argv)){
if (number){
printf("%ld: ", lineno);
}
if (ignore){
printf("%s", oline);
}else{
printf("%s", line);
}
found++;
}
}
}
return found;
}
the functions defined in the header file are:
//strcpy clone
void copy(char to[], char from[]){
int i = 0;
while ((to[i] = from[i]) != '\0'){
++i;
}
}
//tolower clone
char lower(char c){
if (c >= 'A' && c <= 'Z'){
return c + 'a' - 'A';
}else{
return c;
}
}
void strlower(char string[]){
int i;
for(i=0;string[i] != '\0';i++){
string[i] = lower(string[i]);
}
}
int getline(char s[], int lim, FILE *f){
int c, i;
for (i=0; i<lim-1 && (c=getc(f))!=EOF && c!='\n'; ++i){
s[i] = c;
}
if (c == '\n'){
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}
int match(const char *string, char *pattern) {
int status;
regex_t re;
if(regcomp(&re, pattern, REG_EXTENDED|REG_NOSUB) != 0) {
return 0;
}
status = regexec(&re, string, (size_t)0, NULL, 0);
regfree(&re);
if(status != 0) {
return 0;
}
return 1;
}
when run like this:
ls | /.mgrep .c$
I expect it to print out all files that end with '.c', but it prints nothing