From cc799f9d91f937abd032f95d252d49c6cd9a625c Mon Sep 17 00:00:00 2001 From: AGentooCat Date: Thu, 13 Jun 2024 16:16:36 +0000 Subject: [PATCH] add sendline util + fix bugs Signed-off-by: AGentooCat --- config.mk | 2 +- src/util.c | 15 ++++++++++++++- src/util.h | 1 + 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/config.mk b/config.mk index 0d59d5e..7d43bae 100644 --- a/config.mk +++ b/config.mk @@ -21,7 +21,7 @@ PROJ_LD = # set the source files to be built for the program/library # dont prefix src/ or src/lib/ -PROG_SRCS=main.c args.c config.c mail.c sam.c +PROG_SRCS=main.c args.c config.c util.c mail.c sam.c LIB_SRCS= # what the program depends on (-l) diff --git a/src/util.c b/src/util.c index 90f0c11..dd93c99 100644 --- a/src/util.c +++ b/src/util.c @@ -1,9 +1,11 @@ #include +#include +#include #include "util.h" void strip_crlf(const char *text) { - const *cur = strchr(text, 0); + char *cur = strchr(text, 0); // sanely assuming that text has a NUL char cur -= 1; while (*cur == '\r' || *cur == '\n') { @@ -11,3 +13,14 @@ void strip_crlf(const char *text) { cur -= 1; } } + +int sendline(int fd, const char *line) { + long len = strlen(line) + 3; + char *text = malloc(len); + if (!text) return -1; + strcpy(text, line); + strcat(text, "\r\n"); + text[len - 1] = 0; + + return write(fd, line, len - 1); +} diff --git a/src/util.h b/src/util.h index 09acc28..6368dbc 100644 --- a/src/util.h +++ b/src/util.h @@ -10,5 +10,6 @@ } void strip_crlf(const char *text); +int sendline(int fd, const char *line); #endif