commit 5e1e4932689ea48bb91124f7e4b2c0d2136bcc71
parent 27d0d9371aa44c9f00dbc8374ba10cc12270895d
Author: Robin <kroekerrobin@gmail.com>
Date: Sat, 3 Sep 2022 19:03:46 +0200
Allocate only as much memory as needed
Diffstat:
2 files changed, 20 insertions(+), 4 deletions(-)
diff --git a/htex.c b/htex.c
@@ -1,12 +1,12 @@
#include <stdio.h>
+#include <stdbool.h>
+#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
-#include <stdbool.h>
#include <fcntl.h>
-#define ONE_MILLION 1000000
-char text[ONE_MILLION];
+char *text;
char attribute_name[200];
char tag_name[50];
bool inner_html = false;
@@ -323,6 +323,11 @@ int main(int argc, char *argv[]) {
int i = 0;
char buffer;
int o;
+ text = malloc(sizeof(char));
+ if (!text) {
+ printf("malloc error.\n");
+ return -1;
+ }
static struct option long_options[] = {
{ "attribute", required_argument, 0, 'a' },
@@ -347,16 +352,28 @@ int main(int argc, char *argv[]) {
while (read(0, &buffer, 1) > 0) {
text[i] = buffer;
i++;
+ text = realloc(text, (i+1) * sizeof(char));
+ if (!text || text == NULL) {
+ printf("realloc error.\n");
+ return -1;
+ }
}
find_html_tag();
+ free(text);
} else {
int fd = open(argv[argc-1], O_RDONLY);
if (fd != -1) {
while (read(fd, &buffer, 1) > 0) {
text[i] = buffer;
i++;
+ text = realloc(text, (i+1) * sizeof(char));
+ if (!text || text == NULL) {
+ printf("realloc error.\n");
+ return -1;
+ }
}
find_html_tag();
+ free(text);
} else {
printf("Couldn't read file \"%s\"\n", argv[argc-1]);
}
diff --git a/todo b/todo
@@ -1,4 +1,3 @@
-don't allocate one million bytes of memory rather make it dynamic
implement find_attribute_value_by_*
implement filtering not only by class or id, also like this .test[data="asdf"]
improve structure of code