delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2008/03/04/08:32:13

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: V01U2FsdGVkX186NjA2CxA6wtmYxAhnWFmqDKlq9c1tyXPSyCLLhE
OrBq/f5VZlTXpG
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
To: djgpp-workers AT delorie DOT com
Subject: Re: asprintf and vasprintf implementation
Date: Tue, 4 Mar 2008 14:30:30 +0100
User-Agent: KMail/1.9.5
References: <200803040848 DOT m248mCia026383 AT brother DOT ludd DOT ltu DOT se>
In-Reply-To: <200803040848.m248mCia026383@brother.ludd.ltu.se>
MIME-Version: 1.0
Message-Id: <200803041430.30362.juan.guerrero@gmx.de>
X-Y-GMX-Trusted: 0
X-MIME-Autoconverted: from quoted-printable to 8bit by delorie.com id m24DV1J6018256
Reply-To: djgpp-workers AT delorie DOT com

Am Dienstag, 4. März 2008 09:48 schrieben Sie:
> Just a minor improvement.
[snip]
> Add "printf("Length = %d.\n", strlen);
>      if( NULL != strbuf )
>      {
>	  printf("strbuf = '%s'.\n", strbuf);
>	  free( strbuf );
>      }
>      else
>      {
>      	  printf("strbuf = NULL.\n");
>      }" or similar code.

Thanks, a little bit more improved examples have been added.

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-04 14:11:50 +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-04 14:11:50 +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-04 14:11:50 +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-04 14:11:50 +0000
@@ -0,0 +1,52 @@
+@node asprintf, stdio
+@findex asprintf
+@subheading Syntax
+
+@example
+#include <stdio.h>
+
+int asprintf (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 first parameter *@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 EOF 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;  /*  Pointer to the allocated buffer by asprintf.  */
+int strlen;
+long double pi = 3.1415926535897932384626433832795;
+
+strlen = asprintf(&strbuf, "Pi = %.15Lf\n", pi);
+
+if (EOF == strlen)
+  printf("error: asprintf failed.\n");
+else
+{
+  /*
+   *  Your code using the allocated buffer by asprintf.
+   */
+  do_something(strbuf, strlen);
+
+  free(strbuf);  /*  You must release the memory allocated by asprintf.  */
+}
+@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-04 14:11:50 +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-04 14:11:50 +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-04 14:11:50 +0000
@@ -0,0 +1,54 @@
+@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 in (@var{ap}) including
+the terminating null byte to the allocated buffer and returns
+a pointer to it via the first parameter *@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 EOF 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;  /*  Pointer to the allocated buffer by vasprintf.  */
+int strlen;
+
+strlen = vasprintf(&strbuf, "arg[0] = %s\n"
+                            "arg[1] = %s\n"
+                            "arg[2] = %s\n", arg_list);
+
+if (EOF == strlen)
+  printf("error: vasprintf failed.\n");
+else
+{
+  /*
+   *  Your code using the allocated buffer by vasprintf.
+   */
+  do_something(strbuf, strlen);
+
+  free(strbuf);  /*  You must release the memory allocated by vasprintf.  */
+}
+@end example

- Raw text -


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