delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2013/12/27/17:36:51

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
X-Recipient: djgpp-workers AT delorie DOT com
Message-ID: <52BDF357.9030702@gmx.de>
Date: Fri, 27 Dec 2013 22:38:31 +0100
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
MIME-Version: 1.0
To: djgpp-workers AT delorie DOT com
Subject: Implementation of dprintf and vdprintf.
X-Provags-ID: V03:K0:hE9fdZMKC+64mCcVQP++xkn7g7p6aZjDUpRyI4fmQLVPgc6o3mg
gKtaSBEEc2LS0Coi8puxFPRxRi9DnQ1OqDt8AOvG1uTG8hAWRt7OoNzHRrk64B9XhSgUUU2
ofr6OLNGhbVbM+k2fwYuA7O+J6MzIlqhB/GNLhYUJuwmSkXEq243H5shRE3w+iYw7QHgZPF
uFX3lD/4bRIkXbjwvKQcw==
Reply-To: djgpp-workers AT delorie DOT com

The patch below provides an implementation of dprintf and vdprintf together
with a small test program.  AFAIK both are POSIX.1-2008.
As usual, suggestions, objections, comments are welcome.

Regards,
Juan M. Guerrero


2013-12-23  Juan Manuel Guerrero  <juan DOT guerrero AT gmx DOT de>


     * djgpp/include/stdio.h:  Prototypes for dprintf and vdprintf added.

     * djgpp/src/libc/posix/stdio/dprintf.c:  Implementation of dprintf.

     * djgpp/src/libc/posix/stdio/dprintf.txh:  Documentation of dprintf.

     * djgpp/src/libc/posix/stdio/vdprintf.c:  Implementation of vdprintf.

     * djgpp/src/libc/posix/stdio/vdprintf.txh:  Documentation of vdprintf.

     * djgpp/src/libc/posix/stdio/makefile:  dprintf and vdprintf added to target list.

     * djgpp/src/docs/kb/wc204.txi:  Info about dprintf and vdprintf added.

     * djgpp/tests/libc/posix/stdio/dprintf.c: Check for dprintf.

     * djgpp/tests/libc/posix/stdio/makefile:  Check for dprintf added.





diff -aprNU5 djgpp.orig/include/stdio.h djgpp/include/stdio.h
--- djgpp.orig/include/stdio.h    2012-09-23 07:49:12 +0000
+++ djgpp/include/stdio.h    2013-12-23 21:49:16 +0000
@@ -136,16 +136,18 @@ int    vsscanf(const char *_s, const char *

  #define L_ctermid 20
  #define L_cusrid
  /* #define STREAM_MAX    20 - DOS can change this */

+int    dprintf(int _fd, const char *_format, ...);
  int    fileno(FILE *_stream);
  FILE *    fdopen(int _fildes, const char *_type);
  int    mkstemp(char *_template);
  int    pclose(FILE *_pf);
  FILE *    popen(const char *_command, const char *_mode);
  char *    tempnam(const char *_dir, const char *_prefix);
+int    vdprintf(int _fd, const char *_format, va_list _ap);

  #ifndef _POSIX_SOURCE

  extern FILE __dj_stdprn, __dj_stdaux;
  #define stdprn    (&__dj_stdprn)
diff -aprNU5 djgpp.orig/src/docs/kb/wc204.txi djgpp/src/docs/kb/wc204.txi
--- djgpp.orig/src/docs/kb/wc204.txi    2013-12-08 03:08:04 +0000
+++ djgpp/src/docs/kb/wc204.txi    2013-12-23 21:49:16 +0000
@@ -1366,5 +1366,11 @@ A bug was fixed in @code{memalign} and @
  with linux and other @acronym{POSIX} compliant systems.  This has been changed
  so that the argument order now is the same that on @acronym{POSIX} compliant
  systems.  @code{valloc}, that calls @code{memalign}, has been adjusted accordingly.
  The @code{memalign} declaration has been moved from AT file{stdlib.h} to
  @file{malloc.h} to comply with the @acronym{C99} standard.
+
+@cindex @acronym{POSIX} compliance, @code{stdio.h}
+@findex dprintf AT r{ added to the library}
+@findex vdprintf AT r{ added to the library}
+The functions @code{dprintf} and @code{vdprintf} were added to comply with
+the @acronym{POSIX} 1003.1-2008 standard.
diff -aprNU5 djgpp.orig/src/libc/posix/stdio/dprintf.c djgpp/src/libc/posix/stdio/dprintf.c
--- djgpp.orig/src/libc/posix/stdio/dprintf.c    1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/posix/stdio/dprintf.c    2013-12-23 21:49:16 +0000
@@ -0,0 +1,17 @@
+/* Copyright (C) 2013 DJ Delorie, see COPYING.DJ for details */
+#include <stdio.h>
+#include <stdarg.h>
+
+int
+dprintf(int fd, const char *fmt, ...)
+{
+  va_list arg_list;
+  int written;
+
+
+  va_start(arg_list, fmt);
+  written = vdprintf(fd, fmt, arg_list);
+  va_end(arg_list);
+
+  return written;
+}
diff -aprNU5 djgpp.orig/src/libc/posix/stdio/dprintf.txh djgpp/src/libc/posix/stdio/dprintf.txh
--- djgpp.orig/src/libc/posix/stdio/dprintf.txh    1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/posix/stdio/dprintf.txh    2013-12-23 21:49:16 +0000
@@ -0,0 +1,25 @@
+@node dprintf, stdio
+@findex dprintf
+@subheading Syntax
+
+@example
+#include <stdio.h>
+
+int dprintf(int file_desc, const char *format, ...);
+@end example
+
+@subheading Description
+
+The function @code{dprintf} is an exact analog of @code{fprintf} (@pxref{fprintf}),
+except that it output to a file descriptor @var{file_desc}
+instead of to a stdio stream.
+
+@subheading Return Value
+
+The number of characters written is returned; otherwise EOF
+is returned to flag encoding or buffer space errors.
+
+
+@subheading Portability
+
+@portability !ansi, posix
diff -aprNU5 djgpp.orig/src/libc/posix/stdio/makefile djgpp/src/libc/posix/stdio/makefile
--- djgpp.orig/src/libc/posix/stdio/makefile    1995-03-24 12:30:16 +0000
+++ djgpp/src/libc/posix/stdio/makefile    2013-12-23 21:49:16 +0000
@@ -1,8 +1,11 @@
+# Copyright (C) 2013 DJ Delorie, see COPYING.DJ for details
  # Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details
  TOP=../..

+SRC += dprintf.c
  SRC += fdopen.c
  SRC += fileno.c
  SRC += popen.c
+SRC += vdprintf.c

  include $(TOP)/../makefile.inc
diff -aprNU5 djgpp.orig/src/libc/posix/stdio/vdprintf.c djgpp/src/libc/posix/stdio/vdprintf.c
--- djgpp.orig/src/libc/posix/stdio/vdprintf.c    1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/posix/stdio/vdprintf.c    2013-12-23 21:49:16 +0000
@@ -0,0 +1,21 @@
+/* Copyright (C) 2013 DJ Delorie, see COPYING.DJ for details */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int
+vdprintf(int fd, const char *fmt, va_list ap)
+{
+  char *string;
+  int written;
+
+
+  written = vasprintf(&string, fmt, ap);
+  if (string)
+  {
+    written = write(fd, string, written);
+    free(string);
+  }
+
+  return written;
+}
diff -aprNU5 djgpp.orig/src/libc/posix/stdio/vdprintf.txh djgpp/src/libc/posix/stdio/vdprintf.txh
--- djgpp.orig/src/libc/posix/stdio/vdprintf.txh    1970-01-01 00:00:00 +0000
+++ djgpp/src/libc/posix/stdio/vdprintf.txh    2013-12-23 21:49:16 +0000
@@ -0,0 +1,25 @@
+@node vdprintf, stdio
+@findex vdprintf
+@subheading Syntax
+
+@example
+#include <stdio.h>
+
+int vdprintf(int file_desc, const char *format, va_list arguments);
+@end example
+
+@subheading Description
+
+The function @code{vdprintf} is an exact analog of @code{vfprintf} (@pxref{vfprintf}),
+except that it output to a file descriptor @var{file_desc}
+instead of to a stdio stream.
+
+@subheading Return Value
+
+The number of characters written is returned; otherwise EOF
+is returned to flag encoding or buffer space errors.
+
+
+@subheading Portability
+
+@portability !ansi, posix
diff -aprNU5 djgpp.orig/tests/libc/posix/stdio/dprintf.c djgpp/tests/libc/posix/stdio/dprintf.c
--- djgpp.orig/tests/libc/posix/stdio/dprintf.c    1970-01-01 00:00:00 +0000
+++ djgpp/tests/libc/posix/stdio/dprintf.c    2013-12-23 22:54:20 +0000
@@ -0,0 +1,53 @@
+#include <stdio.h>
+
+int
+main(void)
+{
+  FILE *f;
+  char text_written[] = "A_test_text.";
+  int failure = 0;
+
+
+  f = fopen("file.txt", "w");
+  if (f)
+  {
+    int fd = fileno(f);
+    int written = dprintf(fd, "%s", text_written);
+
+    if (written + 1== sizeof text_written)
+    {
+      fclose(f);
+      f = fopen("file.txt", "r");
+
+      if (f)
+      {
+        char text_read[sizeof text_written + 1];
+        int read = fscanf(f, "%s", text_read);
+
+        if (read)
+        {
+          unsigned int i;
+
+          for (i = 0; i < sizeof text_written; i++)
+            if (text_written[i] != text_read[i])
+            {
+              failure++;
+              break;
+            }
+        }
+        else
+          failure++;
+      }
+      else
+        failure++;
+    }
+    else
+      failure++;
+  }
+  else
+    failure++;
+
+  printf("dprintf check %s\n", failure ? "failed." : "was successful.");
+
+  return failure;
+}
diff -aprNU5 djgpp.orig/tests/libc/posix/stdio/makefile djgpp/tests/libc/posix/stdio/makefile
--- djgpp.orig/tests/libc/posix/stdio/makefile    1970-01-01 00:00:00 +0000
+++ djgpp/tests/libc/posix/stdio/makefile    2013-12-23 21:49:16 +0000
@@ -0,0 +1,5 @@
+TOP=../..
+
+SRC += dprintf.c
+
+include $(TOP)/../makefile.inc

- Raw text -


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