LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-09-2009, 01:19 PM   #1
skyknight
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
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Look at :
Code:
man 3 glob
it looks promising however I can't get it to work.
 
Old 03-09-2009, 02:14 PM   #4
sureshsujatha
Member
 
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
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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)

Code:
 #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);
            }
  	}
  	closedir(dirp);
}

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};
	foo();
	printf("press return to continue" );
	fflush(stdout);
	fgets(ch, sizeof(ch), stdin);
        foo2();
	return 0;
}

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


Reply


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


All times are GMT -5. The time now is 08:49 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration