This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

bug with gcc with findfirst/findnext


The functions
_findfirst
and 
_findnext
Will not work with the last version of gcc (mingw32).

Here is an example of the problem (under windows nt 4.0, SP3)
-----------------------------------------------------------------cut here
/* Print all the files in a directory that match a certain pattern. 
  demonstrates: printf, FindFirstFile,FindNextFile,FindClose,
	FileTimeToLocalFileTime,FileTimeToSystemTime
*/
#include <stdio.h>
#include <windows.h>
WIN32_FIND_DATA data;
static void printfile(WIN32_FIND_DATA *pdata);
static char *months[] = { "???",
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static unsigned long totalSize;
static unsigned long nrOfFiles;
static unsigned long nrOfDirs;
int main(int argc,char *argv[])
{
	HANDLE handle;

	if (argc < 2) {
		printf("Usage: findfiles <pattern>\n");
		exit(-1);
	}
	/* print the pattern we received */
	printf("Listing of\t%s\n\n",argv[1]);
	/* find the first file that matches */
	if ((handle = FindFirstFile(argv[1],&data)) == INVALID_HANDLE_VALUE) {
		printf("No %s files found\n",argv[1]);
		exit(-1);
	}
	/* print the first file specs */
	printfile(&data);
	/* loop finding all files that match */
	while (FindNextFile(handle,&data)) {
		printfile(&data);
	}
	if (GetLastError() != ERROR_NO_MORE_FILES) {
		printf("Unknown error %d\n",GetLastError());
	}
	FindClose(handle);
	if ((nrOfFiles+nrOfDirs) > 1)
		printf("\t%d dirs,%d files, Total size:\t%d\n",nrOfDirs,nrOfFiles,totalSize);
	return 0;
}
/* print the file specs and update some global counts */
static void printfile(WIN32_FIND_DATA *pdata)
{
	FILETIME localFileTime;
	SYSTEMTIME SystemTime;

	/* ignore the . and .. entries */
	if (!strcmp(pdata->cFileName,".") || !strcmp(pdata->cFileName,".."))
		return;
	/* print the name */
	printf("%-25s",pdata->cFileName);
	/* If the file name length is greater than 24 print the other
	   attributes in a fresh line */
	if (strlen(pdata->cFileName) > 24)
		printf("\n%25s"," ");
	/* print some attributes */
	if (pdata->dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
		printf("%-6s"," <DIR>");
		nrOfDirs++;
	}
	else {
		nrOfFiles++;
		printf("      ");
	}
	if (pdata->dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
		printf("H ");
	else printf("  ");
	if (pdata->dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
		printf("S ");
	else printf("  ");
	/* print the file size. 
	  Since the nFileSizeHigh member is ignored, this will not work for 
	  files greater than 4GB */
	printf("%9u ",pdata->nFileSizeLow);
	totalSize += pdata->nFileSizeLow;
	/* transform the file creation time to local file time */
	FileTimeToLocalFileTime(&pdata->ftCreationTime,&localFileTime);
	/* transform the local file time into a system time */
	FileTimeToSystemTime(&localFileTime,&SystemTime);
	/* print the time */
	printf("%4d %3s %02d:%02d:%02d:%02d",SystemTime.wYear,months[SystemTime.wMonth],
		SystemTime.wDay,SystemTime.wHour,SystemTime.wMinute,SystemTime.wSecond);
	printf("\n");
}
-------------------------------------------------------------------cut here
Example of usage:
D:\lcc\demo\findfirst>gcc findfiles.c
D:\lcc\demo\findfirst>a
Usage: findfiles <pattern>
D:\lcc\demo\findfirst>a *.*
Listing of      a.exe

a.exe                                  20998 1998 Nov 04:01:33:20

D:\lcc\demo\findfirst>

It will list only the first file and then STOP!!!
_findnext returns inexplicably -1!!!!

I have discovered this problem with egcs because... lcc-win32 has the SAME
problem. With exactly the same symptoms.

What is REALLY WEIRD is that if you run the same executable that fails under
the msvc debugger, it will work!!!

If you run the same executable that fails under wedit, lcc-win32's debugger,
it will work too!

If you compile with gcc, gdb will NOT make that work, probably because it is
a console mode debugger.

If you use *the same code* in a windows program, it will work. The problem
appears only in console applications.


Have any of you any clues to what the hell is going on there?

Thanks
-- 
Jacob Navia	Logiciels/Informatique
41 rue Maurice Ravel			Tel 01 48.23.51.44
93430 Villetaneuse 			Fax 01 48.23.95.39
France
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]