www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2008/03/03/10:57:57

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
X-Recipient: djgpp-workers AT delorie DOT com
X-Authenticated: #27081556
X-Provags-ID: V01U2FsdGVkX19DYr1n72VD2sBMSajLo66VoWGjAT7SfHSkXHRo5a
iJzua+4c80Tq45
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
To: djgpp-workers AT delorie DOT com
Subject: asprintf and vasprintf implementation
Date: Mon, 3 Mar 2008 16:57:57 +0100
User-Agent: KMail/1.9.5
MIME-Version: 1.0
Message-Id: <200803031657.57524.juan.guerrero@gmx.de>
X-Y-GMX-Trusted: 0
Reply-To: djgpp-workers AT delorie DOT com

I have implemented asprintf() and vasprintf() that I have missed last time that
I tried to compile CVS version of texinfo.  Because they are BSD/GNU extensions
I have placed theier declarations outside the ansi and posix part of stdio.h.
At least that was my intention.  I have assumed that the .c files should be
stored in the src/libc/compat/stdio directory but I have no preferences.

Suggestions, objections, comments are welcome.


Regards,
Juan M. Guerrero




2008-03-03  Juan Manuell Guerrero  <juan DOT guerrero AT gmx DOT de>

	* include/stdio.h: Declarartions for asprintf and vasprintf added.

	* src/libc/compat/stdio/asprintf.c: BSD/GNU compatibility function.

	* src/libc/compat/stdio/asprintf.txh: Info about asprintf.

	* src/libc/compat/stdio/vasprintf.c: BSD/GNU compatibility function.

	* src/libc/compat/stdio/vasprintf.txh: Info about vasprintf.

	* src/docs/kb/wc204.txi: Info about asprintf and vasprintf added.




diff -aprNU3 djgpp.orig/include/stdio.h djgpp/include/stdio.h
--- djgpp.orig/include/stdio.h	2007-12-11 07:48:42 +0000
+++ djgpp/include/stdio.h	2008-03-03 16:36:08 +0000
@@ -164,6 +164,8 @@ int	putw(int _v, FILE *_f);
 void	setbuffer(FILE *_f, void *_buf, int _size);
 void	setlinebuf(FILE *_f);
 int	_rename(const char *_old, const char *_new);	/* Simple (no directory) */
+int	asprintf(char **_sp, const char *_format, ...);
+int	vasprintf(char **_sp, const char *_format, va_list _ap);
 
 #ifndef _OFF_T
 __DJ_off_t
diff -aprNU3 djgpp.orig/src/docs/kb/wc204.txi djgpp/src/docs/kb/wc204.txi
--- djgpp.orig/src/docs/kb/wc204.txi	2005-05-11 20:06:08 +0000
+++ djgpp/src/docs/kb/wc204.txi	2008-03-03 16:37:52 +0000
@@ -1094,3 +1094,8 @@ formats for @code{"%x"} and @code{"%X"} 
 
 @pindex djasm AT r{, cr4 register}
 @code{djasm} recognises the fourth control register, @code{cr4}.
+
+@findex asprintf
+@findex vasprintf
+New BSD/GNU compatibility functions @code{asprintf} and @code{vasprintf} added.
+
diff -aprNU3 djgpp.orig/src/libc/compat/stdio/asprintf.c djgpp/src/libc/compat/stdio/asprintf.c
--- djgpp.orig/src/libc/compat/stdio/asprintf.c	1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/compat/stdio/asprintf.c	2008-03-03 16:36:08 +0000
@@ -0,0 +1,42 @@
+/* Copyright (C) 2008 DJ Delorie, see COPYING.DJ for details */
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <libc/file.h>
+
+int
+asprintf(char **strp, const char *fmt, ...)
+{
+  va_list args;
+  FILE *_dev_null, _strbuf;
+  int len;
+
+  *strp = NULL;
+  _dev_null = fopen("/dev/null", "w");
+  if (_dev_null == NULL)
+    return EOF;
+
+  va_start(args, fmt);
+  len = _doprnt(fmt, args, _dev_null);
+  fclose(_dev_null);
+  if (len != EOF)
+  {
+    *strp = malloc(++len);
+    if (*strp)
+    {
+      __stropenw(&_strbuf, *strp, len);
+      len = _doprnt(fmt, args, &_strbuf);
+      __strclosew(&_strbuf);
+      if (len == EOF)
+      {
+        free(*strp);
+        *strp = NULL;
+      }
+    }
+    else
+      len = EOF;
+  }
+  va_end(args);
+
+  return len;
+}
diff -aprNU3 djgpp.orig/src/libc/compat/stdio/asprintf.txh djgpp/src/libc/compat/stdio/asprintf.txh
--- djgpp.orig/src/libc/compat/stdio/asprintf.txh	1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/compat/stdio/asprintf.txh	2008-03-03 16:36:08 +0000
@@ -0,0 +1,40 @@
+@node asprintf, stdio
+@findex asprintf
+@subheading Syntax
+
+@example
+#include <stdio.h>
+
+int vsnprintf (char **@var{bufferp}, const char *@var{format},
+               @dots{});
+@end example
+
+@subheading Description
+
+Sends formatted output from the arguments (@dots{}) including
+the terminating null byte to the allocated buffer and returns
+a pointer to it via the pointer *@var{bufferp}.   This memory
+must be returned to the heap with @code{free} (@pxref{free}). 
+This function is analog of @code{sprintf()} (@pxref{sprintf}).
+
+@subheading Return Value
+
+The number of characters that would have been written (excluding
+the terminating null byte) is returned; otherwise -1 is returned
+to flag encoding or buffer space errors and the pointer
+*@var{bufferp} is set to @code{NULL}.
+
+
+@subheading Portability
+
+@portability !ansi, !posix
+
+@subheading Example
+
+@example
+char *strbuf;
+int strlen;
+long double pi = 3.1415926535897932384626433832795;
+
+strlen = asprintf(&strbuf, "Pi = %.15Lf\n", pi);
+@end example
diff -aprNU3 djgpp.orig/src/libc/compat/stdio/makefile djgpp/src/libc/compat/stdio/makefile
--- djgpp.orig/src/libc/compat/stdio/makefile	2007-12-11 07:48:42 +0000
+++ djgpp/src/libc/compat/stdio/makefile	2008-03-03 16:36:08 +0000
@@ -2,6 +2,7 @@
 # Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details
 TOP=../..
 
+SRC += asprintf.c
 SRC += fseeko.c
 SRC += fseeko64.c
 SRC += ftello.c
@@ -9,8 +10,9 @@ SRC += ftello64.c
 SRC += mkstemp.c
 SRC += mktemp.c
 SRC += tempnam.c
-SRC += vscanf.c
+SRC += vasprintf.c
 SRC += vfscanf.S
+SRC += vscanf.c
 SRC += vsscanf.c
 
 include $(TOP)/../makefile.inc
diff -aprNU3 djgpp.orig/src/libc/compat/stdio/vasprintf.c djgpp/src/libc/compat/stdio/vasprintf.c
--- djgpp.orig/src/libc/compat/stdio/vasprintf.c	1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/compat/stdio/vasprintf.c	2008-03-03 16:36:08 +0000
@@ -0,0 +1,39 @@
+/* Copyright (C) 2008 DJ Delorie, see COPYING.DJ for details */
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <libc/file.h>
+
+int
+vasprintf(char **strp, const char *fmt, va_list argsp)
+{
+  FILE *_dev_null, _strbuf;
+  int len;
+
+  *strp = NULL;
+  _dev_null = fopen("/dev/null", "w");
+  if (_dev_null == NULL)
+    return EOF;
+
+  len = _doprnt(fmt, argsp, _dev_null);
+  fclose(_dev_null);
+  if (len != EOF)
+  {
+    *strp = malloc(++len);
+    if (*strp)
+    {
+      __stropenw(&_strbuf, *strp, len);
+      len = _doprnt(fmt, argsp, &_strbuf);
+      __strclosew(&_strbuf);
+      if (len == EOF)
+      {
+        free(*strp);
+        *strp = NULL;
+      }
+    }
+    else
+      len = EOF;
+  }
+
+  return len;
+}
diff -aprNU3 djgpp.orig/src/libc/compat/stdio/vasprintf.txh djgpp/src/libc/compat/stdio/vasprintf.txh
--- djgpp.orig/src/libc/compat/stdio/vasprintf.txh	1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/compat/stdio/vasprintf.txh	2008-03-03 16:36:08 +0000
@@ -0,0 +1,32 @@
+@node vasprintf, stdio
+@findex vasprintf
+@subheading Syntax
+
+@example
+#include <stdio.h>
+#include <stdarg.h>
+
+int vasprintf (char **@var{bufferp}, const char *@var{format},
+               va_list @var{ap});
+@end example
+
+@subheading Description
+
+Sends formatted output from the arguments (@var{ap}) including
+the terminating null byte to the allocated buffer and returns
+a pointer to it via the pointer *@var{bufferp}.   This memory
+must be returned to the heap with @code{free} (@pxref{free}). 
+This function is analog of @code{vsprintf()} (@pxref{vsprintf}).
+
+@subheading Return Value
+
+The number of characters that would have been written (excluding
+the terminating null byte) is returned; otherwise -1 is returned
+to flag encoding or buffer space errors and the pointer
+*@var{bufferp} is set to @code{NULL}.
+
+
+@subheading Portability
+
+@portability !ansi, !posix
+

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019