add sendline util + fix bugs

Signed-off-by: AGentooCat <agentoocat@mail.i2p>
This commit is contained in:
2024-06-13 16:16:36 +00:00
parent 65311274d9
commit cc799f9d91
3 changed files with 16 additions and 2 deletions

View File

@ -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<ib>)

View File

@ -1,9 +1,11 @@
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#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);
}

View File

@ -10,5 +10,6 @@
}
void strip_crlf(const char *text);
int sendline(int fd, const char *line);
#endif