commit ba9d46cf86de6e89d1e66b6c69d98705205ee1fc
Author: Stephanos Pavlou <steph@stephanospavlou.info>
Date: Fri, 22 Mar 2024 12:41:58 -0600
intial commit
Diffstat:
A | asciisearch.c | | | 123 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 123 insertions(+), 0 deletions(-)
diff --git a/asciisearch.c b/asciisearch.c
@@ -0,0 +1,123 @@
+/*
+** asciisearch.c
+** Author: Stephanos Pavlou
+** Small utility for ascii string search (including control characters).
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#define VERSION "0.1.0"
+#define ESC_CHAR '+'
+
+unsigned char *encode_pattern(char *pattern, int *len);
+int search_for_pattern(unsigned char *pattern, int len, char *file_name);
+
+char *usage_msg =
+"asciisearch-"VERSION"\n"
+"Usage: asciisearch [pattern] [file]\n";
+
+char *ctrl_char_code[] = {
+ "^A", "^B", "^C", "^D", "^E", "^F",
+ "^G", "^H", "^I", "^J", "^K", "^L",
+ "^M", "^N", "^O", "^P", "^Q", "^R",
+ "^S", "^T", "^U", "^V", "^W", "^X",
+ "^Y", "^Z", "^@", "^[", "^\\", "^]",
+ "^^", "^_", "^?"
+};
+
+unsigned char ctrl_char[] = {
+ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
+ 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c,
+ 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12,
+ 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
+ 0x19, 0x1a, 0x00, 0x1b, 0x1c, 0x1d,
+ 0x1e, 0x1f, 0x7f
+};
+
+int
+main(int argc, char **argv) {
+ if (argc != 3) {
+ printf("%s", usage_msg);
+ return 0;
+ }
+
+ int *len= malloc(sizeof(int));
+ unsigned char *pattern = encode_pattern(argv[1], len);
+ printf("encoded pattern: ");
+ for (int i = 0; i < *len; i++) {
+ fputc(pattern[i], stdout);
+ }
+ printf("\n");
+ return search_for_pattern(pattern, *len, argv[2]);
+}
+
+unsigned char *
+encode_pattern(char *pattern, int *len) {
+ *len = 0;
+ unsigned char *result = NULL;
+ for (int i = 0; i < strlen(pattern); i++) {
+ if (pattern[i] == '^') {
+ for (int j = 0; j < 33; j++) {
+ if (strstr(pattern + i, ctrl_char_code[j])
+ == pattern + i) {
+ result = realloc(result,
+ sizeof(unsigned char) * ++(*len));
+ result[*len - 1] = ctrl_char[j];
+ i++;
+ break;
+ }
+ }
+ } else if (i + 1 < strlen(pattern) &&
+ (pattern[i] == ESC_CHAR && pattern[i + 1] == '^')) {
+ for (int j = 0; j < 33; j++) {
+ if (i + 1 < strlen(pattern) &&
+ (strstr(pattern + i, ctrl_char_code[j])
+ == pattern + (i + 1))) {
+ result = realloc(result,
+ sizeof(unsigned char) * ++(*len));
+ result[*len - 1] = pattern[i + 1];
+ i++;
+ break;
+ }
+ }
+ } else {
+ result = realloc(result,
+ sizeof(unsigned char) * ++(*len));
+ result[*len - 1] = pattern[i];
+ }
+ }
+
+ return result;
+}
+
+int
+search_for_pattern(unsigned char *pattern, int len,
+ char *file_name) {
+ int ret = 1;
+ FILE *fp = fopen(file_name, "r");
+ if (fp == NULL) {
+ return ret;
+ }
+
+ int c;
+ while ((c = fgetc(fp)) != EOF) {
+ if (c == (int)pattern[0]) {
+ int found = 1;
+ for (int i = 1; i < len && (c = fgetc(fp)) != EOF; i++) {
+ if ((int)pattern[i] != c) {
+ found = 0;
+ break;
+ }
+ }
+ if (c != EOF && found == 1) {
+ ret = 0;
+ break;
+ }
+ }
+ }
+
+ fclose(fp);
+ return ret;
+}