From: Keith M. <kei...@us...> - 2012-02-09 13:50:41
|
On 9 February 2012 11:29, Linux <linux@...> wrote: > Under Windows, the complier rejects these declarations and functions: > > struct stat64 lst; > stat64(path, &lst); In the windows world, you must prefix underscores: struct __stat64 lst; _stat64( path, &lst ); > Using 'stat' instead of 'stat64' will complile without errors but > 'opendir()' returns 0. readir() and closedir() also fail; WJFFM. $ cat dtree.c #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> #if HAVE_DIRENT_D_TYPE_DT_DIR # define D_TYPE_IS_DT_DIR(ent) ((ent)->d_type == DT_DIR) #else # include <sys/types.h> # include <sys/stat.h> # include <unistd.h> static __inline__ int D_TYPE_IS_DT_DIR( const struct dirent* ent ) { struct stat info; if( stat( ent->d_name, &info ) == 0 ) return S_ISDIR( info.st_mode ); return 0; } #endif void walkcwd( int level ) { const char *cwdname = "."; const char *parent = ".."; DIR *cwd = opendir( cwdname ); if( cwd != NULL) { struct dirent *ent; while( (ent = readdir( cwd )) != NULL ) { printf( "%*s%s\n", level, "", ent->d_name ); if( (D_TYPE_IS_DT_DIR( ent )) && ! (strcmp( ent->d_name, cwdname ) == 0) && ! (strcmp( ent->d_name, parent ) == 0) ) { chdir( ent->d_name ); walkcwd( 2 + level ); chdir( parent ); } } closedir( cwd ); } } int main(){ walkcwd( 2 ); return 0; } $ gcc -o dtree dtree.c $ ./dtree ...displays tree from $PWD... Also, with current CVS snapshot of mingwrt, (not yet released): $ gcc -o dtree -DHAVE_DIRENT_D_TYPE_DT_DIR=1 dtree.c $ ./dtree works equally well using the DT_DIR attribute from dirent.h directly, without using stat() at all. > #define _LARGEFILE64_SOURCE > #define _FILE_OFFSET_BITS 64 These have no effect on windows. > #include <unistd.h> > #include <stdio.h> > #include <stdlib.h> > #include <dirent.h> > #include <string.h> > #include <sys/stat.h> Conspicuously absent: 'man 2 stat' on my Linux box says that you also need, (since you are also supporting Linux hosts): #include <sys/types.h> > #define _MAX_PATH 260 You should let the system headers define this. -- Regards, Keith. |