[comp.lang.c] HELP: ===> wildcard ===> filename listing

lape@cs.utk.edu (Bryon S. Lape) (02/21/91)

	I need to know who to get all the filenames that meet a given
wildcard in Unix from c.  For instance, say my programme my_foo will print
the contents of any file given and I type:

		my_foo *a*.c

	What I would like to get is all files with an a in them and an
extension of .c, and print them to stdout (not the same way pg or more
does, by the way).  How do I get a list of files meeting the passed
wildcard?  I have done this in Quick C but that was using none standard
funcs.


	Please e-mail responses.


Bryon

lape@cs.utk.edu (Bryon S. Lape) (02/22/91)

In article <1991Feb21.140918.1650@cs.utk.edu> lape@cs.utk.edu (Bryon S. Lape) writes:
>
>	I need to know who to get all the filenames that meet a given
>wildcard in Unix from c.  For instance, say my programme my_foo will print
>the contents of any file given and I type:
>
>		my_foo *a*.c
>
>	What I would like to get is all files with an a in them and an
>extension of .c, and print them to stdout (not the same way pg or more
>does, by the way).  How do I get a list of files meeting the passed
>wildcard?  I have done this in Quick C but that was using none standard
>funcs.
>
>
>	Please e-mail responses.
>
>
>Bryon

	Many thanks for those who have sent me mail on this subject.  I had
no idea that the shell did this for you.  I just have one more question, do
all shells do this, or just specific ones?


Bryon Lape

lape@hydra2b.cs.utk.edu (Bryon S. Lape) (02/22/91)

In article <1991Feb21.172953.7488@cs.utk.edu>, lape@cs.utk.edu (Bryon S. Lape) writes:
|> In article <1991Feb21.140918.1650@cs.utk.edu> lape@cs.utk.edu (Bryon S. Lape) writes:
|> >
|> >	I need to know who to get all the filenames that meet a given
|> >wildcard in Unix from c.  For instance, say my programme my_foo will print
|> >the contents of any file given and I type:
|> >
|> >		my_foo *a*.c
|> >
|> >	What I would like to get is all files with an a in them and an
|> >extension of .c, and print them to stdout (not the same way pg or more
|> >does, by the way).  How do I get a list of files meeting the passed
|> >wildcard?  I have done this in Quick C but that was using none standard
|> >funcs.
|> >
|> >
|> >	Please e-mail responses.
|> >
|> >
|> >Bryon|> 

	Now having said Unix, I wish to make it more general, so here goes....

	Is there a generic c solution to the above problem??  For all you flamers out there, please note that this IS a C question and has nothing to do with shells, or os, or etc....


Bryon

jik@athena.mit.edu (Jonathan I. Kamens) (02/22/91)

  Yes, of course it's possible to do the matching in C.  After all, the code
in the shells that does it is written in C.

  Rich Salz has posted to the net a pattern matching function that behaves as
the shell pattern matching.  You can find it in alt.sources archives.  You can
also find it in the sources to GNU tar, so if you ftp the sources to GNU tar
from any GNU archive site, you should be all set.  There's also a globbing
function written by RMS in the bash sources, but it's under the GPL, so if you
don't want to put your code under the GPL, you shouldn't use it.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

peter@ficc.ferranti.com (Peter da Silva) (02/27/91)

Jonathan has a good point. It would be useful to have a standard interface
to "host system filename match routines". This would eliminate a number of
other problems, such as programs that use MS-DOS type filename matching on
systems with other standards.
-- 
Peter da Silva.  `-_-'  peter@ferranti.com
+1 713 274 5180.  'U`  "Have you hugged your wolf today?"

rns@tortuga.SanDiego.NCR.COM (Rick Schubert) (02/27/91)

In article <1991Feb26.144751.25144@athena.mit.edu> jik@athena.mit.edu (Jonathan I. Kamens) writes:
>In article <1991Feb25.194140.15247@SanDiego.NCR.COM>, rns@tortuga.SanDiego.NCR.COM (Rick Schubert) writes:
>|> Yes, it is possible to do pattern matching in C, but there is no reasonable%
>|> portable way of of doing filename expansion in C.  This is because there is
>|> no way in ANSI C to obtain a list of filenames to match against (there are
>|> ways on UNIX and other systems, but ANSI C has no such features).

>  That's true, but the code I mentioned said nothing at all about finding
>filenames against which to match; it was simply shell regular expression
>matching code, which is what was being asked for.  I'm sure the person who
>asked the original question is capable of figuring out how to use that code to
>match against filenames.

>  Most of the time, I agree with the people in comp.lang.c who say, "This
>question doesn't belong in this newsgroup!"  But the distinction Mr. Schubert

(you can call me Rick)

>appears to be attempting to draw here strikes me as absurd.  Sure, ANSI C
>doesn't have readdir().  So what.  You can talk about how to write shell
>regexp matchers without talking about how you're going to get the filenames to
>match against.

I was all ready to apologize for having misunderstood the original question,
but I decided to go back to read it first.  Here is what I found:

|> In article <1991Feb21.140918.1650@cs.utk.edu> lape@cs.utk.edu (Bryon S. Lape) writes:
|> >
|> >	I need to know who to get all the filenames that meet a given
|> >wildcard in Unix from c.  For instance, say my programme my_foo will print
|> >the contents of any file given and I type:
|
|> >		my_foo *a*.c

|> >	What I would like to get is all files with an a in them and an
|> >extension of .c, and print them to stdout (not the same way pg or more
|> >does, by the way).  How do I get a list of files meeting the passed
|> >wildcard?  I have done this in Quick C but that was using none standard
|> >funcs.

My reading of this still is that he wants to start with a single wildcard
expression and from that generate a list of filenames matching that
expression.  If I am wrong then I apologize; if you still read it as not
requiring something like readdir(), then we just differ in interpretation
and I answered according to my interpretation and you answered according to
yours, and there's no point in arguing what was really meant -- we can just
agree to disagree, since it's not relevant to this newsgroup or anything
that matters very much.

Also (although I don't want to get into an extended debate on this), just
because something can be written in C doesn't mean that it is an appropriate
question for comp.lang.c, let alone comp.std.c.  C is a general-purpose
programming language, and many things can be written in C.  If the question
were: "Is there anything in ANSI C that will do most, if not all, of the
work necessary to do regular expression or wildcard pattern matching (e.g.
a library routine)," the answer would be "no, but thanks for asking."  If
the question were "Is it possible to do regular expression or wildcard
pattern matching in C," the answer would be "Yes, but you would have to code
the algorithm yourself.  This is not an algorithm newsgroup (although I'm not
sure there is one), so discussion of how do write such a program is not
appropriate here.  If you have questions about specific features of the C
language in terms of coding such an algorithm, go ahead and ask them in
comp.lang.c if they are general language issues or in comp.std.c if they are
standards issues.  If you want some source code, go ask in comp.sources.wanted."

I have to say that one cannot always know if a question is appropriate for
one of these newsgroups without knowing the answer, and this question may
fall into that category.  That's why my initial response was not a flame
but just an explanation that it was not possible to write portable C code
to do what I thought was being asked.

-- Rick Schubert (rns@tortuga.SanDiego.NCR.COM)

peter@ficc.ferranti.com (Peter da Silva) (03/01/91)

Perhaps this discussion could continue in alt.lang.cfutures?
-- 
Peter da Silva.  `-_-'  peter@ferranti.com
+1 713 274 5180.  'U`  "Have you hugged your wolf today?"