DASH Shell discussions
 help / color / mirror / Atom feed
* Portability patches
@ 2011-01-19  5:07 Brian Koropoff
  2011-01-19  5:15 ` [PATCH 1/3] Port to Solaris Brian Koropoff
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Brian Koropoff @ 2011-01-19  5:07 UTC (permalink / raw)
  To: dash

Hi,

I've been working on a new build system (yeah, I know, *another* one)
that aims to be highly portable to various POSIX systems and light on
dependencies (in the spirit of autotools, though substantially less
insane).  Pure POSIX sh turned out to be robust enough for my project,
but the various UNIX systems I've ported it to lack anything with dash's
raw speed.  Naturally, I just ported dash to them as well.

Patch stack incoming.  I expect some of these might need further
explanation or revision before being accepted.

Regards,
Brian Koropoff


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH 1/3] Port to Solaris
  2011-01-19  5:07 Portability patches Brian Koropoff
@ 2011-01-19  5:15 ` Brian Koropoff
  2011-01-21 13:06   ` Jilles Tjoelker
  2011-01-19  5:15 ` [PATCH 2/3] Port to AIX Brian Koropoff
  2011-01-19  5:16 ` [PATCH 3/3] Port to HP-UX Brian Koropoff
  2 siblings, 1 reply; 13+ messages in thread
From: Brian Koropoff @ 2011-01-19  5:15 UTC (permalink / raw)
  To: dash

- Solaris lacks paths.h and the various _PATH_* #defines.
  Check for them in configure.ac and fall back on the
  usual suspects when they are missing.

- Older Solaris lacks isblank(), and versions that have it
  use a macro.  Check for the declaration in configure.ac
  and fall back on a naive version when missing.

- Older Solaris does not support %jd (intmax_t) in format
  strings, but it does support %lld (long long), which is
  the same size on all architectures it supports.  Do
  a configure check for the sizes of both and prefer %lld
  when it is safe to do so.

- Older Solaris lacks stdint.h, but inttypes.h provides the
  same types and works on all platforms I've tried dash on,
  so just use it instead.

- Older Solaris doesn't like it when vsnprintf() is passed
  a NULL buffer (in violation of the POSIX spec, of course).
  Pass a 1-byte dummy buffer instead.

- Solaris lacks tempfile and mktemp programs.  Fall back on a
  "good-enough" custom function in mkbuiltins.

Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>
---
 configure.ac     |   36 +++++++++++++++++++++++++++++++++++-
 src/arith_yacc.c |    1 -
 src/bltin/test.c |    2 +-
 src/cd.c         |    4 ++++
 src/exec.c       |    5 +++++
 src/expand.c     |    4 ++--
 src/expand.h     |    2 +-
 src/histedit.c   |    5 +++++
 src/jobs.c       |    5 +++++
 src/jobs.h       |    2 +-
 src/miscbltin.c  |    4 ++--
 src/mkbuiltins   |   15 +++++++++++++++
 src/mystring.c   |    2 +-
 src/mystring.h   |    2 +-
 src/output.c     |   14 ++++++++++++++
 src/system.c     |    8 ++++++++
 src/system.h     |    5 +++++
 src/var.c        |    7 ++++++-
 src/var.h        |    2 +-
 19 files changed, 112 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index c943725..0a60055 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,7 +43,41 @@ AC_ARG_ENABLE(glob, AS_HELP_STRING(--enable-glob, [Use glob(3) from libc]))
 dnl Checks for libraries.
 
 dnl Checks for header files.
-AC_CHECK_HEADERS(alloca.h)
+AC_CHECK_HEADERS(alloca.h paths.h)
+
+dnl Check for declarations
+AC_CHECK_DECL([_PATH_BSHELL],,AC_DEFINE_UNQUOTED([_PATH_BSHELL], "/bin/sh", [Define to system shell path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+AC_CHECK_DECL([_PATH_DEVNULL],,AC_DEFINE_UNQUOTED([_PATH_DEVNULL], "/dev/null", [Define to devnull device node path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+AC_CHECK_DECL([_PATH_TTY],,AC_DEFINE_UNQUOTED([_PATH_TTY], "/dev/tty", [Define to tty device node path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+
+dnl Some systems lack isblank
+AC_CHECK_DECLS([isblank],,,[#include <ctype.h>])
+
+dnl Check for types and sizes
+AC_CHECK_SIZEOF([intmax_t])
+AC_CHECK_SIZEOF([long long int])
+
+dnl Conservatively use %lld format string rather than %jd where possible
+AC_MSG_CHECKING([format string for intmax_t])
+if test "x$ac_cv_sizeof_intmax_t" = "x$ac_cv_sizeof_long_long_int"; then
+  intmax_fstr="%lld"
+else
+  intmax_fstr="%jd"
+fi
+AC_MSG_RESULT($intmax_fstr)
+AC_DEFINE_UNQUOTED([INTMAX_FSTR], "$intmax_fstr", [Define to printf format string for intmax_t])
 
 dnl Checks for library functions.
 AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit imaxdiv isalpha killpg \
diff --git a/src/arith_yacc.c b/src/arith_yacc.c
index 6c5a720..bf21830 100644
--- a/src/arith_yacc.c
+++ b/src/arith_yacc.c
@@ -33,7 +33,6 @@
  */
 
 #include <inttypes.h>
-#include <stdint.h>
 #include <stdlib.h>
 #include "arith_yacc.h"
 #include "expand.h"
diff --git a/src/bltin/test.c b/src/bltin/test.c
index 7888f38..90135e1 100644
--- a/src/bltin/test.c
+++ b/src/bltin/test.c
@@ -12,7 +12,7 @@
 #include <sys/types.h>
 
 #include <fcntl.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
diff --git a/src/cd.c b/src/cd.c
index 9a69b69..cf80df8 100644
--- a/src/cd.c
+++ b/src/cd.c
@@ -32,11 +32,15 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 
 /*
  * The cd and pwd commands.
diff --git a/src/exec.c b/src/exec.c
index b273420..d8e6a40 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -32,12 +32,17 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 
 /*
  * When commands are first encountered, they are entered in a hash table.
diff --git a/src/expand.c b/src/expand.c
index 1b77b7c..57ef7e1 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -42,7 +42,7 @@
 #endif
 #include <stdlib.h>
 #include <stdio.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <string.h>
 #include <fnmatch.h>
@@ -1695,7 +1695,7 @@ cvtnum(intmax_t num)
 	int len = max_int_length(sizeof(num));
 
 	expdest = makestrspace(len, expdest);
-	len = fmtstr(expdest, len, "%jd", num);
+	len = fmtstr(expdest, len, INTMAX_FSTR, num);
 	STADJUST(len, expdest);
 	return len;
 }
diff --git a/src/expand.h b/src/expand.h
index 4251a4a..6a90f67 100644
--- a/src/expand.h
+++ b/src/expand.h
@@ -34,7 +34,7 @@
  *	@(#)expand.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 
 struct strlist {
 	struct strlist *next;
diff --git a/src/histedit.c b/src/histedit.c
index 9a1e533..84f9a48 100644
--- a/src/histedit.c
+++ b/src/histedit.c
@@ -32,8 +32,13 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <sys/param.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/src/jobs.c b/src/jobs.c
index 826a9af..fdb122a 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -32,11 +32,16 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <fcntl.h>
 #include <signal.h>
 #include <unistd.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 #include <sys/types.h>
 #include <sys/param.h>
 #ifdef BSD
diff --git a/src/jobs.h b/src/jobs.h
index 9c095ea..953ee87 100644
--- a/src/jobs.h
+++ b/src/jobs.h
@@ -34,7 +34,7 @@
  *	@(#)jobs.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 #include <sys/types.h>
 
 /* Mode argument to forkshell.  Don't change FORK_FG or FORK_BG. */
diff --git a/src/miscbltin.c b/src/miscbltin.c
index 653c92f..51432af 100644
--- a/src/miscbltin.c
+++ b/src/miscbltin.c
@@ -44,7 +44,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <stdint.h>
+#include <inttypes.h>
 
 #include "shell.h"
 #include "options.h"
@@ -396,7 +396,7 @@ static void printlim(enum limtype how, const struct rlimit *limit,
 		out1fmt("unlimited\n");
 	else {
 		val /= l->factor;
-		out1fmt("%jd\n", (intmax_t) val);
+		out1fmt(INTMAX_FSTR "\n", (intmax_t) val);
 	}
 }
 
diff --git a/src/mkbuiltins b/src/mkbuiltins
index e38ce4c..bb1e2a4 100644
--- a/src/mkbuiltins
+++ b/src/mkbuiltins
@@ -40,6 +40,21 @@ if ! type tempfile > /dev/null 2>&1; then
 	tempfile="mktemp ${TMPDIR:-/tmp}/builtin.XXXXXX"
 fi
 
+if ! type mktemp > /dev/null 2>&1; then
+	_my_tempfile()
+	{
+	    index=0
+	    while test -f "${TMPDIR:-/tmp}/builtin.$$.$index"; do
+		index=`expr $index + 1`
+	    done
+
+	    touch "${TMPDIR:-/tmp}/builtin.$$.$index"
+	    echo "${TMPDIR:-/tmp}/builtin.$$.$index"
+	}
+
+	tempfile="_my_tempfile"
+fi
+
 trap 'rm -f $temp $temp2' EXIT
 temp=$($tempfile)
 temp2=$($tempfile)
diff --git a/src/mystring.c b/src/mystring.c
index ce48c82..cb3d208 100644
--- a/src/mystring.c
+++ b/src/mystring.c
@@ -46,7 +46,7 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include "shell.h"
 #include "syntax.h"
diff --git a/src/mystring.h b/src/mystring.h
index 2e0540a..4a1511a 100644
--- a/src/mystring.h
+++ b/src/mystring.h
@@ -34,7 +34,7 @@
  *	@(#)mystring.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 #include <string.h>
 
 extern const char snlfmt[];
diff --git a/src/output.c b/src/output.c
index 2f9b5c4..3425603 100644
--- a/src/output.c
+++ b/src/output.c
@@ -378,6 +378,20 @@ xvsnprintf(char *outbuf, size_t length, const char *fmt, va_list ap)
 {
 	int ret;
 
+#ifdef __sun
+	/* 
+	 * vsnprintf() on older versions of Solaris returns -1 when
+	 * passed a length of 0.  To avoid this, use a dummy
+	 * 1-character buffer instead.
+	 */
+	char dummy[1];
+
+	if (length == 0) {
+		outbuf = dummy;
+		length = sizeof(dummy);
+	}
+#endif
+
 	INTOFF;
 	ret = vsnprintf(outbuf, length, fmt, ap);
 	INTON;
diff --git a/src/system.c b/src/system.c
index 5fd6062..844a641 100644
--- a/src/system.c
+++ b/src/system.c
@@ -170,9 +170,11 @@ int isupper(int c) {
 }
 

+#if HAVE_DECL_ISBLANK
 int isblank(int c) {
 	return _isblank(c);
 }
+#endif
 

 int isgraph(int c) {
@@ -189,3 +191,9 @@ int isxdigit(int c) {
 	return _isxdigit(c);
 }
 #endif
+
+#if !HAVE_DECL_ISBLANK
+int isblank(int c) {
+	return c == ' ' || c == '\t';
+}
+#endif
diff --git a/src/system.h b/src/system.h
index 17a9533..e712605 100644
--- a/src/system.h
+++ b/src/system.h
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include "config.h"
 #include <limits.h>
 #include <signal.h>
 #include <sys/types.h>
@@ -98,6 +99,10 @@ static inline int killpg(pid_t pid, int signal)
 long sysconf(int) __attribute__((__noreturn__));
 #endif
 
+#if !HAVE_DECL_ISBLANK
+int isblank(int c);
+#endif
+
 /*
  * A trick to suppress uninitialized variable warning without generating any
  * code
diff --git a/src/var.c b/src/var.c
index 25c2216..03cac1c 100644
--- a/src/var.c
+++ b/src/var.c
@@ -32,9 +32,14 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <unistd.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 
 /*
  * Shell variables.
@@ -223,7 +228,7 @@ intmax_t setvarint(const char *name, intmax_t val, int flags)
 	int len = max_int_length(sizeof(val));
 	char buf[len];
 
-	fmtstr(buf, len, "%jd", val);
+	fmtstr(buf, len, INTMAX_FSTR, val);
 	setvar(name, buf, flags);
 	return val;
 }
diff --git a/src/var.h b/src/var.h
index 7aa051f..35dd099 100644
--- a/src/var.h
+++ b/src/var.h
@@ -34,7 +34,7 @@
  *	@(#)var.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 
 /*
  * Shell variables.
-- 
1.7.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 2/3] Port to AIX
  2011-01-19  5:07 Portability patches Brian Koropoff
  2011-01-19  5:15 ` [PATCH 1/3] Port to Solaris Brian Koropoff
@ 2011-01-19  5:15 ` Brian Koropoff
  2011-03-10 12:25   ` Herbert Xu
  2011-01-19  5:16 ` [PATCH 3/3] Port to HP-UX Brian Koropoff
  2 siblings, 1 reply; 13+ messages in thread
From: Brian Koropoff @ 2011-01-19  5:15 UTC (permalink / raw)
  To: dash

- AIX lacks a WCOREDUMP macro.  It's just used to
  append "(core dumped)" to the crash message, so
  #ifdef around it.

- For some reason, the nl program on AIX defaults
  to not printing line numbers ("-b n"), even though
  the spec says it should default to "-b t".
  Explicitly pass "-b a" for good measure in mkbuiltins.

Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>
---
 src/jobs.c     |    2 ++
 src/mkbuiltins |    2 +-
 2 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/src/jobs.c b/src/jobs.c
index fdb122a..88b5792 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -431,9 +431,11 @@ sprint_status(char *s, int status, int sigonly)
 #endif
 		}
 		col = fmtstr(s, 32, strsignal(st));
+#ifdef WCOREDUMP
 		if (WCOREDUMP(status)) {
 			col += fmtstr(s + col, 16, " (core dumped)");
 		}
+#endif
 	} else if (!sigonly) {
 		if (st)
 			col = fmtstr(s, 16, "Done(%d)", st);
diff --git a/src/mkbuiltins b/src/mkbuiltins
index bb1e2a4..495274e 100644
--- a/src/mkbuiltins
+++ b/src/mkbuiltins
@@ -99,7 +99,7 @@ cat <<\!
  */
 
 !
-sed 's/	-[a-z]*//' $temp2 | nl -v 0 | LC_COLLATE=C sort -u -k 3,3 |
+sed 's/	-[a-z]*//' $temp2 | nl -b a -v 0 | LC_COLLATE=C sort -u -k 3,3 |
 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ |
 	awk '{	printf "#define %s (builtincmd + %d)\n", $3, $1}'
 printf '\n#define NUMBUILTINS %d\n' $(wc -l < $temp2)
-- 
1.7.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH 3/3] Port to HP-UX
  2011-01-19  5:07 Portability patches Brian Koropoff
  2011-01-19  5:15 ` [PATCH 1/3] Port to Solaris Brian Koropoff
  2011-01-19  5:15 ` [PATCH 2/3] Port to AIX Brian Koropoff
@ 2011-01-19  5:16 ` Brian Koropoff
  2011-03-10 12:26   ` Herbert Xu
  2 siblings, 1 reply; 13+ messages in thread
From: Brian Koropoff @ 2011-01-19  5:16 UTC (permalink / raw)
  To: dash

- strtoimax and strtoumax are macros and not functions,
   so switch to using AC_CHECK_DECLS in configure.ac
   to find them.

 - HP-UX lacks strsignal() and sys_siglist.  Check for
   sys_siglist in configure.ac and #ifdef around it.

 - HP-UX's vsnprintf() completely violates the standard
   by returning -1 (and neglecting to set errno) if the
   buffer is not large enough rather than returning the
   size necessary.  Work around it by (yuck) reallocating
   a dynamic buffer until it succeeds so we can return
   the expected result.

 - HP-UX needs _LARGEFILE64_SOURCE to be defined for open64()
   and friends to be available.  This seems to be safe to
   define everywhere, so do so.

 - The nl program doesn't like spaces between flags and their
   arguments, so remove them in mkbuiltins.

Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>
---
 configure.ac    |   16 +++++++++++++---
 src/Makefile.am |    3 ++-
 src/mkbuiltins  |    2 +-
 src/output.c    |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/system.c    |    2 ++
 src/system.h    |    4 ++--
 6 files changed, 72 insertions(+), 7 deletions(-)

diff --git a/configure.ac b/configure.ac
index 0a60055..07c2726 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,7 +43,7 @@ AC_ARG_ENABLE(glob, AS_HELP_STRING(--enable-glob, [Use glob(3) from libc]))
 dnl Checks for libraries.
 
 dnl Checks for header files.
-AC_CHECK_HEADERS(alloca.h paths.h)
+AC_CHECK_HEADERS(alloca.h paths.h unistd.h signal.h)
 
 dnl Check for declarations
 AC_CHECK_DECL([_PATH_BSHELL],,AC_DEFINE_UNQUOTED([_PATH_BSHELL], "/bin/sh", [Define to system shell path]),[
@@ -65,6 +65,17 @@ AC_CHECK_DECL([_PATH_TTY],,AC_DEFINE_UNQUOTED([_PATH_TTY], "/dev/tty", [Define t
 dnl Some systems lack isblank
 AC_CHECK_DECLS([isblank],,,[#include <ctype.h>])
 
+dnl strtoimax is a macro on some systems
+AC_CHECK_DECLS([strtoimax, strtoumax],,,[#include <inttypes.h>])
+
+dnl Check for sys_siglist
+AC_CHECK_DECLS([sys_siglist], [], [],
+          [#include <signal.h>
+          #ifdef HAVE_UNISTD_H
+          # include <unistd.h>
+          #endif
+          ])
+
 dnl Check for types and sizes
 AC_CHECK_SIZEOF([intmax_t])
 AC_CHECK_SIZEOF([long long int])
@@ -82,8 +93,7 @@ AC_DEFINE_UNQUOTED([INTMAX_FSTR], "$intmax_fstr", [Define to printf format strin
 dnl Checks for library functions.
 AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit imaxdiv isalpha killpg \
 	       mempcpy \
-	       sigsetmask stpcpy strchrnul strsignal strtod strtoimax \
-	       strtoumax sysconf)
+	       sigsetmask stpcpy strchrnul strsignal strtod sysconf)
 
 if test "$enable_fnmatch" = yes; then
 	use_fnmatch=
diff --git a/src/Makefile.am b/src/Makefile.am
index ba68d55..b99daf1 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -4,7 +4,8 @@ COMMON_CFLAGS = -Wall
 COMMON_CPPFLAGS = \
 	-include $(top_builddir)/config.h \
 	-DBSD=1 -DSHELL \
-	-DIFS_BROKEN
+	-DIFS_BROKEN \
+	-D_LARGEFILE64_SOURCE
 
 AM_CFLAGS = $(COMMON_CFLAGS)
 AM_CPPFLAGS = $(COMMON_CPPFLAGS)
diff --git a/src/mkbuiltins b/src/mkbuiltins
index 495274e..f6260b2 100644
--- a/src/mkbuiltins
+++ b/src/mkbuiltins
@@ -99,7 +99,7 @@ cat <<\!
  */
 
 !
-sed 's/	-[a-z]*//' $temp2 | nl -b a -v 0 | LC_COLLATE=C sort -u -k 3,3 |
+sed 's/	-[a-z]*//' $temp2 | nl -ba -v0 | LC_COLLATE=C sort -u -k 3,3 |
 tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ |
 	awk '{	printf "#define %s (builtincmd + %d)\n", $3, $1}'
 printf '\n#define NUMBUILTINS %d\n' $(wc -l < $temp2)
diff --git a/src/output.c b/src/output.c
index 3425603..db4a8a7 100644
--- a/src/output.c
+++ b/src/output.c
@@ -372,6 +372,56 @@ __closememout(void) {
 #endif
 #endif
 
+#ifdef __hpux
+static int
+xvsnprintf(char *outbuf, size_t length, const char *fmt, va_list ap)
+{
+	int ret;
+	char* dummy = NULL;
+	char* dummy_new = NULL;
+	size_t dummy_len = 8;
+	va_list ap_mine;
+
+	if (length > 0) {
+		INTOFF;
+		va_copy(ap_mine, ap);
+                errno = 0;
+		ret = vsnprintf(outbuf, length, fmt, ap_mine);
+		va_end(ap_mine);
+		INTON;
+	} else {
+		ret = -1;
+		errno = 0;
+	}
+
+	if (ret < 0 && errno == 0) {
+		do {
+			dummy_len *= 2;
+			dummy_new = realloc(dummy, dummy_len);
+			if (!dummy_new) {
+				ret = -1;
+				errno = ENOMEM;
+				break;
+			}
+			dummy = dummy_new;
+			INTOFF;
+			va_copy(ap_mine, ap);
+                        errno = 0;
+			ret = vsnprintf(dummy, dummy_len, fmt, ap_mine);
+			va_end(ap_mine);
+			INTON;
+		} while (ret < 0 && errno == 0);
+
+		if (ret >= 0 && length) {
+			memcpy(outbuf, dummy, length);
+		}
+		if (dummy) free(dummy);
+	}
+
+	return ret;
+}
+
+#else
 
 static int
 xvsnprintf(char *outbuf, size_t length, const char *fmt, va_list ap)
@@ -397,3 +447,5 @@ xvsnprintf(char *outbuf, size_t length, const char *fmt, va_list ap)
 	INTON;
 	return ret;
 }
+
+#endif
diff --git a/src/system.c b/src/system.c
index 844a641..dbf4601 100644
--- a/src/system.c
+++ b/src/system.c
@@ -92,8 +92,10 @@ char *strsignal(int sig)
 {
 	static char buf[19];
 
+#if HAVE_DECL_SYS_SIGLIST
 	if ((unsigned)sig < NSIG && sys_siglist[sig])
 		return (char *)sys_siglist[sig];
+#endif
 	fmtstr(buf, sizeof(buf), "Signal %d", sig); 
 	return buf;
 }
diff --git a/src/system.h b/src/system.h
index e712605..b1e9b63 100644
--- a/src/system.h
+++ b/src/system.h
@@ -70,11 +70,11 @@ static inline double strtod(const char *nptr, char **endptr)
 }
 #endif
 
-#ifndef HAVE_STRTOIMAX
+#if !HAVE_DECL_STRTOIMAX
 #define strtoimax strtoll
 #endif
 
-#ifndef HAVE_STRTOUMAX
+#if !HAVE_DECL_STRTOUMAX
 #define strtoumax strtoull
 #endif
 
-- 
1.7.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/3] Port to Solaris
  2011-01-19  5:15 ` [PATCH 1/3] Port to Solaris Brian Koropoff
@ 2011-01-21 13:06   ` Jilles Tjoelker
  2011-03-10 12:16     ` Herbert Xu
  0 siblings, 1 reply; 13+ messages in thread
From: Jilles Tjoelker @ 2011-01-21 13:06 UTC (permalink / raw)
  To: Brian Koropoff; +Cc: dash

On Tue, Jan 18, 2011 at 09:15:40PM -0800, Brian Koropoff wrote:
> - Older Solaris does not support %jd (intmax_t) in format
>   strings, but it does support %lld (long long), which is
>   the same size on all architectures it supports.  Do
>   a configure check for the sizes of both and prefer %lld
>   when it is safe to do so.

Pedantically, that's a regression for systems that do support %jd, and I
expect compilers to warn about it. If %jd is available, it must be used
for intmax_t, and not %lld, because that is for long long, not intmax_t.

What you can do is use PRIdMAX from <inttypes.h>, normally defined as
"jd". You can then define this to "lld" or "jd" if it is not defined.
I think this makes the code uglier (just like your change), but oh well.

> - Older Solaris lacks stdint.h, but inttypes.h provides the
>   same types and works on all platforms I've tried dash on,
>   so just use it instead.

<inttypes.h> is defined to be a superset of <stdint.h>, so that is ok.
It is also needed for the PRIdMAX suggestion above.

> [...]
> diff --git a/src/arith_yacc.c b/src/arith_yacc.c
> index 6c5a720..bf21830 100644
> --- a/src/arith_yacc.c
> +++ b/src/arith_yacc.c
> @@ -33,7 +33,6 @@
>   */
>  
>  #include <inttypes.h>
> -#include <stdint.h>
>  #include <stdlib.h>
>  #include "arith_yacc.h"
>  #include "expand.h"

This is useful regardless as the <stdint.h> is redundant. The
<inttypes.h> is already needed here because of imaxdiv().

By the way, I wonder what the advantage of imaxdiv() above separate %
and / is. Compilers can detect the matching between a % b and a / b and
do it in one operation, and any use of imaxdiv() trips gcc's
-Waggregate-return.

-- 
Jilles Tjoelker

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/3] Port to Solaris
  2011-01-21 13:06   ` Jilles Tjoelker
@ 2011-03-10 12:16     ` Herbert Xu
  2011-03-13  1:18       ` Brian Koropoff
  0 siblings, 1 reply; 13+ messages in thread
From: Herbert Xu @ 2011-03-10 12:16 UTC (permalink / raw)
  To: Jilles Tjoelker; +Cc: Brian Koropoff, dash

On Fri, Jan 21, 2011 at 01:06:02PM +0000, Jilles Tjoelker wrote:
>
> What you can do is use PRIdMAX from <inttypes.h>, normally defined as
> "jd". You can then define this to "lld" or "jd" if it is not defined.
> I think this makes the code uglier (just like your change), but oh well.

I agree.  Brian, please update your patch accordingly.

Thanks!

> By the way, I wonder what the advantage of imaxdiv() above separate %
> and / is. Compilers can detect the matching between a % b and a / b and
> do it in one operation, and any use of imaxdiv() trips gcc's
> -Waggregate-return.

It used to generate smaller code with gcc.  If gcc now knows how
to optimise it properly then I guess we can remove imaxdiv.

I just checked and gcc 4.4.5 still generates two divisions without
imaxdiv.  On the other hand gcc doesn't even inline imaxdiv.

Cheers,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 2/3] Port to AIX
  2011-01-19  5:15 ` [PATCH 2/3] Port to AIX Brian Koropoff
@ 2011-03-10 12:25   ` Herbert Xu
  0 siblings, 0 replies; 13+ messages in thread
From: Herbert Xu @ 2011-03-10 12:25 UTC (permalink / raw)
  To: Brian Koropoff; +Cc: dash

On Wed, Jan 19, 2011 at 05:15:49AM +0000, Brian Koropoff wrote:
> - AIX lacks a WCOREDUMP macro.  It's just used to
>   append "(core dumped)" to the crash message, so
>   #ifdef around it.
> 
> - For some reason, the nl program on AIX defaults
>   to not printing line numbers ("-b n"), even though
>   the spec says it should default to "-b t".
>   Explicitly pass "-b a" for good measure in mkbuiltins.
> 
> Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>

Patch applied.  Thanks.
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/3] Port to HP-UX
  2011-01-19  5:16 ` [PATCH 3/3] Port to HP-UX Brian Koropoff
@ 2011-03-10 12:26   ` Herbert Xu
  2011-03-12 23:54     ` Brian Koropoff
  0 siblings, 1 reply; 13+ messages in thread
From: Herbert Xu @ 2011-03-10 12:26 UTC (permalink / raw)
  To: Brian Koropoff; +Cc: dash

On Wed, Jan 19, 2011 at 05:16:02AM +0000, Brian Koropoff wrote:
>
>  - HP-UX needs _LARGEFILE64_SOURCE to be defined for open64()
>    and friends to be available.  This seems to be safe to
>    define everywhere, so do so.

Shouldn't this go into configure.ac? In fact, how can your patch
have any effect if configure.ac doesn't detect open64 in the
first place?

The rest looks OK.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/3] Port to HP-UX
  2011-03-10 12:26   ` Herbert Xu
@ 2011-03-12 23:54     ` Brian Koropoff
  2011-03-15  7:38       ` Herbert Xu
  0 siblings, 1 reply; 13+ messages in thread
From: Brian Koropoff @ 2011-03-12 23:54 UTC (permalink / raw)
  To: Herbert Xu; +Cc: dash

AC_CHECK_FUNC just checks that the symbol exists in libc, so it
succeeds.  _LARGEFILE64_SOURCE is necessary to get the system headers to
give you function prototypes, but it doesn't affect the configure test.

-- Brian

On Thu, 2011-03-10 at 20:26 +0800, Herbert Xu wrote:
> On Wed, Jan 19, 2011 at 05:16:02AM +0000, Brian Koropoff wrote:
> >
> >  - HP-UX needs _LARGEFILE64_SOURCE to be defined for open64()
> >    and friends to be available.  This seems to be safe to
> >    define everywhere, so do so.
> 
> Shouldn't this go into configure.ac? In fact, how can your patch
> have any effect if configure.ac doesn't detect open64 in the
> first place?
> 
> The rest looks OK.
> 
> Thanks,



^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [PATCH 1/3] Port to Solaris
  2011-03-10 12:16     ` Herbert Xu
@ 2011-03-13  1:18       ` Brian Koropoff
  2011-03-13  1:25         ` [PATCH] " Brian Koropoff
  0 siblings, 1 reply; 13+ messages in thread
From: Brian Koropoff @ 2011-03-13  1:18 UTC (permalink / raw)
  To: Herbert Xu; +Cc: Jilles Tjoelker, dash

I'll send an updated and rebased patch shortly.  Once we've agreed about
what to do with _LARGEFILE64_SOURCE in the HP-UX patch, I'll send a new
version of that as well.

-- Brian

On Thu, 2011-03-10 at 20:16 +0800, Herbert Xu wrote:
> On Fri, Jan 21, 2011 at 01:06:02PM +0000, Jilles Tjoelker wrote:
> >
> > What you can do is use PRIdMAX from <inttypes.h>, normally defined as
> > "jd". You can then define this to "lld" or "jd" if it is not defined.
> > I think this makes the code uglier (just like your change), but oh well.
> 
> I agree.  Brian, please update your patch accordingly.
> 
> Thanks!
> 
> > By the way, I wonder what the advantage of imaxdiv() above separate %
> > and / is. Compilers can detect the matching between a % b and a / b and
> > do it in one operation, and any use of imaxdiv() trips gcc's
> > -Waggregate-return.
> 
> It used to generate smaller code with gcc.  If gcc now knows how
> to optimise it properly then I guess we can remove imaxdiv.
> 
> I just checked and gcc 4.4.5 still generates two divisions without
> imaxdiv.  On the other hand gcc doesn't even inline imaxdiv.
> 
> Cheers,



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH] Port to Solaris
  2011-03-13  1:18       ` Brian Koropoff
@ 2011-03-13  1:25         ` Brian Koropoff
  2011-03-15  7:36           ` Herbert Xu
  0 siblings, 1 reply; 13+ messages in thread
From: Brian Koropoff @ 2011-03-13  1:25 UTC (permalink / raw)
  To: dash

- Solaris lacks paths.h and the various _PATH_* #defines.
  Check for them in configure.ac and fall back on the
  usual suspects when they are missing.

- Older Solaris lacks isblank(), and versions that have it
  use a macro.  Check for the declaration in configure.ac
  and fall back on a naive version when missing.

- Older Solaris does not support %jd (intmax_t) in format
  strings, but it does support the PRIdMAX macro from inttypes.h.
  Do a configure check for PRIdMAX and use it in the code.
  If it doesn't exist, define it to "lld" when sizeof(long long)
  equals sizeof(intmax_t) as this is more likely to work on
  older systems.  Otherwise, use "jd" and hope for the best.

- Older Solaris lacks stdint.h, but inttypes.h provides the
  same types and works on all platforms I've tried dash on,
  so just use it instead.

- Older Solaris doesn't like it when vsnprintf() is passed
  a NULL buffer (in violation of the POSIX spec, of course).
  Pass a 1-byte dummy buffer instead.

- Solaris lacks tempfile and mktemp programs.  Fall back on a
  "good-enough" custom function in mkbuiltins.

Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>
---
 configure.ac     |   41 ++++++++++++++++++++++++++++++++++++++++-
 src/arith_yacc.c |    1 -
 src/bltin/test.c |    2 +-
 src/cd.c         |    4 ++++
 src/exec.c       |    5 +++++
 src/expand.c     |    4 ++--
 src/expand.h     |    2 +-
 src/histedit.c   |    5 +++++
 src/jobs.c       |    5 +++++
 src/jobs.h       |    2 +-
 src/miscbltin.c  |    4 ++--
 src/mkbuiltins   |   15 +++++++++++++++
 src/mystring.c   |    2 +-
 src/mystring.h   |    2 +-
 src/output.c     |   14 ++++++++++++++
 src/system.c     |    8 ++++++++
 src/system.h     |    5 +++++
 src/var.c        |    7 ++++++-
 src/var.h        |    2 +-
 19 files changed, 117 insertions(+), 13 deletions(-)

diff --git a/configure.ac b/configure.ac
index c943725..effdffc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,7 +43,46 @@ AC_ARG_ENABLE(glob, AS_HELP_STRING(--enable-glob, [Use glob(3) from libc]))
 dnl Checks for libraries.
 
 dnl Checks for header files.
-AC_CHECK_HEADERS(alloca.h)
+AC_CHECK_HEADERS(alloca.h paths.h)
+
+dnl Check for declarations
+AC_CHECK_DECL([_PATH_BSHELL],,AC_DEFINE_UNQUOTED([_PATH_BSHELL], "/bin/sh", [Define to system shell path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+AC_CHECK_DECL([_PATH_DEVNULL],,AC_DEFINE_UNQUOTED([_PATH_DEVNULL], "/dev/null", [Define to devnull device node path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+AC_CHECK_DECL([_PATH_TTY],,AC_DEFINE_UNQUOTED([_PATH_TTY], "/dev/tty", [Define to tty device node path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+
+dnl Some systems lack isblank
+AC_CHECK_DECLS([isblank],,,[#include <ctype.h>])
+
+dnl Check for sizes of types
+AC_CHECK_SIZEOF([intmax_t])
+AC_CHECK_SIZEOF([long long int])
+
+dnl Select a fallback format string for intmax_t in case we don't find PRIdMAX
+if test "x$ac_cv_sizeof_intmax_t" = "x$ac_cv_sizeof_long_long_int"; then
+  intmax_fstr="lld"
+else
+  intmax_fstr="jd"
+fi
+
+dnl Check for PRIdMAX and define it to a fallback if not found
+AC_CHECK_DECL([PRIdMAX],,
+	[AC_DEFINE_UNQUOTED([PRIdMAX], "$intmax_fstr",
+				       [Define to printf format string for intmax_t])],
+        [
+#include <inttypes.h>
+])
 
 dnl Checks for library functions.
 AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit imaxdiv isalpha killpg \
diff --git a/src/arith_yacc.c b/src/arith_yacc.c
index 6c5a720..bf21830 100644
--- a/src/arith_yacc.c
+++ b/src/arith_yacc.c
@@ -33,7 +33,6 @@
  */
 
 #include <inttypes.h>
-#include <stdint.h>
 #include <stdlib.h>
 #include "arith_yacc.h"
 #include "expand.h"
diff --git a/src/bltin/test.c b/src/bltin/test.c
index 7888f38..90135e1 100644
--- a/src/bltin/test.c
+++ b/src/bltin/test.c
@@ -12,7 +12,7 @@
 #include <sys/types.h>
 
 #include <fcntl.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
diff --git a/src/cd.c b/src/cd.c
index 9a69b69..cf80df8 100644
--- a/src/cd.c
+++ b/src/cd.c
@@ -32,11 +32,15 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 
 /*
  * The cd and pwd commands.
diff --git a/src/exec.c b/src/exec.c
index b273420..d8e6a40 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -32,12 +32,17 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 
 /*
  * When commands are first encountered, they are entered in a hash table.
diff --git a/src/expand.c b/src/expand.c
index 7a9b157..f155ea0 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -42,7 +42,7 @@
 #endif
 #include <stdlib.h>
 #include <stdio.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <string.h>
 #include <fnmatch.h>
@@ -1691,7 +1691,7 @@ cvtnum(intmax_t num)
 	int len = max_int_length(sizeof(num));
 
 	expdest = makestrspace(len, expdest);
-	len = fmtstr(expdest, len, "%jd", num);
+	len = fmtstr(expdest, len, "%" PRIdMAX, num);
 	STADJUST(len, expdest);
 	return len;
 }
diff --git a/src/expand.h b/src/expand.h
index 4251a4a..6a90f67 100644
--- a/src/expand.h
+++ b/src/expand.h
@@ -34,7 +34,7 @@
  *	@(#)expand.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 
 struct strlist {
 	struct strlist *next;
diff --git a/src/histedit.c b/src/histedit.c
index 9a1e533..84f9a48 100644
--- a/src/histedit.c
+++ b/src/histedit.c
@@ -32,8 +32,13 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <sys/param.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/src/jobs.c b/src/jobs.c
index f67116e..88b5792 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -32,11 +32,16 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <fcntl.h>
 #include <signal.h>
 #include <unistd.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 #include <sys/types.h>
 #include <sys/param.h>
 #ifdef BSD
diff --git a/src/jobs.h b/src/jobs.h
index 9c095ea..953ee87 100644
--- a/src/jobs.h
+++ b/src/jobs.h
@@ -34,7 +34,7 @@
  *	@(#)jobs.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 #include <sys/types.h>
 
 /* Mode argument to forkshell.  Don't change FORK_FG or FORK_BG. */
diff --git a/src/miscbltin.c b/src/miscbltin.c
index f507381..e354df4 100644
--- a/src/miscbltin.c
+++ b/src/miscbltin.c
@@ -44,7 +44,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <stdint.h>
+#include <inttypes.h>
 
 #include "shell.h"
 #include "options.h"
@@ -410,7 +410,7 @@ static void printlim(enum limtype how, const struct rlimit *limit,
 		out1fmt("unlimited\n");
 	else {
 		val /= l->factor;
-		out1fmt("%jd\n", (intmax_t) val);
+		out1fmt("%" PRIdMAX "\n", (intmax_t) val);
 	}
 }
 
diff --git a/src/mkbuiltins b/src/mkbuiltins
index 99107c2..495274e 100644
--- a/src/mkbuiltins
+++ b/src/mkbuiltins
@@ -40,6 +40,21 @@ if ! type tempfile > /dev/null 2>&1; then
 	tempfile="mktemp ${TMPDIR:-/tmp}/builtin.XXXXXX"
 fi
 
+if ! type mktemp > /dev/null 2>&1; then
+	_my_tempfile()
+	{
+	    index=0
+	    while test -f "${TMPDIR:-/tmp}/builtin.$$.$index"; do
+		index=`expr $index + 1`
+	    done
+
+	    touch "${TMPDIR:-/tmp}/builtin.$$.$index"
+	    echo "${TMPDIR:-/tmp}/builtin.$$.$index"
+	}
+
+	tempfile="_my_tempfile"
+fi
+
 trap 'rm -f $temp $temp2' EXIT
 temp=$($tempfile)
 temp2=$($tempfile)
diff --git a/src/mystring.c b/src/mystring.c
index bbb6b77..0106bd2 100644
--- a/src/mystring.c
+++ b/src/mystring.c
@@ -46,7 +46,7 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include "shell.h"
 #include "syntax.h"
diff --git a/src/mystring.h b/src/mystring.h
index 3522523..083ea98 100644
--- a/src/mystring.h
+++ b/src/mystring.h
@@ -34,7 +34,7 @@
  *	@(#)mystring.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 #include <string.h>
 
 extern const char snlfmt[];
diff --git a/src/output.c b/src/output.c
index 2f9b5c4..3425603 100644
--- a/src/output.c
+++ b/src/output.c
@@ -378,6 +378,20 @@ xvsnprintf(char *outbuf, size_t length, const char *fmt, va_list ap)
 {
 	int ret;
 
+#ifdef __sun
+	/* 
+	 * vsnprintf() on older versions of Solaris returns -1 when
+	 * passed a length of 0.  To avoid this, use a dummy
+	 * 1-character buffer instead.
+	 */
+	char dummy[1];
+
+	if (length == 0) {
+		outbuf = dummy;
+		length = sizeof(dummy);
+	}
+#endif
+
 	INTOFF;
 	ret = vsnprintf(outbuf, length, fmt, ap);
 	INTON;
diff --git a/src/system.c b/src/system.c
index 5fd6062..844a641 100644
--- a/src/system.c
+++ b/src/system.c
@@ -170,9 +170,11 @@ int isupper(int c) {
 }
 

+#if HAVE_DECL_ISBLANK
 int isblank(int c) {
 	return _isblank(c);
 }
+#endif
 

 int isgraph(int c) {
@@ -189,3 +191,9 @@ int isxdigit(int c) {
 	return _isxdigit(c);
 }
 #endif
+
+#if !HAVE_DECL_ISBLANK
+int isblank(int c) {
+	return c == ' ' || c == '\t';
+}
+#endif
diff --git a/src/system.h b/src/system.h
index 17a9533..e712605 100644
--- a/src/system.h
+++ b/src/system.h
@@ -26,6 +26,7 @@
  * SUCH DAMAGE.
  */
 
+#include "config.h"
 #include <limits.h>
 #include <signal.h>
 #include <sys/types.h>
@@ -98,6 +99,10 @@ static inline int killpg(pid_t pid, int signal)
 long sysconf(int) __attribute__((__noreturn__));
 #endif
 
+#if !HAVE_DECL_ISBLANK
+int isblank(int c);
+#endif
+
 /*
  * A trick to suppress uninitialized variable warning without generating any
  * code
diff --git a/src/var.c b/src/var.c
index 25c2216..ddb28f4 100644
--- a/src/var.c
+++ b/src/var.c
@@ -32,9 +32,14 @@
  * SUCH DAMAGE.
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 #include <unistd.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 
 /*
  * Shell variables.
@@ -223,7 +228,7 @@ intmax_t setvarint(const char *name, intmax_t val, int flags)
 	int len = max_int_length(sizeof(val));
 	char buf[len];
 
-	fmtstr(buf, len, "%jd", val);
+	fmtstr(buf, len, "%" PRIdMAX, val);
 	setvar(name, buf, flags);
 	return val;
 }
diff --git a/src/var.h b/src/var.h
index 7aa051f..35dd099 100644
--- a/src/var.h
+++ b/src/var.h
@@ -34,7 +34,7 @@
  *	@(#)var.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 
 /*
  * Shell variables.
-- 
1.7.1



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH] Port to Solaris
  2011-03-13  1:25         ` [PATCH] " Brian Koropoff
@ 2011-03-15  7:36           ` Herbert Xu
  0 siblings, 0 replies; 13+ messages in thread
From: Herbert Xu @ 2011-03-15  7:36 UTC (permalink / raw)
  To: Brian Koropoff; +Cc: dash

On Sun, Mar 13, 2011 at 01:25:21AM +0000, Brian Koropoff wrote:
> Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>

Thanks for the patch.  I've made some minor updates before applying:

* Remove unnecessary config.h inclusions (already included on command
  line)
* Only check mktemp when tempfile is unavailable

Please take a look at the end result to see if I've messed anything
up.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
--
commit bfcdc4969510997fe81debf52982641febfa1bdf
Author: Brian Koropoff <bkoropoff@gmail.com>
Date:   Tue Mar 15 15:35:14 2011 +0800

    [SHELL] Port to Solaris
    
    - Solaris lacks paths.h and the various _PATH_* #defines.
      Check for them in configure.ac and fall back on the
      usual suspects when they are missing.
    
    - Older Solaris lacks isblank(), and versions that have it
      use a macro.  Check for the declaration in configure.ac
      and fall back on a naive version when missing.
    
    - Older Solaris does not support %jd (intmax_t) in format
      strings, but it does support the PRIdMAX macro from inttypes.h.
      Do a configure check for PRIdMAX and use it in the code.
      If it doesn't exist, define it to "lld" when sizeof(long long)
      equals sizeof(intmax_t) as this is more likely to work on
      older systems.  Otherwise, use "jd" and hope for the best.
    
    - Older Solaris lacks stdint.h, but inttypes.h provides the
      same types and works on all platforms I've tried dash on,
      so just use it instead.
    
    - Older Solaris doesn't like it when vsnprintf() is passed
      a NULL buffer (in violation of the POSIX spec, of course).
      Pass a 1-byte dummy buffer instead.
    
    - Solaris lacks tempfile and mktemp programs.  Fall back on a
      "good-enough" custom function in mkbuiltins.
    
    Signed-off-by: Brian Koropoff <bkoropoff@gmail.com>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

diff --git a/ChangeLog b/ChangeLog
index e96bdc4..08c3792 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2011-03-15  Brian Koropoff <bkoropoff@gmail.com>
+
+	* Port to Solaris.
+
 2011-03-11  Herbert Xu <herbert@gondor.apana.org.au>
 
 	* Fix backslash handling in read(1).
diff --git a/configure.ac b/configure.ac
index c943725..effdffc 100644
--- a/configure.ac
+++ b/configure.ac
@@ -43,7 +43,46 @@ AC_ARG_ENABLE(glob, AS_HELP_STRING(--enable-glob, [Use glob(3) from libc]))
 dnl Checks for libraries.
 
 dnl Checks for header files.
-AC_CHECK_HEADERS(alloca.h)
+AC_CHECK_HEADERS(alloca.h paths.h)
+
+dnl Check for declarations
+AC_CHECK_DECL([_PATH_BSHELL],,AC_DEFINE_UNQUOTED([_PATH_BSHELL], "/bin/sh", [Define to system shell path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+AC_CHECK_DECL([_PATH_DEVNULL],,AC_DEFINE_UNQUOTED([_PATH_DEVNULL], "/dev/null", [Define to devnull device node path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+AC_CHECK_DECL([_PATH_TTY],,AC_DEFINE_UNQUOTED([_PATH_TTY], "/dev/tty", [Define to tty device node path]),[
+#ifdef HAVE_PATHS_H
+#include <paths.h>
+#endif
+])
+
+dnl Some systems lack isblank
+AC_CHECK_DECLS([isblank],,,[#include <ctype.h>])
+
+dnl Check for sizes of types
+AC_CHECK_SIZEOF([intmax_t])
+AC_CHECK_SIZEOF([long long int])
+
+dnl Select a fallback format string for intmax_t in case we don't find PRIdMAX
+if test "x$ac_cv_sizeof_intmax_t" = "x$ac_cv_sizeof_long_long_int"; then
+  intmax_fstr="lld"
+else
+  intmax_fstr="jd"
+fi
+
+dnl Check for PRIdMAX and define it to a fallback if not found
+AC_CHECK_DECL([PRIdMAX],,
+	[AC_DEFINE_UNQUOTED([PRIdMAX], "$intmax_fstr",
+				       [Define to printf format string for intmax_t])],
+        [
+#include <inttypes.h>
+])
 
 dnl Checks for library functions.
 AC_CHECK_FUNCS(bsearch faccessat getpwnam getrlimit imaxdiv isalpha killpg \
diff --git a/src/arith_yacc.c b/src/arith_yacc.c
index 6c5a720..bf21830 100644
--- a/src/arith_yacc.c
+++ b/src/arith_yacc.c
@@ -33,7 +33,6 @@
  */
 
 #include <inttypes.h>
-#include <stdint.h>
 #include <stdlib.h>
 #include "arith_yacc.h"
 #include "expand.h"
diff --git a/src/bltin/test.c b/src/bltin/test.c
index 7888f38..90135e1 100644
--- a/src/bltin/test.c
+++ b/src/bltin/test.c
@@ -12,7 +12,7 @@
 #include <sys/types.h>
 
 #include <fcntl.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
diff --git a/src/cd.c b/src/cd.c
index 9a69b69..2d9d4b5 100644
--- a/src/cd.c
+++ b/src/cd.c
@@ -37,6 +37,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <limits.h>
 
 /*
  * The cd and pwd commands.
diff --git a/src/exec.c b/src/exec.c
index b273420..194088b 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -37,7 +37,9 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 
 /*
  * When commands are first encountered, they are entered in a hash table.
diff --git a/src/expand.c b/src/expand.c
index 7a9b157..f155ea0 100644
--- a/src/expand.c
+++ b/src/expand.c
@@ -42,7 +42,7 @@
 #endif
 #include <stdlib.h>
 #include <stdio.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <string.h>
 #include <fnmatch.h>
@@ -1691,7 +1691,7 @@ cvtnum(intmax_t num)
 	int len = max_int_length(sizeof(num));
 
 	expdest = makestrspace(len, expdest);
-	len = fmtstr(expdest, len, "%jd", num);
+	len = fmtstr(expdest, len, "%" PRIdMAX, num);
 	STADJUST(len, expdest);
 	return len;
 }
diff --git a/src/expand.h b/src/expand.h
index 4251a4a..6a90f67 100644
--- a/src/expand.h
+++ b/src/expand.h
@@ -34,7 +34,7 @@
  *	@(#)expand.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 
 struct strlist {
 	struct strlist *next;
diff --git a/src/histedit.c b/src/histedit.c
index 9a1e533..b27d629 100644
--- a/src/histedit.c
+++ b/src/histedit.c
@@ -33,7 +33,9 @@
  */
 
 #include <sys/param.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/src/jobs.c b/src/jobs.c
index f67116e..4b1b938 100644
--- a/src/jobs.c
+++ b/src/jobs.c
@@ -36,7 +36,9 @@
 #include <signal.h>
 #include <unistd.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 #include <sys/types.h>
 #include <sys/param.h>
 #ifdef BSD
diff --git a/src/jobs.h b/src/jobs.h
index 9c095ea..953ee87 100644
--- a/src/jobs.h
+++ b/src/jobs.h
@@ -34,7 +34,7 @@
  *	@(#)jobs.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 #include <sys/types.h>
 
 /* Mode argument to forkshell.  Don't change FORK_FG or FORK_BG. */
diff --git a/src/miscbltin.c b/src/miscbltin.c
index f507381..e354df4 100644
--- a/src/miscbltin.c
+++ b/src/miscbltin.c
@@ -44,7 +44,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <stdint.h>
+#include <inttypes.h>
 
 #include "shell.h"
 #include "options.h"
@@ -410,7 +410,7 @@ static void printlim(enum limtype how, const struct rlimit *limit,
 		out1fmt("unlimited\n");
 	else {
 		val /= l->factor;
-		out1fmt("%jd\n", (intmax_t) val);
+		out1fmt("%" PRIdMAX "\n", (intmax_t) val);
 	}
 }
 
diff --git a/src/mkbuiltins b/src/mkbuiltins
index 99107c2..f562ae2 100644
--- a/src/mkbuiltins
+++ b/src/mkbuiltins
@@ -36,7 +36,20 @@
 #	@(#)mkbuiltins	8.2 (Berkeley) 5/4/95
 
 tempfile=tempfile
-if ! type tempfile > /dev/null 2>&1; then
+if ! type tempfile > /dev/null 2>&1 && ! type mktemp > /dev/null 2>&1; then
+	_my_tempfile()
+	{
+		local index=0
+		while test -f "${TMPDIR:-/tmp}/builtin.$$.$index"; do
+			index=`expr $index + 1`
+		done
+
+		touch "${TMPDIR:-/tmp}/builtin.$$.$index"
+		echo "${TMPDIR:-/tmp}/builtin.$$.$index"
+	}
+
+	tempfile="_my_tempfile"
+elif ! type tempfile > /dev/null 2>&1; then
 	tempfile="mktemp ${TMPDIR:-/tmp}/builtin.XXXXXX"
 fi
 
diff --git a/src/mystring.c b/src/mystring.c
index bbb6b77..0106bd2 100644
--- a/src/mystring.c
+++ b/src/mystring.c
@@ -46,7 +46,7 @@
 #include <errno.h>
 #include <inttypes.h>
 #include <limits.h>
-#include <stdint.h>
+#include <inttypes.h>
 #include <stdlib.h>
 #include "shell.h"
 #include "syntax.h"
diff --git a/src/mystring.h b/src/mystring.h
index 3522523..083ea98 100644
--- a/src/mystring.h
+++ b/src/mystring.h
@@ -34,7 +34,7 @@
  *	@(#)mystring.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 #include <string.h>
 
 extern const char snlfmt[];
diff --git a/src/output.c b/src/output.c
index 2f9b5c4..f62e7ea 100644
--- a/src/output.c
+++ b/src/output.c
@@ -378,6 +378,20 @@ xvsnprintf(char *outbuf, size_t length, const char *fmt, va_list ap)
 {
 	int ret;
 
+#ifdef __sun
+	/*
+	 * vsnprintf() on older versions of Solaris returns -1 when
+	 * passed a length of 0.  To avoid this, use a dummy
+	 * 1-character buffer instead.
+	 */
+	char dummy[1];
+
+	if (length == 0) {
+		outbuf = dummy;
+		length = sizeof(dummy);
+	}
+#endif
+
 	INTOFF;
 	ret = vsnprintf(outbuf, length, fmt, ap);
 	INTON;
diff --git a/src/system.c b/src/system.c
index 5fd6062..844a641 100644
--- a/src/system.c
+++ b/src/system.c
@@ -170,9 +170,11 @@ int isupper(int c) {
 }
 
 
+#if HAVE_DECL_ISBLANK
 int isblank(int c) {
 	return _isblank(c);
 }
+#endif
 
 
 int isgraph(int c) {
@@ -189,3 +191,9 @@ int isxdigit(int c) {
 	return _isxdigit(c);
 }
 #endif
+
+#if !HAVE_DECL_ISBLANK
+int isblank(int c) {
+	return c == ' ' || c == '\t';
+}
+#endif
diff --git a/src/system.h b/src/system.h
index 17a9533..a8d09b3 100644
--- a/src/system.h
+++ b/src/system.h
@@ -98,6 +98,10 @@ static inline int killpg(pid_t pid, int signal)
 long sysconf(int) __attribute__((__noreturn__));
 #endif
 
+#if !HAVE_DECL_ISBLANK
+int isblank(int c);
+#endif
+
 /*
  * A trick to suppress uninitialized variable warning without generating any
  * code
diff --git a/src/var.c b/src/var.c
index 25c2216..aec1076 100644
--- a/src/var.c
+++ b/src/var.c
@@ -34,7 +34,9 @@
 
 #include <unistd.h>
 #include <stdlib.h>
+#ifdef HAVE_PATHS_H
 #include <paths.h>
+#endif
 
 /*
  * Shell variables.
@@ -223,7 +225,7 @@ intmax_t setvarint(const char *name, intmax_t val, int flags)
 	int len = max_int_length(sizeof(val));
 	char buf[len];
 
-	fmtstr(buf, len, "%jd", val);
+	fmtstr(buf, len, "%" PRIdMAX, val);
 	setvar(name, buf, flags);
 	return val;
 }
diff --git a/src/var.h b/src/var.h
index 7aa051f..35dd099 100644
--- a/src/var.h
+++ b/src/var.h
@@ -34,7 +34,7 @@
  *	@(#)var.h	8.2 (Berkeley) 5/4/95
  */
 
-#include <stdint.h>
+#include <inttypes.h>
 
 /*
  * Shell variables.

^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [PATCH 3/3] Port to HP-UX
  2011-03-12 23:54     ` Brian Koropoff
@ 2011-03-15  7:38       ` Herbert Xu
  0 siblings, 0 replies; 13+ messages in thread
From: Herbert Xu @ 2011-03-15  7:38 UTC (permalink / raw)
  To: Brian Koropoff; +Cc: dash

On Sat, Mar 12, 2011 at 03:54:43PM -0800, Brian Koropoff wrote:
> AC_CHECK_FUNC just checks that the symbol exists in libc, so it
> succeeds.  _LARGEFILE64_SOURCE is necessary to get the system headers to
> give you function prototypes, but it doesn't affect the configure test.

OK, please regenerate the patch against the head of my tree and
please move the macro definition from Makefile into config.h
through configure.ac.

Thanks,
-- 
Email: Herbert Xu <herbert@gondor.apana.org.au>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2011-03-15  7:38 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-01-19  5:07 Portability patches Brian Koropoff
2011-01-19  5:15 ` [PATCH 1/3] Port to Solaris Brian Koropoff
2011-01-21 13:06   ` Jilles Tjoelker
2011-03-10 12:16     ` Herbert Xu
2011-03-13  1:18       ` Brian Koropoff
2011-03-13  1:25         ` [PATCH] " Brian Koropoff
2011-03-15  7:36           ` Herbert Xu
2011-01-19  5:15 ` [PATCH 2/3] Port to AIX Brian Koropoff
2011-03-10 12:25   ` Herbert Xu
2011-01-19  5:16 ` [PATCH 3/3] Port to HP-UX Brian Koropoff
2011-03-10 12:26   ` Herbert Xu
2011-03-12 23:54     ` Brian Koropoff
2011-03-15  7:38       ` Herbert Xu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox