Skip to main content
GameDev.net gamedev.net
🔒 Locked

Linux - FindFirstFile - Which Header???

Started by Tonic151 Jan 8, 2003 at 3:26 AM 11 replies 11.2k views
Original Post
Tonic151
Tonic151
in linux, gcc 2.95.3, what header do i need to include for the following function?? FindFirstFile()
Qw3r7yU10p!
Qw3r7yU10p!
linux has all kinds of tools for seaching for text in files
Tonic151
Tonic151
quote:
Original post by petewood
linux has all kinds of tools for seaching for text in files


Hmm what i'm looking for excatly is a way in linux to get all the filenames of all files in a selected directory.. And idea where i can find some example of implimentation.

tx

[edited by - tonic151 on January 8, 2003 4:48:42 AM]
Qw3r7yU10p!
Qw3r7yU10p!
No, what I mean is search for the function 'FindFirstFile' using a text search. That's what I'd do if I wanted to know the relevant include file.

[edited by - petewood on January 8, 2003 4:59:24 AM]
Russell
Russell
In unix/linux, you use the right tool for the job, which in this case is a shell script containing:

#!/bin/sh
ls $1

Then at the command prompt type: myscript.sh directory
and it will list the files in that directory.

According to what you asked for, that will do the job. But I have a feeling that''s not what you''re wanting to do...
Qw3r7yU10p!
Qw3r7yU10p!
okay, unable to resist I searched and found that FindFirstFile is a windows function so you won''t get it on linux.

Is your question really ''What is the equivalent to FindFirstFile on linux?''

Null and Void
Null and Void
quote:
Original post by petewood
Is your question really ''What is the equivalent to FindFirstFile on linux?''

And if it is what you meant: look up opendir, readdir, and closedir (POSIX functions). Their man pages are probably good enough, but ask if you have questions about their usage.

Qw3r7yU10p!
Qw3r7yU10p!
I was simply saying that, if I want to know where a function is declared, I search for it myself rather than asking on a forum.

Nothing to do with being a Linux user (which unfortunately I''m not).
Tonic151
Tonic151
quote:
Original post by petewood
I was simply saying that, if I want to know where a function is declared, I search for it myself rather than asking on a forum.

Nothing to do with being a Linux user (which unfortunately I''m not).


Well thanks anyways.. i''ll try the man pages for opendir() etc.. i have searched on google and didn''t come up with any good examples implimentation for getting all filenames in a certain directory, i recall a _FindFirstFile() for linux that was mentioned in the past. unforuntally the search function on these darn forums hasn''t worked in months so i can''t get back to that post about it so i was asking if anyone knew the implimentation headers per chance.. but i guess not.
RandomTask
RandomTask
If you are trying to make your code work on both Linux and Windows what I would do is implement your own FindFirstFile() function in Linux using the the POSIX functions previously mentioned and flag it only for the linux version. Then, Windows uses the windows API version and Linux uses your version but the code above FindFirstFile() doesn''t change.

RandomTask
Arild Fines
Arild Fines
quote:
Original post by RandomTask
If you are trying to make your code work on both Linux and Windows what I would do is implement your own FindFirstFile() function in Linux using the the POSIX functions previously mentioned and flag it only for the linux version. Then, Windows uses the windows API version and Linux uses your version but the code above FindFirstFile() doesn''t change.


The Microsoft Rotor[1](aka sscli aka .NET for freebsd and MacOSX) uses this approach. They have a layer called PAL(Platform Adaptation Layer) which implements a subset of the Win32 API in terms of a posix API. This makes it a lot easier for them to do the port, since they can localize the platform dependent stuff in a small number of files.
Here is their FindFirstFileA implementation:

  
/*++
Function:
FindNextFileA

See MSDN doc.
--*/
BOOL
PALAPI
FindNextFileA(
IN HANDLE hFindFile,
OUT LPWIN32_FIND_DATAA lpFindFileData)
{
find_obj *find_data;

BOOL bRet = FALSE;
DWORD dwLastError = 0;
DWORD Attr;

ENTRY("FindNextFileA(hFindFile=%p, lpFindFileData=%p)\n",
hFindFile, lpFindFileData);

find_data = (find_obj*)hFindFile;

if ( hFindFile == INVALID_HANDLE_VALUE ||
find_data == NULL ||
find_data->self_addr != find_data )
{
TRACE("FindNextFileA received an invalid handle\n");
dwLastError = ERROR_INVALID_HANDLE;
goto done;
}

if ( find_data->next && *(find_data->next) )
{
struct stat stat_data;
char ext[_MAX_EXT];

TRACE("Found [%s]\n", *(find_data->next));

/* split the path into a dir and filename */
_splitpath( *(find_data->next), NULL,
find_data->dir, find_data->fname, ext );
if ( find_data->fname == NULL )
{
ASSERT("_splitpath failed on %s\n", *(find_data->next));
dwLastError = ERROR_INTERNAL_ERROR;
goto done;
}
strcpy( find_data->fname + strlen(find_data->fname), ext );

/* get the attributes, but continue if it fails */
Attr = GetFileAttributesA( *(find_data->next) );
if ( Attr == -1 )
{
WARN("GetFileAttributes returned -1 on file [%s]\n",
*(find_data->next));
}
lpFindFileData->dwFileAttributes = Attr;

/* Note that cFileName is NOT the relative path */
strcpy( lpFindFileData->cFileName, find_data->fname );

/* we don''t support 8.3 filenames, so just leave it empty */
lpFindFileData->cAlternateFileName[0] = 0;

/* get the filetimes */
if ( stat(*(find_data->next), &stat_data) != 0 )
{
ERROR("stat failed on %s\n", *(find_data->next));

if ( ENOENT == errno )
{
FILEGetProperNotFoundError( *(find_data->next), &dwLastError );
dwLastError = ERROR_INTERNAL_ERROR;
}
else
{
dwLastError = FILEGetLastErrorFromErrno();
}
goto done;
}
lpFindFileData->ftCreationTime =
FILEUnixTimeToFileTime( stat_data.st_ctime,
ST_CTIME_NSEC(&stat_data) );
lpFindFileData->ftLastAccessTime =
FILEUnixTimeToFileTime( stat_data.st_atime,
ST_ATIME_NSEC(&stat_data) );
lpFindFileData->ftLastWriteTime =
FILEUnixTimeToFileTime( stat_data.st_mtime,
ST_MTIME_NSEC(&stat_data) );

/* get file size */

lpFindFileData->nFileSizeLow = (DWORD)stat_data.st_size;
lpFindFileData->nFileSizeHigh = (DWORD)(stat_data.st_size >> 32);

bRet = TRUE;

++find_data->next;
}
else
{
if ( find_data->next )
{
TRACE("Reached end of glob results\n");
dwLastError = ERROR_NO_MORE_FILES;
goto done;
}
else
{
ASSERT("find_data->next is (mysteriously) NULL\n");
}
}

done:
if (dwLastError)
{
SetLastError(dwLastError);
}

LOGEXIT("FindNextFileA returns BOOL %d\n", bRet);
return bRet;
}





I''ve looked at the source, and there are pieces that are good and pieces that are not ... My experience and some of my friends'' experience is that Linux is quite unreliable. Microsoft is really unreliable but Linux is worse. -- Ken Thompson
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
spiffgq
spiffgq
quote:
Original post by Null and Void
And if it is what you meant: look up opendir, readdir, and closedir (POSIX functions).


These are the functions I think you want. There are many more to check out, too, such as dirfd, rewinddir, scandir, seekdir, and telldir.

Here is an example program that uses opendir, readdir, and closedir to list the contents of the current directory:


  
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>

int main (){

// Open the current directory (NOTE: the period "." denotes

// the current working directory in UNIX

DIR * dir = opendir (".");

// If the directory specified in opendir cannot be opened,

// it will return NULL.

if (dir == NULL){
fprintf (stderr, "Unable to open the directory.\n");
exit ( 1 );
}

// Now, we''ll read in all the files and directories in this

// current directory and then print them to stdout. NOTE:

// you''ll see a "." and ".." listed. These stand for the

// current directory and the current directory''s parent.

// Again, those are standard UNIX things.

struct dirent *d = readdir (dir);
while (d != NULL){
printf ("\"%s\"\n", d->d_name);
d = readdir (dir);
}

// Now, close the directory.

closedir (dir);
return 0;

}


Compile it with this command:

gcc readdir.c

and run it

./a.out


SpiffGQ

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.