diff options
| author | Paul Eggert | 2011-08-31 15:18:16 -0700 |
|---|---|---|
| committer | Paul Eggert | 2011-08-31 15:18:16 -0700 |
| commit | 55e5faa18952a5608cf653f8fd268a7645a2f876 (patch) | |
| tree | 5056032a9e9270cb79639f2e1fa515d0bbde8d8b /src | |
| parent | e91caa6011949f45790cd131883b2b3b26d6a0a2 (diff) | |
| download | emacs-55e5faa18952a5608cf653f8fd268a7645a2f876.tar.gz emacs-55e5faa18952a5608cf653f8fd268a7645a2f876.zip | |
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.
Diffstat (limited to 'src')
| -rw-r--r-- | src/ChangeLog | 2 | ||||
| -rw-r--r-- | src/sysdep.c | 39 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/ChangeLog b/src/ChangeLog index 463f2965baa..0ba2df42186 100644 --- a/src/ChangeLog +++ b/src/ChangeLog | |||
| @@ -90,6 +90,8 @@ | |||
| 90 | * process.c (make_process): Use printmax_t, not int, to format | 90 | * process.c (make_process): Use printmax_t, not int, to format |
| 91 | process-name gensyms. | 91 | process-name gensyms. |
| 92 | 92 | ||
| 93 | * sysdep.c (snprintf) [! HAVE_SNPRINTF]: New function. | ||
| 94 | |||
| 93 | * term.c (produce_glyphless_glyph): Make sprintf buffer a bit bigger | 95 | * term.c (produce_glyphless_glyph): Make sprintf buffer a bit bigger |
| 94 | to avoid potential buffer overrun. | 96 | to avoid potential buffer overrun. |
| 95 | 97 | ||
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) | |||
| 1811 | } | 1811 | } |
| 1812 | #endif /* not WINDOWSNT */ | 1812 | #endif /* not WINDOWSNT */ |
| 1813 | #endif /* ! HAVE_STRERROR */ | 1813 | #endif /* ! HAVE_STRERROR */ |
| 1814 | |||
| 1815 | #ifndef HAVE_SNPRINTF | ||
| 1816 | /* Approximate snprintf as best we can on ancient hosts that lack it. */ | ||
| 1817 | int | ||
| 1818 | snprintf (char *buf, size_t bufsize, char const *format, ...) | ||
| 1819 | { | ||
| 1820 | ptrdiff_t size = min (bufsize, PTRDIFF_MAX); | ||
| 1821 | ptrdiff_t nbytes = size - 1; | ||
| 1822 | va_list ap; | ||
| 1823 | |||
| 1824 | if (size) | ||
| 1825 | { | ||
| 1826 | va_start (ap, format); | ||
| 1827 | nbytes = doprnt (buf, size, format, 0, ap); | ||
| 1828 | va_end (ap); | ||
| 1829 | } | ||
| 1830 | |||
| 1831 | if (nbytes == size - 1) | ||
| 1832 | { | ||
| 1833 | /* Calculate the length of the string that would have been created | ||
| 1834 | had the buffer been large enough. */ | ||
| 1835 | char stackbuf[4000]; | ||
| 1836 | char *b = stackbuf; | ||
| 1837 | ptrdiff_t bsize = sizeof stackbuf; | ||
| 1838 | va_start (ap, format); | ||
| 1839 | nbytes = evxprintf (&b, &bsize, stackbuf, -1, format, ap); | ||
| 1840 | va_end (ap); | ||
| 1841 | if (b != stackbuf) | ||
| 1842 | xfree (b); | ||
| 1843 | } | ||
| 1844 | |||
| 1845 | if (INT_MAX < nbytes) | ||
| 1846 | { | ||
| 1847 | errno = EOVERFLOW; | ||
| 1848 | return -1; | ||
| 1849 | } | ||
| 1850 | return nbytes; | ||
| 1851 | } | ||
| 1852 | #endif | ||
| 1814 | 1853 | ||
| 1815 | int | 1854 | int |
| 1816 | emacs_open (const char *path, int oflag, int mode) | 1855 | emacs_open (const char *path, int oflag, int mode) |