libc/newlib/libc/stdio/asniprintf.c
Jeff Johnston 7dd0c33097 2008-12-11 Craig Howland <howland@LGSInnovations.com>
* libc/include/sys/lock.h:  Add void cast to avoid "statement has no
        effect" warnings from gcc.
        * libc/include/sys/stdio.h:  Ditto.
        * libc/include/sys/time.h:  Correct gettimeofday() prototype.
        * libc/stdlib/__exp10.c:  Add #include "std.h" for function prototype.
        * libc/stdlib/__ten_mu.c:  Ditto.
        * libc/stdlib/std.h:  Correct __exp10's ANSI prototype.
        * libc/stdlib/ldtoa.c:  Change eiisinf definition to ANSI form.  (Are
        already others in file without _ansi method, so did not bother.)
        * libc/stdlib/system.c:  Use _ansi forms for function prototypes and
        definitions.
        * libc/time/mktime.c:  Ditto.
        * libc/misc/__dprintf.c:  Ditto.
        * libc/include/stdio.h:  Add function prototypes for _fgetc_r,
        _fgetpos_r, _fsetpos_r, _freopen_r, _rewind_r, freopen64, _freopen64_r,
        _funopen_r, and _fopencookie_r.
        * libc/include/reent.h:  Add function prototype for _stat64_r, align
        _execve_r prototype with POSIX definition for execve.
        * libc/reent/execr.c:  Align function prototype with POSIX definition.
        * libc/stdio/asniprintf.c:  Add #include "local.h".
        * libc/stdio/vasniprintf.c:  Ditto.
        * libc/stdio/fread.c:  Remove unused variable newcount.
        * libc/stdio/local.h:  Add function prototype for __sccl.
        * libc/stdio/open_memstream.c:  Remove unused variable flags.
        * libc/stdio/vfscanf.c:  Proper prototyping for ccfn, remove prototype
        for __sccl since now in local.h.
        * libc/string/memcpy.c:  Add #include <string.h> (for real and for
        traditional synopsis), remove extraneous stddef.h and limits.h.
        * libc/syscalls/sysclose.c:  Add #include <unistd.h>.
        * libc/syscalls/sysfork.c:  Ditto.
        * libc/syscalls/sysgetpid.c:  Ditto.
        * libc/syscalls/sysexecve.c:  Add #include <unistd.h>, align function
        prototype with POSIX definition.
        * libc/syscalls/sysfstat.c:  Add #include <sys/stat.h>.
        * libc/syscalls/sysgettod.c:  Correct sys/times.h to sys/time.h.
        * libc/syscalls/syskill.c:  Add #include <signal.h>.
        * libc/syscalls/syslink.c:  Add #include <unistd.h>, fix prototype.
        * libc/syscalls/sysunlink.c:  Ditto.
        * libc/syscalls/sysstat.c:  Add #include <sys/stat.h>, fix prototype.
        * libc/syscalls/syswait.c:  Add #include <sys/wait.h>, fix prototype.
2008-12-11 17:27:56 +00:00

109 lines
2.4 KiB
C

/* Copyright (C) 2007, 2008 Eric Blake
* Permission to use, copy, modify, and distribute this software
* is freely granted, provided that this notice is preserved.
*/
/* This code was derived from asprintf.c */
/* doc in siprintf.c */
#include <_ansi.h>
#include <reent.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>
#include <errno.h>
#include "local.h"
char *
_DEFUN(_asniprintf_r, (ptr, buf, lenp, fmt),
struct _reent *ptr _AND
char *buf _AND
size_t *lenp _AND
const char *fmt _DOTS)
{
int ret;
va_list ap;
FILE f;
size_t len = *lenp;
if (buf && len)
{
/* mark an existing buffer, but allow allocation of larger string */
f._flags = __SWR | __SSTR | __SOPT;
}
else
{
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
len = 0;
buf = NULL;
}
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t
for _size. */
if (len > INT_MAX)
{
ptr->_errno = EOVERFLOW;
return NULL;
}
f._bf._size = f._w = len;
f._file = -1; /* No file. */
va_start (ap, fmt);
ret = _svfiprintf_r (ptr, &f, fmt, ap);
va_end (ap);
if (ret < 0)
return NULL;
*lenp = ret;
*f._p = '\0';
return (char *) f._bf._base;
}
#ifndef _REENT_ONLY
char *
_DEFUN(asniprintf, (buf, lenp, fmt),
char *buf _AND
size_t *lenp _AND
const char *fmt _DOTS)
{
int ret;
va_list ap;
FILE f;
size_t len = *lenp;
struct _reent *ptr = _REENT;
if (buf && len)
{
/* mark an existing buffer, but allow allocation of larger string */
f._flags = __SWR | __SSTR | __SOPT;
}
else
{
/* mark a zero-length reallocatable buffer */
f._flags = __SWR | __SSTR | __SMBF;
len = 0;
buf = NULL;
}
f._bf._base = f._p = (unsigned char *) buf;
/* For now, inherit the 32-bit signed limit of FILE._bf._size.
FIXME - it would be nice to rewrite sys/reent.h to support size_t
for _size. */
if (len > INT_MAX)
{
ptr->_errno = EOVERFLOW;
return NULL;
}
f._bf._size = f._w = len;
f._file = -1; /* No file. */
va_start (ap, fmt);
ret = _svfiprintf_r (ptr, &f, fmt, ap);
va_end (ap);
if (ret < 0)
return NULL;
*lenp = ret;
*f._p = '\0';
return (char *) f._bf._base;
}
#endif /* ! _REENT_ONLY */