LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-26-2006, 07:23 PM   #1
Francinoman
LQ Newbie
 
Registered: Sep 2006
Posts: 13

Rep: Reputation: 0
fnmatch() function for wildcards


Hi there!

I am creating a new shell in C, and I am trying to implement the ability of wildcards. So for example if the user was to type : ls *.c

a list of all the .c files in a directory would be displayed.

I have done a bit of research and found that the function fnmatch() will help me do this. however i was wondering if anyone could help me do this? Or if anyone knows of a good example using fnmatch() so I can get a better idea of how the function works...

Thanks guys!

These forums have got me soooooo far in my shell program.
 
Old 10-27-2006, 08:51 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Shells normally use globbing. The glob function is easy to call.
Consider:
Code:
#include <stdlib.h>
#include <glob.h>

int main(int argc, char *argv[])
{
    glob_t globbuf;
    int i=0;

    if(glob (argv[1], 0, NULL, &globbuf)==GLOB_NOMATCH)
        return 1;
    while(i<globbuf.gl_pathc)
    {
        printf("%s\n", globbuf.gl_pathv[i++]);
    }
    globfree(&globbuf);
    return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling wildcards... artificialGekko Linux - General 5 03-24-2005 08:29 PM
CLI wildcards ShakyJake Linux - Newbie 4 02-28-2005 03:42 PM
Wildcards dazdaz Linux - Newbie 3 01-23-2005 05:33 AM
fnmatch version with and without FNM_EXTMATCH setupray Programming 1 06-15-2004 12:53 AM
Wildcards in kernel Squall Linux - General 1 04-20-2004 05:22 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration