asciisearch.c (3342B)
1 /* 2 ** asciisearch.c 3 ** Author: Stephanos Pavlou 4 ** Small utility for ascii string search (including control characters). 5 */ 6 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <string.h> 10 11 #define VERSION "0.1.0" 12 #define ESC_CHAR '+' 13 14 unsigned char *encode_pattern(char *pattern, int *len); 15 int search_for_pattern(unsigned char *pattern, int len, char *file_name); 16 17 char *usage_msg = 18 "asciisearch-"VERSION"\n" 19 "Usage: asciisearch [pattern] [file]\n"; 20 21 char *ctrl_char_code[] = { 22 "^A", "^B", "^C", "^D", "^E", "^F", 23 "^G", "^H", "^I", "^J", "^K", "^L", 24 "^M", "^N", "^O", "^P", "^Q", "^R", 25 "^S", "^T", "^U", "^V", "^W", "^X", 26 "^Y", "^Z", "^@", "^[", "^\\", "^]", 27 "^^", "^_", "^?" 28 }; 29 30 unsigned char ctrl_char[] = { 31 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 32 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 33 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 34 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 35 0x19, 0x1a, 0x00, 0x1b, 0x1c, 0x1d, 36 0x1e, 0x1f, 0x7f 37 }; 38 39 int 40 main(int argc, char **argv) { 41 if (argc != 3) { 42 printf("%s", usage_msg); 43 return 0; 44 } 45 46 int *len= malloc(sizeof(int)); 47 unsigned char *pattern = encode_pattern(argv[1], len); 48 printf("encoded pattern: "); 49 for (int i = 0; i < *len; i++) { 50 fputc(pattern[i], stdout); 51 } 52 printf("\n"); 53 return search_for_pattern(pattern, *len, argv[2]); 54 } 55 56 unsigned char * 57 encode_pattern(char *pattern, int *len) { 58 *len = 0; 59 unsigned char *result = NULL; 60 for (int i = 0; i < strlen(pattern); i++) { 61 if (pattern[i] == '^') { 62 for (int j = 0; j < 33; j++) { 63 if (strstr(pattern + i, ctrl_char_code[j]) 64 == pattern + i) { 65 result = realloc(result, 66 sizeof(unsigned char) * ++(*len)); 67 result[*len - 1] = ctrl_char[j]; 68 i++; 69 break; 70 } 71 } 72 } else if (i + 1 < strlen(pattern) && 73 (pattern[i] == ESC_CHAR && pattern[i + 1] == '^')) { 74 for (int j = 0; j < 33; j++) { 75 if (i + 1 < strlen(pattern) && 76 (strstr(pattern + i, ctrl_char_code[j]) 77 == pattern + (i + 1))) { 78 result = realloc(result, 79 sizeof(unsigned char) * ++(*len)); 80 result[*len - 1] = pattern[i + 1]; 81 i++; 82 break; 83 } 84 } 85 } else { 86 result = realloc(result, 87 sizeof(unsigned char) * ++(*len)); 88 result[*len - 1] = pattern[i]; 89 } 90 } 91 92 return result; 93 } 94 95 int 96 search_for_pattern(unsigned char *pattern, int len, 97 char *file_name) { 98 int ret = 1; 99 FILE *fp = fopen(file_name, "r"); 100 if (fp == NULL) { 101 return ret; 102 } 103 104 int c; 105 while ((c = fgetc(fp)) != EOF) { 106 if (c == (int)pattern[0]) { 107 int found = 1; 108 for (int i = 1; i < len && (c = fgetc(fp)) != EOF; i++) { 109 if ((int)pattern[i] != c) { 110 found = 0; 111 break; 112 } 113 } 114 if (c != EOF && found == 1) { 115 ret = 0; 116 break; 117 } 118 } 119 } 120 121 fclose(fp); 122 return ret; 123 }