Sponsored Content
Top Forums Programming Combine GetOpt And Variadic Function In C Post 303031030 by Corona688 on Wednesday 20th of February 2019 02:41:27 PM
Old 02-20-2019
main and getopt aren't variadic, they're array-based.

So one solution could be converting the variadic arguments into an array.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

void argfunction(char *howmany, ...);

int main(void) {
        argfunction("abc", "atext", "btext", "ctext");
        return(0);
}

void argfunction(char *howmany, ...)
{
        int n=0;
        // Never forget the NULLs, they convert mysterious "why" errors
        //  into "oh, I forgot to assign X" errors
        char *host=NULL, *user=NULL, *pass=NULL, *port=NULL;
        int argc=0;
        char *argv[16];
        va_list ap; // IIRC ap should always be the very last variable declared in a function.
        va_start(ap, howmany);

        argv[0]=""; // just tradition, argv[0] in main() is not an argument

        // checks howmany[0], assigns argv[1].
        // checks howmany[1], assigs argv[2], etc.
        while((argc<(16-2))&&howmany[argc++]) argv[argc]=va_arg(ap, char *);

        argv[argc]=NULL;         // Also traditional that argv[argc] be NULL.

        va_end(ap);

        printf("Howmany is: %s\n", howmany);
        printf("Arguments are:\n");
        for(n=1; n<argc; n++)
                printf("\t%d\t%s\n", n, argv[n]);

        optind=1; // This is global, so reset it

        // You now have argv, argc, and optind just like main to feed getopt with.
}


Last edited by Corona688; 02-20-2019 at 04:07 PM..
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

question about getopt()

I'm using getopt() to get command line options.One the optons accepts and argument.The argument is and offset.I was wondering how can I scecify that it's argument is of the type off_t.I've something like this "offset=(off_t)optarg" and it don't work. (1 Reply)
Discussion started by: angelfly
1 Replies

2. Shell Programming and Scripting

getopt help

scriptname i have made a script to perform so tasks and i managed to complete the tasks for all the options the problem i am facing is that i can run the scripts individually but i would like to make it such that it can accept multiple options and give me the appropriate output e.g.... (1 Reply)
Discussion started by: problems
1 Replies

3. Shell Programming and Scripting

getopt

#!/bin/sh set -- `getopt "abco:" "$@"` a= b= c= o= while : do case "$1" in -a) a=1;; -b) b=1;; -c) c=1;; -o) shift; o="$1";; --) break;; esac shift done shift # get rid of -- # rest of script... # e.g. ls -l $@ (6 Replies)
Discussion started by: Hitori
6 Replies

4. Shell Programming and Scripting

getopt help

I m trying to use getopt This is my script, but it doesn't take argument in variable, Please help. set - - `getopt mscl: $*` if then echo "Exiting...." exit 2 fi for i in $* do case $i in -m) MAIL="$i"; shift;; -s) SCRIPT=$OPTARG; shift;; -c) COB=$OPTARG; shift;;... (2 Replies)
Discussion started by: darshakraut
2 Replies

5. Shell Programming and Scripting

getopt help

:) Can anybody help me about how to use getopt in shell scripting. (3 Replies)
Discussion started by: darshakraut
3 Replies

6. Solaris

use of getopt command

Hi All, Could anyone tell me how to use getopt command.....? Thanks, Pintu (2 Replies)
Discussion started by: pintupatro
2 Replies

7. Shell Programming and Scripting

Help with getopt

Hi, I want to use the getopt function to parse some arguments for a script. while getopts "i:f:r:" OPTION do case $OPTION in i) iter=$OPTARG;; f) frame=$OPTARG;; r) roi=$OPTARG;; ?) echo Usage: ...... exit 2;; esac done However, I... (5 Replies)
Discussion started by: giorgos193
5 Replies

8. HP-UX

Variadic macros compilation error HP C on HP-UX

I have a macro defined like this: #define MM7_RETURN(errorNr, ...) \ { \ if(errorNr != MM7_RS_NoError) \ { ... (1 Reply)
Discussion started by: Marzullo
1 Replies

9. Shell Programming and Scripting

getopt in CSH

I am struggling to understand how getopt can be used in a csh script. can anybody post a csh script using getopt. Please! (4 Replies)
Discussion started by: animesharma
4 Replies

10. Shell Programming and Scripting

Getopt Help

Hi All, An old work friend wrote a script which I've been trying to understand how a section of it currently works and work out how i can add some command line switches which i can use later in the script to append the output depending on the command line arguements. Currently it works by... (1 Reply)
Discussion started by: mutley2202
1 Replies
getopt(3)						     Library Functions Manual							 getopt(3)

Name
       getopt - get option letter from argument vector

Syntax
       #include <stdio.h>
       int getopt (argc, argv, optstring)
       int argc;
       char **argv;
       char *optstring;

       extern char *optarg;
       extern int optind, opterr;

Description
       The  subroutine	returns the next option letter in argv that matches a letter in optstring.  The optstring is a string of recognized option
       letters; if a letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it  by  white
       space.  The optarg is set to point to the start of the option argument on return from

       The  function places in optind the argv index of the next argument to be processed.  The external variable optind is automatically initial-
       ized to 1 before the first call to

       When all options have been processed (that is, up to the first non-option argument), returns EOF.  The special option --  may  be  used	to
       delimit the end of the options; EOF will be returned, and -- will be skipped.

Diagnostics
       The function prints an error message on stderr and returns a question mark (?)  when it encounters an option letter that is not included in
       optstring.  Setting opterr to 0 disables this error message.

Examples
       The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options a  and	b,
       and the options f and o, both of which require arguments:
       #include <stdio.h>
       main (argc, argv)
       int argc;
       char **argv;
       {
	      int c;
	      extern int optind, opterr;
	      extern char *optarg;
	      .
	      .
	      .
	      .
	      while ((c = getopt (argc, argv, "abf:o:")) != EOF)
		       switch (c) {
		       case 'a':
			       if (bflg)
				       errflg++;
			       else
				       aflg++;
			       break;
		       case 'b':
			       if (aflg)
				       errflg++;
			       else
				       bproc( );
			       break;
		       case 'f':
			       ifile = optarg;
			       break;
		       case 'o':
			       ofile = optarg;
			       bufsiza = 512;
			       break;
		       case '?':
			       errflg++;
		       }
	       if (errflg) {
		       fprintf (stderr, "usage: . . . ");
		       exit (2);
	       }
	       for ( ; optind < argc; optind++) {
		      if (access (argv[optind], 4)) {
	       .
	       .
	       .
       }

See Also
       getopt(1)

																	 getopt(3)
All times are GMT -4. The time now is 01:45 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy