commit 5cc9ba1cdcc91683bf1c39beb3a6d21d37526ef9
parent 09bda953d3b4050864a963801e3d6c952fb9fafa
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date: Fri, 20 Mar 2026 15:45:51 +0100
Add ANNTP_STRDUP
Diffstat:
| M | anntp.h | | | 79 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- |
1 file changed, 71 insertions(+), 8 deletions(-)
diff --git a/anntp.h b/anntp.h
@@ -1,6 +1,54 @@
-/*
- * See LICENSE file or copyright and license details.
- */
+/**
+ ** anntp v0.1.0 - public-domain nntp client implementation in C - by Mario Rosell an contributors
+ ** THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED.
+ ** use at your own risk.
+ ** #include ...
+ **
+ ** Add:
+ ** #define ANNTP_IMPLEMENTATION
+ ** before including anntp on exactly ONE C or C++ file to generate all implementation code. Else you will get
+ ** ``undefined reference to ...'' erorrs :).
+ **
+ ** It should look something like:
+ **
+ ** #include ...
+ ** #include ...
+ ** #define ANNTP_IMPLEMENTATION
+ ** [extra config here]
+ ** #include "anntp.h"
+ **
+ ** You can define ANNTP_BUFSIZE to set the buffer size for networking, ANNTP_MALLOC and ANNTP_FREE for the memory
+ ** allocator and free function, respectively, and ANNTP_API to add a declaration specifier for all API functions.
+ **
+ ** You can also define ANNTP_STRDUP to set a custom (perhaps portable) string duplication function.
+ **
+ ** NOTES
+ ** - This library is slow (although work is being done to make it better).
+ ** - This library is low level (higher-level wrappers coming soon, but don't expect a lot, this is by design).
+ ** - This library has and will always have a limited API by design.
+ ** - This library is in development, everything can change fast.
+ ** - This library doesn't work in Windows, and I doubt it will be easy to port...
+ ** + This library is small, relatively readable to be networked C, and hackable.
+ ** + This library is minimalist.
+ ** + This library is very easy to use.
+ ** + This library has good error handling.
+ ** + This library uses idiomatic C.
+ ** + This library exists, which makes it one of the first exclusively-NNTP libraries.
+ ** + Technically, this library is capable of doing everything (although it can be tedious!).
+ **
+ ** The model is based on connections, which are basically client-server, command-response abstractions. The rest is
+ ** pretty straight forward.
+ **
+ ** LICENSE
+ ** See the ./LICENSE file on the distribution (or click the LICENSE link on the Git viewer) to see copyright info.
+ ** Exclusively CC0, so cool :).
+ **
+ ** CONTRIBUTORS
+ ** - Mario Rosell <mario@mariorosell.es> Most things
+ **
+ ** TODOS:
+ ** See the TODOS file.
+ **/
#ifndef ANNTP_H
#define ANNTP_H
@@ -43,6 +91,21 @@
# define ANNTP_API extern
#endif
+#ifndef ANNTP_STRDUP
+# if (defined(__GNUC__) && defined(_GNU_SOURCE) || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200809L)
+# define ANNTP_STRDUP strdup
+# else
+# define ANNTP_STRDUP anntp__NC__strdup
+char*
+anntp__NC__strdup(const char* s) {
+ size_t len = strlen(s) + 1;
+ char* p = (char*)malloc(len);
+ if (p) memcpy(p, s, len);
+ return p;
+}
+# endif
+#endif
+
/*** defs ***/
#define Bool int
@@ -132,7 +195,7 @@ anntp_mkconn(const char* host, const char* port, Bool tls)
if (!cv) return NULL;
memset(cv, 0, sizeof(*cv));
- cv->host = strdup(host);
+ cv->host = ANNTP_STRDUP(host);
cv->use_tls = tls;
cv->state = ANS_OFF;
cv->fd = -1;
@@ -224,7 +287,7 @@ anntp_freeconn(AnntpConnection* cv)
ssize_t
anntp_read(AnntpConnection* cv, uchar_t* buf, size_t len)
{
- if (!cv || !buf) ANNTPE(ANE_PARAMS);
+ if (!cv || !buf) return ANNTPE(ANE_PARAMS);
#ifdef ANNTP_TLS
if (cv->use_tls && cv->ssl) {
@@ -249,8 +312,7 @@ anntp_write(AnntpConnection* cv, const uchar_t* buf, size_t len)
}
#endif
- ssize_t n = write(cv->fd, buf, len);
- return (n < 0) ? ANNTPE(ANE_IO) : write(cv->fd, buf, len);
+ return (n < 0) ? ANNTPE(ANE_IO) : n;
}
ssize_t
@@ -316,7 +378,7 @@ anntp_readdot(AnntpConnection* cv, char* buf, size_t maxlen)
if (!cv || !buf) return -1;
size_t pos = 0;
- char* line = (char*)malloc(ANNTP_BUFSIZE);
+ char* line = (char*)ANNTP_MALLOC(ANNTP_BUFSIZE);
ssize_t n;
for (;;) {
@@ -341,6 +403,7 @@ anntp_readdot(AnntpConnection* cv, char* buf, size_t maxlen)
pos += len;
}
+ ANNTP_FREE(line);
buf[pos] = '\0';
return (ssize_t)pos;
}