Converting a argc / argv construct into a va_list

This is a discussion on Converting a argc / argv construct into a va_list within the C Programming forums, part of the General Programming Boards category; Hi I'm new here, so appologies for my first post being a request!! I was wondering if anyone can help, ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    13

    Converting a argc / argv construct into a va_list

    Hi

    I'm new here, so appologies for my first post being a request!!

    I was wondering if anyone can help, I want to write a piece of code that converts an argc / argv to a variable argument list.

    Basically I want to do something like the following

    Code:
    void
    print_args(int argc, char *argv[])
    // first arg fmt string, subsequent args
    {
        if(argc == 1)
            printf(argv[1]); // assumes nothing to be substituted
        else if (argc == 2)
            printf(argv[1], argv[2]) ;
        else if (argc == 3)
            printf(argv[1], argv[2], argv[3]) ;
        // etc.
    }
    Unfortunatly I need to stick with argc / argv prototype

    What I was wondering is if I could write something to create a va_list and call vprintf?

    Anyone know if this is possible?


    Thanks in advance

    Craig
    Last edited by chambece; 06-30-2005 at 07:24 AM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're pretty much going to have to write your own. I'm assuming you're going to do something like:
    Code:
    ./progname "foo %s bar %d" "hello" 20
    And have it output:
    Code:
    foo hello bar 20
    You're going to have to roll your own. Which means, you're going to have to parse the first argument, and tokenize it up into chunks, then output them one at a time. Like so:
    Code:
    foo <snip out token, replace with next argument> bar <snip out token, replace with next argument>
    Which, if all you're doing is outputting it, is very easy, because all of your command line arguments are already strings. So you just chop the token, and strcat the next argument. Then repeat. Just throw it in a parsing loop.
    Code:
    while character[ x ] in first argument isn't null
        if this is a 'token'
            strcat the next argument (kept track of with counter) onto this string
        else
            copy this character onto the output string
    Really easy.

    If you need more than that, that is to say, if you need that 20 to be a decimal 20 for something other than just outputting text, you'll need to read the secondary token, (like the d that follows the % sign) to see what function you need to call. For example, we might use a switch to drop down with the 'd' as our switch argument, wich tells us we need to call something like atoi on that argument.
    Code:
    switch( secondarytoken )
    {
        case 'd':    somevar = atoi( argv[ somecounter++ ] ); break;
        case 's':    strcpy( somebuffer, argv[ somecounter++ ] ); break;
        ...
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Thanks for your post quzah, although.....

    I've been having a look at the lib code for stdarg.h and come up with the following solution:

    As I'm only looking for something to print strings, it has simplified it a bit....

    Code:
    Bool
    print_args( int argc, char * argv[] )
    /*
     * Takes fmt str as argv[1], and then params as argv[2...]
     */
    {
    	if ( argc > 2 )
    	{
    		vprintf(argv[1], (va_list)&argv[2]);
    	}
    	else if (argc == 1)
    	{
    		printf(argv[1]);
    	}
    	else
    	{
    		printf("No params");
    	}
    	return TRUE;
    }
    It isn't the most portable solution looking at the lib code, I'm on a sparc and I have a feeling it wouldn't work on a powerpc, but seems like it should do the job on most systems

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > vprintf(argv[1], (va_list)&argv[2]);
    Aside from the obvious hackery, I'd be very surprised if you said
    ./myprog "%d" 20
    you'd get 20 printed out as the answer.

    Oh look
    ./a.out "%d" 20
    -1073743250

    > printf(argv[1]);
    Passing unknown strings to printf() is just asking for a world of pain
    Just throw in a few choice "%n" conversions and watch the stack smashing fun begin
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    As I mentioned in my previous post, this isn't meant to be a 'new implementation of printf' hence my function not being called main!

    I know that what is being passed to me are strings and strings alone, so the only substition I'm 'allowing' is %s.

    I admit that if the fmt string had any other parameter it would cause problems, but if you have any other ideas I'd love to hear them!!

    I suppose I could parse the format string looking for %d, %p etc, but as this is only going to pull them out as strings from argv[], so I'd then need to convert them to the appropriate form

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well you've already been shown how to do it, so have at it.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Yeah,Thanks guys.....
    I've left it for the moment, but a more fully fledged version is on the todo list....

    Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help getting a grib on argc and argv
    By elwad in forum C Programming
    Replies: 10
    Last Post: 05-01-2009, 10:13 AM
  2. Using argc and argv data
    By Rad_Turnip in forum C Programming
    Replies: 4
    Last Post: 03-31-2006, 05:09 AM
  3. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  4. how do i? re: argc - argv
    By luigi40 in forum C Programming
    Replies: 2
    Last Post: 06-11-2004, 10:17 AM
  5. more argv and argc
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 09-08-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21