=================================================================== RCS file: /cvs/mandoc/compat_fts.c,v retrieving revision 1.12 retrieving revision 1.17 diff -u -p -r1.12 -r1.17 --- mandoc/compat_fts.c 2016/10/18 23:58:12 1.12 +++ mandoc/compat_fts.c 2020/06/15 01:37:14 1.17 @@ -1,14 +1,6 @@ -#include "config.h" +/* $Id: compat_fts.c,v 1.17 2020/06/15 01:37:14 schwarze Exp $ */ +/* $OpenBSD: compat_fts.c,v 1.17 2020/06/15 01:37:14 schwarze Exp $ */ -#if HAVE_FTS - -int dummy; - -#else - -/* $Id: compat_fts.c,v 1.12 2016/10/18 23:58:12 schwarze Exp $ */ -/* $OpenBSD: compat_fts.c,v 1.12 2016/10/18 23:58:12 schwarze Exp $ */ - /*- * Copyright (c) 1990, 1993, 1994 * The Regents of the University of California. All rights reserved. @@ -37,6 +29,7 @@ int dummy; * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ +#include "config.h" #include #include @@ -62,16 +55,12 @@ static int fts_palloc(FTS *, size_t); static FTSENT *fts_sort(FTS *, FTSENT *, int); static unsigned short fts_stat(FTS *, FTSENT *); +typedef int (*qsort_compar_proto)(const void *, const void *); + #define ISDOT(a) (a[0] == '.' && (!a[1] || (a[1] == '.' && !a[2]))) -#ifndef O_DIRECTORY -#define O_DIRECTORY 0 -#endif #ifndef O_CLOEXEC #define O_CLOEXEC 0 #endif -#ifndef PATH_MAX -#define PATH_MAX 4096 -#endif #define CLR(opt) (sp->fts_options &= ~(opt)) #define ISSET(opt) (sp->fts_options & (opt)) @@ -84,7 +73,7 @@ fts_open(char * const *argv, int options, FTS *sp; FTSENT *p, *root; int nitems; - FTSENT *parent, *tmp; + FTSENT *parent, *prev; /* Options check. */ if (options & ~FTS_OPTIONMASK) { @@ -117,7 +106,7 @@ fts_open(char * const *argv, int options, parent->fts_level = FTS_ROOTPARENTLEVEL; /* Allocate/initialize root(s). */ - for (root = NULL, nitems = 0; *argv; ++argv, ++nitems) { + for (root = prev = NULL, nitems = 0; *argv; ++argv, ++nitems) { if ((p = fts_alloc(sp, *argv, strlen(*argv))) == NULL) goto mem3; p->fts_level = FTS_ROOTLEVEL; @@ -139,11 +128,10 @@ fts_open(char * const *argv, int options, } else { p->fts_link = NULL; if (root == NULL) - tmp = root = p; - else { - tmp->fts_link = p; - tmp = p; - } + root = p; + else + prev->fts_link = p; + prev = p; } } if (compar && nitems > 1) @@ -580,19 +568,20 @@ fts_sort(FTS *sp, FTSENT *head, int nitems) if (nitems > sp->fts_nitems) { struct _ftsent **a; - sp->fts_nitems = nitems + 40; if ((a = reallocarray(sp->fts_array, - sp->fts_nitems, sizeof(FTSENT *))) == NULL) { + nitems + 40, sizeof(FTSENT *))) == NULL) { free(sp->fts_array); sp->fts_array = NULL; sp->fts_nitems = 0; return (head); } + sp->fts_nitems = nitems + 40; sp->fts_array = a; } for (ap = sp->fts_array, p = head; p; p = p->fts_link) *ap++ = p; - qsort(sp->fts_array, nitems, sizeof(FTSENT *), sp->fts_compar); + qsort(sp->fts_array, nitems, sizeof(FTSENT *), + (qsort_compar_proto)sp->fts_compar); for (head = *(ap = sp->fts_array); --nitems; ++ap) ap[0]->fts_link = ap[1]; ap[0]->fts_link = NULL; @@ -655,13 +644,14 @@ fts_palloc(FTS *sp, size_t more) errno = ENAMETOOLONG; return (1); } - sp->fts_pathlen += more; - p = realloc(sp->fts_path, sp->fts_pathlen); + p = recallocarray(sp->fts_path, sp->fts_pathlen, + sp->fts_pathlen + more, 1); if (p == NULL) { free(sp->fts_path); sp->fts_path = NULL; return (1); } + sp->fts_pathlen += more; sp->fts_path = p; return (0); } @@ -704,5 +694,3 @@ fts_maxarglen(char * const *argv) max = len; return (max + 1); } - -#endif