Contiki 3.x
sp-snprintf.c
1 /**
2  * \addtogroup stm32w-cpu
3  *
4  * @{
5  */
6 
7 /*
8  * Copyright (c) 1990 The Regents of the University of California.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms are permitted
12  * provided that the above copyright notice and this paragraph are
13  * duplicated in all such forms and that any documentation,
14  * advertising materials, and other materials related to such
15  * distribution and use acknowledge that the software was developed
16  * by the University of California, Berkeley. The name of the
17  * University may not be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
20  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22  */
23 
24 /* doc in sp-sprintf.c */
25 /* This code created by modifying sp-sprintf.c so copyright inherited. */
26 
27 #include <stdio.h>
28 #ifdef _HAVE_STDC
29 #include <stdarg.h>
30 #else
31 #include <varargs.h>
32 #endif
33 #include <limits.h>
34 #include <errno.h>
35 #include <_ansi.h>
36 
37 #ifndef _SMALL_PRINTF
38  #include "local.h"
39 #else
40  #ifdef INTEGER_ONLY
41  #define _vfprintf_r _vfiprintf_r
42  #endif
43 #endif
44 
45 
46 #ifndef _SMALL_PRINTF
47  int
48  #ifdef _HAVE_STDC
49  _DEFUN (_snprintf_r, (ptr, str, size, fmt), struct _reent *ptr _AND char *str _AND size_t size _AND _CONST char *fmt _DOTS)
50  #else
51  _snprintf_r (ptr, str, size, fmt, va_alist)
52  struct _reent *ptr;
53  char *str;
54  size_t size;
55  _CONST char *fmt;
56  va_dcl
57  #endif
58  {
59  int ret;
60  va_list ap;
61  FILE f;
62 
63  if (size > INT_MAX)
64  {
65  ptr->_errno = EOVERFLOW;
66  return EOF;
67  }
68 
69  f._flags = __SWR | __SSTR;
70  f._bf._base = f._p = (unsigned char *) str;
71  f._bf._size = f._w = (size > 0 ? size - 1 : 0);
72  f._file = -1; /* No file. */
73  #ifdef _HAVE_STDC
74  va_start (ap, fmt);
75  #else
76  va_start (ap);
77  #endif
78  ret = _vfprintf_r (ptr, &f, fmt, ap);
79  va_end (ap);
80  if (ret < EOF)
81  ptr->_errno = EOVERFLOW;
82  if (size > 0)
83  *f._p = 0;
84  return (ret);
85  }
86 #endif
87 
88 #ifndef _REENT_ONLY
89 int
90 #ifdef _HAVE_STDC
91 _DEFUN (snprintf, (str, size, fmt), char *str _AND size_t size _AND _CONST char *fmt _DOTS)
92 #else
93 snprintf (str, size, fmt, va_alist)
94  char *str;
95  size_t size;
96  _CONST char *fmt;
97  va_dcl
98 #endif
99 {
100  int ret;
101  va_list ap;
102  FILE f;
103 
104  struct _reent *ptr = _REENT;
105 
106  if (size > INT_MAX)
107  {
108  ptr->_errno = EOVERFLOW;
109  return EOF;
110  }
111 
112  f._flags = __SWR | __SSTR;
113  f._bf._base = f._p = (unsigned char *) str;
114  f._bf._size = f._w = (size > 0 ? size - 1 : 0);
115  f._file = -1; /* No file. */
116 #ifdef _HAVE_STDC
117  va_start (ap, fmt);
118 #else
119  va_start (ap);
120 #endif
121  ret = _vfprintf_r (ptr, &f, fmt, ap);
122  va_end (ap);
123  if (ret < EOF)
124  ptr->_errno = EOVERFLOW;
125  if (size > 0)
126  *f._p = 0;
127  return (ret);
128 }
129 #endif
130 
131 /** @} */