fix: config, major bugs remaining (optarg is NULL)

This commit is contained in:
Gu://em_ 2025-11-18 19:25:31 +01:00
parent b9c9b21612
commit 9d3798503d
4 changed files with 37 additions and 18 deletions

View file

@ -3,7 +3,7 @@ CFLAGS = -std=c99 -Werror -Wall -Wextra -Wvla -pedantic
LDFLAGS =
LDLIBS =
LDFLAGS_DBG = -g
CFLAGS_DBG = -g
ASAN_DBG_FLAGS = -fsanitize=address
UTILS_SRCS = src/utils/string/string.c
@ -22,8 +22,14 @@ $(TARGET): $(OBJS)
check:
dash tests/run.sh
debug: LDFLAGS += LDFLAGS_DBG
debug:
debug: CFLAGS += $(CFLAGS_DBG)
debug: $(OBJS)
$(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LDLIBS)
asan: CFLAGS += $(CFLAGS_DBG)
asan: LDFLAGS += $(ASAN_DBG_FLAGS)
asan: $(OBJS)
$(CC) -o $(TARGET) $(OBJS) $(LDFLAGS) $(LDLIBS)
clean:

BIN
httpd/httpd Executable file

Binary file not shown.

View file

@ -28,10 +28,25 @@
static void print_help(char *program_name)
{
printf("Usage: %s [OPTION]\n", program_name);
printf("Usage: %s [OPTIONS]..\n", program_name);
puts("");
}
// Just to reduce the number of lines inside handle_opt to not
static int parse_daemon_arg(struct config *cfg)
{
if (strcmp(optarg, "start") == 0)
cfg->daemon = START;
else if (strcmp(optarg, "stop") == 0)
cfg->daemon = STOP;
else if (strcmp(optarg, "restart") == 0)
cfg->daemon = RESTART;
else
return ARG_INVALID;
return ARG_VALID;
}
// Returns 0 if argument is valid, otherwise
static int handle_opt(char **argv, char opt, struct config *cfg)
{
@ -52,15 +67,7 @@ static int handle_opt(char **argv, char opt, struct config *cfg)
{
// Daemon
case 'd':
if (strcmp(optarg, "start") == 0)
cfg->daemon = START;
else if (strcmp(optarg, "stop") == 0)
cfg->daemon = STOP;
else if (strcmp(optarg, "restart") == 0)
cfg->daemon = RESTART;
else
return ARG_INVALID;
return ARG_VALID;
return parse_daemon_arg(cfg);
// Server name
case 's':
@ -143,16 +150,18 @@ struct config *parse_configuration(int argc, char *argv[])
}
char opt;
int optindex;
while ((opt = getopt_long(argc, argv, "*", options, &optindex)) != -1)
// int optindex;
while ((opt = getopt_long(argc, argv, "dhspirl:", options, NULL)) != -1)
{
int err = handle_opt(argv, opt, config);
if (err != ARG_VALID)
{
if (err == ARG_INVALID)
printf("%s: Invalid argument '%s'", argv[0], argv[optindex]);
// printf("%s: Invalid argument '%s'\n", argv[0],
// argv[optindex]);
printf("%s: Invalid argument\n", argv[0]);
if (err == ARG_NOT_IMPLEMENTED)
printf("%s: This function is not implemented", argv[0]);
printf("%s: This function is not implemented\n", argv[0]);
free(config->servers);
free(config);

View file

@ -1,4 +1,8 @@
int main(void)
#include "config/config.h"
int main(int argc, char **argv)
{
// struct config *config = parse_configuration(argc, argv);
parse_configuration(argc, argv);
return 0;
}