Re: function with ? parameters

From: Larry Brasfield (donotspam_larry_brasfield_at_hotmail.com)
Date: 11/08/04


Date: Mon, 8 Nov 2004 00:08:20 -0800


"David L" <drummy@nowhere.fr> wrote in message news:418f1285$0$5365$626a14ce@news.free.fr...
> Hello,
Hi.
> how to implement a sprintf-like function with unknown number of parameters
> AND with unknown type of parameters?

Here are some code snippets that demonstrate use
of varadic parameters in a portable manner. Note
that it would be possible to combine the functions
shown into a single function, but having them
separated makes the usage of va_list clearer with
respect to setup, per-argument processing, and
cleanup.

#include <stdarg.h>

ushort
StrFormatV(char *buffer, ushort maxlen, const char *format, va_list args)
{
  ...
  while (char fmtc = *format++) {
  ...
  switch (fcode) {
  case int_type:
    {
      long ivalue;
      ivalue = (va_arg(args, long));
  ...
    }

  ...
  }
  ...
}

ushort
StrFormat(char *buffer, ushort maxlen, const char *format, ...)
{
  va_list vlist;
  va_start(vlist, format);
  short rv = StrFormatV(buffer, maxlen, format, vlist);
  va_end(vlist);
  return rv;
}

> thanx
> David

-- 
--Larry Brasfield
email: donotspam_larry_brasfield@hotmail.com
Above views may belong only to me. 


Relevant Pages

  • [NEWS] How to Remotely and Automatically Exploit a Format Bug
    ... Exploiting a format bug remotely is not as difficult as one would think. ... We will use very minimalist server along this paper. ... Since the buffer is directly available to a malicious user, ... Guessing the address of the shellcode in the stack ...
    (Securiteam)
  • Re: quick overview of the perfmon2 interface
    ... The initial perfmon interface design as implemented in the 2.4 kernel series ... at their sampling buffer, they were aggregating samples instead of storing ... Our default sampling format stores samples sequentially, ...
    (Linux-Kernel)
  • Re: 2.6.11-rc1-mm1
    ... Because it wants to enforce a data format on buffer boundaries. ... Basically, in the case of LTT at least, the kernel tracing infrastructure ... one of the goals was to have a trace format that provided ...
    (Linux-Kernel)
  • Re: [Perfctr-devel] Re: quick overview of the perfmon2 interface
    ... > only valid if the process has bound itself to a single CPU? ... This option is for a context monitoring the whole system, ... > implementation would suit all sampling buffer formats. ... format" isn't a particularly good one. ...
    (Linux-Kernel)
  • Re: date manupulation strangeness
    ... change to summer time. ... what should perhaps be erased is the default dst data. ... time_t test1(const char* datestr, const char* format) ...
    (freebsd-stable)