-CC = musl-gcc
-CFLAGS = -Wall -Wextra -g -static -O3
-LIBS = /usr/local/lib/libcurl.a /usr/local/lib/libzip.a -lzip -lssl -lcrypto -lz -lbz2 -llzma -lzstd -lnghttp2
+DBG_CFLAGS = -Wall -Wextra -g -fsanitize=address
+PROD_CFLAGS = -Wall -Wextra -Os
+LIBS = -lzip -lcurl
SRC = src/main.c src/cpu.c src/downloader.c src/cli.c src/unzip.c
OBJ = build/main.o build/cpu.o build/downloader.o build/cli.o build/unzip.o
RES = build/Thoryum
-all: setup $(OBJ) build
- $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS)
-
-build/main.o: src/main.c
- $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
-
-build/cpu.o: src/cpu.c
- $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
+release: CFLAGS = $(PROD_CFLAGS)
+debug: CFLAGS = $(DBG_CFLAGS)
-build/downloader.o: src/downloader.c
- $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
+release: setup $(OBJ)
+ $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS)
-build/cli.o: src/cli.c
- $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
+debug: setup $(OBJ)
+ $(CC) $(CFLAGS) $(OBJ) -o $(RES) $(LIBS)
-build/unzip.o: src/unzip.c
- $(CC) $(CFLAGS) -c $^ -o $@ $(LIBS)
+build/%.o: src/%.c
+ $(CC) $(CFLAGS) -c $< -o $@
setup:
mkdir -p build
run:
./$(RES)
-debug:
- gdb -tui $(RES)
-
clean:
$(RM) $(OBJ) $(RES)
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
+#include <err.h>
#include "cli.h"
extern char *optarg; // What the hell is that glibc ???
check_help(argc, argv);
struct ctx *ctx = calloc(1, sizeof(struct ctx));
- int opt;
+ if (!ctx)
+ err(1, "Failed to allocate space for the context, abort.\n");
+ int opt;
while ((opt = getopt(argc, argv, "ycl:")) != -1) {
switch (opt) {
- case 'y':
- ctx->silent = 1;
- break;
- case 'c':
- ctx->mode = UPDATE;
- break;
- case 'l':
- ctx->install_loc = strdup(optarg);
- break;
- default: // bozo
- fprintf(stderr, "Usage: %s [-h] [-c] [-y] [-l path]\n",
- argv[0]);
- free(ctx);
- exit(EXIT_FAILURE);
+ case 'y':
+ ctx->silent = 1;
+ break;
+ case 'c':
+ ctx->mode = UPDATE;
+ break;
+ case 'l':
+ ctx->install_loc = strdup(optarg);
+ break;
+ default: // bozo
+ fprintf(stderr, "Usage: %s [-h] [-c] [-y] [-l path]\n", argv[0]);
+ free(ctx);
+ exit(EXIT_FAILURE);
}
}
if (!ctx->install_loc)
return ctx;
}
-void *free_ctx(struct ctx *ctx) {
- free(ctx->install_loc);
- if (ctx->opti_name)
- free(ctx->opti_name);
- free(ctx);
- return NULL;
+void free_ctx(struct ctx **ctx) {
+ if (!ctx || !*ctx)
+ return;
+ free((*ctx)->install_loc);
+ if ((*ctx)->opti_name)
+ free((*ctx)->opti_name);
+ free(*ctx);
+ *ctx = NULL;
}
void print_ctx(struct ctx *ctx) {
- printf(
- "Thoryum context:\n"
- "Mode = %s\n"
- "Install location = '%s'\n"
- "Silent = %s\n",
- ctx->mode ? "UPDATE" : "NORMAL",
- ctx->install_loc,
- ctx->silent ? "YES" : "NOPE"
- );
+ if (!ctx)
+ printf("Thorium context: (null)\n");
+ else
+ printf(
+ "Thoryum context:\n"
+ "Mode = %s\n"
+ "Install location = '%s'\n"
+ "Silent = %s\n",
+ ctx->mode ? "UPDATE" : "NORMAL",
+ ctx->install_loc,
+ ctx->silent ? "YES" : "NOPE"
+ );
}
void print_help() {
};
struct ctx *init_ctx(int argc, char **argv);
-void *free_ctx(struct ctx *ctx);
+void free_ctx(struct ctx **ctx);
void print_ctx(struct ctx *ctx);
void print_help();