[comp.unix.questions] expanding wildcard options to main

harold@fmrco (Harold Naparst) (04/02/91)

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*

-- 
Harold Naparst               |  (uunet!fmrco!harold) 
Fidelity Investments         |  (617)-570-2587
82 Devonshire St., #I40B     |  The opinions expressed herein are not those
Boston, MA  02109            |  of my employer.

asg@sage.cc.purdue.edu (Bruce Varney) (04/02/91)

In article <1991Apr1.194817.20469@fmrco> harold@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*
}
}-- 
}Harold Naparst               |  (uunet!fmrco!harold) 

The glob expansion will be done by the shell before the arguments are
passed to the program.
Answer: You don't have to do anything special in your program to handle
      glob patterns
---------
sar.casm \'sa:r-.kaz-*m\ \sa:r-'kas-tik\ \-ti-k(*-)le-\ n [F sarcasme, fr. 
   LL sarcasmos, fr. Gk sarkasmos, fr. sarkazein to tear flesh, bite the lips 
   in rage, sneer, fr. sark-, sarx flesh; akin to Av thwar*s to cut] 1: a 
   cutting, hostile, or contemptuous remark : GIBE 2: the use of caustic or 
   ironic language - sar.cas.tic aj

                                   ###             ##
Courtesy of Bruce Varney           ###               #
aka -> The Grand Master                               #
asg@sage.cc.purdue.edu             ###    #####       #
PUCC                               ###                #
;-)                                 #                #
;'>                                #               ##

hunt@dg-rtp.rtp.dg.com (Greg Hunt) (04/02/91)

In article <1991Apr1.194817.20469@fmrco>, harold@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@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.

gwyn@smoke.brl.mil (Doug Gwyn) (04/03/91)

In article <1991Apr1.194817.20469@fmrco> harold@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*

You should have learned from your UNIX tutorial that wildcards
are expanded by the shell (command language interpreter) before
the commands are run, and the commands receive the expanded SET
of arguments.  Type "echo foo*" to see it in action (assuming
that you do have files "foo1" and "foo2" present).  Program
arguments are passed to C programs as an array of string pointers
as the second argument to the main() function, and to Bourne
shell scripts as "dollar" variables: $1, $2, ...  There is also
an indication of the number of arguments.  Any book on UNIX
programming should explain how to parse arguments.

harold@fmrco (Harold Naparst) (04/04/91)

Thanks to everyone who responded to my question about
how to expand wildcard options to main().  Obviously,
the shell does it for you.  

I thought I tried that but I guess I must have had
noglob set.

Harold Naparst (uunet!fmrco!harold)
-- 
Harold Naparst               |  (uunet!fmrco!harold) 
Fidelity Investments         |  (617)-570-2587
82 Devonshire St., #I40B     |  The opinions expressed herein are not those
Boston, MA  02109            |  of my employer.