sacc

sacc(omys), simple console gopher client (mirror)
git clone https://git.parazyd.org/sacc
Log | Files | Refs | LICENSE

commit 7147f4745a6d08c9986e26df265d59db174663f8
parent 551de298348c406d14ab8f2787cfeabbeb89bd6e
Author: Quentin Rameau <quinq@fifth.space>
Date:   Sat, 24 Feb 2018 13:24:26 +0100

Add local asprintf in case not available on target system

Diffstat:
Mcommon.h | 3+++
Msacc.c | 29++++++++++++++++++++++++++---
2 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/common.h b/common.h @@ -22,6 +22,9 @@ struct dir { size_t curline; }; +#ifndef asprintf +int asprintf(char **s, const char *fmt, ...); +#endif /* asprintf */ void die(const char *fmt, ...); size_t mbsprint(const char *s, size_t len); const char *typedisplay(char t); diff --git a/sacc.c b/sacc.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <errno.h> #include <fcntl.h> +#include <limits.h> #include <locale.h> #include <netdb.h> #include <netinet/in.h> @@ -39,6 +40,28 @@ die(const char *fmt, ...) exit(1); } +#ifndef asprintf +int +asprintf(char **s, const char *fmt, ...) +{ + va_list ap; + int n; + + va_start(ap, fmt); + n = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + if (n == INT_MAX || !(*s = malloc(++n))) + return -1; + + va_start(ap, fmt); + vsnprintf(*s, n, fmt, ap); + va_end(ap); + + return n; +} +#endif /* asprintf */ + /* print `len' columns of characters. */ size_t mbsprint(const char *s, size_t len) @@ -558,9 +581,9 @@ plumbitem(Item *item) if (!path[0]) { clear(&path); if (!tag) { - n = snprintf(NULL, 0, "%s/%s", tmpdir, file); - path = xmalloc(++n); - snprintf(path, n, "%s/%s", tmpdir, file); + if (asprintf(&path, "%s/%s", tmpdir, file) < 0) + die("Can't generate tmpdir path: ", + strerror(errno)); } }