source: trunk/libs/newlib/src/newlib/libc/stdio/snprintf.c @ 567

Last change on this file since 567 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 2.5 KB
Line 
1/*
2 * Copyright (c) 1990, 2007 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley.  The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 */
17/* doc in sprintf.c */
18/* This code created by modifying sprintf.c so copyright inherited. */
19
20#include <_ansi.h>
21#include <reent.h>
22#include <stdio.h>
23#include <stdarg.h>
24#include <limits.h>
25#include <errno.h>
26#include "local.h"
27
28int
29_snprintf_r (struct _reent *ptr,
30       char *__restrict str,
31       size_t size,
32       const char *__restrict fmt, ...)
33{
34  int ret;
35  va_list ap;
36  FILE f;
37
38  if (size > INT_MAX)
39    {
40      ptr->_errno = EOVERFLOW;
41      return EOF;
42    }
43  f._flags = __SWR | __SSTR;
44  f._bf._base = f._p = (unsigned char *) str;
45  f._bf._size = f._w = (size > 0 ? size - 1 : 0);
46  f._file = -1;  /* No file. */
47  va_start (ap, fmt);
48  ret = _svfprintf_r (ptr, &f, fmt, ap);
49  va_end (ap);
50  if (ret < EOF)
51    ptr->_errno = EOVERFLOW;
52  if (size > 0)
53    *f._p = 0;
54  return (ret);
55}
56
57#ifdef _NANO_FORMATTED_IO
58int
59_sniprintf_r (struct _reent *, char *, size_t, const char *, ...)
60       _ATTRIBUTE ((__alias__("_snprintf_r")));
61#endif
62
63#ifndef _REENT_ONLY
64
65int
66snprintf (char *__restrict str,
67       size_t size,
68       const char *__restrict fmt, ...)
69{
70  int ret;
71  va_list ap;
72  FILE f;
73  struct _reent *ptr = _REENT;
74
75  if (size > INT_MAX)
76    {
77      ptr->_errno = EOVERFLOW;
78      return EOF;
79    }
80  f._flags = __SWR | __SSTR;
81  f._bf._base = f._p = (unsigned char *) str;
82  f._bf._size = f._w = (size > 0 ? size - 1 : 0);
83  f._file = -1;  /* No file. */
84  va_start (ap, fmt);
85  ret = _svfprintf_r (ptr, &f, fmt, ap);
86  va_end (ap);
87  if (ret < EOF)
88    ptr->_errno = EOVERFLOW;
89  if (size > 0)
90    *f._p = 0;
91  return (ret);
92}
93
94#ifdef _NANO_FORMATTED_IO
95int
96sniprintf (char *, size_t, const char *, ...)
97       _ATTRIBUTE ((__alias__("snprintf")));
98#endif
99#endif
Note: See TracBrowser for help on using the repository browser.