Contiki 2.5
_SP_snprintf.c
1 /*
2  * Copyright (c) 1990 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 _SP_sprintf.c */
18 /* This code created by modifying _SP_sprintf.c so copyright inherited. */
19 
20 #include <stdio.h>
21 #ifdef _HAVE_STDC
22 #include <stdarg.h>
23 #else
24 #include <varargs.h>
25 #endif
26 #include <limits.h>
27 #include <errno.h>
28 #include <_ansi.h>
29 
30 #ifndef _SMALL_PRINTF
31  #include "local.h"
32 #else
33  #ifdef INTEGER_ONLY
34  #define _vfprintf_r _vfiprintf_r
35  #endif
36 #endif
37 
38 
39 #ifndef _SMALL_PRINTF
40  int
41  #ifdef _HAVE_STDC
42  _DEFUN (_snprintf_r, (ptr, str, size, fmt), struct _reent *ptr _AND char *str _AND size_t size _AND _CONST char *fmt _DOTS)
43  #else
44  _snprintf_r (ptr, str, size, fmt, va_alist)
45  struct _reent *ptr;
46  char *str;
47  size_t size;
48  _CONST char *fmt;
49  va_dcl
50  #endif
51  {
52  int ret;
53  va_list ap;
54  FILE f;
55 
56  if (size > INT_MAX)
57  {
58  ptr->_errno = EOVERFLOW;
59  return EOF;
60  }
61 
62  f._flags = __SWR | __SSTR;
63  f._bf._base = f._p = (unsigned char *) str;
64  f._bf._size = f._w = (size > 0 ? size - 1 : 0);
65  f._file = -1; /* No file. */
66  #ifdef _HAVE_STDC
67  va_start (ap, fmt);
68  #else
69  va_start (ap);
70  #endif
71  ret = _vfprintf_r (ptr, &f, fmt, ap);
72  va_end (ap);
73  if (ret < EOF)
74  ptr->_errno = EOVERFLOW;
75  if (size > 0)
76  *f._p = 0;
77  return (ret);
78  }
79 #endif
80 
81 #ifndef _REENT_ONLY
82 int
83 #ifdef _HAVE_STDC
84 _DEFUN (snprintf, (str, size, fmt), char *str _AND size_t size _AND _CONST char *fmt _DOTS)
85 #else
86 snprintf (str, size, fmt, va_alist)
87  char *str;
88  size_t size;
89  _CONST char *fmt;
90  va_dcl
91 #endif
92 {
93  int ret;
94  va_list ap;
95  FILE f;
96 
97  struct _reent *ptr = _REENT;
98 
99  if (size > INT_MAX)
100  {
101  ptr->_errno = EOVERFLOW;
102  return EOF;
103  }
104 
105  f._flags = __SWR | __SSTR;
106  f._bf._base = f._p = (unsigned char *) str;
107  f._bf._size = f._w = (size > 0 ? size - 1 : 0);
108  f._file = -1; /* No file. */
109 #ifdef _HAVE_STDC
110  va_start (ap, fmt);
111 #else
112  va_start (ap);
113 #endif
114  ret = _vfprintf_r (ptr, &f, fmt, ap);
115  va_end (ap);
116  if (ret < EOF)
117  ptr->_errno = EOVERFLOW;
118  if (size > 0)
119  *f._p = 0;
120  return (ret);
121 }
122 #endif
123 
124