diff options
author | Danny Holman <dholman@gymli.org> | 2023-07-26 11:40:36 -0500 |
---|---|---|
committer | Danny Holman <dholman@gymli.org> | 2023-07-26 11:40:36 -0500 |
commit | f3d50195e43fd33e7f36e4e55a84af790b7de5d1 (patch) | |
tree | 271f11ccb2297375245df5f9783ca31d84dd5c52 | |
parent | ee0f0f95d6972e66c56812e0e3abe9275d5f0fdc (diff) |
common: create the common directory
Create a directory for files that are shared between the reference
client and server. Refactor the build scripts to reflect this
restructuring.
Signed-off-by: Danny Holman <dholman@gymli.org>
-rw-r--r-- | client/Makefile.am | 3 | ||||
-rw-r--r-- | client/configure.ac | 2 | ||||
-rw-r--r-- | common/include/util.h (renamed from client/include/util.h) | 0 | ||||
-rw-r--r-- | common/src/util.c (renamed from client/src/util.c) | 8 | ||||
-rw-r--r-- | server/Makefile.am | 3 | ||||
-rw-r--r-- | server/include/util.h | 13 | ||||
-rw-r--r-- | server/src/util.c | 76 |
7 files changed, 13 insertions, 92 deletions
diff --git a/client/Makefile.am b/client/Makefile.am index 4f994ee..3812ae5 100644 --- a/client/Makefile.am +++ b/client/Makefile.am @@ -1,2 +1,3 @@ bin_PROGRAMS = mrat -mrat_SOURCES = src/mini-rat.c src/util.c +mrat_SOURCES = src/mini-rat.c ../common/src/util.c +mrat_CFLAGS = -I../common/include diff --git a/client/configure.ac b/client/configure.ac index 325f582..c67c929 100644 --- a/client/configure.ac +++ b/client/configure.ac @@ -12,7 +12,7 @@ AC_CONFIG_FILES([Makefile]) AC_CHECK_HEADERS([stdio.h stdlib.h string.h arpa/inet.h sys/socket.h], [], AC_MSG_ERROR([missing required headers])) AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS+=" -Wall"]) -AX_CHECK_COMPILE_FLAG([-Wextra], [CFLAGS+= "-Wextra"]) +AX_CHECK_COMPILE_FLAG([-Wextra], [CFLAGS+=" -Wextra"]) AC_PROG_CC AC_OUTPUT diff --git a/client/include/util.h b/common/include/util.h index 39c130b..39c130b 100644 --- a/client/include/util.h +++ b/common/include/util.h diff --git a/client/src/util.c b/common/src/util.c index f9053f5..63eefa2 100644 --- a/client/src/util.c +++ b/common/src/util.c @@ -7,6 +7,10 @@ char* str_rstrip(char *str) { int brakes = 1; while (brakes) { switch (str[len]) { + case '\r': + str[len] = '\0'; + len--; + break; case '\n': str[len] = '\0'; len--; @@ -31,6 +35,10 @@ char* str_lstrip(char *str) { int brakes = 1; while (brakes) { switch (str[0]) { + case '\r': + str[0] = '\0'; + str++; + break; case '\n': str[0] = '\0'; str++; diff --git a/server/Makefile.am b/server/Makefile.am index 613cd6d..8e59258 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -1,3 +1,4 @@ bin_PROGRAMS = mratd -mratd_SOURCES = src/mini-rat.c src/server.c src/logging.c src/session.c src/util.c +mratd_SOURCES = src/mini-rat.c src/server.c src/logging.c src/session.c ../common/src/util.c mratd_LDADD = -lpthread +mratd_CFLAGS = -I../common/include diff --git a/server/include/util.h b/server/include/util.h deleted file mode 100644 index 39c130b..0000000 --- a/server/include/util.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef MRAT_UTIL_H -#define MRAT_UTIL_H - -#include <stddef.h> - -#define container_of(ptr, type, member) ({ \ - const typeof(((type*)0)->member)*__mptr = (ptr); \ - (type*)((char*)__mptr - offsetof(type, member)); }) - -char* str_strip(char *str); -char** str_split(char *str, const char *delim); - -#endif diff --git a/server/src/util.c b/server/src/util.c deleted file mode 100644 index f9053f5..0000000 --- a/server/src/util.c +++ /dev/null @@ -1,76 +0,0 @@ -#include <util.h> -#include <string.h> -#include <stdlib.h> - -char* str_rstrip(char *str) { - size_t len = strlen(str) - 1; - int brakes = 1; - while (brakes) { - switch (str[len]) { - case '\n': - str[len] = '\0'; - len--; - break; - case '\t': - str[len] = '\0'; - len--; - break; - case ' ': - str[len] = '\0'; - len--; - break; - default: - brakes = 0; - break; - } - } - return str; -} - -char* str_lstrip(char *str) { - int brakes = 1; - while (brakes) { - switch (str[0]) { - case '\n': - str[0] = '\0'; - str++; - break; - case '\t': - str[0] = '\0'; - str++; - break; - case ' ': - str[0] = '\0'; - str++; - break; - default: - brakes = 0; - break; - } - } - return str; -} - -char* str_strip(char *str) { - str_rstrip(str); - str_lstrip(str); - return str; -} - -char** str_split(char *str, const char *delim) { - size_t count = 0; - char **ret = NULL; - char *token; - char *saveptr; - token = strtok_r(str, delim, &saveptr); - while (token != NULL) { - count++; - ret = realloc(ret, sizeof(char*) * count); - ret[count-1] = strdup(str_strip(token)); - token = strtok_r(NULL, delim, &saveptr); - } - count++; - ret = realloc(ret, sizeof(char*) * count); - ret[count-1] = NULL; - return ret; -} |