From: Paul Eggert Date: Wed, 31 Aug 2011 22:18:16 +0000 (-0700) Subject: Add a stub for snprintf, for ancient hosts lacking it. X-Git-Tag: emacs-pretest-24.0.90~104^2~153^2~1^2~4 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=55e5faa18952a5608cf653f8fd268a7645a2f876;p=emacs.git Add a stub for snprintf, for ancient hosts lacking it. * configure.in (snprintf): New check. * nt/config.nt (HAVE_SNPRINTF): New macro. * src/sysdep.c (snprintf) [! HAVE_SNPRINTF]: New function. --- diff --git a/ChangeLog b/ChangeLog index 1f38dbf71ca..c973a82e8a4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2011-08-31 Paul Eggert + + * configure.in (snprintf): New check. + 2011-08-30 Paul Eggert * configure.in (opsys): Change pattern to *-*-linux* diff --git a/configure.in b/configure.in index 715e8278df2..dcc2bdb99d1 100644 --- a/configure.in +++ b/configure.in @@ -3071,6 +3071,8 @@ fi AC_FUNC_FORK +AC_CHECK_FUNCS(snprintf) + dnl Adapted from Haible's version. AC_CACHE_CHECK([for nl_langinfo and CODESET], emacs_cv_langinfo_codeset, [AC_LINK_IFELSE([AC_LANG_PROGRAM([[#include ]], diff --git a/nt/ChangeLog b/nt/ChangeLog index edbd1a1c1d4..f3c57c7e0d0 100644 --- a/nt/ChangeLog +++ b/nt/ChangeLog @@ -1,3 +1,7 @@ +2011-08-31 Paul Eggert + + * config.nt (HAVE_SNPRINTF): New macro. + 2011-07-28 Paul Eggert Assume freestanding C89 headers, string.h, stdlib.h. diff --git a/nt/config.nt b/nt/config.nt index 3436bc989e0..ae3807538c0 100644 --- a/nt/config.nt +++ b/nt/config.nt @@ -240,6 +240,7 @@ along with GNU Emacs. If not, see . */ #define HAVE_SETSOCKOPT 1 #define HAVE_GETSOCKNAME 1 #define HAVE_GETPEERNAME 1 +#define HAVE_SNPRINTF 1 #define HAVE_LANGINFO_CODESET 1 /* Local (unix) sockets are not supported. */ #undef HAVE_SYS_UN_H diff --git a/src/ChangeLog b/src/ChangeLog index 463f2965baa..0ba2df42186 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -90,6 +90,8 @@ * process.c (make_process): Use printmax_t, not int, to format process-name gensyms. + * sysdep.c (snprintf) [! HAVE_SNPRINTF]: New function. + * term.c (produce_glyphless_glyph): Make sprintf buffer a bit bigger to avoid potential buffer overrun. diff --git a/src/sysdep.c b/src/sysdep.c index 57fff94f552..e20bd591da1 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -1811,6 +1811,45 @@ strerror (int errnum) } #endif /* not WINDOWSNT */ #endif /* ! HAVE_STRERROR */ + +#ifndef HAVE_SNPRINTF +/* Approximate snprintf as best we can on ancient hosts that lack it. */ +int +snprintf (char *buf, size_t bufsize, char const *format, ...) +{ + ptrdiff_t size = min (bufsize, PTRDIFF_MAX); + ptrdiff_t nbytes = size - 1; + va_list ap; + + if (size) + { + va_start (ap, format); + nbytes = doprnt (buf, size, format, 0, ap); + va_end (ap); + } + + if (nbytes == size - 1) + { + /* Calculate the length of the string that would have been created + had the buffer been large enough. */ + char stackbuf[4000]; + char *b = stackbuf; + ptrdiff_t bsize = sizeof stackbuf; + va_start (ap, format); + nbytes = evxprintf (&b, &bsize, stackbuf, -1, format, ap); + va_end (ap); + if (b != stackbuf) + xfree (b); + } + + if (INT_MAX < nbytes) + { + errno = EOVERFLOW; + return -1; + } + return nbytes; +} +#endif int emacs_open (const char *path, int oflag, int mode)