Compare commits

..

24 Commits

Author SHA1 Message Date
Landon Curt Noll
769ac51f8c Release v2.14.0.10 2021-12-01 23:38:56 -08:00
Landon Curt Noll
cdda633369 Update CHANGES file to note previous 2 commits 2021-12-01 23:34:40 -08:00
Landon Curt Noll
50f349f4d7 Update copyright dates to account for 2021 mods 2021-12-01 23:28:59 -08:00
Landon Curt Noll
ef00e00328 Remove unnecessary spaces inbetween or next to tabs 2021-12-01 23:25:49 -08:00
Landon Curt Noll
bb041098bc Reduce the length of long lines 2021-12-01 23:15:06 -08:00
Landon Curt Noll
f794b8d859 Reduce global status constants
The constants:

	9, 9/10, 10/9, 24, 360, 400

are used by func.c only, so there were moved
from {q,z}math.{c,h} to func.c.

The constants:

	3, 4

are used by zrandom.c only, so there were moved
from {q,z}math.{c,h} to zrandom.c.
2021-12-01 22:50:52 -08:00
Landon Curt Noll
bd990cef1f Added freeglobals() to libcalc_call_me_last() 2021-12-01 21:56:01 -08:00
Landon Curt Noll
febb9d96b3 Convert freeh() call to zfree() call 2021-12-01 21:55:18 -08:00
Landon Curt Noll
13ae9b804e Add missing blank lines 2021-12-01 21:53:46 -08:00
Landon Curt Noll
56cc4897d6 Reapply memory leak fixes to zmod.c
Revert previously applied zmod.c mods and
then apply better mods to fix zmod memory leaks.
2021-12-01 21:48:13 -08:00
Landon Curt Noll
eb940e0a21 Fix paranoia guard in literal string alloc 2021-11-30 03:44:38 -08:00
Landon Curt Noll
f3adb35e36 Add string and symbol guard allocation paranoia 2021-11-30 03:40:40 -08:00
Landon Curt Noll
d1d365d7ba Added initialization paranoia 2021-11-30 03:37:20 -08:00
Landon Curt Noll
af214b166d Reorder zfree ops to assign NULL to ptr after free 2021-11-30 03:36:22 -08:00
Landon Curt Noll
51b933dfff Add initialization paranoia 2021-11-30 03:35:09 -08:00
Landon Curt Noll
c4e5007587 Clarify strlcpy size check 2021-11-30 03:33:48 -08:00
Landon Curt Noll
c838798f04 Fix more memory leaks, improve zfree() action 2021-11-29 01:55:53 -08:00
Landon Curt Noll
23d49a41fe Remove references to externals that are no longer used 2021-11-28 20:52:03 -08:00
Landon Curt Noll
3d300acca1 Improve internal pre-defined constant handling
Improved the way that internal pre-defined constants are managed.
Removed unused internal pre-defined constants.  Added code to
prevent an internal pre-defined constant (that was never
allocated) from being freed.
2021-11-28 20:47:52 -08:00
Landon Curt Noll
8f449ba6d2 Revert local test mistake in Makefile.local 2021-11-27 00:23:54 -08:00
Landon Curt Noll
f4d754b47d Checkpoint in valgrind work 2021-11-27 00:05:28 -08:00
Landon Curt Noll
fde724aa2d Release v2.14.0.9
The following are the changes in this release:

    Due to issues with clang and Apple Silicon, ARCH_CFLAGS is now,
    by default, empty:

	ARCH_CFLAGS=

    If you want to use, say, -march=native, then either change
    the Makefile or make with:

	make all ARCH_CFLAGS='-march=native'

    Added arch and hardware as GNU Makefile computed values.
    As with ${target}, ${arch} and ${hardware} is computed by uname:

	target: uname -a
	arch: uname -p
	hardware: uname -m

    Fixed compiling calc on Apple Silicon with homebrew.  On Apple
    Silicon, HomeBrew installs on into a different location.  The
    Makefile checks if ${hardware} is arm64 and adjusts the location
    of libraries such as readline and history accordingly.
2021-11-24 01:35:01 -08:00
Landon Curt Noll
e411a3e6bf Trim long lines in Makefile 2021-11-24 01:33:00 -08:00
Landon Curt Noll
7db81649b0 Fixed compiling for Apple Silicon arm64 w/HomeBrew 2021-11-24 01:25:49 -08:00
40 changed files with 421 additions and 226 deletions

44
CHANGES
View File

@@ -1,4 +1,42 @@
The following are the changes from calc version 2.14.0.7 to date:
The following are the changes from calc version 2.14.0.9 to date:
Due to issues with clang and Apple Silicon, ARCH_CFLAGS is now,
by default, empty:
ARCH_CFLAGS=
If you want to use, say, -march=native, then either change
the Makefile or make with:
make all ARCH_CFLAGS='-march=native'
Added arch and hardware as GNU Makefile computed values.
As with ${target}, ${arch} and ${hardware} is computed by uname:
target: uname -a
arch: uname -p
hardware: uname -m
Fixed compiling calc on Apple Silicon with homebrew. On Apple
Silicon, HomeBrew installs on into a different location. The
Makefile checks if ${hardware} is arm64 and adjusts the location
of libraries such as readline and history accordingly.
Pluged a number of memory leaks.
Fixed a few cases where v_subtype was not properly initialuzed.
Improved the way that internal pre-defined constants are managed.
Removed unused internal pre-defined constants. Added code to
prevent an internal pre-defined constant (that was never allocated)
from being freed.
Remove unnecessary spaces inbetween or next to tabs.
Update copright dates to account for 2021 modifications.
The following are the changes from calc version 2.14.0.7 to 2.14.0.8:
Fizzbin is a better word. :-)
@@ -1539,7 +1577,7 @@ The following are the changes from calc version 2.12.1.10 to 2.12.2:
NOTE: If your target is not supported below and the default target
is not suitable for your needs, please send to the:
calc-contrib at asthe dot com
calc-contrib at asthe dot com
Email address an "ifeq ($(target),YOUR_TARGET_NAME)"
... "endif" set of lines from the Makefile so that
@@ -2438,7 +2476,7 @@ The following are the changes from calc version 2.11.8.0 to 2.11.8.1:
and oldstd -n & newstd classic cfg default
--------------------------------------------------------
epsilon 1e-20 1e-10 1e-20 1e-20
quo 2 2 2 2
quo 2 2 2 2
outround 2 24 2 24
leadzero 0 1 0 1
fullzero 0 1 0 0

View File

@@ -87,7 +87,7 @@ Installing calc from the bzip2-ed tarball in 4 easy steps:
BINDIR Where to install calc binary files.
LIBDIR Where to install calc link library (*.a) files.
CALC_SHAREDIR Where to install calc help, .cal, startup, and
config files.
config files.
You may want to change the default installation locations for
these values, which are based on the 4 values listed above:
@@ -108,13 +108,13 @@ Installing calc from the bzip2-ed tarball in 4 easy steps:
For example, if:
BINDIR= /usr/bin
BINDIR= /usr/bin
LIBDIR= /usr/lib
CALC_SHAREDIR= /usr/share/calc
and if:
T= /var/tmp/testing
T= /var/tmp/testing
Then the installation locations will be:

View File

@@ -216,7 +216,7 @@ Your program must handle parse/scan errors in one of two ways:
/* report the parse/scan */
if (calc_use_scanerr_jmpbuf == 0) {
printf("parse error: %s\n", calc_err_msg);
}
}
/* initialize calc after the longjmp */
initialize();
@@ -622,7 +622,7 @@ call. This is not required, but is does bring things to a closure.
The function libcalc_call_me_last() takes no args and returns void. You
need call libcalc_call_me_last() only once.
## Copyright (C) 1999 David I. Bell and Landon Curt Noll
## Copyright (C) 1999,2021 David I. Bell and Landon Curt Noll
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -46,6 +46,12 @@
ifeq ($(target),)
target=$(shell uname -s 2>/dev/null)
endif
ifeq ($(arch),)
arch=$(shell uname -p 2>/dev/null)
endif
ifeq ($(hardware),)
hardware=$(shell uname -m 2>/dev/null)
endif
# The shell used by this Makefile
#
@@ -135,15 +141,31 @@ USE_READLINE= -DUSE_READLINE
#READLINE_LIB= -L/usr/gnu/lib -lreadline -lhistory -lncurses
#READLINE_LIB= -L${PREFIX}/lib -lreadline -lhistory -lncurses
ifeq ($(target),Darwin)
# homebrew installs readline & history libs in ${PREFIX}/opt/readline/lib
ifeq ($(hardware),arm64)
# Darwin arm64 homebrew installs readline & history
# libs in /opt/homebrew/opt/readline/lib
READLINE_LIB= -L/opt/homebrew/opt/readline/lib -lreadline -lhistory -lncurses
else
# Assume Darwin non-arm64 is x86_64
# Darwin x86_64 homebrew installs readline & history
# libs in ${PREFIX}/opt/readline/lib
READLINE_LIB= -L${PREFIX}/opt/readline/lib -lreadline -lhistory -lncurses
endif
else
READLINE_LIB= -lreadline -lhistory -lncurses
endif
#
ifeq ($(target),Darwin)
# homebrew installs readline & history *.h under ${PREFIX}/opt/readline/include
ifeq ($(hardware),arm64)
# Darwin arm64 homebrew installs readline & history *.h
# under /opt/homebrew/opt/readline/include
READLINE_INCLUDE= -I${PREFIX}/opt/homebrew/opt/readline/include
else
# Assume Darwin non-arm64 is x86_64
# Darwin x86_64 homebrew installs readline & history *.h
# under ${PREFIX}/opt/readline/include
READLINE_INCLUDE= -I${PREFIX}/opt/readline/include
endif
else
READLINE_INCLUDE=
endif
@@ -361,7 +383,7 @@ update_version:
fi; \
${RM} -f "$$i.tmp"; \
${SED} -e 's/^VERSION=.*/VERSION= '"$$CALC_VERSION"'/' \
$$i > $$i.tmp; \
$$i > $$i.tmp; \
if ${CMP} -s "$$i" "$$i.tmp"; then \
${RM} -f "$$i.tmp"; \
echo "versions already up to date in $$i"; \

View File

@@ -79,6 +79,12 @@ SHELL= /bin/sh
ifeq ($(target),)
target=$(shell uname -s 2>/dev/null)
endif
ifeq ($(arch),)
arch=$(shell uname -p 2>/dev/null)
endif
ifeq ($(hardware),)
hardware=$(shell uname -m 2>/dev/null)
endif
#endif /* end of skip for non-Gnu makefiles */
##############################################################################
@@ -748,9 +754,9 @@ endif
# LIBDIR= ${PREFIX}/lib
# CALC_SHAREDIR= ${PREFIX}/share/calc
#
# NOTE: Starting with macOS El Capitan OS X 10.11, root by default
# could not mkdir under system locations, so macOS must now
# use the ${PREFIX} tree.
# NOTE: Starting with macOS El Capitan OS X 10.11, root by default
# could not mkdir under system locations, so macOS must now
# use the ${PREFIX} tree.
#if 0 /* start of skip for non-Gnu makefiles */
ifeq ($(target),Darwin)
@@ -949,12 +955,12 @@ CATEXT= 1
# If NROFF is non-empty, then
#
# ${NROFF} ${NROFF_ARG} calc.1 > ${CATDIR}/calc.${CATEXT}
# is used to build and install the cat page
# is used to build and install the cat page
#
# else (NROFF is empty)
#
# ${MANMAKE} calc.1 ${CATDIR}
# is used to build and install the cat page
# is used to build and install the cat page
# else
#
# The cat page is not built or installed
@@ -1205,7 +1211,7 @@ EXT=
# The default calc versions
#
VERSION= 2.14.0.8
VERSION= 2.14.0.10
# Names of shared libraries with versions
#
@@ -1280,8 +1286,8 @@ EXTRA_LDFLAGS=
# The ARCH_CFLAGS are ${CC} when compiling C files. They follow
# CCMISC and precede EXTRA_CFLAGS.
#
ARCH_CFLAGS= -march=native
#ARCH_CFLAGS=
ARCH_CFLAGS=
#ARCH_CFLAGS= -march=native
# COMMON_CFLAGS are the common ${CC} flags used for all programs, both
# intermediate and final calc and calc related programs
@@ -5064,7 +5070,7 @@ endif
fi
-${Q} if [ -f calc-static${EXT} ]; then \
if ${CMP} -s calc-static${EXT} \
${T}${BINDIR}/calc-static${EXT}; then \
${T}${BINDIR}/calc-static${EXT}; then \
${TRUE}; \
else \
${RM} -f ${T}${BINDIR}/calc-static.new${EXT}; \
@@ -5117,23 +5123,23 @@ endif
else \
${RM} -f ${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}.new; \
${CP} -f libcalc${LIB_EXT_VERSION} \
${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}.new; \
${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}.new; \
${CHMOD} 0644 ${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}.new; \
${MV} -f ${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}.new \
${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}; \
${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}; \
echo "installed ${T}${LIBDIR}/libcalc${LIB_EXT_VERSION}"; \
${LN} -f -s libcalc${LIB_EXT_VERSION} \
${T}${LIBDIR}/libcalc${LIB_EXT}; \
${T}${LIBDIR}/libcalc${LIB_EXT}; \
echo "installed ${T}${LIBDIR}/libcalc${LIB_EXT}"; \
${RM} -f ${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}.new; \
${CP} -f custom/libcustcalc${LIB_EXT_VERSION} \
${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}.new; \
${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}.new; \
${CHMOD} 0644 ${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}.new; \
${MV} -f ${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}.new \
${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}; \
${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}; \
echo "installed ${T}${LIBDIR}/libcustcalc${LIB_EXT_VERSION}"; \
${LN} -f -s libcustcalc${LIB_EXT_VERSION} \
${T}${LIBDIR}/libcustcalc${LIB_EXT}; \
${T}${LIBDIR}/libcustcalc${LIB_EXT}; \
echo "installed ${T}${LIBDIR}/libcalc${LIB_EXT}"; \
if [ -z "${T}" -o "/" = "${T}" ]; then \
if [ ! -z "${LDCONFIG}" ]; then \
@@ -5396,7 +5402,7 @@ calc-symlink:
; do \
if [ -e "${T}$$i" ]; then \
if [ ! -L "$$i" -a "${T}$$i" -ef "$$i" ]; then \
echo "ERROR: ${T}$$i is the same as $$i" 1>&2; \
echo "ERROR: ${T}$$i is the same as $$i" 1>&2; \
else \
if [ -e "$$i" ]; then \
echo ${RM} -f "$$i"; \
@@ -5404,7 +5410,7 @@ calc-symlink:
fi; \
echo ${LN} -s "${T}$$i" "$$i"; \
${LN} -s "${T}$$i" "$$i"; \
fi; \
fi; \
else \
echo "Warning: not found: ${T}$$i" 1>&2; \
fi; \

View File

@@ -461,7 +461,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CALC_SHAREDIR}/$$i" ]; then \
${RM} -f "${T}${CALC_SHAREDIR}/$$i"; \
${RM} -f "${T}${CALC_SHAREDIR}/$$i"; \
if [ -f "${T}${CALC_SHAREDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CALC_SHAREDIR}/$$i"; \
else \

View File

@@ -497,7 +497,7 @@ define best_mul2()
low = mid;
high = mid;
if (config("user_debug") > 0) {
printf("\twe are close enough to unity ratio at: %d\n", mid);
printf("\twe are close enough to unity ratio at: %d\n", mid);
}
break;
}
@@ -505,7 +505,7 @@ define best_mul2()
/* bump lower range up if we went over */
if (ratio > 1.0) {
if (config("user_debug") > 2) {
printf("\tmove low from %d up to %d\n",
printf("\tmove low from %d up to %d\n",
low, mid);
}
low = mid;
@@ -513,7 +513,7 @@ define best_mul2()
/* drop higher range down if we went under */
} else {
if (config("user_debug") > 2) {
printf("\tmove high from %d down to %d\n",
printf("\tmove high from %d down to %d\n",
high, mid);
}
high = mid;
@@ -522,7 +522,7 @@ define best_mul2()
/* report on test loop progress */
if (config("user_debug") > 1) {
printf("\tsetting low: %d high: %d diff: %d\n",
low, high, high-low);
low, high, high-low);
}
}
@@ -884,7 +884,7 @@ define best_sq2()
high *= expand;
if (config("user_debug") > 1) {
printf(" expand the next test range by a factor of %d\n",
expand);
expand);
}
/*
@@ -979,7 +979,7 @@ define best_sq2()
low = mid;
high = mid;
if (config("user_debug") > 0) {
printf("\twe are close enough to unity ratio at: %d\n", mid);
printf("\twe are close enough to unity ratio at: %d\n", mid);
}
break;
}
@@ -987,7 +987,7 @@ define best_sq2()
/* bump lower range up if we went over */
if (ratio > 1.0) {
if (config("user_debug") > 2) {
printf("\tmove low from %d up to %d\n",
printf("\tmove low from %d up to %d\n",
low, mid);
}
low = mid;
@@ -995,7 +995,7 @@ define best_sq2()
/* drop higher range down if we went under */
} else {
if (config("user_debug") > 2) {
printf("\tmove high from %d down to %d\n",
printf("\tmove high from %d down to %d\n",
high, mid);
}
high = mid;
@@ -1004,7 +1004,7 @@ define best_sq2()
/* report on test loop progress */
if (config("user_debug") > 1) {
printf("\tsetting low: %d high: %d diff: %d\n",
low, high, high-low);
low, high, high-low);
}
}
@@ -1385,7 +1385,7 @@ define best_pow2()
printf(" pmod alg1/alg2 ratio = %.3f\n", ratio);
if (ratio > 1.0 && ratio <= 1.02) {
printf(" while alg1 is slightly better than alg2, "
"it is not clearly better\n");
"it is not clearly better\n");
}
}
} while (ratio <= 1.02);
@@ -1420,7 +1420,7 @@ define best_pow2()
high *= expand;
if (config("user_debug") > 1) {
printf(" expand the next test range by a factor of %d\n",
expand);
expand);
}
/*
@@ -1478,7 +1478,7 @@ define best_pow2()
low = mid;
high = mid;
if (config("user_debug") > 0) {
printf("\twe are close enough to unity ratio at: %d\n", mid);
printf("\twe are close enough to unity ratio at: %d\n", mid);
}
break;
}
@@ -1486,7 +1486,7 @@ define best_pow2()
/* bump lower range up if we went over */
if (ratio > 1.0) {
if (config("user_debug") > 2) {
printf("\tmove low from %d up to %d\n",
printf("\tmove low from %d up to %d\n",
low, mid);
}
low = mid;
@@ -1494,7 +1494,7 @@ define best_pow2()
/* drop higher range down if we went under */
} else {
if (config("user_debug") > 2) {
printf("\tmove high from %d down to %d\n",
printf("\tmove high from %d down to %d\n",
high, mid);
}
high = mid;
@@ -1503,7 +1503,7 @@ define best_pow2()
/* report on test loop progress */
if (config("user_debug") > 1) {
printf("\tsetting low: %d high: %d diff: %d\n",
low, high, high-low);
low, high, high-low);
}
}

20
calc.c
View File

@@ -277,14 +277,14 @@ main(int argc, char **argv)
if (nextcp(&cp, &index, argc, argv,
FALSE)) {
fprintf(stderr,
"-D expects argument\n");
"-D expects argument\n");
exit(5);
}
havearg = TRUE;
if (*cp != ':') {
if (*cp < '0' || *cp > '9') {
fprintf(stderr,
"-D expects"
"-D expects"
" integer\n");
exit(6);
}
@@ -296,13 +296,13 @@ main(int argc, char **argv)
if (*cp != '\0' &&
*cp != ' ' && *cp != ':') {
fprintf(stderr,
"Bad syntax im -D"
"Bad syntax im -D"
" arg\n");
exit(7);
}
if (*cp != ':') {
if (nextcp(&cp, &index,
argc, argv,
argc, argv,
FALSE)
|| *cp != ':')
break;
@@ -318,7 +318,7 @@ main(int argc, char **argv)
if (*cp != ':') {
if (*cp < '0' || *cp > '9') {
fprintf(stderr,
"-D : expects"
"-D : expects"
" integer\n");
exit(9);
}
@@ -330,13 +330,13 @@ main(int argc, char **argv)
if (*cp != '\0' &&
*cp != ' ' && *cp != ':') {
fprintf(stderr,
"Bad syntax im -D"
"Bad syntax im -D"
" : arg\n");
exit(10);
}
if (*cp != ':') {
if (nextcp(&cp, &index,
argc, argv,
argc, argv,
FALSE)
|| *cp != ':') {
break;
@@ -346,12 +346,12 @@ main(int argc, char **argv)
if (nextcp(&cp, &index, argc, argv,
FALSE)) {
fprintf(stderr, "-D : : expects"
" argument\n");
" argument\n");
exit(11);
}
if (*cp < '0' || *cp > '9') {
fprintf(stderr, "-D :: expects"
" integer\n");
" integer\n");
exit(12);
}
user_debug = cp;
@@ -398,7 +398,7 @@ main(int argc, char **argv)
if (nextcp(&cp, &index, argc,
argv, haveendstr)) {
fprintf(stderr, "-f -once"
" expects"
" expects"
" filename\n");
exit(16);
}

View File

@@ -497,7 +497,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${SCRIPTDIR}/$$i" ]; then \
${RM} -f "${T}${SCRIPTDIR}/$$i"; \
${RM} -f "${T}${SCRIPTDIR}/$$i"; \
if [ -f "${T}${SCRIPTDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${SCRIPTDIR}/$$i"; \
else \

View File

@@ -496,7 +496,7 @@ EXT=
# The default calc versions
#
VERSION= 2.14.0.8
VERSION= 2.14.0.10
# Names of shared libraries with versions
#
@@ -1708,7 +1708,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CUSTOMHELPDIR}/$$i" ]; then \
${RM} -f "${T}${CUSTOMHELPDIR}/$$i"; \
${RM} -f "${T}${CUSTOMHELPDIR}/$$i"; \
if [ -f "${T}${CUSTOMHELPDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CUSTOMHELPDIR}/$$i"; \
else \
@@ -1721,7 +1721,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CUSTOMCALDIR}/$$i" ]; then \
${RM} -f "${T}${CUSTOMCALDIR}/$$i"; \
${RM} -f "${T}${CUSTOMCALDIR}/$$i"; \
if [ -f "${T}${CUSTOMCALDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CUSTOMCALDIR}/$$i"; \
else \
@@ -1734,7 +1734,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CUSTOMINCDIR}/$$i" ]; then \
${RM} -f "${T}${CUSTOMINCDIR}/$$i"; \
${RM} -f "${T}${CUSTOMINCDIR}/$$i"; \
if [ -f "${T}${CUSTOMINCDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CUSTOMINCDIR}/$$i"; \
else \

View File

@@ -496,7 +496,7 @@ EXT=
# The default calc versions
#
VERSION= 2.14.0.8
VERSION= 2.14.0.10
# Names of shared libraries with versions
#

View File

@@ -498,7 +498,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CUSTOMHELPDIR}/$$i" ]; then \
${RM} -f "${T}${CUSTOMHELPDIR}/$$i"; \
${RM} -f "${T}${CUSTOMHELPDIR}/$$i"; \
if [ -f "${T}${CUSTOMHELPDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CUSTOMHELPDIR}/$$i"; \
else \
@@ -511,7 +511,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CUSTOMCALDIR}/$$i" ]; then \
${RM} -f "${T}${CUSTOMCALDIR}/$$i"; \
${RM} -f "${T}${CUSTOMCALDIR}/$$i"; \
if [ -f "${T}${CUSTOMCALDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CUSTOMCALDIR}/$$i"; \
else \
@@ -524,7 +524,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${CUSTOMINCDIR}/$$i" ]; then \
${RM} -f "${T}${CUSTOMINCDIR}/$$i"; \
${RM} -f "${T}${CUSTOMINCDIR}/$$i"; \
if [ -f "${T}${CUSTOMINCDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${CUSTOMINCDIR}/$$i"; \
else \

2
file.c
View File

@@ -926,7 +926,7 @@ printid(FILEID id, int flags)
if (get_open_pos(fp, &pos) < 0) {
if (fileno(fp) > 2)
math_str("Error while determining file position!");
math_str("Error while determining file position!");
math_chr(')');
return 0;
}

27
func.c
View File

@@ -154,6 +154,25 @@ STATIC int env_pool_max = 0; /* number of env_pool elements allocated */
STATIC struct env_pool *e_pool = NULL; /* env_pool elements */
/*
* constants used for hours or degrees conversion functions
*/
STATIC HALF _nineval_[] = { 9 };
STATIC HALF _twentyfourval_[] = { 24 };
STATIC HALF _threesixtyval_[] = { 360 };
STATIC HALF _fourhundredval_[] = { 400 };
STATIC NUMBER _qtendivnine_ = { { _tenval_, 1, 0 },
{ _nineval_, 1, 0 }, 1, NULL };
STATIC NUMBER _qninedivten_ = { { _nineval_, 1, 0 },
{ _tenval_, 1, 0 }, 1, NULL };
STATIC NUMBER _qtwentyfour = { { _twentyfourval_, 1, 0 },
{ _oneval_, 1, 0 }, 1, NULL };
STATIC NUMBER _qthreesixty = { { _threesixtyval_, 1, 0 },
{ _oneval_, 1, 0 }, 1, NULL };
STATIC NUMBER _qfourhundred = { { _fourhundredval_, 1, 0 },
{ _oneval_, 1, 0 }, 1, NULL };
/*
* user-defined error strings
*/
@@ -1106,6 +1125,9 @@ f_randombit(int count, NUMBER **vals)
long cnt; /* bits needed or skipped */
/* parse args */
ztmp.len = 0; /* paranoia */
ztmp.v = NULL;
ztmp.sign = 0;
if (count == 0) {
zrandom(1, &ztmp);
ans = ziszero(ztmp) ? qlink(&_qzero_) : qlink(&_qone_);
@@ -1298,6 +1320,7 @@ f_digit(int count, VALUE **vals)
base = _ten_;
}
res.v_type = V_NUM;
res.v_subtype = V_NOSUBTYPE;
res.v_num = qdigit(vals[0]->v_num, vals[1]->v_num->num, base);
if (res.v_num == NULL)
return error_value(E_DGT3);
@@ -1323,6 +1346,7 @@ f_digits(int count, VALUE **vals)
base = _ten_;
}
res.v_type = V_NUM;
res.v_subtype = V_NOSUBTYPE;
res.v_num = itoq(qdigits(vals[0]->v_num, base));
return res;
}
@@ -1346,6 +1370,7 @@ f_places(int count, VALUE **vals)
places = qdecplaces(vals[0]->v_num);
res.v_type = V_NUM;
res.v_subtype = V_NOSUBTYPE;
res.v_num = itoq(places);
return res;
}
@@ -3606,6 +3631,7 @@ f_comb(VALUE *v1, VALUE *v2)
copyvalue(v1, &result);
decvalue(v1, &tmp1);
div.v_type = V_NUM;
div.v_subtype = V_NOSUBTYPE;
div.v_num = qlink(&_qtwo_);
n--;
for (;;) {
@@ -9039,6 +9065,7 @@ f_sleep(int count, VALUE **vals)
NUMBER *q1, *q2;
res.v_type = V_NULL;
res.v_subtype = V_NOSUBTYPE;
#if !defined(_WIN32)
if (count > 0) {
if (vals[0]->v_type != V_NUM || qisneg(vals[0]->v_num))

View File

@@ -894,7 +894,7 @@ uninstall:
continue; \
fi; \
if [ -f "${T}${HELPDIR}/$$i" ]; then \
${RM} -f "${T}${HELPDIR}/$$i"; \
${RM} -f "${T}${HELPDIR}/$$i"; \
if [ -f "${T}${HELPDIR}/$$i" ]; then \
echo "cannot uninstall ${T}${HELPDIR}/$$i"; \
else \

View File

@@ -75,7 +75,7 @@ DESCRIPTION
jacobi(x,y) == 0 if x <= 0
jacobi(x,y) == 0 if y <= 0
jacobi(x,y) == 0 if y is even
jacobi(x,y) == 0 if y is even
jacobi(x,b) == 0 if gcd(x,b) > 1
@@ -105,7 +105,7 @@ DESCRIPTION
It is also worth noting that:
jacobi(x,y) == 0 if gcd(y,x) != 1
jacobi(x,y) == 0 if gcd(y,x) != 1
jacobi(y,y) == 0 if y > 1
Based on the generalization of the quadratic reciprocity theorem,
@@ -125,8 +125,8 @@ DESCRIPTION
When b is odd:
jacobi(x^2,b) == 1 if gcd(x,b) == 1
jacobi(x,b^2) == 1 if gcd(x,b) == 1
jacobi(x^2,b) == 1 if gcd(x,b) == 1
jacobi(x,b^2) == 1 if gcd(x,b) == 1
jacobi(x0,b) == jacobi(x1,b) if x0 == x1 % b
@@ -162,7 +162,7 @@ DESCRIPTION
j = 1;
while (x != 0) {
while (iseven(x)) {
x = x / 2;
x = x / 2;
t = b % 8;
if (t == 3 || t == 5) {
j = -j;
@@ -170,7 +170,7 @@ DESCRIPTION
}
swap(x,b);
if (((x % 4) == 3) && ((b % 4) == 3)) {
j = -j;
j = -j;
}
x = x % b;
}

View File

@@ -163,7 +163,7 @@ DESCRIPTION
printf(" [%d,%d]: %e", i, j, A[i,j]);
if (i != matmax(A,1))
printf("\n");
}
}
}
So that when one defines a 2D matrix such as:
@@ -414,7 +414,7 @@ SEE ALSO
det, inverse, isident, test, config, search, rsearch, reverse, copy,
blkcpy, dp, cp, randperm, sort
## Copyright (C) 1999-2006,2018 Landon Curt Noll
## Copyright (C) 1999-2006,2018,2021 Landon Curt Noll
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -135,7 +135,7 @@ Using objects
xx_print 1 print value, default prints elements
xx_one 1 multiplicative identity, default is 1
xx_test 1 logical test (false,true => 0,1),
default tests elements
default tests elements
xx_add 2
xx_sub 2
xx_neg 1 negative
@@ -146,10 +146,10 @@ Using objects
xx_norm 1 square of absolute value
xx_conj 1 conjugate
xx_pow 2 integer power, default does multiply,
square, inverse
square, inverse
xx_sgn 1 sign of value (-1, 0, 1)
xx_cmp 2 equality (equal,nonequal => 0,1),
default tests elements
default tests elements
xx_rel 2 relative order, positive for >, etc.
xx_quo 3 integer quotient
xx_mod 3 remainder of division
@@ -196,7 +196,7 @@ Using objects
test2300.cal
test3100.cal
## Copyright (C) 1999,2010 Landon Curt Noll
## Copyright (C) 1999,2010,2021 Landon Curt Noll
##
## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -29,13 +29,13 @@ Statements
This works as expected:
if (expr) {
if (expr) {
...
}
However this WILL NOT WORK AS EXPECTED:
if (expr)
if (expr)
{
...
}
@@ -43,7 +43,7 @@ Statements
because calc will parse the if being terminated by
an empty statement followed by a
if (expr) ;
if (expr) ;
{
...
}
@@ -294,7 +294,7 @@ Statements
help command top level commands
help expression calc expression syntax
help builtin calc builtin functions
help usage how to invoke the calc command and calc -options
help usage how to invoke the calc command and calc -options
You may obtain help on individual builtin functions. For example:

View File

@@ -636,6 +636,11 @@ libcalc_call_me_last(void)
}
}
/*
* free all globals
*/
freeglobals();
/*
* all done
*/

View File

@@ -161,7 +161,7 @@ removelistfirst(LIST *lp, VALUE *vp)
}
*vp = lp->l_first->e_value;
lp->l_first->e_value.v_type = V_NULL;
lp->l_first->e_value.v_type = V_NOSUBTYPE;
lp->l_first->e_value.v_subtype = V_NOSUBTYPE;
removelistelement(lp, lp->l_first);
}

View File

@@ -927,6 +927,7 @@ matindices(MATRIX *mp, long index)
lp = listalloc();
val.v_type = V_NUM;
val.v_subtype = V_NOSUBTYPE;
j = mp->m_dim;
while (--j >= 0) {
@@ -1068,6 +1069,7 @@ matident(MATRIX *m)
for (row = 0; row < rows; row++) {
for (col = 0; col < rows; col++) {
val->v_type = V_NUM;
val->v_subtype = V_NOSUBTYPE;
val->v_num = ((row == col) ? qlink(&_qone_) :
qlink(&_qzero_));
val++;
@@ -1126,6 +1128,7 @@ matinv(MATRIX *m)
else
val->v_num = qlink(&_qzero_);
val->v_type = V_NUM;
val->v_subtype = V_NOSUBTYPE;
val++;
}
}

View File

@@ -446,7 +446,7 @@ qisqrt(NUMBER *q)
}
zquo(q->num, q->den, &tmp, 0);
(void) zsqrt(tmp, &r->num,0);
freeh(tmp.v);
zfree(tmp);
return r;
}
@@ -683,6 +683,7 @@ qdigit(NUMBER *q, ZVALUE dpos, ZVALUE base)
return qlink(&_qzero_);
k = zfacrem(q->num, base, &N);
if (k == 0) {
zfree(N);
k = zgcdrem(q->den, base, &D);
if (k > 0) {
zequo(q->den, D, &A);

17
qmath.c
View File

@@ -37,26 +37,15 @@
NUMBER _qzero_ = { { _zeroval_, 1, 0 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qone_ = { { _oneval_, 1, 0 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qtwo_ = { { _twoval_, 1, 0 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qthree_ = { { _threeval_, 1, 0 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qfour_ = { { _fourval_, 1, 0 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qten_ = { { _tenval_, 1, 0 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qnegone_ = { { _oneval_, 1, 1 }, { _oneval_, 1, 0 }, 1, NULL };
NUMBER _qonehalf_ = { { _oneval_, 1, 0 }, { _twoval_, 1, 0 }, 1, NULL };
NUMBER _qneghalf_ = { { _oneval_, 1, 1 }, { _twoval_, 1, 0 }, 1, NULL };
NUMBER _qonesqbase_ = { { _oneval_, 1, 0 }, { _sqbaseval_, 2, 0 }, 1, NULL };
NUMBER _qtendivnine_ = { { _tenval_, 1, 0 }, { _nineval_, 1, 0 }, 1, NULL };
NUMBER _qninedivten_ = { { _nineval_, 1, 0 }, { _tenval_, 1, 0 }, 1, NULL };
NUMBER _qthreesixty = { { _threesixtyval_, 1, 0 },
{ _oneval_, 1, 0 }, 1, NULL };
NUMBER _qfourhundred = { { _fourhundredval_, 1, 0 },
{ _oneval_, 1, 0 }, 1, NULL };
NUMBER _qtwentyfour = { { _twentyfourval_, 1, 0 },
{ _oneval_, 1, 0 }, 1, NULL };
NUMBER * initnumbs[] = {&_qzero_, &_qone_, &_qtwo_, &_qthree_,
&_qfour_, &_qten_, &_qnegone_, &_qonehalf_, &_qneghalf_,
&_qonesqbase_, &_qtendivnine_, &_qninedivten_,
&_qthreesixty, &_qfourhundred, &_qtwentyfour,
NUMBER * initnumbs[] = {&_qzero_, &_qone_, &_qtwo_,
&_qten_, &_qnegone_, &_qonehalf_, &_qneghalf_,
&_qonesqbase_,
NULL /* must be last */
};

View File

@@ -272,9 +272,7 @@ static inline NUMBER* qlink(NUMBER* q) { if(q) { (q)->links++; } return q; }
* constants used often by the arithmetic routines
*/
EXTERN NUMBER _qzero_, _qone_, _qnegone_, _qonehalf_, _qneghalf_, _qonesqbase_;
EXTERN NUMBER _qtwo_, _qthree_, _qfour_, _qten_;
EXTERN NUMBER _qtendivnine_, _qninedivten_;
EXTERN NUMBER _qthreesixty, _qfourhundred, _qtwentyfour;
EXTERN NUMBER _qtwo_, _qten_;
EXTERN NUMBER * initnumbs[];

View File

@@ -1129,6 +1129,7 @@ qln(NUMBER *q, NUMBER *epsilon)
sum.sign = neg;
if (k + n >= m) {
zshift(sum, n - m, &qtmp->num);
zfree(sum);
} else {
if (k) {
zshift(sum, -k, &qtmp->num);

View File

@@ -96,16 +96,16 @@ S_FUNC QCKHASH blk_hash(BLOCK *blk, QCKHASH val);
* the next 32 bit QCKHASH
*
* Example:
* QCKHASH val;
* int x;
* QCKHASH val;
* int x;
*
* quasi_fnv(x, val);
* quasi_fnv(x, val);
*
* NOTE: The (x) argument may be an expression such as something with
* a ++ or --. The macro must only use (x) once.
* a ++ or --. The macro must only use (x) once.
*
* NOTE: The (val) argument just be a lvalue / something to which
* a value can be assigned.
* a value can be assigned.
*
* The careful observer will note that (x) need not be a simple
* octet. This is not a bug, but a feature. The FNV hash was
@@ -366,7 +366,7 @@ randhash(RAND *r, QCKHASH val)
return V_RAND+val;
} else {
/* hash control values */
val += V_RAND;
val += V_RAND;
quasi_fnv(r->j, val);
quasi_fnv(r->k, val);
quasi_fnv(r->bits, val);

4
rpm.mk
View File

@@ -49,7 +49,7 @@
# %_gpgbin /usr/bin/gpg
# IMPORTANT NOTE: Unless the package redhat-rpm-config is installed,
# the calc-debuginfo rpm will not be created.
# the calc-debuginfo rpm will not be created.
#
# IMPORTANT NOTE: These packages are important for general
# rpm processing:
@@ -266,7 +266,7 @@ chkpkg:
${V} echo '=-=-=-=-= rpm.mk start of $@ rule =-=-=-=-='
for i in "$(RPM_TOP)/RPMS/$(TARCH)/$(RPMx86_64)" \
"$(RPM_TOP)/RPMS/$(TARCH)/$(DRPMx86_64)" \
"$(RPM_TOP)/SRPMS/$(SRPM)" ; do \
"$(RPM_TOP)/SRPMS/$(SRPM)" ; do \
echo "***** start $$i" ; \
${RPM_TOOL} -qpi "$$"i ; \
echo "***** files $$i" ; \

View File

@@ -103,7 +103,7 @@ main(int argc, char **argv)
/*
* seed the generator
*/
prev_state = zsrandom2(seed, zconst[1]);
prev_state = zsrandom2(seed, _one_);
if (prev_state == NULL) {
math_error("previous random state is NULL");
/*NOTREACHED*/

View File

@@ -93,7 +93,7 @@ main(int argc, char **argv)
/*
* seed the generator
*/
prev_state = zsrandom2(seed, zconst[10]);
prev_state = zsrandom2(seed, _ten_);
if (prev_state == NULL) {
math_error("previous random state is NULL");
/*NOTREACHED*/

1
seed.c
View File

@@ -373,6 +373,7 @@ pseudo_seed(void)
* We do care (that much) if these calls fail. We do not
* need to process any data in the 'sdata' structure.
*/
memset(&sdata, 0, sizeof(sdata));
#if defined(HAVE_GETTIME)
# if defined(CLOCK_SGI_CYCLE)
(void) clock_gettime(CLOCK_SGI_CYCLE, &sdata.sgi_cycle);

79
str.c
View File

@@ -43,6 +43,7 @@
#define STR_TABLECHUNK 100 /* how often to reallocate string table */
#define STR_CHUNK (1<<11) /* size of string storage allocation */
#define OCTET_VALUES 256 /* number of different values in a OCTET */
#define STR_UNIQUE (1<<7) /* size of string to allocate separately */
STRING _nullstring_ = {"", 0, 1, NULL};
@@ -68,12 +69,14 @@ void
initstr(STRINGHEAD *hp)
{
if (hp->h_list == NULL) {
hp->h_list = (char *)malloc(2000);
/* alloc + 1 guard paranoia */
hp->h_list = (char *)malloc(STR_CHUNK + 1);
if (hp->h_list == NULL) {
math_error("Cannot allocate string header");
/*NOTREACHED*/
}
hp->h_avail = 2000;
hp->h_list[STR_CHUNK] = '\0'; /* guard paranoia */
hp->h_avail = STR_CHUNK;
hp->h_used = 0;
}
hp->h_avail += hp->h_used;
@@ -105,11 +108,14 @@ addstr(STRINGHEAD *hp, char *str)
if ((str == NULL) || (*str == '\0'))
return NULL;
len = strlen(str) + 1;
if (hp->h_avail <= len) {
newsize = len + 2000 + hp->h_used + hp->h_avail;
list = (char *)realloc(hp->h_list, newsize);
if (len >= hp->h_avail) {
/* alloc + 1 guard paranoia */
newsize = len + STR_CHUNK + hp->h_used + hp->h_avail + 1;
/* alloc + 1 guard paranoia */
list = (char *)realloc(hp->h_list, newsize + 1);
if (list == NULL)
return NULL;
list[newsize] = '\0'; /* guard paranoia */
hp->h_list = list;
hp->h_avail = newsize - hp->h_used;
}
@@ -134,16 +140,19 @@ charstr(int ch)
int i;
if (chartable == NULL) {
cp = (char *)malloc(512);
/* alloc + 1 guard paranoia */
cp = (char *)malloc((OCTET_VALUES + 1)*2);
if (cp == NULL) {
math_error("Cannot allocate character table");
/*NOTREACHED*/
}
for (i = 0; i < 256; i++) {
for (i = 0; i < OCTET_VALUES; i++) {
*cp++ = (char)i;
*cp++ = '\0';
}
chartable = cp - 512;
chartable = cp - (OCTET_VALUES*2);
*cp++ = '\0'; /* guard paranoia */
*cp++ = '\0'; /* guard paranoia */
}
return &chartable[(ch & 0xff) * 2];
}
@@ -274,11 +283,16 @@ addliteral(char *str)
*/
if (literals.l_count >= literals.l_maxcount) {
count = literals.l_maxcount + STR_TABLECHUNK;
if (literals.l_maxcount)
table = (char **) realloc(literals.l_table, count *
sizeof(char *));
else
table = (char **) malloc(count * sizeof(char *));
if (literals.l_maxcount) {
/* alloc + 1 guard paranoia */
table = (char **) realloc(literals.l_table,
(count + 1) * sizeof(char *));
table[count] = NULL; /* guard paranoia */
} else {
/* alloc + 1 guard paranoia */
table = (char **) malloc((count + 1) * sizeof(char *));
table[count] = NULL; /* guard paranoia */
}
if (table == NULL) {
math_error("Cannot allocate string literal table");
/*NOTREACHED*/
@@ -296,11 +310,13 @@ addliteral(char *str)
*/
len = ROUNDUP(len+1, FULL_LEN);
if (len >= STR_UNIQUE) {
newstr = (char *)malloc(len);
/* alloc + 1 guard paranoia */
newstr = (char *)malloc(len + 1);
if (newstr == NULL) {
math_error("Cannot allocate large literal string");
/*NOTREACHED*/
}
newstr[len] = '\0'; /* guard paranoia */
strlcpy(newstr, str, len);
table[literals.l_count++] = newstr;
return newstr;
@@ -310,11 +326,13 @@ addliteral(char *str)
* then allocate a new one.
*/
if (literals.l_avail < len) {
newstr = (char *)malloc(STR_CHUNK);
/* alloc + 1 guard paranoia */
newstr = (char *)malloc(STR_CHUNK + 1);
if (newstr == NULL) {
math_error("Cannot allocate new literal string");
/*NOTREACHED*/
}
newstr[STR_CHUNK] = '\0'; /* guard paranoia */
literals.l_alloc = newstr;
literals.l_avail = STR_CHUNK;
}
@@ -1106,11 +1124,14 @@ stralloc(void)
STRING **newfn;
if (freeStr == NULL) {
freeStr = (STRING *) malloc(sizeof (STRING) * STRALLOC);
/* alloc + 1 guard paranoia */
freeStr = (STRING *) malloc(sizeof (STRING) * (STRALLOC + 1));
if (freeStr == NULL) {
math_error("Unable to allocate memory for stralloc");
/*NOTREACHED*/
}
/* guard paranoia */
memset(freeStr+STRALLOC, 0, sizeof(STRING));
freeStr[STRALLOC - 1].s_next = NULL;
freeStr[STRALLOC - 1].s_links = 0;
@@ -1132,10 +1153,16 @@ stralloc(void)
blockcount++;
if (firstStrs == NULL) {
newfn = (STRING **) malloc( blockcount * sizeof(STRING *));
} else {
/* alloc + 1 guard paranoia */
newfn = (STRING **)
realloc(firstStrs, blockcount * sizeof(STRING *));
malloc((blockcount + 1) * sizeof(STRING *));
newfn[blockcount] = NULL; /* guard paranoia */
} else {
/* alloc + 1 guard paranoia */
newfn = (STRING **)
realloc(firstStrs,
(blockcount + 1) * sizeof(STRING *));
newfn[blockcount] = NULL; /* guard paranoia */
}
if (newfn == NULL) {
math_error("Cannot allocate new string block");
@@ -1274,11 +1301,14 @@ STATIC STRING **stringconsttable;
void
initstrings(void)
{
stringconsttable = (STRING **) malloc(sizeof(STRING *) * STRCONSTALLOC);
/* alloc + 1 guard paranoia */
stringconsttable = (STRING **)
malloc(sizeof(STRING *) * (STRCONSTALLOC + 1));
if (stringconsttable == NULL) {
math_error("Unable to allocate constant table");
/*NOTREACHED*/
}
stringconsttable[STRCONSTALLOC] = NULL; /* guard paranoia */
stringconsttable[0] = &_nullstring_;
stringconstcount = 1;
stringconstavail = STRCONSTALLOC - 1;
@@ -1305,13 +1335,18 @@ addstring(char *str, size_t len)
if (stringconsttable == NULL) {
initstrings();
} else {
sp = (STRING **) realloc((char *) stringconsttable,
sizeof(STRING *) * (stringconstcount + STRCONSTALLOC));
/* alloc + 1 guard paranoia */
sp = (STRING **)
realloc((char *) stringconsttable,
sizeof(STRING *) *
(stringconstcount + STRCONSTALLOC + 1));
if (sp == NULL) {
math_error("Unable to reallocate string "
"const table");
/*NOTREACHED*/
}
/* guard paranoia */
sp[stringconstcount + STRCONSTALLOC] = NULL;
stringconsttable = sp;
stringconstavail = STRCONSTALLOC;
}

2
strl.c
View File

@@ -93,7 +93,7 @@ strlcpy(char * dst, const char * src, size_t dstsize)
/*
* perform the size limited copy and NUL terminate
*/
if (srclen > dstsize-1) {
if (srclen+1 > dstsize) {
memcpy(dst, src, dstsize-1);
dst[dstsize-1] = '\0';
#if defined(STRL_TEST)

View File

@@ -110,7 +110,8 @@ addglobal(char *name, BOOL isstatic)
return NULL;
hp = &globalhash[HASHSYM(name, len)];
for (sp = *hp; sp; sp = sp->g_next) {
if ((sp->g_len == len) && (strcmp(sp->g_name, name) == 0)
if ((sp->g_len == len) &&
(strncmp(sp->g_name, name, len+1) == 0)
&& (sp->g_filescope == newfilescope)
&& (sp->g_funcscope == newfuncscope))
return sp;
@@ -145,8 +146,9 @@ findglobal(char *name)
bestsp = NULL;
len = strlen(name);
for (sp = globalhash[HASHSYM(name, len)]; sp; sp = sp->g_next) {
if ((sp->g_len == len) && !strcmp(sp->g_name, name)) {
for (sp = globalhash[HASHSYM(name, len)]; sp != NULL; sp = sp->g_next) {
if ((sp->g_len == len) &&
(strncmp(sp->g_name, name, len+1) == 0)) {
if ((bestsp == NULL) ||
(sp->g_filescope > bestsp->g_filescope) ||
(sp->g_funcscope > bestsp->g_funcscope))
@@ -232,6 +234,7 @@ showallglobals(void)
printf("No global variables\n");
}
S_FUNC void
printtype(VALUE *vp)
{
@@ -360,6 +363,7 @@ writeglobals(char *name)
return 0;
}
/*
* Free all non-null global and visible static variables
*/

View File

@@ -54,7 +54,7 @@ static char *program;
#define MAJOR_VER 2 /* major library version */
#define MINOR_VER 14 /* minor library version */
#define MAJOR_PATCH 0 /* major software version level */
#define MINOR_PATCH 8 /* minor software version level */
#define MINOR_PATCH 10 /* minor software version level */
/*

20
zfunc.c
View File

@@ -1439,6 +1439,8 @@ zlog10(ZVALUE z, BOOL *was_10_power)
if (was_10_power != NULL) {
*was_10_power = TRUE;
}
zfree(pow10);
zfree(temp);
return power;
/* ignore this entry if we went too large */
@@ -1452,6 +1454,7 @@ zlog10(ZVALUE z, BOOL *was_10_power)
pow10 = temp;
}
}
zfree(pow10);
return power;
}
@@ -1616,7 +1619,7 @@ zfacrem(ZVALUE z1, ZVALUE z2, ZVALUE *rem)
long
zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
{
ZVALUE tmp1, tmp2;
ZVALUE tmp1, tmp2, tmp3, tmp4;
long count, onecount;
long sh;
@@ -1647,8 +1650,10 @@ zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
}
zgcd(z1, z2, &tmp1);
if (zisunit(tmp1) || ziszero(tmp1))
if (zisunit(tmp1) || ziszero(tmp1)) {
zfree(tmp1);
return 0;
}
zequo(z1, tmp1, &tmp2);
count = 1;
z1 = tmp2;
@@ -1658,16 +1663,19 @@ zgcdrem(ZVALUE z1, ZVALUE z2, ZVALUE *res)
* the gcd becomes one.
*/
while (!zisunit(z2)) {
onecount = zfacrem(z1, z2, &tmp1);
onecount = zfacrem(z1, z2, &tmp3);
if (onecount) {
count += onecount;
zfree(z1);
z1 = tmp1;
z1 = tmp3;
} else {
zfree(tmp3);
}
zgcd(z1, z2, &tmp1);
zgcd(z1, z2, &tmp4);
zfree(z2);
z2 = tmp1;
z2 = tmp4;
}
zfree(z2);
*res = z1;
return count;
}

104
zmath.c
View File

@@ -35,54 +35,27 @@
HALF _zeroval_[] = { 0 };
HALF _oneval_[] = { 1 };
HALF _twoval_[] = { 2 };
HALF _threeval_[] = { 3 };
HALF _fourval_[] = { 4 };
HALF _fiveval_[] = { 5 };
HALF _sixval_[] = { 6 };
HALF _sevenval_[] = { 7 };
HALF _eightval_[] = { 8 };
HALF _nineval_[] = { 9 };
HALF _tenval_[] = { 10 };
HALF _elevenval_[] = { 11 };
HALF _twelveval_[] = { 12 };
HALF _thirteenval_[] = { 13 };
HALF _fourteenval_[] = { 14 };
HALF _fifteenval_[] = { 15 };
HALF _sixteenval_[] = { 16 };
HALF _seventeenval_[] = { 17 };
HALF _eightteenval_[] = { 18 };
HALF _nineteenval_[] = { 19 };
HALF _twentyval_[] = { 20 };
HALF _sqbaseval_[] = { 0, 1 };
HALF _pow4baseval_[] = { 0, 0, 1 };
HALF _pow8baseval_[] = { 0, 0, 0, 0, 1 };
HALF _threesixtyval_[] = { 360 };
HALF _fourhundredval_[] = { 400 };
HALF _twentyfourval_[] = { 24 };
ZVALUE zconst[] = {
{ _zeroval_, 1, 0}, { _oneval_, 1, 0}, { _twoval_, 1, 0},
{ _threeval_, 1, 0}, { _fourval_, 1, 0}, { _fiveval_, 1, 0},
{ _sixval_, 1, 0}, { _sevenval_, 1, 0}, { _eightval_, 1, 0},
{ _nineval_, 1, 0}, { _tenval_, 1, 0}, { _elevenval_, 1, 0},
{ _twelveval_, 1, 0}, { _thirteenval_, 1, 0}, { _fourteenval_, 1, 0},
{ _fifteenval_, 1, 0}, { _sixteenval_, 1, 0}, { _seventeenval_, 1, 0},
{ _eightteenval_, 1, 0}, { _nineteenval_, 1, 0}, { _twentyval_, 1, 0},
{ _threesixtyval_, 1, 0}, { _fourhundredval_, 1, 0},
{ _twentyfourval_, 1, 0}
};
ZVALUE _zero_ = { _zeroval_, 1, 0};
HALF _oneval_[] = { 1 };
ZVALUE _one_ = { _oneval_, 1, 0 };
ZVALUE _two_ = { _twoval_, 1, 0 };
ZVALUE _ten_ = { _tenval_, 1, 0 };
ZVALUE _sqbase_ = { _sqbaseval_, 2, 0 };
ZVALUE _pow4base_ = { _pow4baseval_, 4, 0 };
ZVALUE _pow8base_ = { _pow8baseval_, 4, 0 };
ZVALUE _neg_one_ = { _oneval_, 1, 1 };
HALF _twoval_[] = { 2 };
ZVALUE _two_ = { _twoval_, 1, 0 };
HALF _tenval_[] = { 10 };
ZVALUE _ten_ = { _tenval_, 1, 0 };
HALF _sqbaseval_[] = { 0, 1 };
ZVALUE _sqbase_ = { _sqbaseval_, 2, 0 };
HALF _pow4baseval_[] = { 0, 0, 1 };
ZVALUE _pow4base_ = { _pow4baseval_, 4, 0 };
HALF _pow8baseval_[] = { 0, 0, 0, 0, 1 };
ZVALUE _pow8base_ = { _pow8baseval_, 4, 0 };
/*
* 2^64 as a ZVALUE
*/
@@ -96,6 +69,20 @@ ZVALUE _b64_ = { _pow8baseval_, 5, 0 };
-=@=- BASEB not 16 or 32 -=@=-
#endif
/*
* ZVALUE - values that should not be freed
*/
HALF *half_tbl[] = {
_zeroval_,
_oneval_,
_twoval_,
_tenval_,
_sqbaseval_,
_pow4baseval_,
_pow8baseval_,
NULL /* must be last */
};
/*
* highhalf[i] - masks off the upper i bits of a HALF
@@ -260,11 +247,38 @@ alloc(LEN len)
}
/*
* is_const - determine if a HALF array is an pre-allocated array
*
* given:
* h pointer to the beginning of the HALF array
*
* returns:
* TRUE - h is found in the half_tbl array
* FALSE - is is not found in the half_tbl array
*/
int
is_const(HALF* h)
{
HALF **h_p; /* half_tbl array pointer */
/* search the half_tbl for h */
for (h_p = &half_tbl[0]; *h_p != NULL; ++h_p) {
if (h == *h_p) {
return TRUE; /* found in the half_tbl array */
}
}
/* not found in the half_tbl array */
return FALSE;
}
#ifdef ALLOCTEST
void
freeh(HALF *h)
{
if ((h != _zeroval_) && (h != _oneval_)) {
if (!is_const(h)) {
free(h);
++nfree;
}

46
zmath.h
View File

@@ -55,8 +55,7 @@
#ifndef ALLOCTEST
# define freeh(p) { if (((void *)p != (void *)_zeroval_) && \
((void *)p != (void *)_oneval_)) free((void *)p); }
# define freeh(p) { if (!is_const(p)) { free((void *)(p)); } }
#endif
@@ -289,6 +288,7 @@ typedef struct {
* Function prototypes for integer math routines.
*/
E_FUNC HALF * alloc(LEN len);
E_FUNC int is_const(HALF* h);
#ifdef ALLOCTEST
E_FUNC void freeh(HALF *);
#endif
@@ -579,7 +579,12 @@ E_FUNC void zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res);
#define zcopyval(z1,z2) memcpy((z2).v, (z1).v, (z1).len * sizeof(HALF))
#define zquicktrim(z) {if (((z).len > 1) && ((z).v[(z).len-1] == 0)) \
(z).len--;}
#define zfree(z) freeh((z).v)
#define zfree(z) {if ((z).len != 0 && (z).v != NULL) { \
freeh((z).v); \
(z).v = NULL; \
(z).len = 0; \
(z).sign = 0; } \
}
/*
@@ -642,20 +647,35 @@ E_FUNC ZVALUE *swap_HALF_in_ZVALUE(ZVALUE *dest, ZVALUE *src, BOOL all);
/*
* constants used often by the arithmetic routines
*/
EXTERN HALF _zeroval_[], _oneval_[], _twoval_[], _threeval_[], _fourval_[];
EXTERN HALF _fiveval_[], _sixval_[], _sevenval_[], _eightval_[], _nineval_[];
EXTERN HALF _tenval_[], _elevenval_[], _twelveval_[], _thirteenval_[];
EXTERN HALF _fourteenval_[], _fifteenval_[];
EXTERN HALF _zeroval_[];
EXTERN ZVALUE _zero_;
EXTERN HALF _oneval_[];
EXTERN ZVALUE _one_;
EXTERN ZVALUE _neg_one_;
EXTERN HALF _twoval_[];
EXTERN ZVALUE _two_;
EXTERN HALF _tenval_[];
EXTERN ZVALUE _ten_;
EXTERN HALF _sqbaseval_[];
EXTERN HALF _fourthbaseval_[];
EXTERN HALF _threesixtyval_[], _fourhundredval_[], _twentyfourval_[];
EXTERN ZVALUE _sqbase_;
EXTERN ZVALUE zconst[]; /* ZVALUE integers from 0 thru 15 */
EXTERN HALF _pow4baseval_[];
EXTERN ZVALUE _pow4base_;
EXTERN ZVALUE _zero_, _one_, _two_, _ten_, _neg_one_;
EXTERN ZVALUE _sqbase_, _pow4base_, _pow8base_;
EXTERN HALF _pow8baseval_[];
EXTERN ZVALUE _pow8base_;
EXTERN ZVALUE _b32_, _b64_;
/* _b32_ is _sqbaseval_ or _pow4baseval_ depending on BASEB */
EXTERN ZVALUE _b32_;
/* _b64_ is _pow4baseval_ or _pow8baseval_ depending on BASEB */
EXTERN ZVALUE _b64_;
EXTERN HALF *half_tbl[]; /* preset HALF constants, NULL termated list */
EXTERN BOOL _math_abort_; /* nonzero to abort calculations */
EXTERN ZVALUE _tenpowers_[]; /* table of 10^2^n */

33
zmod.c
View File

@@ -493,6 +493,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
HALF curhalf; /* current word of power */
unsigned int curpow; /* current low power */
unsigned int curbit; /* current bit of low power */
BOOL free_z1; /* TRUE => need to free z1 */
int i;
if (zisneg(z3) || ziszero(z3)) {
@@ -535,17 +536,22 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
* within the modulo range. Then check for zero or one specially.
*/
ztmp.len = 0;
free_z1 = FALSE;
if (zisneg(z1) || zrel(z1, z3) >= 0) {
zmod(z1, z3, &ztmp, 0);
zfree(z1);
z1 = ztmp;
free_z1 = TRUE;
}
if (ziszero(z1)) {
zfree(z1);
if (ztmp.len)
zfree(ztmp);
*res = _zero_;
return;
}
if (zisone(z1)) {
zfree(z1);
if (ztmp.len)
zfree(ztmp);
*res = _one_;
@@ -598,10 +604,12 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
* future use.
*/
if (pp->v == NULL) {
if (curpow & 0x1)
if (curpow & 0x1) {
zcopy(z1, &modpow);
else
free_z1 = FALSE;
} else {
modpow = _one_;
}
for (curbit = 0x2;
curbit <= curpow;
@@ -667,8 +675,7 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
}
for (pp = &lowpowers[2]; pp <= &lowpowers[POWNUMS-1]; pp++) {
if (pp->v != NULL)
freeh(pp->v);
zfree(*pp);
}
*res = ans;
if (ztmp.len)
@@ -689,6 +696,9 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
powermodredc = zredcalloc(z3);
rp = powermodredc;
zredcencode(rp, z1, &temp);
if (free_z1 == TRUE) {
zfree(z1);
}
zredcpower(rp, temp, z2, &z1);
zfree(temp);
zredcdecode(rp, z1, res);
@@ -727,10 +737,12 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
* calculate it and remember it in the table for future use.
*/
if (pp->v == NULL) {
if (curpow & 0x1)
if (curpow & 0x1) {
zcopy(z1, &modpow);
else
free_z1 = FALSE;
} else {
modpow = _one_;
}
for (curbit = 0x2; curbit <= curpow; curbit *= 2) {
pp = &lowpowers[curbit];
@@ -789,12 +801,14 @@ zpowermod(ZVALUE z1, ZVALUE z2, ZVALUE z3, ZVALUE *res)
}
for (pp = &lowpowers[2]; pp <= &lowpowers[POWNUMS-1]; pp++) {
if (pp->v != NULL)
freeh(pp->v);
zfree(*pp);
}
*res = ans;
if (ztmp.len)
zfree(ztmp);
if (free_z1 == TRUE) {
zfree(z1);
}
}
/*
@@ -1836,8 +1850,7 @@ zredcpower(REDC *rp, ZVALUE z1, ZVALUE z2, ZVALUE *res)
}
for (pp = lowpowers; pp < &lowpowers[POWNUMS]; pp++) {
if (pp->len)
freeh(pp->v);
zfree(*pp);
}
if (sign && !ziszero(ans)) {
zsub(rp->mod, ans, res);

View File

@@ -1092,6 +1092,15 @@
STATIC RANDOM blum;
/*
* static constants 3 and 4 used by zsrandom4
*/
STATIC HALF _threeval_[] = { 3 };
STATIC ZVALUE _three_ = { _threeval_, 1, 0 };
STATIC HALF _fourval_[] = { 4 };
STATIC ZVALUE _four_ = { _fourval_, 1, 0 };
/*
* Default Blum generator
*
@@ -2296,19 +2305,19 @@ zsrandom1(CONST ZVALUE seed, BOOL need_ret)
last_r.v = NULL;
do {
/* free temp storage */
if (last_r.v != NULL) {
zfree_random(last_r);
}
zfree_random(last_r);
/*
* last_r = r;
* r = pmod(last_r, 2, n);
*/
last_r = r;
zcopy(r, &last_r);
zfree_random(r);
zsquaremod(last_r, blum.n, &r);
} while (zrel(r, last_r) > 0);
zfree_random(blum.r);
blum.r = r;
/* free temp storage */
zfree_random(last_r);
@@ -2532,11 +2541,11 @@ zsrandom4(CONST ZVALUE seed, CONST ZVALUE ip, CONST ZVALUE iq, long trials)
/*
* search the 'p' and 'q' Blum prime (3 mod 4) candidates
*/
if (!znextcand(ip, trials, _zero_, zconst[3], zconst[4], &p)) {
if (!znextcand(ip, trials, _zero_, _three_, _four_, &p)) {
math_error("failed to find 1st Blum prime");
/*NOTREACHED*/
}
if (!znextcand(iq, trials, _zero_, zconst[3], zconst[4], &q)) {
if (!znextcand(iq, trials, _zero_, _three_, _four_, &q)) {
math_error("failed to find 2nd Blum prime");
/*NOTREACHED*/
}
@@ -2663,6 +2672,9 @@ zrandomskip(long cnt)
free(p_blum);
}
loglogn = (long)blum.loglogn;
new_r.len = 0; /* paranoia */
new_r.v = NULL;
new_r.sign = 0;
/*
* skip required bits in the buffer
@@ -2769,6 +2781,9 @@ zrandom(long cnt, ZVALUE *res)
}
loglogn = blum.loglogn;
mask = blum.mask;
new_r.len = 0; /* paranoia */
new_r.v = NULL;
new_r.sign = 0;
/*
* allocate storage
@@ -3062,21 +3077,16 @@ randomcopy(CONST RANDOM *state)
void
randomfree(RANDOM *state)
{
/* avoid free of the pre-defined states */
if (state == &init_blum) {
return;
}
if (state >= &random_pregen[0] &&
state <= &random_pregen[BLUM_PREGEN-1]) {
return;
}
/* free the values */
zfree_random(state->n);
zfree_random(state->r);
/* free it if it is not pre-defined */
state->seeded = 0;
state->bits = 0; /* paranoia */
state->buffer = 0;
/* free it if it is not pre-defined */
if (state != &blum) {
free(state);
}