From 2281f142880a26dd96c666bcc26ef5c30525d671 Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sun, 14 May 2023 14:24:06 +0530 Subject: 7.7: initial commit --- 7.7/Makefile | 11 +++++++++ 7.7/find | Bin 0 -> 17496 bytes 7.7/main.c | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 7.7/Makefile create mode 100755 7.7/find create mode 100644 7.7/main.c diff --git a/7.7/Makefile b/7.7/Makefile new file mode 100644 index 0000000..6de9d25 --- /dev/null +++ b/7.7/Makefile @@ -0,0 +1,11 @@ +OBJECTS = main.c +CC = gcc +CFLAGS = -Wvla -Wall -Wextra -Wstrict-prototypes -Wmissing-prototypes -fsanitize=address + +find : $(OBJECTS) + $(CC) $(CFLAGS) -o find $(OBJECTS) + +.PHONY : clean + +clean : + rm -f find diff --git a/7.7/find b/7.7/find new file mode 100755 index 0000000..4e0284f Binary files /dev/null and b/7.7/find differ diff --git a/7.7/main.c b/7.7/main.c new file mode 100644 index 0000000..ee944b4 --- /dev/null +++ b/7.7/main.c @@ -0,0 +1,77 @@ +#include +#include +#include +#include + +#define MAXLINE 1000 + +struct flags { + unsigned int except : 1; + unsigned int number : 1; +} flags; + +void fpat(FILE *fp, char *fname, char *pattern, struct flags flags); + +int main(int argc, char *argv[]) +{ + FILE *fp; + char pattern[MAXLINE]; + + while (--argc > 0 && **++argv == '-') { + while (*++*argv) { + switch (**argv) { + case 'x': + flags.except = true; + break; + case 'n': + flags.number = true; + break; + default: + fprintf(stderr, "find: %c: unknown flag\n", **argv); + exit(1); + break; + } + } + } + + if (argc >= 1) { + strncpy(pattern, *argv, MAXLINE); + } else { + fprintf(stderr, "Usage: find -xn pattern [file ...]\n"); + exit(1); + } + + if (argc == 1) { + fpat(stdin, "", pattern, flags); + fp = stdin; + } else { + while (--argc > 0) { + if (!(fp = fopen(*++argv, "r"))) { + fprintf(stderr, "find: can't open %s\n", *argv); + exit (1); + } else { + fpat(fp, *argv, pattern, flags); + fclose(fp); + } + } + } + + return 0; +} + +void fpat(FILE *fp, char *fname, char *pattern, struct flags flags) +{ + char line[MAXLINE]; + unsigned long lineno; + + lineno = 0; + while (fgets(line, MAXLINE, fp) != NULL) { + if (!!(strstr(line, pattern)) != flags.except) { + if (*fname) + printf("%s - ", fname); + if (flags.number) + printf("%5lu: ", ++lineno); + printf("%s", line); + } + } +} -- cgit v1.2.3