]> git.armaanb.net Git - mmenu.git/commitdiff
Clean up and implement calloc/realloc
authorArmaan Bhojwani <me@armaanb.net>
Sun, 9 May 2021 03:11:26 +0000 (23:11 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Sun, 9 May 2021 03:18:33 +0000 (23:18 -0400)
* Replace /* comments with //
* Replace malloc with calloc + realloc
* Remove extra space between function and arguments
* Check for failed *allocs

mmenu.c

diff --git a/mmenu.c b/mmenu.c
index 2dd8c75bbbdcb342ff7b1ff8b69400170f79f34a..b00af5af1b2d7ee11962f2c003fb7559b50c53f4 100644 (file)
--- a/mmenu.c
+++ b/mmenu.c
@@ -10,6 +10,14 @@ struct MemoryStruct {
        size_t size;
 };
 
        size_t size;
 };
 
+int
+memfail(void)
+{
+       printf("ERROR: Out of memory\n");
+       exit(4);
+}
+
+
 static size_t
 memback(void *contents, size_t size, size_t nmemb, void *userp)
 {
 static size_t
 memback(void *contents, size_t size, size_t nmemb, void *userp)
 {
@@ -17,11 +25,7 @@ memback(void *contents, size_t size, size_t nmemb, void *userp)
        struct MemoryStruct *mem = (struct MemoryStruct *)userp;
 
        char *ptr = realloc(mem->memory, mem->size + realsize + 1);
        struct MemoryStruct *mem = (struct MemoryStruct *)userp;
 
        char *ptr = realloc(mem->memory, mem->size + realsize + 1);
-       if(!ptr) {
-               /* out of memory! */
-               printf("not enough memory (realloc returned NULL)\n");
-               return 0;
-       }
+       if (!ptr) memfail();
 
        mem->memory = ptr;
        memcpy(&(mem->memory[mem->size]), contents, realsize);
 
        mem->memory = ptr;
        memcpy(&(mem->memory[mem->size]), contents, realsize);
@@ -44,8 +48,8 @@ main(int argc, char *argv[])
        }
 
        struct MemoryStruct chunk;
        }
 
        struct MemoryStruct chunk;
-       chunk.memory = malloc(1);  // will be grown as needed by the realloc above
-       chunk.size = 0; // no data at this point
+       chunk.memory = malloc(1); // Grown as needed by the realloc in memback()
+       chunk.size = 0; // No data yet
 
        curl_easy_setopt(curl, CURLOPT_URL,
                        "https://nobilis.nobles.edu/skyworld/castlemenu.php");
 
        curl_easy_setopt(curl, CURLOPT_URL,
                        "https://nobilis.nobles.edu/skyworld/castlemenu.php");
@@ -67,32 +71,41 @@ main(int argc, char *argv[])
        // Parse HTML
        printf("INFO: Parsing HTML\n");
        bool intag = false;
        // Parse HTML
        printf("INFO: Parsing HTML\n");
        bool intag = false;
-       char *outp = (char *) malloc(strlen(chunk.memory) + 1); // TODO: Realloc?
-       strcpy(outp, "");
+       char *outp = (char *) calloc(1, sizeof(char));
+       if (!outp) memfail();
 
        // Extract text from between HTML tags
 
        // Extract text from between HTML tags
+       int j = 1;
        for (int i = 345; i < strlen(chunk.memory); i++) {
                char c = chunk.memory[i];
                if (c == '<') intag = true;
        for (int i = 345; i < strlen(chunk.memory); i++) {
                char c = chunk.memory[i];
                if (c == '<') intag = true;
-               if (!intag) strncat(outp, &c, 1);
+               if (!intag) {
+                       j++;
+                       outp = (char *) realloc(outp, j);
+                       if (!outp) memfail();
+                       strncat(outp, &c, 1);
+               }
                if (c == '>') intag = false;
        }
 
        // Strip empty newlines
                if (c == '>') intag = false;
        }
 
        // Strip empty newlines
-       char *nl = (char *) malloc(strlen(outp));
-       strncpy(nl, "\0", 1);
+       char *nl = (char *) calloc(1, sizeof(char));
+       if (!nl) memfail();
+
        for (int i = 0; i < strlen(outp) - 1; i++) {
                if (outp[i] == '\n' && outp[i+1] == '\n') i+=4;
        for (int i = 0; i < strlen(outp) - 1; i++) {
                if (outp[i] == '\n' && outp[i+1] == '\n') i+=4;
+               nl = (char *) realloc(nl, i);
+               if (!nl) memfail();
                strncat(nl, &outp[i], 1);
        }
 
        // Initialize motif
                strncat(nl, &outp[i], 1);
        }
 
        // Initialize motif
-       Widget             toplevel;
-       XtAppContext       app;
-       Widget             text_w;
-       Arg                args[4];
+       Widget       toplevel;
+       XtAppContext app;
+       Widget       text_w;
+       Arg          args[4];
 
 
-       XtSetLanguageProc (NULL, NULL, NULL);
+       XtSetLanguageProc(NULL, NULL, NULL);
        toplevel = XtVaOpenApplication (&app, "Castle Menu", NULL, 0, &argc, argv,
                        NULL, sessionShellWidgetClass, NULL);
 
        toplevel = XtVaOpenApplication (&app, "Castle Menu", NULL, 0, &argc, argv,
                        NULL, sessionShellWidgetClass, NULL);