Original Post
in linux, gcc 2.95.3, what header do i need to include for the following function?? FindFirstFile()
quote:
Original post by petewood
linux has all kinds of tools for seaching for text in files
#!/bin/sh
ls $1
quote:
Original post by petewood
Is your question really ''What is the equivalent to FindFirstFile on linux?''

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).
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.
/*++
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;
}
quote:
Original post by Null and Void
And if it is what you meant: look up opendir, readdir, and closedir (POSIX functions).
#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;
}
gcc readdir.c
./a.out
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more