delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/30/17:45:42

From: George Foot <mert0407 AT sable DOT ox DOT ac DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: *argv[] help!
Date: 30 Jan 1998 15:42:17 GMT
Organization: Oxford University, England
Lines: 65
Message-ID: <6asscp$5c4$6@news.ox.ac.uk>
References: <Chameleon DOT 980129220158 DOT nrotem AT netvision DOT netvision>
NNTP-Posting-Host: sable.ox.ac.uk
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

On Thu, 29 Jan 98 21:56:26 PST in comp.os.msdos.djgpp Noam Rotem
<nrotem AT johnbryce DOT co DOT il> wrote:

: Can I use the automatic globbing for more than one argument with wildcards?
: Let's say my command line is:

: foo *.c *.bck

: I guess I'll get the matching files of both arguments in argv[i], but how 
: would I know then where goes the line between *.c's globbed arguments and 
: *.bck's?

There have been several replies to this already, and there's no
built-in way to know this; I'll clarify a few consequences though.

Firstly, the automatic globbing means that it's most worthwhile to
write applications that can work on lists of files, and in particular
that ask the user to prefix lists of files with options.

Writing a program which assumes, for example, that its first argument
is an input file and its second an output file is asking for trouble,
because whenever the user specifies a first argument that matches more
than one file, the second matched will become the output file -- not
what was wanted.  It would be far safer to ask the user to prefix
input files with one option and output files with another -- similarly
if there are several types of input file, prefix them with different
options.  This answers your question in a way -- although you can't
automatically find out which arguments came from the first and second
wildcards, if you force the user to put a switch in between them you
can now see the gap.

Secondly, although I only mentioned (IIRC) that you can disable the
built-in globbing, you can of course provide your own globbing
instead, in much the same way.  You could write these routines based
on DJGPP's built-in routines (see the djlsr archive), but make them
insert special (untypeable) strings into the argument list to mark the
start and end of each wildcard.  This would probably be the most
versatile solution to your problem, but it's not really using the
built-in routines as built-in routines.  Because it would involve
modifying them, it would theoretically place your code under GPL --
but you could easily write your own globbing routines.  Having done
this, though, it may be better to just disable globbing and call the
routines from your program.

Writing your own globbing routines should be fairly simple for
DJGPP-specific purposes -- just use findfirst and findnext, bearing in
mind that (I think) they only support * and ? as wildcards.

Thirdly, if you ask your user to quote wildcarded arguments (thus
preventing the built-in globber from globbing them) you should be able
to call the built-in globber later on on single arguments from the
list (with their quotes stripped).  This is slightly dirty, since the
globber wasn't meant to be called from your code, but it ought to
work.

Lastly, you wrote `foo *.c *.bck'.  Using the built-in globber this
would expand to `foo' followed by a list of all *.c files existing,
followed by a list of all *.bck files existing.  I'm not sure that
this is what you wanted -- if you meant to have one %.bck file for
each %.c file then you'll have to glob by hand.  Again, doing so
shouldn't be too hard; ideally you'd implement something like Make's
pattern matching (using `%' symbols).

-- 
george DOT foot AT merton DOT oxford DOT ac DOT uk

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019