Contiki 2.6

_SP_snprintf.c

00001 /*
00002  * Copyright (c) 1990 The Regents of the University of California.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms are permitted
00006  * provided that the above copyright notice and this paragraph are
00007  * duplicated in all such forms and that any documentation,
00008  * advertising materials, and other materials related to such
00009  * distribution and use acknowledge that the software was developed
00010  * by the University of California, Berkeley.  The name of the
00011  * University may not be used to endorse or promote products derived
00012  * from this software without specific prior written permission.
00013  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
00014  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
00015  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00016  */
00017 /* doc in _SP_sprintf.c */
00018 /* This code created by modifying _SP_sprintf.c so copyright inherited. */
00019 
00020 #include <stdio.h>
00021 #ifdef _HAVE_STDC
00022 #include <stdarg.h>
00023 #else
00024 #include <varargs.h>
00025 #endif
00026 #include <limits.h>
00027 #include <errno.h>
00028 #include <_ansi.h>
00029 
00030 #ifndef _SMALL_PRINTF
00031                 #include "local.h"
00032 #else
00033         #ifdef INTEGER_ONLY
00034                 #define _vfprintf_r  _vfiprintf_r 
00035         #endif
00036 #endif
00037 
00038 
00039 #ifndef _SMALL_PRINTF
00040         int
00041         #ifdef _HAVE_STDC
00042         _DEFUN (_snprintf_r, (ptr, str, size, fmt), struct _reent *ptr _AND char *str _AND size_t size _AND _CONST char *fmt _DOTS)
00043         #else
00044         _snprintf_r (ptr, str, size, fmt, va_alist)
00045              struct _reent *ptr;
00046              char *str;
00047                  size_t size;
00048              _CONST char *fmt;
00049              va_dcl
00050         #endif
00051         {
00052           int ret;
00053           va_list ap;
00054           FILE f;
00055           
00056           if (size > INT_MAX)
00057                 {
00058                  ptr->_errno = EOVERFLOW;
00059                  return EOF;
00060                 }
00061         
00062           f._flags = __SWR | __SSTR;
00063           f._bf._base = f._p = (unsigned char *) str;
00064           f._bf._size = f._w = (size > 0 ? size - 1 : 0);
00065           f._file = -1;  /* No file. */
00066         #ifdef _HAVE_STDC
00067           va_start (ap, fmt);
00068         #else
00069           va_start (ap);
00070         #endif
00071           ret = _vfprintf_r (ptr, &f, fmt, ap);
00072           va_end (ap);
00073           if (ret < EOF)
00074                 ptr->_errno = EOVERFLOW;
00075           if (size > 0)
00076                 *f._p = 0;
00077           return (ret);
00078         }
00079 #endif
00080 
00081 #ifndef _REENT_ONLY
00082 int
00083 #ifdef _HAVE_STDC
00084 _DEFUN (snprintf, (str, size, fmt), char *str _AND size_t size _AND _CONST char *fmt _DOTS)
00085 #else
00086 snprintf (str, size, fmt, va_alist)
00087      char *str;
00088          size_t size;
00089      _CONST char *fmt;
00090      va_dcl
00091 #endif
00092 {
00093   int ret;
00094   va_list ap;
00095   FILE f;
00096 
00097   struct _reent *ptr = _REENT;
00098 
00099   if (size > INT_MAX)
00100     {
00101       ptr->_errno = EOVERFLOW;
00102       return EOF;
00103     }
00104 
00105   f._flags = __SWR | __SSTR;
00106   f._bf._base = f._p = (unsigned char *) str;
00107   f._bf._size = f._w = (size > 0 ? size - 1 : 0);
00108   f._file = -1;  /* No file. */
00109 #ifdef _HAVE_STDC
00110   va_start (ap, fmt);
00111 #else
00112   va_start (ap);
00113 #endif
00114   ret = _vfprintf_r (ptr, &f, fmt, ap);
00115   va_end (ap);
00116   if (ret < EOF)
00117     ptr->_errno = EOVERFLOW;
00118   if (size > 0)
00119         *f._p = 0;
00120   return (ret);
00121 }
00122 #endif
00123 
00124