nobotgate/check_token.c
2026-09-08 18:36:12 +02:00

39 lines
852 B
C

#include <libowfat/stralloc.h>
#include <libowfat/errmsg.h>
#include <libowfat/open.h>
#include <libowfat/str.h>
#include <errno.h>
#include "nobotgate.h"
int check_token(char *tokenfile, stralloc *token)
{
int fd;
char buf[4096];
buffer b;
int r,found;
stralloc line;
fd = open_read(tokenfile);
if (fd < 0)
diesys(errno == ENOENT ? 100 : 111, "open_read ", tokenfile);
buffer_init_read(&b, fd, buf, sizeof(buf));
stralloc_init(&line);
found = 0;
for (;;) {
r = buffer_getnewline_sa(&b, &line);
if (r == 0) break;
if (r < 0) diesys(111, "failed reading tokenfile ", tokenfile);
stralloc_chomp(&line);
stralloc_0(&line);
line.s[str_chr(line.s, ' ')] = 0;
if (stralloc_equals(token, line.s)) {
found = 1;
break;
}
}
buffer_close(&b);
stralloc_free(&line);
return found;
}