fatbase

portable OpenBSD tools
git clone git://git.2f30.org/fatbase.git
Log | Files | Refs

commit 1b3d71c6e6cbd4cb308d0ce5f2b5452e1df78cf6
parent fb2850ba8cbf9831d6a14dbff9525f0bbda7726f
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Fri Nov 21 22:00:05 +0100

bc: improve build

Diffstat:
bc/Makefile | 39++++++++++++++++-----------------------
bc/TODO | 1+
bc/reallocarray.c | 38++++++++++++++++++++++++++++++++++++++
bc/scan.l | 2+-
bc/strlcat.c | 52++++++++++++++++++++++++++++++++++++++++++++++++++++
bc/strlcpy.c | 48++++++++++++++++++++++++++++++++++++++++++++++++
bc/util.h | 8++++++++
7 files changed, 164 insertions(+), 24 deletions(-)
diff --git a/bc/Makefile b/bc/Makefile @@ -1,25 +1,18 @@ -build: clean - ../yacc/yacc -d bc.y - ../lex/lex -oscan.c scan.l - cc -c reallocarray.c - cc -c y.tab.c scan.c tty.c - cc -o bc scan.o tty.o y.tab.o reallocarray.o -ledit -lcurses +OBJ = scan.o ytab.o tty.o reallocarray.o strlcpy.o strlcat.o +TARG = bc +LDLIBS = -ledit -lcurses -clean: - rm -f bc *.o y.tab.c y.tab.h scan.c +all: ytab.c scan.c $(TARG) + +ytab.c ytab.h: bc.y + $(YACC) -d bc.y + mv y.tab.c ytab.c + mv y.tab.h ytab.h + +scan.c: scan.l + $(LEX) -oscan.c scan.l -# $OpenBSD: Makefile,v 1.7 2013/09/19 16:12:00 otto Exp $ -#PROG= bc -#SRCS= bc.y scan.l tty.c -#CPPFLAGS+= -I. -I${.CURDIR} -#CFLAGS+= -Wall -Wno-unused -#YFLAGS+= -#LDADD+= -ledit -lcurses -#DPADD+= ${LIBEDIT} ${LIBCURSES} -# -# -#beforeinstall: -# install -c -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/bc.library \ -# ${DESTDIR}/usr/share/misc -# -#.include <bsd.prog.mk> +include ../std.mk + +clean: + rm -f $(TARG) $(OBJ) scan.c ytab.[ch] diff --git a/bc/TODO b/bc/TODO @@ -0,0 +1 @@ +rule to install bc.library diff --git a/bc/reallocarray.c b/bc/reallocarray.c @@ -0,0 +1,38 @@ +/* $OpenBSD: reallocarray.c,v 1.1 2014/05/08 21:43:49 deraadt Exp $ */ +/* + * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/types.h> +#include <errno.h> +#include <stdint.h> +#include <stdlib.h> + +/* + * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX + * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW + */ +#define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) + +void * +reallocarray(void *optr, size_t nmemb, size_t size) +{ + if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && + nmemb > 0 && SIZE_MAX / nmemb < size) { + errno = ENOMEM; + return NULL; + } + return realloc(optr, size * nmemb); +} diff --git a/bc/scan.l b/bc/scan.l @@ -25,7 +25,7 @@ #include "extern.h" #include "pathnames.h" -#include "y.tab.h" +#include "ytab.h" int lineno; bool interactive; diff --git a/bc/strlcat.c b/bc/strlcat.c @@ -0,0 +1,52 @@ +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <string.h> +#include <sys/types.h> + +#include "util.h" + +/* + * Appends src to string dst of size siz (unlike strncat, siz is the + * full size of dst, not space left). At most siz-1 characters + * will be copied. Always NUL terminates (unless siz <= strlen(dst)). + * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * If retval >= siz, truncation occurred. + */ +size_t +strlcat(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + size_t dlen; + /* Find the end of dst and adjust bytes left but don't go past end */ + while (n-- != 0 && *d != '\0') + d++; + dlen = d - dst; + n = siz - dlen; + if (n == 0) + return(dlen + strlen(s)); + while (*s != '\0') { + if (n != 1) { + *d++ = *s; + n--; + } + s++; + } + *d = '\0'; + return(dlen + (s - src)); /* count does not include NUL */ +} diff --git a/bc/strlcpy.c b/bc/strlcpy.c @@ -0,0 +1,48 @@ +/* + * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <string.h> +#include <sys/types.h> + +#include "util.h" + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + /* Copy as many bytes as will fit */ + if (n != 0) { + while (--n != 0) { + if ((*d++ = *s++) == '\0') + break; + } + } + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + return(s - src - 1); /* count does not include NUL */ +} diff --git a/bc/util.h b/bc/util.h @@ -0,0 +1,8 @@ +#include <sys/stat.h> + +#include <stdint.h> + +void *reallocarray(void *, size_t, size_t); + +size_t strlcat(char *, const char *, size_t); +size_t strlcpy(char *, const char *, size_t);