Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-09-2009, 01:19 PM   #1
LQ Newbie
Registered: Sep 2008
Posts: 17

Rep: Reputation: 1
help in C needed

my problem is,

consider /root/Desktop/user1/rootfile

in shell scripting if i dont know the user1 but i know rootfile i can find the in between folder(if there is only one) using '*'

what is the equivalent in C?
Old 03-09-2009, 01:31 PM   #2
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
What you are talking about is called "wildcard expansion" in shells such as BASH---eg:
"ls /*/*/xorg.conf" will find /etc/X11/xorg.conf

I am not aware of any equivalent in C, except by using a system call to a BASH function.

What is the context---ie why do you need to do this?
Old 03-09-2009, 02:07 PM   #3
Senior Member
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 231Reputation: 231Reputation: 231
Look at :
man 3 glob
it looks promising however I can't get it to work.
Old 03-09-2009, 02:14 PM   #4
Registered: Mar 2009
Posts: 40

Rep: Reputation: 15
Could you describe your requirements in a little better detail. That would be very helpful.

Last edited by sureshsujatha; 03-14-2009 at 12:20 AM.
Old 03-09-2009, 03:16 PM   #5
jim mcnamara
Registered: May 2002
Posts: 964

Rep: Reputation: 36
Usually you call fnmatch inside a C program to match an unexpanded pattern.

Why? Because normally the shell calls glob to do path name generation on wildcards on the command line. This means inside the shell/C program the filenames are already available.
The only way to stop this is to turn off globbing by the shell. Then you can call glob if you need it.

This C code emulates two shell commands:

ls *.c (fnmatch)

wc -l *.c (glob)

 #include <dirent.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <glob.h>
 #include <fnmatch.h>
 #include <unistd.h>

 void foo(void)  /* emulates: ls on the pattern given   */
  	char *pattern = "*.c";
  	struct dirent *dp=NULL;
        DIR *dirp=opendir("/home/jmcnama/c");
  	while(dp = readdir(dirp))
           if((fnmatch(pattern, dp->d_name,0)) == 0)
             printf("%s\n", dp->d_name);

void foo2(void )  /* emulates wc -l *.c */
 	glob_t globbuf;

 	globbuf.gl_offs = 2;
 	glob("*.c", GLOB_DOOFFS, NULL, &globbuf);
 	globbuf.gl_pathv[0] = "/usr/bin/wc";
 	globbuf.gl_pathv[1] = "-l";
 	execvp("/usr/bin/wc", &globbuf.gl_pathv[0]);

int main()
	char ch[10]={0x0};
	printf("press return to continue" );
	fgets(ch, sizeof(ch), stdin);
	return 0;

Last edited by jim mcnamara; 03-09-2009 at 03:19 PM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Help needed for make file problem : make: *** No rule to make target `flash', needed rameshsatyavaram Linux - Newbie 3 09-11-2008 02:13 AM
XGL needed for Beryl is conflicting with OpenGL needed for Cedega chunkeydelight Linux - Desktop 2 07-18-2007 05:50 AM
Errors installing Q, which is needed for Lex, which is needed for PHP Virtuality Linux - Software 1 05-29-2007 04:47 PM
help needed cellfedora Linux - Networking 2 05-01-2007 11:35 PM
help needed irfanasim Linux - Networking 2 09-23-2002 05:10 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:19 AM.

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