hope all r doing fine..
i need a quick and very quick reply
m porting some cod:confused: e from windows to linux box..n i just encountered a function findfirstfile() used in windows..this function also takes wildcards as regular expression in the string
I searched the net for an alternate implementation in linux but hadnt got any! so m asking u guys tht if u have any idea or if someone has coded this function in linux then plz let me know... u can search findfirstfile() detail in MSDN:icon_mrgreen:

Recommended Answers

All 10 Replies

You probably want to use opendir, readdir, and closedir. Ask man for more details.

yes i have used those functions but the problem is that I couldnt find any builtin function in linux that can accept regular expressions as well... the windows function finddirstfile() accepts regular expression like "file*.txt", "?a*.c" ... i can use the linux functions for simple find first search by using dirent but i need to search in dirent structure with regular expression support as well... i want to know if there is any prebuilt function that at least searches with regular expression bcoz i dont want to write this function by myself if there exists any already.

Well you could always use a regex library on the Linux platform (there are several), to match the filenames read by readdir().

Besides, finddirstfile() is really only wildcards, not full regular expressions.

Or there's also the glob interface in GlibC

thnx guys ... 'm near to end this..
infact i wasnt aware of regex b4 as i never needed it so 'm new to this..
i just wrote this code to test regex.h
#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <regex.h>
int main(int argc,char **argv)
{
char str[20]="iamnabil";
int status;
regex_t myre;
regcomp(&myre, argv[1], REG_EXTENDED|REG_NOSUB);
status = regexec(&myre, str, (size_t)0, NULL, 0);
if(err==REG_NOMATCH)
printf("\n nOT matcheD\n");
else
if(err==0) printf("\nMatcHed !\n");
return 0;
}

it is working great but when i give * or ? wildcards at start of my RE string..it complains for segmentation fault..
like if i give expression as "*nabil" or "?am*" then segmentation error is caused then
can any1 help me here now and fix this...

OK, you've been a member for 6 months now, were you at any point planning to read the rules for posting code ?

Sorry Salem... i do admit i never read those rules and just skimmed that now... i think now it is properly posted. Correct me if i am wrong any where.

#include <stdio.h>
#include <sys/types.h>
#include <string.h>
#include <regex.h>
int main(int argc,char **argv)
{
char str[20]="iamnabil";
int status;
regex_t myre;
regcomp(&myre, argv[1], REG_EXTENDED|REG_NOSUB);
status = regexec(&myre, str, (size_t)0, NULL, 0);
if(err==REG_NOMATCH)
printf("\n nOT matcheD\n");
else
if(err==0) printf("\nMatcHed !\n");
return 0;
}

it is working great but when i give * or ? wildcards at start of my RE string..it complains for segmentation fault..
like if i give expression as "*nabil" or "?am*" then segmentation error is caused then
can any1 help me here now and fix this...

If it only get's that error if the FIRST char = '?' or '*'try checking if 'err' == REG_BADRPT . If so: report back.

Niek

Bear in mind that if you type in ./myprog *nabil at most Linux/Unix prompts, then the default is for the shell to have a go at expanding the wildcard expression to begin with. If that fails, then it's likely that your program will see argv[1] as being NULL, and that's where your segfault is coming from.

Perhaps check argc / argv before trying to use them in your code.

In bash, you can modify the shell's wildcard expansion, to allow such expressions to be passed to your program.

$ echo *.c
bar.c foo.c new.c util.c wrapper.c
# Turn if off
$ set -o noglob
$ echo *.c
*.c
# Turn it back on
$ set +o noglob
$ echo *.c
bar.c foo.c new.c util.c wrapper.c

Oh, and yes the code is better in that it is in code tags, but still looks pretty horrible because there is no indentation.

well thnx to all of u... i found a function fnmatch() that solved my problem for now ..i wrote an alternate findfirstfile() in linux using scandir() and fnmatch() functions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.