Finding wildcards in readdir


 
Thread Tools Search this Thread
Top Forums Programming Finding wildcards in readdir
# 1  
Old 06-19-2008
Question Finding wildcards in readdir

I am having a hard time doing this and can't seem to find an example to help me. This is my code:

DIR *dirp=opendir(pathname);
struct stat filebuf;
struct dirent entry;
struct dirent *dp=&entry;
RWCString pattern;
for (int i = 0; i < request_->getNumStreams(); i++)
{
stream = request_->getStream(i);
tpdfilter = vehicle + filter + stream + filter + tpd;
if (prefix == "")
pattern = "*" + tpdfilter;
else
pattern = prefix + filter + tpdfilter;
while(dp = readdir(dirp))
{
stat(dp->d_name, &filebuf);
if ((!(fnmatch(pattern, dp->d_name,0)) == 0) && (stat(dp->d_name, &filebuf) != 0) && (S_ISDIR(filebuf.st_mode) == 1))
{
tpdfiles.append(pathname + dp->d_name);
}
}
}
closedir(dirp);

I am trying to find files in the directory that could look like this if I do an ls:

ls *FTRIG.NORMAL.*tpd*

There are too many files in the directory to run an ls with the wildcards. The wildcards are unknown and are user supplied from a file that is read in.

The fnmatch does not filter out the files that don't match the wildcard. I do not get directories so the other parts of the if statement are working.

Can anyone help? I am on unix, sun sparc 5.8 is that helps at all. Thanks.

Allyson
# 2  
Old 06-19-2008
use code tags please.

fnmatch returns zero when it finds a match. !fnmatch() == 0 does the exact opposite.
Consider testing for FNM_NOMATCH if you want to skip entries. You also should be checking for FNM_ERROR.
# 3  
Old 06-23-2008
If glob() exists in this version of SUN's os it may be what you are looking for.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

readdir and dynamic array memory corruption

Hi everyone I am developing an utility. At some part of it I read directory entries to a dynamic array: struct list It stores pointers to items: list.entries, which are structures: struct entry If a number of files in a directory is greater then number of elements an array was initially... (11 Replies)
Discussion started by: torbium
11 Replies

2. SuSE

problem of readdir on IA64 suse

Dear Experts, i am trying to find whether the given name is file or a directory dirp = opendir(dirname); direntp = readdir(dirp); if(direntp->d_type & DT_DIR) { printf("\n is a dirctory"); } else { //dir_or_file = Mtrue; printf("\n not a directory"); } it always... (9 Replies)
Discussion started by: vin_pll
9 Replies

3. Shell Programming and Scripting

grep readdir

Quick question. I can not get the context corrert on this code. opendir(DIR, "."); @fileldiv = grep(/l*/,readdir(DIR)); closedir(DIR); I am trying to search all html files within a dir that start with l. Thanks for your help. (1 Reply)
Discussion started by: mrlayance
1 Replies

4. Programming

diren.h : readdir( ) - sorting requirement

hi all, im using dirent.h headers readdir() function to traverse down a directory ( after openning it with opendir() ), and im printing the dir->d_name in a program. while(( dir = readdir( d ) ) != NULL ){ if (stat(, &info) != 0){ fprintf(stderr, "stat() error on %s: %s\n",... (2 Replies)
Discussion started by: wolwy_pete
2 Replies

5. UNIX for Dummies Questions & Answers

wildcards NOT

Hi All Please excuse another straightforward question. When creating a tar archive from a directory I am attempting to use wildcards to eliminate certain filetypes (otherwise the archive gets too large). So I am looking for something along these lines. tar -cf archive.tar * <minus all *.rst... (5 Replies)
Discussion started by: C3000
5 Replies

6. UNIX for Dummies Questions & Answers

how to man readdir(3)

I read the description of the command readdir by using 'man readdir'. However, in the description i was suggesed to refer to readdir(3). I wonder how to see the manual of readdir(3) Thanks (1 Reply)
Discussion started by: cy163
1 Replies

7. Shell Programming and Scripting

finding duplicate files by size and finding pattern matching and its count

Hi, I have a challenging task,in which i have to find the duplicate files by its name and size,then i need to take anyone of the file.Then i need to open the file and find for more than one pattern and count of that pattern. Note:These are the samples of two files,but i can have more... (2 Replies)
Discussion started by: jerome Sukumar
2 Replies

8. UNIX for Dummies Questions & Answers

wildcards

when writing a shell script (bourne) and using a unix command like 'ls' is there anything special you need to do to use a wildcard (like *)? (3 Replies)
Discussion started by: benu302000
3 Replies

9. Filesystems, Disks and Memory

Preblem with readdir system call

I am trying to read the directory contents throgh the readdir system call. After getting the directory entry I am testing the type of it by using the folllowing macros S_ISDIR() S_ISREG() etc. But in some systems every file in the directory is displaying like a sub directory. and in some systems... (2 Replies)
Discussion started by: gandhevinod
2 Replies

10. UNIX for Dummies Questions & Answers

Wildcards in VI

I'm trying to delete lines from a large text file using VI. Every line that I am wanting to delete start with 'S' - all others do not. (A list of users) I've tried using * but doesn't seem to like it...any ideas... Doesn't have to be VI - but I'm better with VI than sed/awk. (8 Replies)
Discussion started by: peter.herlihy
8 Replies
Login or Register to Ask a Question