va_list passthrough

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • raphfrk

    va_list passthrough

    Is there any way to pass a variable list to a function, so as to act
    as a passthrough?

    for example:

    void new_printf ( char *buf , ... )
    {
    va_list vlist;

    <modify buf slightly>

    printf( buf , vlist );
    }
  • santosh

    #2
    Re: va_list passthrough

    raphfrk wrote:
    Is there any way to pass a variable list to a function, so as to act
    as a passthrough?
    >
    for example:
    >
    void new_printf ( char *buf , ... )
    {
    va_list vlist;
    >
    <modify buf slightly>
    >
    printf( buf , vlist );
    }
    Use va_copy.

    Comment

    • raphfrk

      #3
      Re: va_list passthrough

      On Jul 7, 11:03 am, santosh <santosh....@gm ail.comwrote:
      raphfrk wrote:
      Is there any way to pass a variable list to a function, so as to act
      as a passthrough?
      >
      for example:
      >
      void new_printf ( char *buf , ... )
      {
      va_list vlist;
      >
      <modify buf slightly>
      >
      printf( buf , vlist );
      }
      >
      Use va_copy.

      I don't think that is what I want.

      I want a program that would allow something like the following to
      work.

      #include <stdio.h>

      int main()
      {

      int a=7l;

      new_printf("%d\ n" , a );

      return 0;
      }

      void new_printf ( char *buf , ... )
      {

      printf( buf , ... ); // the va list is passed to printf

      }

      The effect of the above would be that new_printf acts the same as
      printf.

      Comment

      • rahul

        #4
        Re: va_list passthrough

        On Jul 7, 3:01 pm, raphfrk <raph...@netsca pe.netwrote:
        Is there any way to pass a variable list to a function, so as to act
        as a passthrough?
        >
        for example:
        >
        void new_printf ( char *buf , ... )
        {
          va_list vlist;
        >
          <modify buf slightly>
        >
          printf( buf , vlist );
        >
        }
        >
        >
        Use va_copy.
        #include <stdarg.h>
        void va_copy(va_list dest, va_list src);

        The prototype is self explanatory. How do you plan to modify va_list?

        Comment

        • raphfrk

          #5
          Re: va_list passthrough

          On Jul 7, 11:59 am, rahul <rahulsin...@gm ail.comwrote:
          On Jul 7, 3:01 pm, raphfrk <raph...@netsca pe.netwrote:
          >
          >
          >
          Is there any way to pass a variable list to a function, so as to act
          as a passthrough?
          >
          for example:
          >
          void new_printf ( char *buf , ... )
          {
          va_list vlist;
          >
          <modify buf slightly>
          >
          printf( buf , vlist );
          >
          }
          >
          Use va_copy.
          #include <stdarg.h>
          void va_copy(va_list dest, va_list src);
          >
          The prototype is self explanatory. How do you plan to modify va_list?

          I don't, I want to pass the va_list to the printf function (any
          modifications would be to the buf field).

          Comment

          • Kenny McCormack

            #6
            Re: va_list passthrough

            In article <623e246f-f0ff-4bbf-a334-a9bb5aecf25a@j2 2g2000hsf.googl egroups.com>,
            raphfrk <raphfrk@netsca pe.netwrote:
            ....
            >Use va_copy.
            >#include <stdarg.h>
            >void va_copy(va_list dest, va_list src);
            >>
            >The prototype is self explanatory. How do you plan to modify va_list?
            >
            >
            >I don't, I want to pass the va_list to the printf function (any
            >modification s would be to the buf field).
            For some reason, nobody here wants to tell you about the vprintf() function.

            This is in line with official, stated, CLC policy - which is to never
            give a sucker an even break.

            Comment

            • Ben Bacarisse

              #7
              Re: va_list passthrough

              raphfrk <raphfrk@netsca pe.netwrites:
              On Jul 7, 11:03 am, santosh <santosh....@gm ail.comwrote:
              >raphfrk wrote:
              Is there any way to pass a variable list to a function, so as to act
              as a passthrough?
              Only if it accepts a va_list.
              for example:
              >>
              void new_printf ( char *buf , ... )
              {
              va_list vlist;
              >>
              <modify buf slightly>
              >>
              printf( buf , vlist );
              }
              >>
              >Use va_copy.
              >
              >
              I don't think that is what I want.
              >
              I want a program that would allow something like the following to
              work.
              >
              #include <stdio.h>
              >
              int main()
              {
              >
              int a=7l;
              >
              new_printf("%d\ n" , a );
              >
              return 0;
              }
              >
              void new_printf ( char *buf , ... )
              {
              >
              printf( buf , ... ); // the va list is passed to printf
              >
              }
              >
              The effect of the above would be that new_printf acts the same as
              printf.
              Thare are v* versions of the printf family of function for this
              purpose. You want vprintf.

              --
              Ben.

              Comment

              • raphfrk

                #8
                Re: va_list passthrough

                On Jul 7, 12:21 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
                wrote:
                In article <623e246f-f0ff-4bbf-a334-a9bb5aecf...@j2 2g2000hsf.googl egroups.com>,ra phfrk <raph...@netsca pe.netwrote:
                >
                ...
                >
                Use va_copy.
                #include <stdarg.h>
                void va_copy(va_list dest, va_list src);
                >
                The prototype is self explanatory. How do you plan to modify va_list?
                >
                I don't, I want to pass the va_list to the printf function (any
                modifications would be to the buf field).
                >
                For some reason, nobody here wants to tell you about the vprintf() function.
                Thanks. I guess I can assume from the existance of vprintf, that
                there is no way to pass a va_list directly to printf itself ?
                This is in line with official, stated, CLC policy - which is to never
                give a sucker an even break.
                Generally, they will correct every error in your code, including those
                unrelated to the original question. However, they usually do also
                answer the question too (and the list of errors can be helpful too).

                Comment

                • pete

                  #9
                  Re: va_list passthrough

                  raphfrk wrote:
                  Thanks. I guess I can assume from the existance of vprintf, that
                  there is no way to pass a va_list directly to printf itself ?
                  That's not so bad if you realize if you realize that
                  printf may use, and frequently does use, vfprintf,
                  either directly or through vprintf.

                  int printf(const char *format, ...)
                  {
                  int count;
                  va_list arg;

                  va_start(arg, format);
                  count = vprintf(format, arg);
                  va_end(arg);
                  return count;
                  }

                  int vprintf(const char *format, va_list arg)
                  {
                  return vfprintf(stdout , format, arg);
                  }

                  I have a toy library.


                  --
                  pete

                  Comment

                  • Kenny McCormack

                    #10
                    Re: va_list passthrough

                    In article <c74cd17e-7f3c-4353-bfb5-985a6125d9f0@m4 4g2000hsc.googl egroups.com>,
                    raphfrk <raphfrk@netsca pe.netwrote:
                    >On Jul 7, 12:21 pm, gaze...@xmissio n.xmission.com (Kenny McCormack)
                    >wrote:
                    >In article
                    ><623e246f-f0ff-4bbf-a334-a9bb5aecf...@j2 2g2000hsf.googl egroups.com>,ra phfrk
                    ><raph...@netsc ape.netwrote:
                    >>
                    >...
                    >>
                    >Use va_copy.
                    >#include <stdarg.h>
                    >void va_copy(va_list dest, va_list src);
                    >>
                    >The prototype is self explanatory. How do you plan to modify va_list?
                    >>
                    >I don't, I want to pass the va_list to the printf function (any
                    >modification s would be to the buf field).
                    >>
                    >For some reason, nobody here wants to tell you about the vprintf() function.
                    >
                    >Thanks. I guess I can assume from the existance of vprintf, that
                    >there is no way to pass a va_list directly to printf itself ?
                    Correct. What this means is that every variable-number-of-args function
                    that exists must have a corresponding v...() version. Luckily, there
                    aren't very many (off the top of my head, I can only think of the printf
                    and scanf families) that "come with the system". But it also means that
                    any that you write yourself, you should also provide a v...() version of.

                    Which, IMHO, makes the whole va_args functionality rather useless. I
                    think it mainly exists for backwards compatibility with the existing
                    printf and scanf families.
                    >This is in line with official, stated, CLC policy - which is to never
                    >give a sucker an even break.
                    >
                    >Generally, they will correct every error in your code, including those
                    >unrelated to the original question.
                    Human compilers, as they say. And about as useful. If I want a
                    compiler, I know where to find them.
                    >However, they usually do also answer the question too (and the list of
                    >errors can be helpful too).
                    My experience has been otherwise. Note that I am not talking about my
                    own issues - i.e., I'm not whining about them not answering my questions;
                    I have long since realized that looking for help here is like looking in
                    the sewer. I'm talking about my observations of years and years of them
                    joyously smackin' the newbies given any opportunity.

                    You may, of course, have another opinion. Fantasies die hard.

                    Comment

                    • Chris Torek

                      #11
                      Re: va_list passthrough

                      In article <c74cd17e-7f3c-4353-bfb5-985a6125d9f0@m4 4g2000hsc.googl egroups.com>
                      raphfrk <raphfrk@netsca pe.netwrote:
                      >... I guess I can assume from the existance of vprintf, that
                      >there is no way to pass a va_list directly to printf itself ?
                      The conclusion here is correct -- you must call vprintf() rather
                      than printf() -- but the premise itself is faulty. In the past,
                      some implementations of printf() had a "%r" format that meant (more
                      or less) "the corresponding argument is a va_list that you should
                      switch to for all subsequent arguments". (In fact, at least one
                      %r directive took a format followed by a va_list, and worked them
                      in a sort of recursive fashion, hence the 'r' could be said to
                      stand for "recursive" . Since %r was never standardized, other
                      printf()s that handled %r may have done other things.)

                      (To prove the need for the vprintf() family, you can simply read
                      the definition for printf() in the appropriate C Standard, and see
                      that there is no standard directive that takes a va_list.)

                      As a general rule, if you ever write your own variadic function:

                      TYPE my_variadic_fun ction(T1 fixed1, T2 fixed2, ...) {
                      ... some code will go here ...
                      }

                      you should begin by writing its entire guts as a "v"-prefixed
                      variant:

                      TYPE vmy_variadic_fu nction(T1 fixed1, T2 fixed2, va_list ap) {
                      ... the guts of the function ...
                      return whatever;
                      }

                      and then write the "wrapper" as the relatively short:

                      TYPE my_variadic_fun ction(T1 fixed1, T2 fixed2, ...) {
                      TYPE ret;
                      va_list ap;

                      va_start(ap, fixed2);
                      ret = vmy_variadic_fu nction(fixed1, fixed2, ap);
                      va_end(ap);
                      return ret;
                      }

                      That way, even though you (presumably) do not need it yourself yet,
                      the "v"-prefixed variant exists for whoever -- possibly you --
                      discovers a need for it later, in the same way that vprintf()
                      existed for you when you recently discovered a need for it.

                      The original C89 standard failed to provide a vscanf() family.
                      This is fixed in C99.
                      --
                      In-Real-Life: Chris Torek, Wind River Systems
                      Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
                      email: gmail (figure it out) http://web.torek.net/torek/index.html

                      Comment

                      Working...