From d33449e1283c9eaf9332d2def67b2ef698cef28b Mon Sep 17 00:00:00 2001 From: Werner Almesberger Date: Sun, 1 Jul 2012 20:32:56 -0300 Subject: [PATCH] tools/libtxt/: move alloc_sprintf from font.c to util.h, for later sharing --- tools/libtxt/Makefile | 3 ++- tools/libtxt/font.c | 26 +------------------------- tools/libtxt/util.h | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 26 deletions(-) create mode 100644 tools/libtxt/util.h diff --git a/tools/libtxt/Makefile b/tools/libtxt/Makefile index 39f80b6..cc3dd0d 100644 --- a/tools/libtxt/Makefile +++ b/tools/libtxt/Makefile @@ -13,7 +13,8 @@ LIB = libtxt.a -CFLAGS = -g -Wall +# _GNU_SOURCE for vasprintf +CFLAGS = -g -Wall -D_GNU_SOURCE OBJS = edit.o font.o diff --git a/tools/libtxt/font.c b/tools/libtxt/font.c index 62ec705..e8d68ff 100644 --- a/tools/libtxt/font.c +++ b/tools/libtxt/font.c @@ -11,14 +11,12 @@ */ -#define _GNU_SOURCE /* for vasprintf */ -#include #include #include -#include #include #include +#include "util.h" #include "libtxt.h" @@ -45,28 +43,6 @@ struct font { }; -/* ----- Helper functions -------------------------------------------------- */ - - -static const char *alloc_sprintf(const char *fmt, ...) -{ - va_list ap; - char *tmp, *res; - int n; - - va_start(ap, fmt); - n = vasprintf(&tmp, fmt, ap); - va_end(ap); - if (n < 0) - abort(); - res = malloc(n+1); - if (!res) - abort(); - memcpy(res, tmp, n+1); - return res; -} - - /* ----- XBM image --------------------------------------------------------- */ diff --git a/tools/libtxt/util.h b/tools/libtxt/util.h new file mode 100644 index 0000000..8d77b52 --- /dev/null +++ b/tools/libtxt/util.h @@ -0,0 +1,40 @@ +/* + * tools/libtxt/util.h - Utility functions + * + * Written 2012 by Werner Almesberger + * Copyright 2012 Werner Almesberger + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef UTIL_H +#define UTIL_H + +#include +#include +#include +#include + + +static const char *alloc_sprintf(const char *fmt, ...) +{ + va_list ap; + char *tmp, *res; + int n; + + va_start(ap, fmt); + n = vasprintf(&tmp, fmt, ap); + va_end(ap); + if (n < 0) + abort(); + res = malloc(n+1); + if (!res) + abort(); + memcpy(res, tmp, n+1); + return res; +} + +#endif /* !UTIL_H */