search for files

J

Jrdman

hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.
thanks.
 
W

Walter Roberson

hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.

I would imagine that using the Windows APIs would be fairly useless
on non- MS Windows systems, so if we take it as understood that
people have indeed been able to write code to search for files in
a directory on at least one non- MS Windows system, then the answer
to your question would logically have to be "Yes".


You might find it instructive to read the recent thread "directories"
started by 'sid' on June 27th,
http://groups.google.ca/group/comp.lang.c/msg/4d23538a011bd637
 
J

Jrdman

No.
ANSI C has no directory functions, which at first sight may seem surprising
since there are fucntions to open files from paths.
However on many systems directories can be very large, and can be altered by
other users. This makes it quite difficult to devise a robust interface
which will hold for every system.
Thus you have to use OS-specific functions.

yes i know that ANSI C has no directory functions,to make my question
clear,how can i write a function that search for files using a native
C code (without using Windows APIs)i know it's possible to write that
code, the obvious example is the windows APIs
themselves(FindFirstFile() and FindNextFile())if it's not possible how
these two functions are coded?
 
L

Lew Pitcher

hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())

Technically, with the proper 3rd-party addon, anyone can write this. In
fact, given my current platform, I can definitely write a directory search
program in C (with POSIX extensions) without using /any/ Windows APIs. It
helps that I don't run Windows, of course ;-)

or even has a small idea on how to do that.
thanks.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I'm doing. ------
 
S

Stephen Sprunk

Jrdman said:
yes i know that ANSI C has no directory functions,to make my question
clear,how can i write a function that search for files using a native
C code (without using Windows APIs)i know it's possible to write that
code, the obvious example is the windows APIs
themselves(FindFirstFile() and FindNextFile())if it's not possible how
these two functions are coded?

There is no requirement that Windows API functions are coded in C. For
all we know, they're coded in assembler. There are many ways to make
functions available to C without writing those functions themselves in
C, though the techniques are off-topic here.

There is _no_ portable way to do what you're asking. C does not even
acknowledge that directories exist, since C has to work on platforms
that have no concept of directories. Therefore, there is no 100%
portable way to write code that manipulates or even examines directories.

The usual solution when running into problems like this is to create
your own wrapper interface and a few different implementations (with
suitable #ifdef's) that call the native functions. Of course, at that
point your program is only "portable" to the systems that implement one
of the native interfaces you know how to call...

S
 
J

Jrdman

It seems as though:
    1    Those functions are your examples of functions
         coded in "native c code".
    2    You have no idea how those functions are coded.

Am I reading you correctly?

As i know(i've heard) and i'm not sure that windows APIs are coded in
C and those two functions are part of Windows APIs.if i'm wrong you
can correct me not just tell me that i'm wrong coz this is not
helpfull anymore
 
J

Jrdman

There is no requirement that Windows API functions are coded in C.  For
all we know, they're coded in assembler.  There are many ways to make
functions available to C without writing those functions themselves in
C, though the techniques are off-topic here.

There is _no_ portable way to do what you're asking.  C does not even
acknowledge that directories exist, since C has to work on platforms
that have no concept of directories.  Therefore, there is no 100%
portable way to write code that manipulates or even examines directories.

The usual solution when running into problems like this is to create
your own wrapper interface and a few different implementations (with
suitable #ifdef's) that call the native functions.  Of course, at that
thanks.but what do you mean by native functions(what functions?)?
 
O

osmium

Jrdman said:
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.

That's going to be a pain in the butt. Kind of like learning how to make
gunpowder with horse urine and swamp grass; a dying art. Lot's of arcane
jargon to work your way through. You have to be have some competence with
trees and recursion. I am sure you have figured out by now that this is not
the right froup. The best idea I have is to search old entries (ca.
1990-1995) in ms-dos oriented google groups. It's not a *good* idea, it is
just the best I can come up with.

Don't try to do it all at once, creep rather than walk. Start by using
findfirst() on a file you know exists. Then one you know doesn't exist.
And so on.
 
J

Jrdman

Not just tell you that you're wrong?
OK.

You have absolutely no idea of what you're talking about.
You can't even use the word "know" correctly in a sentence.
Give up programming.

you can't even make the difference between computer languages and
human languages,sorry but here we are learning C not english
and noone is pushing you to answear my question, if you don't "KNOW"
just shutup,and if you have a problem with humans relationships
i'm really sorry for you...
 
C

CBFalconer

Jrdman said:
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.

No. There is no reason to assume directories exist on a file
system. As an example, consider the CP/M-80 file system. Which
supported a lot of software.
 
B

Barry Schwarz

thanks.but what do you mean by native functions(what functions?)?

Functions specifically provided by the system you intend to execute on
for the purpose of dealing with directories. The Windows API is one
set of such functions designed to work on Windows. There may be
others for Windows. There probably are others for Unix. You might
check to see if POSIX provides some. In any event, you need to ask in
a group that deals with your execution environment because the
language itself offers no support in this area.


Remove del for email
 
S

santosh

Jrdman said:
hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.
thanks.

Just curious but why do you want to do this? Is it for purposes of
learning (dubious value), or because you might believe the APIs
provided by the system are too inefficient, or some other reason?

Serious code should use APIs provided by the system vendor unless there
is a very good reason to do otherwise.

Now coming to your actual question, you will essentially have to
directly interpret the filesystem structure that the system uses to
represent directories. This may be some version of the FAT or NTFS
filesystem. Your program might need administrative rights to do this
too. Your best bet is to ask in a group for your system like
<or <or
some related group. There are no dearth of Usenet groups for Windows
programming. Just search the groups list of your news provider or use
the "search for group" facility of Google Groups.
 
J

Jrdman

Just curious but why do you want to do this? Is it for purposes of
learning (dubious value), or because you might believe the APIs
provided by the system are too inefficient, or some other reason?

Serious code should use APIs provided by the system vendor unless there
is a very good reason to do otherwise.

Now coming to your actual question, you will essentially have to
directly interpret the filesystem structure that the system uses to
represent directories. This may be some version of the FAT or NTFS
filesystem. Your program might need administrative rights to do this
too. Your best bet is to ask in a group for your system like
<or <or
some related group. There are no dearth of Usenet groups for Windows
programming. Just search the groups list of your news provider or use
the "search for group" facility of Google Groups.

thanks for your help,and as an answer to your first question ,i'm just
doing this for learning purposes.I will certainly use the API
functions provided with MS Windows to programming in that system,but i
first prefer to know how these functions work and then use them.i've
created a group for that purpose (lang.c&asm) if you wanna help you
are welcome.
 
B

Bill Reid

hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.
thanks.

Yeah, I've read the responses here, but here is some actual code that
may or may not be what you're looking for:

/* read only certain types of entries in a directory to a list */
char **filter_directory_entry_list
(unsigned filter_type,char *filter,
unsigned entry_type,char *file_suffix,directory *directory) {
char **entry_item,**filtered_item;

if(directory==NULL) return NULL;

entry_item=filtered_item=directory->entry_list;

directory->num_filtered=0;

/* populate entry list with only filtered entries */
while(*entry_item!=NULL) {

if(filter_type==FILTERALPHA) {

if(!isalpha(*(*entry_item+1)))
goto Next_Entry;
}

else if(filter_type==FILTERNUMER) {

if(!isdigit(*(*entry_item+1)))
goto Next_Entry;
}

if(entry_type==DIRENTRIES) {

if(**entry_item!='d')
goto Next_Entry;
}

else if(entry_type==FILEENTRIES) {

if(**entry_item!='f')
goto Next_Entry;

if(file_suffix!=NULL) {

if(!is_file_type(*entry_item,file_suffix))
goto Next_Entry;
}
}

/* if you want all entries, pass NULL as filter */
if(filter==NULL)
goto Get_Item;

/* or match first few characters of entry */
else {

if(IS_STR_N_SAME(*entry_item+1,filter,strlen(filter)))
goto Get_Item;

else goto Next_Entry;
}

Get_Item :
*filtered_item++=*entry_item+1;
directory->num_filtered++;

Next_Entry :
entry_item++;
}

*filtered_item=NULL;

/* if we have two or more filtered entries, sort them alphabetically */
if(directory->num_filtered>1) {

/* temporarily strip suffix from file entries for proper sort */
if(entry_type!=DIRENTRIES) {

entry_item=directory->entry_list;

while(*entry_item!=NULL) {

if(*(*entry_item-1)=='f') {

*(strrchr(*entry_item,'.'))='\0';
entry_item++;
}
}
}

/* sort directory entries list to make sure it is in alpha order */
qsort(directory->entry_list,directory->num_filtered,
sizeof(char **),collate_alpha_strings_nocase);

/* replace suffix for file entries */
if(entry_type!=DIRENTRIES) {

entry_item=directory->entry_list;

while(*entry_item!=NULL) {

if(*(*entry_item-1)=='f') {

*(*entry_item+strlen(*entry_item))='.';
entry_item++;
}
}
}
}

return directory->entry_list;
}

What this does is allow you to find files that match certain criteria,
including, of course, the exact file you are looking for, by passing
the filename as *filter, and perhaps some other helpful restrictions,
such as appropriate values for filter_type, entry_type, and *file_suffix.
You have to pass it a *directory structure that was previously populated
by a function that implements your favorite directory reading functions
(I use POSIX-compliant functions as much as possible for that,
opendir(), readdir(), and closedir(), which are usually available in
most compilers).

Of course, I really use this to find lists of files in a directory with a
certain characteristic or name similarity, not find a single file...for
that,
probably the quickest way is to use the POSIX function access();
if you get back a "file not found" error, it ain't there, otherwise, it
is...
 
V

vippstar

hi there
can any one write a C code that search for files in a directory
without using the windows apis(FindFirstFile() and FindNextFile())
or even has a small idea on how to do that.

How about the following?

#include <stdio.h>
#include <stdlib.h>

#define MAXPATTERN 12
#define MAXCOMMAND 80

int main(void) {
char cmd_string[MAXCOMMAND +1];
char pattern[MAXPATTERN + 1]; why the +1's?

printf("Enter the pattern to search for: ");
flush stdout() if you expect this to be written by the time you reach
the fgets() call.
if (fgets(pattern, MAXPATTERN + 1, stdin) == NULL) {
printf("An error occurred.\n");
Write to 'stderr' instead or use perror().
exit(EXIT_FAILURE);
}

strcpy(cmd_string, "dir /s ");
Where's said:
strcat(cmd_string, pattern);
sprintf(cmd_string "dir /s %s", pattern);
MAXCOMMAND is quite bigger than what it actually needs to be.
system(cmd_string);
At least try this:
if(system(NULL)) system(cmd_string);
return EXIT_SUCCESS;

}

See - no mention of FindFirstFile or FindNextFile. Course, it only
works for files with short names, but it should be good enough for
It doesn't work. You assume a lot of things, that the implementation
has a command processor, that 'dir' does exist, that '/s' is
meaningful for 'dir', that it does what is assumed, that the user
won't input something malicious.
Your code invokes undefined behavior in several places.
your homework. For extra credit you could try using gets instead of
fgets, but that is left as an exercise for the reader.
gets() does not seem appropriate for this problem.
 
V

vippstar

Hi vippstar,

my post was intended as a joke - I'm sorry you missed that. However, I
am interested by your claim that the code invokes undefined behaviour
"in several places". I intended to write correct, though useless, code
in response to a hopelessly vague specification. A sort of "everything
you asked for but none of what you need" response. So please do point
out the undefined behaviour, because I am always eager to learn from
my mistakes. Thanks for pointing out the missing header though.
The strcpy() line, the strcat() line, the system() line.
The first two, you call the functions without the declaration in
scope.
The last one, anything passed to system() other than a null pointer
invokes undefined behavior.
That's 3 UBs in 17 lines code, thus several.
 
V

vippstar

OK, I have already said oops about the missing stdio.h header
inclusion. Is the use of strcpy, strcat UB apart from not including
their prototypes? As for system, my reading of 7.20.4.6 in the C99
Apart from *not* including the prototypes? Of course not, if you
include the prototype these two lines in your program do not invoke
undefined behavior.
standard is that as long as the string passed to system() is not a
NULL pointer, the behaviour is *implementation defined* because the
string is passed to the system's command processor (I think appendix
J.3.2 makes this explicit). How is that undefined behaviour? I think I
am probably taking your objections more seriously than I should, since
the original was not intended seriously and you seem to have a humour
impairment, but if you can show me where I am going wrong, I would
I don't have a humour impairment (or do I?) but I believe that a
newbie would trust your code instead of realizing you're joking.
still value the lesson. Unfortunately what you have said so far is too
lacking in detail to be of much use to me.
For system() I think you will enjoy this article and the follow-ups:
<[email protected]>
As for strcpy() and strcat() I'm only positive undefined behavior is
invoked but I cannot back up this with a section/verse from the
standard because I don't know where exactly to look. Perhaps someone
else will shed some light on the matter?
 
C

CBFalconer

.... snip ...

As for strcpy() and strcat() I'm only positive undefined behavior
is invoked but I cannot back up this with a section/verse from
the standard because I don't know where exactly to look. Perhaps
someone else will shed some light on the matter?

For a sufficiently accurate draft of the standard, formatted and
suitable for quoting on usenet, try the following link. The item
is bzip compressed. This is the last version available as text.

<http://cbfalconer.home.att.net/download/n869_txt.bz2>
 
K

Keith Thompson

CBFalconer said:
For a sufficiently accurate draft of the standard, formatted and
suitable for quoting on usenet, try the following link. The item
is bzip compressed. This is the last version available as text.

<http://cbfalconer.home.att.net/download/n869_txt.bz2>

For a more accurate post-C99 draft of the standard, see:

<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>

It's perfectly suitable for quoting on Usenet if you have the right
tools, and it includes semantically significant formatting that's lost
in any conversion to plain text.

Chuck, I understand that you prefer your plain-text n869, but wouldn't
it be better to refer to *both*?
 
C

CBFalconer

Keith said:
.... snip ...

For a more accurate post-C99 draft of the standard, see:

<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf>

It's perfectly suitable for quoting on Usenet if you have the
right tools, and it includes semantically significant formatting
that's lost in any conversion to plain text.

Chuck, I understand that you prefer your plain-text n869, but
wouldn't it be better to refer to *both*?

Well, when I use the following sig, I do. :)

--
Some useful references about C:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://c-faq.com/> (C-faq)
<http://benpfaff.org/writings/clc/off-topic.html>
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf> (C99)
<http://cbfalconer.home.att.net/download/n869_txt.bz2> (C99, txt)
<http://www.dinkumware.com/c99.aspx> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
<http://clc-wiki.net/wiki/C_community:comp.lang.c:Introduction>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top