things necessary for Sun Solaris 9 @ sparc

diff --git a/ChangeLog b/ChangeLog
index 0a8030c..4bb7e71 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,8 +1,10 @@
 
-0.9.8:	2004-Oct-13
+0.9.8:	2004-Oct-20
 
 	* -X command line option added to asn1c to generate XML DTD.
 	* Empty SEQUENCE and SET clauses are now allowed.
+	* Code compiled and tested on Sun Solaris 9 @ sparc.
+	  Improved includes/defines of/for system headers.
 
 0.9.7.1:	2004-Oct-12
 
@@ -200,7 +202,7 @@
 	* Compiler: fixed recursive ASN.1 types inclusion (Severity: low,
 	  Security impact: none).
 	* Parser: IMPORTS/FROM fixes, now allowing multiple sections.
-	* Code compiled and checked on PowerPC (@MacOS X). No major portability
+	* Code compiled and tested on MacOS X (@ PowerPC). No major portability
 	  issues experienced.
 
 0.8.7:	2004-Apr-11	T-version-0-8-7
diff --git a/asn1c/check-parsing.sh b/asn1c/check-parsing.sh
index c5c2d6a..5480d0b 100755
--- a/asn1c/check-parsing.sh
+++ b/asn1c/check-parsing.sh
@@ -2,6 +2,8 @@
 
 tmpfile=".check-parsing.$$.tmp"
 
+diff -a . . 2>/dev/null && diffArgs="-a"
+
 ec=0
 
 for ref in ../tests/*.asn1.-*; do
@@ -10,7 +12,7 @@
 	echo "Checking $src against $ref"
 	./asn1c "-$flags" "$src" > "$tmpfile" || ec=$?
 	if [ $? = 0 ]; then
-		diff -a -u "$ref" "$tmpfile" || ec=$?
+		diff $diffArgs -u "$ref" "$tmpfile" || ec=$?
 	fi
 	if [ "$1" != "regenerate" ]; then
 		rm -f "$tmpfile"
diff --git a/asn1c/tests/Makefile.am b/asn1c/tests/Makefile.am
index 4a4b461..a6ab54a 100644
--- a/asn1c/tests/Makefile.am
+++ b/asn1c/tests/Makefile.am
@@ -3,7 +3,7 @@
 
 check_SCRIPTS = check-assembly.sh
 
-TESTS_ENVIRONMENT= CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" ./check-assembly.sh
+TESTS_ENVIRONMENT= CC="${CC}" CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" ./check-assembly.sh
 
 if CPLUSPLUS_FOUND
 TESTS = check-*.c check-*.cc	## Include C++ test filed (.cc)
diff --git a/asn1c/tests/Makefile.in b/asn1c/tests/Makefile.in
index 91ad426..80dcd2a 100644
--- a/asn1c/tests/Makefile.in
+++ b/asn1c/tests/Makefile.in
@@ -157,7 +157,7 @@
 target_vendor = @target_vendor@
 AM_CFLAGS = @ADD_CFLAGS@
 check_SCRIPTS = check-assembly.sh
-TESTS_ENVIRONMENT = CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" ./check-assembly.sh
+TESTS_ENVIRONMENT = CC="${CC}" CFLAGS="${CFLAGS}" CXXFLAGS="${CXXFLAGS}" ./check-assembly.sh
 @CPLUSPLUS_FOUND_FALSE@TESTS = check-*.c
 @CPLUSPLUS_FOUND_TRUE@TESTS = check-*.c check-*.cc	## Include C++ test filed (.cc)
 EXTRA_DIST = \
diff --git a/asn1c/tests/check-assembly.sh b/asn1c/tests/check-assembly.sh
index 43ce001..2edc8f4 100755
--- a/asn1c/tests/check-assembly.sh
+++ b/asn1c/tests/check-assembly.sh
@@ -11,10 +11,10 @@
 fi
 
 # Compute the .asn1 spec name by the given file name.
-source=$(echo "$1" | sed -e 's/.*\///')
+source=`echo "$1" | sed -e 's/.*\///'`
 testno=`echo "$source" | cut -f2 -d'-' | cut -f1 -d'.'`
 
-args=$(echo "$source" | sed -e 's/\.c[c]*$//')
+args=`echo "$source" | sed -e 's/\.c[c]*$//'`
 testdir=test-${args}
 
 OFS=$IFS
@@ -30,9 +30,10 @@
 	mkdir $testdir		|| exit $?
 fi
 cd $testdir			|| exit $?
-ln -fs ../$source		|| exit $?
+rm -f ./$source 2>/dev/null
+ln -fns ../$source		|| exit $?
 
-asn_module=$(echo ../../../tests/${testno}-*.asn1)
+asn_module=`echo ../../../tests/${testno}-*.asn1`
 
 # Create a Makefile for the project.
 cat > Makefile <<EOM
@@ -42,6 +43,8 @@
 CFLAGS=\${COMMON_FLAGS} ${CFLAGS}
 CXXFLAGS=\${COMMON_FLAGS} ${CXXFLAGS}
 
+CC=${CC}
+
 all: check-executable
 check-executable: compiled-module *.c*
 	@rm -f *.core
diff --git a/libasn1compiler/asn1c_out.c b/libasn1compiler/asn1c_out.c
index ebf2fb1..14b1f18 100644
--- a/libasn1compiler/asn1c_out.c
+++ b/libasn1compiler/asn1c_out.c
@@ -12,7 +12,6 @@
 	int lf_found;
 	va_list ap;
 	out_chunk_t *m;
-	char *buf;
 	int ret;
 
 	switch(arg->target->target) {
@@ -49,34 +48,29 @@
 		dst->indented = 0;
 
 	/*
-	 * Estimate necessary size.
-	 */
-	buf = "";
-	va_start(ap, fmt);
-	ret = vsnprintf(buf, 0, fmt, ap);
-	va_end(ap);
-	assert(ret >= 0);
-
-	/*
 	 * Allocate buffer.
 	 */
 	m = calloc(1, sizeof(out_chunk_t));
 	if(m == NULL) return -1;
-	m->len = ret + 1;
-	m->buf = malloc(ret + 1);
-	if(m->buf == NULL) {
-		free(m);
-		return -1;
-	}
 
-	/*
-	 * Fill the buffer.
-	 */
-	va_start(ap, fmt);
-	ret = vsnprintf(m->buf, m->len, fmt, ap);
-	assert(ret < m->len);
+	m->len = 16;
+	do {
+		void *tmp;
+		m->len <<= 2;
+		tmp = realloc(m->buf, m->len);
+		if(tmp) {
+			m->buf = (char *)tmp;
+		} else {
+			free(m->buf);
+			free(m);
+			return -1;
+		}
+		va_start(ap, fmt);
+		ret = vsnprintf(m->buf, m->len, fmt, ap);
+		va_end(ap);
+	} while(ret >= (m->len - 1) || ret < 0);
+
 	m->len = ret;
-	va_end(ap);
 
 	if(arg->target->target == OT_INCLUDES) {
 		out_chunk_t *v;
diff --git a/libasn1fix/asn1fix_misc.c b/libasn1fix/asn1fix_misc.c
index b049d1a..e9d156c 100644
--- a/libasn1fix/asn1fix_misc.c
+++ b/libasn1fix/asn1fix_misc.c
@@ -345,9 +345,11 @@
 
 int
 asn1f_check_known_external_type(const char *type_name) {
-	void *p = bsearch(&type_name, known_types, known_types_count,
-		sizeof(known_types[0]), _known_types_cmp);
-	if(p) return 0;
+	if(known_types_count) {
+		void *p = bsearch(&type_name, known_types, known_types_count,
+				sizeof(known_types[0]), _known_types_cmp);
+		if(p) return 0;
+	}
 	errno = ESRCH;
 	return -1;
 }
diff --git a/skeletons/GeneralizedTime.c b/skeletons/GeneralizedTime.c
index 631f3ed..9d5fe4a 100644
--- a/skeletons/GeneralizedTime.c
+++ b/skeletons/GeneralizedTime.c
@@ -2,6 +2,8 @@
  * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
  * Redistribution and modifications are permitted subject to BSD license.
  */
+#define	_POSIX_PTHREAD_SEMANTICS	/* for Sun */
+#define	_REENTRANT			/* for Sun */
 #include <asn_internal.h>
 #include <GeneralizedTime.h>
 #include <time.h>
@@ -34,10 +36,15 @@
 }
 
 #define	tzset()	_tzset()
+#define	putenv()	_putenv()
 #define	_EMULATE_TIMEGM
 
 #endif	/* WIN32 */
 
+#if	defined(sun)
+#define	_EMULATE_TIMEGM
+#endif
+
 /*
  * Where to look for offset from GMT, Phase I.
  * Several platforms are known.
@@ -87,7 +94,7 @@
 	char *buf;
 
 	tz = getenv("TZ");
-	_putenv("TZ=UTC");
+	putenv("TZ=UTC");
 	tzset();
 	tloc = mktime(tm);
 	if (tz) {
@@ -97,7 +104,7 @@
 	} else {
 		buf = "TZ=";
 	}
-	_putenv(buf);
+	putenv(buf);
 	tzset();
 	return tloc;
 }
diff --git a/skeletons/REAL.c b/skeletons/REAL.c
index 6d3e55e..d395c84 100644
--- a/skeletons/REAL.c
+++ b/skeletons/REAL.c
@@ -57,7 +57,7 @@
 		buf = "<NOT-A-NUMBER/>";
 		buflen = 15;
 		return (cb(buf, buflen, app_key) < 0) ? -1 : buflen;
-	} else if(isinf(d)) {
+	} else if(!finite(d)) {
 		if(copysign(1.0, d) < 0.0) {
 			buf = "<MINUS-INFINITY/>";
 			buflen = 17;
@@ -416,7 +416,7 @@
 			st->buf[0] = 0x42;	/* NaN */
 			st->buf[1] = 0;
 			st->size = 1;
-		} else if(isinf(dbl_value)) {
+		} else if(!finite(dbl_value)) {
 			if(copysign(1.0, dbl_value) < 0.0) {
 				st->buf[0] = 0x41;	/* MINUS-INFINITY */
 			} else {
diff --git a/skeletons/asn_types.h b/skeletons/asn_types.h
index 81a00c3..cf5c87e 100644
--- a/skeletons/asn_types.h
+++ b/skeletons/asn_types.h
@@ -12,7 +12,7 @@
 #include "config.h"
 #endif
 
-#include <stdio.h>	/* For fprintf() */
+#include <stdio.h>	/* For snprintf(3) */
 #include <stdlib.h>	/* For *alloc(3) */
 #include <string.h>	/* For memcpy(3) */
 #include <sys/types.h>	/* For size_t */
@@ -21,13 +21,23 @@
 
 #include <inttypes.h>	/* C99 specifies this file */
 
+#if	defined(sun)
+#include <ieeefp.h>	/* for finite(3) */
+#endif
+
 /*
- * Earlier FreeBSD version didn't have <stdint.h>,
+ * 1. Earlier FreeBSD version didn't have <stdint.h>,
  * but <inttypes.h> was present.
+ * 2. Sun Solaris requires <alloca.h> for alloca(3),
+ * but does not have <stdint.h>.
  */
-#if	!defined(__FreeBSD__) || !defined(_SYS_INTTYPES_H_) /* Workaround */
+#if	(!defined(__FreeBSD__) || !defined(_SYS_INTTYPES_H_))
+#if	defined(sun)
+#include <alloca.h>	/* For alloca(3) */
+#else
 #include <stdint.h>	/* SUSv2+ and C99 specify this file, for uintXX_t */
 #endif
+#endif
 
 #ifdef	WIN32
 #define	 snprintf	_snprintf
diff --git a/skeletons/tests/check-GeneralizedTime.c b/skeletons/tests/check-GeneralizedTime.c
index 6c35a1c..0cec17d 100644
--- a/skeletons/tests/check-GeneralizedTime.c
+++ b/skeletons/tests/check-GeneralizedTime.c
@@ -14,20 +14,24 @@
 	tloc = asn_GT2time(&gt, &tm, as_gmt);
 	printf("%s: [%s] -> %ld == %ld\n",
 		as_gmt?"GMT":"ofs", time_str, (long)tloc, (long)expect);
-	if(tloc != -1)
-	printf("\t%04d-%02d-%02dT%02d:%02d:%02d%+03ld%02ld\n",
+
+	if(tloc != -1) {
+		printf("\t%04d-%02d-%02dT%02d:%02d:%02d%+03ld%02ld\n",
 		tm.tm_year + 1900,
 		tm.tm_mon + 1,
 		tm.tm_mday,
 		tm.tm_hour,
 		tm.tm_min,
 		tm.tm_sec,
-		(tm.tm_gmtoff / 3600),
-		labs(tm.tm_gmtoff % 3600)
-	);
+		(GMTOFF(tm) / 3600),
+		labs(GMTOFF(tm) % 3600)
+		);
+	}
 	assert(tloc == expect);
 
-	assert(tloc == -1 || as_gmt == 0 || tm.tm_gmtoff == 0);
+#ifdef	HAVE_TM_GMTOFF
+	assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0);
+#endif
 
 	if(!as_gmt) check(time_str, expect, 1);
 }
diff --git a/skeletons/tests/check-UTCTime.c b/skeletons/tests/check-UTCTime.c
index d626359..6762865 100644
--- a/skeletons/tests/check-UTCTime.c
+++ b/skeletons/tests/check-UTCTime.c
@@ -1,7 +1,7 @@
 #define	__NO_ASN_TABLE__
-#include <UTCTime.c>
-#define	__NO_ASSERT_H__
 #include <GeneralizedTime.c>
+#define	__NO_ASN_TABLE__
+#include <UTCTime.c>
 #include <constraints.c>
 
 static void
@@ -23,11 +23,11 @@
 		tm.tm_hour,
 		tm.tm_min,
 		tm.tm_sec,
-		tm.tm_gmtoff
+		GMTOFF(tm)
 	);
 	assert(tloc == sample);
 
-	assert(tloc == -1 || as_gmt == 0 || tm.tm_gmtoff == 0);
+	assert(tloc == -1 || as_gmt == 0 || GMTOFF(tm) == 0);
 
 	if(as_gmt) check(time_str, sample, as_gmt);
 }