Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!sun-barr!newstop!sun!amdcad!dgcad!dg-rtp!hunt From: hunt@dg-rtp.rtp.dg.com (Greg Hunt) Newsgroups: comp.unix.questions Subject: Re: Expanding wildcard options to main() Message-ID: <1991Apr2.020340.28611@dg-rtp.dg.com> Date: 2 Apr 91 02:03:40 GMT References: <1991Apr1.194817.20469@fmrco> Sender: hunt@hobbit.rtp.dg.com (Greg Hunt) Reply-To: hunt@dg-rtp.rtp.dg.com Organization: Data General Corp., Research Triangle Park, NC Lines: 42 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.