public inbox for ecos-patches@sourceware.org
 help / color / mirror / Atom feed
* _vprintf() return code fix and diag_vsnprintf() addition
@ 2008-10-21  2:33 Berend Ozceri
  2008-11-03 14:51 ` Andrew Lunn
  0 siblings, 1 reply; 2+ messages in thread
From: Berend Ozceri @ 2008-10-21  2:33 UTC (permalink / raw)
  To: ecos-patches

[-- Attachment #1: Type: text/plain, Size: 209 bytes --]

This fixes a return value error in _vprintf() (if _sputc() clips the  
output due to the maximum output buffer length being reached) and adds  
diag_vsnprintf() to the library of functions available.

Berend


[-- Attachment #2: diag.patch --]
[-- Type: application/octet-stream, Size: 1909 bytes --]

Index: packages/infra/current/include/diag.h
===================================================================
--- packages/infra/current/include/diag.h	(.../etna/ecos/ecos-cvs/packages/infra/current)	(revision 1835)
+++ packages/infra/current/include/diag.h	(.../trunk/etna/ecos/ecos-cvs/packages/infra/current)	(revision 1861)
@@ -102,6 +102,8 @@
      CYGBLD_ATTRIB_PRINTF_FORMAT(3,4);
 externC int  diag_vsprintf(char *buf, const char *fmt, va_list ap)
      CYGBLD_ATTRIB_PRINTF_FORMAT(2,0);
+externC int  diag_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
+     CYGBLD_ATTRIB_PRINTF_FORMAT(3,0);
 externC int  diag_vprintf(const char *fmt, va_list ap)
      CYGBLD_ATTRIB_PRINTF_FORMAT(1,0);
 
Index: packages/infra/current/src/diag.cxx
===================================================================
--- packages/infra/current/src/diag.cxx	(.../etna/ecos/ecos-cvs/packages/infra/current)	(revision 1835)
+++ packages/infra/current/src/diag.cxx	(.../trunk/etna/ecos/ecos-cvs/packages/infra/current)	(revision 1861)
@@ -284,6 +284,11 @@
 
 #define is_digit(c) ((c >= '0') && (c <= '9'))
 
+struct _sputc_info {
+    char *ptr;
+    int max, len;
+};
+
 static int
 _vprintf(void (*putc)(char c, void **param), void **param, const char *fmt, va_list ap)
 {
@@ -501,14 +506,10 @@
             res++;
         }
     }
-    return (res);
+    
+    return (param ? ((struct _sputc_info *)param)->len : res);
 }
 
-struct _sputc_info {
-    char *ptr;
-    int max, len;
-};
-
 static void 
 _sputc(char c, void **param)
 {
@@ -566,6 +567,19 @@
     return (info.len);
 }
 
+int 
+diag_vsnprintf(char *buf, size_t len, const char *fmt, va_list ap)
+{
+    int ret;
+    struct _sputc_info info;
+
+    info.ptr = buf;
+    info.max = len-1;
+    info.len = 0;
+    ret = _vprintf(_sputc, (void **)&info, fmt, ap);
+    return (info.len);
+}
+
 int
 diag_printf(const char *fmt, ...)
 {

[-- Attachment #3: Type: text/plain, Size: 1 bytes --]



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: _vprintf() return code fix and diag_vsnprintf() addition
  2008-10-21  2:33 _vprintf() return code fix and diag_vsnprintf() addition Berend Ozceri
@ 2008-11-03 14:51 ` Andrew Lunn
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Lunn @ 2008-11-03 14:51 UTC (permalink / raw)
  To: Berend Ozceri; +Cc: ecos-patches

On Mon, Oct 20, 2008 at 07:32:19PM -0700, Berend Ozceri wrote:
> This fixes a return value error in _vprintf() (if _sputc() clips the  
> output due to the maximum output buffer length being reached) 

Could you explain this a bit more. Which functions are returning the
wrong value?

The man page for vprintf says this:

       Upon successful return, these functions return the number of
       characters printed (not including the trailing '\0' used to end
       output to strings).

       The functions snprintf() and vsnprintf() do not write more than
       size bytes (including the trailing '\0').  If the output was
       truncated due to this limit then the return value is the
       number of characters (not including the trailing '\0') which
       would have been written to the final string if enough space had
       been available.  Thus, a return value of size or more means
       that the output was truncated.

My understanding of the eCos code is that the current _vprintf()
function returns the unrestricted number of characters. info->len is
the number of characters actually output, which may of been truncated.

It seems to me diag_vsprintf(), diag_snprintf() and diag_sprintf() are
wrong. These all return info.len when they should return the return
value from _vprintf().

Would you agree?

      Thanks
        Andrew

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2008-11-03 14:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-10-21  2:33 _vprintf() return code fix and diag_vsnprintf() addition Berend Ozceri
2008-11-03 14:51 ` Andrew Lunn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).