Expanding wildcard options to main()

Greg Hunt hunt at dg-rtp.rtp.dg.com
Tue Apr 2 12:03:40 AEST 1991


In article <1991Apr1.194817.20469 at fmrco>, harold at fmrco (Harold Naparst) writes:
> 
> How do you pass wildcard options to a program and get the
> program to expand them like ls does.  Example: suppose I 
> have two files called foo1 and foo2, and I want to run
> myprog on them.  How would I write myprog so that I could
> just do this:
>    % myprog foo*

The easiest way I can think of is to let the shell do the expansion for
you, just as you listed in your example.  The shell will do wildcard
expansion for the command line you type, regardless of what program
you're trying to run.  So, when you type this to the shell:

    myprog foo*

it gets expanded to:

    myprog foo1 foo2

Then, in your main routine (assuming you are using C), each one of the
arguments that was expanded will appear as a separate argv array
element.  You can check argc to see how many arguments were passed.
You can then loop through the argv array elements, processing each one
in turn, like this:

    main (int argc, char *argv [])
    {
    int i;

        for (i = 0; i < argc; i++) {
            printf ("argument %d is '%s'\n", i, argv [i]);
        }
    }

Take a look at the man page for main(3c) for details on argc and argv,
or look at a C reference book.  Enjoy!

-- 
Greg Hunt                        Internet: hunt at dg-rtp.rtp.dg.com
DG/UX Kernel Development         UUCP:     {world}!mcnc!rti!dg-rtp!hunt
Data General Corporation
Research Triangle Park, NC, USA  These opinions are mine, not DG's.



More information about the Comp.unix.questions mailing list