Subversion Repositories Kolibri OS

Rev

Rev 4921 | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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. /* This code was copied from sprintf.c */
  18. /* doc in sprintf.c */
  19.  
  20. #include <_ansi.h>
  21. #include <reent.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include <limits.h>
  25. #include "local.h"
  26.  
  27. int
  28. _DEFUN(_asprintf_r, (ptr, strp, fmt),
  29.        struct _reent *ptr _AND
  30.        char **__restrict strp        _AND
  31.        const char *__restrict fmt _DOTS)
  32. {
  33.   int ret;
  34.   va_list ap;
  35.   FILE f;
  36.  
  37.   /* mark a zero-length reallocatable buffer */
  38.   f._flags = __SWR | __SSTR | __SMBF;
  39.   f._bf._base = f._p = NULL;
  40.   f._bf._size = f._w = 0;
  41.   f._file = -1;  /* No file. */
  42.   va_start (ap, fmt);
  43.   ret = _svfprintf_r (ptr, &f, fmt, ap);
  44.   va_end (ap);
  45.   if (ret >= 0)
  46.     {
  47.       *f._p = 0;
  48.       *strp = (char *) f._bf._base;
  49.     }
  50.   return (ret);
  51. }
  52.  
  53. #ifdef _NANO_FORMATTED_IO
  54. int
  55. _EXFUN(_asiprintf_r, (struct _reent *, char **, const char *, ...)
  56.        _ATTRIBUTE ((__alias__("_asprintf_r"))));
  57. #endif
  58.  
  59. #ifndef _REENT_ONLY
  60.  
  61. int
  62. _DEFUN(asprintf, (strp, fmt),
  63.        char **__restrict strp _AND
  64.        const char *__restrict fmt _DOTS)
  65. {
  66.   int ret;
  67.   va_list ap;
  68.   FILE f;
  69.  
  70.   /* mark a zero-length reallocatable buffer */
  71.   f._flags = __SWR | __SSTR | __SMBF;
  72.   f._bf._base = f._p = NULL;
  73.   f._bf._size = f._w = 0;
  74.   f._file = -1;  /* No file. */
  75.   va_start (ap, fmt);
  76.   ret = _svfprintf_r (_REENT, &f, fmt, ap);
  77.   va_end (ap);
  78.   if (ret >= 0)
  79.     {
  80.       *f._p = 0;
  81.       *strp = (char *) f._bf._base;
  82.     }
  83.   return (ret);
  84. }
  85.  
  86. #ifdef _NANO_FORMATTED_IO
  87. int
  88. _EXFUN(asiprintf, (char **, const char *, ...)
  89.        _ATTRIBUTE ((__alias__("asprintf"))));
  90. #endif
  91. #endif /* ! _REENT_ONLY */
  92.