=================================================================== RCS file: /cvs/mandoc/Attic/xstd.c,v retrieving revision 1.1 retrieving revision 1.13 diff -u -p -r1.1 -r1.13 --- mandoc/Attic/xstd.c 2008/12/23 05:30:49 1.1 +++ mandoc/Attic/xstd.c 2009/03/23 14:22:11 1.13 @@ -1,6 +1,6 @@ -/* $Id: xstd.c,v 1.1 2008/12/23 05:30:49 kristaps Exp $ */ +/* $Id: xstd.c,v 1.13 2009/03/23 14:22:11 kristaps Exp $ */ /* - * Copyright (c) 2008 Kristaps Dzonsons + * Copyright (c) 2008, 2009 Kristaps Dzonsons * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the @@ -16,13 +16,23 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ +#include #include #include #include -#include "private.h" +#include "libmdoc.h" +#ifdef __linux__ +extern size_t strlcpy(char *, const char *, size_t); +extern size_t strlcat(char *, const char *, size_t); +#endif +/* + * Contains wrappers for common functions to simplify their general + * usage throughout this codebase. + */ + int xstrcmp(const char *p1, const char *p2) { @@ -30,7 +40,6 @@ xstrcmp(const char *p1, const char *p2) return(0 == strcmp(p1, p2)); } - int xstrlcat(char *dst, const char *src, size_t sz) { @@ -38,7 +47,6 @@ xstrlcat(char *dst, const char *src, size_t sz) return(strlcat(dst, src, sz) < sz); } - int xstrlcpy(char *dst, const char *src, size_t sz) { @@ -46,15 +54,13 @@ xstrlcpy(char *dst, const char *src, size_t sz) return(strlcpy(dst, src, sz) < sz); } - - void * -xcalloc(size_t num, size_t sz) +xrealloc(void *ptr, size_t sz) { void *p; - if (NULL == (p = calloc(num, sz))) - err(EXIT_FAILURE, "calloc"); + if (NULL == (p = realloc(ptr, sz))) + err(EXIT_FAILURE, "realloc"); return(p); } @@ -69,3 +75,23 @@ xstrdup(const char *p) return(pp); } +int +xstrlcpys(char *buf, const struct mdoc_node *n, size_t sz) +{ + char *p; + + assert(sz > 0); + assert(buf); + *buf = 0; + + for ( ; n; n = n->next) { + assert(MDOC_TEXT == n->type); + p = n->string; + if ( ! xstrlcat(buf, p, sz)) + return(0); + if (n->next && ! xstrlcat(buf, " ", sz)) + return(0); + } + + return(1); +}