All pastes #1228895 Raw Edit

vasoq

public c v1 · immutable
#1228895 ·published 2008-10-16 23:58 UTC
rendered paste body
#include <stdio.h>#include <string.h>#include <math.h>#include <ctype.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <dirent.h>#include <fcntl.h>#include <sys/stat.h>#define LINE_LENGTH 2048int grep_line(char[], char[], int, int);void write_line(char[], int);int fd = '\0';int rd = '\0';int wd = '\0';int filecount = 1;int matched_in_file = 0;DIR *dir;struct dirent *dirent;int main(int argc, char *argv[]){    char buffer[LINE_LENGTH*LINE_LENGTH];    char line[LINE_LENGTH];    {        int temp = '\0';        temp = open("output.dat", O_WRONLY|O_TRUNC|0600);		/*printf("%d", temp);		perror("open");        char temp_buff[LINE_LENGTH];        memset(temp_buff, '\0', LINE_LENGTH);        write(temp,temp_buff, strlen(temp_buff));*/		write(temp, "\0", 1);        close(temp);    }    //printf("%d\n", argc);    //printf("arg 0: %s, arg 1 %s, arg 2 %s\n", argv[0], argv[1], argv[2]);    if(argc <= 2){        perror("Error: Insufficient arguments.\nUsage: mygrep PATTERN FILE\n");        return 1;    }    else if(argc > 3){        perror("Error: Too many arguments.\nUsage: mygrep PATTERN FILE\n");        return 1;    }    argv[1][strlen(argv[1])] = '\0';    argv[2][strlen(argv[2])] = '\0';    if(argc == 3){        int i = 0;        while(i < strlen(argv[1])){            //printf("%c is alphanum? %d\n", argv[1][i], isalnum(argv[1][i]));            if(!isalnum(argv[1][i])){                perror("Error: Argument 1 was not alphanumeric.\nUsage: mygrep PATTERN FILE\n");                return 1;            }            i++;        }        //printf("arg2 is %s\n", argv[2]);        i = 0;        while(i < strlen(argv[2])){            //printf("%c is alphanum? %d\n", argv[2][i], isalnum(argv[2][i]));            if(!isalnum(argv[2][i])){                perror("Error: Argument 2 was not alphanumeric.\nUsage: mygrep PATTERN FILE\n");                return 1;            }            i++;        }    }            if(argv[2]){        dir = opendir(argv[2]);    }        if(dir == '\0'){        perror("Error: Failed to open the directory.\nUsage: mygrep PATTERN FILE\n");        return 1;    }        dirent = readdir(dir);        // here starts the magic    int is_text_file = 0;    while(dirent != '\0'){        is_text_file = 0;        //printf("%s\n", dirent->d_name);        chdir(argv[2]);        if(dirent->d_type == 8){            //printf(" is regular.\n");        }        else{            //printf("\n");        }                {        int i = 0;        while(i < strlen(dirent->d_name)){            if(!strcmp(dirent->d_name + strlen(dirent->d_name) - 4, ".txt")){                //printf("%s is a .txt file.\n", dirent->d_name);                is_text_file = 1;                break;            }            else{                i++;            }        }        }                // read a file        if(is_text_file){                        memset(buffer, '\0', LINE_LENGTH*LINE_LENGTH);                        fd = open(dirent->d_name, O_RDONLY);                        if(fd == -1){                perror("Error: couldn't open file for reading.");                return 1;            }            rd = read(fd, buffer, LINE_LENGTH*LINE_LENGTH*(sizeof(char)));                        //printf("File contents: %s\n", buffer);            close(fd);        }        // split buffer into lines                if(is_text_file){            {            int i = 0;            int j = 0;            int linecount = 0;            while(buffer[i] != '\0'){               if(buffer[i] == '\n'){                    line[j] = '\0';                    j = 0;                    i++;                    if(grep_line(line, argv[1], filecount, matched_in_file)){		                if(matched_in_file == 0){                            filecount++;                        }                        matched_in_file = 1;                    }                    linecount++;                }                else{                    line[j] = buffer[i];                    j++;                    i++;                }            }            }        }        dirent = readdir(dir);        matched_in_file = 0;	}        closedir(dir);        return 0;}int grep_line(char line[], char pattern[], int n, int b){    int found = 0;    //printf("considering line: %s\n", line);    int i = 0;    while(i < strlen(line) && i + strlen(pattern) < strlen(line)){        int match = 0;        int z = 0;        while(z < strlen(pattern)){            //printf("i + z is %d and strlen %d\n", i+z, strlen(line));            if(pattern[z] == line[i + z]){                match = 1;            }            else{                match = 0;                break;            }            z++;        }        if(match == 1){            found = 1;        }        if(match && !b){            //printf("First match from %s\n", dirent->d_name);            char temp_line[LINE_LENGTH];            char temp_int_str[17];            char temp_int_str2[17];            memset(temp_int_str, '\0', 17);            memset(temp_int_str2, '\0', 17);            memset(temp_line, '\0', LINE_LENGTH);            int tmp;            int tmp2;            struct stat buf;                        if(stat(dirent->d_name, &buf) < 0){                perror("Error: Couldn't stat file");                return 0;            }                        tmp = sprintf(temp_int_str, "%d", n);            tmp2 = sprintf(temp_int_str2, "%d", buf.st_size);            strcat(temp_line, "\n");            strcat(temp_line, temp_int_str);            strcat(temp_line, "\t");            strcat(temp_line, dirent->d_name);            strcat(temp_line, "\t");            strcat(temp_line, temp_int_str2);            printf("%s\n", temp_line);            write_line(temp_line, n);            b = 1;        }        if(match){            printf("%s\n", line);            write_line(line, n);            break;        }        i++;    }    return found;}void write_line(char line[], int n){    char buffer[LINE_LENGTH];    int wd;        wd = open("output.dat", O_WRONLY|O_CREAT|O_APPEND,0700);    strcpy(buffer, line);    write(wd, buffer, strlen(buffer));    write(wd, "\n", 1);    close(wd);}