From 5fe4539dc1f7e96dca82a83e41ce6f6ee24c8694 Mon Sep 17 00:00:00 2001 From: Emil Date: Wed, 2 Aug 2023 09:09:34 -0600 Subject: [PATCH] Fixed_it_guys.jpg --- GNUmakefile | 6 ++--- Makefile | 6 ++--- include/config.h | 9 +++++++ include/config.mk.h | 9 +++++++ include/creds_parser.h | 8 +++--- include/utils.h | 10 ++++++++ src/creds_parser.c | 70 ++++++++++++++++++++++++++------------------------ src/main.c | 8 ++---- 8 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 include/config.h create mode 100644 include/config.mk.h create mode 100644 include/utils.h diff --git a/GNUmakefile b/GNUmakefile index b070e96..a043eef 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -14,7 +14,7 @@ OBJ.DIR := $(PREFIX)/obj INC.DIR := include OBJ := $(addprefix $(OBJ.DIR)/,fetch.o main.o creds_parser.o) -HDR := creds_parser.h +HDR := config.h utils.h creds_parser.h VPATH := $(INC.DIR) $(SRC.DIR) $(OBJ.DIR) @@ -36,8 +36,8 @@ $(OBJ.DIR)/%.o: $(SRC.DIR)/%.c $(PREFIX)/$(PROGN): $(VPATH) $(PREFIX) $(HDR) | $(OBJ) $(CC) $(CFLAGS) $(CPPFLAGS) -o $@ $| $(LDFLAGS) -# include/config.h: include/config.mk.h -# cp -f $< $@ +include/config.h: include/config.mk.h + cp -f $< $@ $(VPATH) $(PREFIX): mkdir -p $@ diff --git a/Makefile b/Makefile index 55ad647..43f58bd 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ INC.DIR := include SRC := fetch.c main.c creds_parser.c OBJ := fetch.o main.o creds_parser.o -HDR := creds_parser.h +HDR := config.h utils.h creds_parser.h VPATH := ${INC.DIR}:${SRC.DIR}:${OBJ.DIR} @@ -34,8 +34,8 @@ CPPFLAGS := ${CPPFLAGS} -DPROGN="\"${PROGN}\"" ${PROGN}: ${OBJ.DIR} ${HDR} ${OBJ} ${CC} ${CFLAGS} ${CPPFLAGS} -o $@ ${OBJ} ${LDFLAGS} -# include/config.h: include/config.mk.h -# cp -f $< $@ +include/config.h: include/config.mk.h + cp -f $< $@ ${OBJ.DIR}: mkdir -p $@ diff --git a/include/config.h b/include/config.h new file mode 100644 index 0000000..70070ea --- /dev/null +++ b/include/config.h @@ -0,0 +1,9 @@ +#ifndef CONFIG_H_ + +#define SERVER "irc.rizon.net" +#define PORT 6667 +#define CHANNEL "#/g/chad" +#define USERNAME "probotic" + +#define CONFIG_H_ +#endif diff --git a/include/config.mk.h b/include/config.mk.h new file mode 100644 index 0000000..70070ea --- /dev/null +++ b/include/config.mk.h @@ -0,0 +1,9 @@ +#ifndef CONFIG_H_ + +#define SERVER "irc.rizon.net" +#define PORT 6667 +#define CHANNEL "#/g/chad" +#define USERNAME "probotic" + +#define CONFIG_H_ +#endif diff --git a/include/creds_parser.h b/include/creds_parser.h index 25dbc13..b18d655 100644 --- a/include/creds_parser.h +++ b/include/creds_parser.h @@ -1,13 +1,13 @@ #ifndef CREDS_PARSER_H -#define CREDS_PARSER_H typedef struct { char * username; - char * password; + char * password; } creds_t; -int parse_creds(creds_t * creds, char const * creds_file); +int parse_creds(creds_t * creds, char const * creds_file); void clean_creds(creds_t * creds); -#endif \ No newline at end of file +#define CREDS_PARSER_H +#endif diff --git a/include/utils.h b/include/utils.h new file mode 100644 index 0000000..02123f8 --- /dev/null +++ b/include/utils.h @@ -0,0 +1,10 @@ +#ifndef UTILS_H_ + +#include + +#define ERR(ret,msg) do { fputs(msg "\n", stderr); return (ret); } while (0) +#define ERRMSG(msg) fputs(msg "\n", stderr) +#define PERROR(name) perror(name) + +#define UTILS_H_ +#endif diff --git a/src/creds_parser.c b/src/creds_parser.c index 3796666..af700cf 100644 --- a/src/creds_parser.c +++ b/src/creds_parser.c @@ -1,53 +1,55 @@ -#include "creds_parser.h" - #include #include +#include "creds_parser.h" +#include "utils.h" + +/* possibly globalize creds so that we can ensure by using + atexit that it is always cleansed? */ + int parse_creds(creds_t * creds, char const * creds_file) { - FILE * stream; - size_t nread = 0; - - creds->username = NULL; - creds->password = NULL; + FILE * stream; + size_t nread = 0; - stream = fopen(creds_file, "r"); - if (stream == NULL) - { - // TODO: Error macro? - return 1; - } + creds->username = NULL; + creds->password = NULL; - if (getline(&(creds->username), &nread, stream) < 1) { - // Cannot get username - // TODO: error macro? - goto fail; - } + stream = fopen(creds_file, "r"); + if (stream == NULL) + { PERROR(PROGN); } - if (getline(&(creds->password), &nread, stream) < 1) { - // Cannot get password - // TODO: error macro? - goto fail; - } + if (getline(&(creds->username), &nread, stream) < 1) + { + ERRMSG("Cannot get username"); + goto fail; + } - fclose(stream); - return 0; + if (getline(&(creds->password), &nread, stream) < 1) + { + ERRMSG("Cannot get password"); + goto fail; + } - // Releasing everything in cause of a failure + fclose(stream); + return 0; + + // Releasing everything in cause of a failure fail: - fclose(stream); - clean_creds(creds); - return 1; + fclose(stream); + clean_creds(creds); + return 1; } void clean_creds(creds_t * creds) { - free(creds->username); - creds->username = NULL; + /* Should we memset these? */ + free(creds->username); + creds->username = NULL; - free(creds->password); - creds->password = NULL; -} \ No newline at end of file + free(creds->password); + creds->password = NULL; +} diff --git a/src/main.c b/src/main.c index e37ec8b..68a45f3 100644 --- a/src/main.c +++ b/src/main.c @@ -8,12 +8,8 @@ #include -#define SERVER "irc.rizon.net" -#define PORT 6667 -#define CHANNEL "#/g/chad" -#define USERNAME "probotic" - -#define ERR(ret,msg) do { fputs(msg "\n", stderr); return (ret); } while (0) +#include "utils.h" +#include "config.h" irc_session_t * session; irc_callbacks_t callbacks;