|
| 1 | +// win32_dirent.c |
| 2 | +// directory entry functions in Windows platform like Ubix/Linux |
| 3 | +// opendir(), readdir(), closedir(). |
| 4 | + |
| 5 | +#include <stdlib.h> |
| 6 | +#include <stdio.h> |
| 7 | +#include <ctype.h> |
| 8 | +#include <string.h> |
| 9 | +#include <windows.h> |
| 10 | + |
| 11 | +#include "win32_dirent.h" |
| 12 | + |
| 13 | +/* Open a directory stream on NAME. |
| 14 | + Return a DIR stream on the directory, or NULL if it could not be opened. */ |
| 15 | +DIR * opendir(char *name) |
| 16 | +{ |
| 17 | + struct _finddata_t c_file; |
| 18 | + intptr_t hFile; |
| 19 | + DIR *pdir; |
| 20 | + char searchstr[256]; |
| 21 | + |
| 22 | + sprintf_s(searchstr, sizeof(searchstr), "%s\\*.*",name); // add *.* to it for NT |
| 23 | + |
| 24 | + if( (hFile = _findfirst( searchstr, &c_file )) == -1L ) { |
| 25 | + if (1) // verbose |
| 26 | + printf( "No files found for %s search.\n", name ); |
| 27 | + return (DIR *) NULL; |
| 28 | + } |
| 29 | + else { |
| 30 | + pdir = (DIR *) malloc( sizeof(DIR) ); |
| 31 | + pdir->hFile = hFile ; |
| 32 | + pdir->c_file = c_file ; |
| 33 | + |
| 34 | + return pdir ; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/* Close the directory stream DIRP. |
| 39 | + Return 0 if successful, -1 if not. */ |
| 40 | +int closedir(DIR *dirp) |
| 41 | +{ |
| 42 | + if ( dirp && (dirp->hFile) ) { |
| 43 | + _findclose( dirp->hFile ); |
| 44 | + dirp->hFile = 0; |
| 45 | + free (dirp); |
| 46 | + } |
| 47 | + |
| 48 | + return 0; |
| 49 | +} |
| 50 | + |
| 51 | +/* Read a directory entry from DIRP. |
| 52 | + Return a pointer to a `struct dirent' describing the entry, |
| 53 | + or NULL for EOF or error. The storage returned may be overwritten |
| 54 | + by a later readdir call on the same DIR stream. */ |
| 55 | +struct dirent *readdir(void *avp) |
| 56 | +{ |
| 57 | + struct dirent *pdirentry; |
| 58 | + DIR *dirp = (DIR *)avp; |
| 59 | + |
| 60 | + for (;;) { |
| 61 | + if ( _findnext( dirp->hFile, &(dirp->c_file) ) == 0 ) { |
| 62 | + if ( ( strcmp (dirp->c_file.name,".") == 0 ) || |
| 63 | + ( strcmp (dirp->c_file.name,"..") == 0 ) ) { |
| 64 | + continue ; |
| 65 | + } |
| 66 | + pdirentry = (struct dirent *) malloc( sizeof(struct dirent) ); |
| 67 | + pdirentry->d_name = dirp->c_file.name ; |
| 68 | + pdirentry->d_ino = 1; // a fictious one like UNIX to say it is nonzero |
| 69 | + return pdirentry ; |
| 70 | + } |
| 71 | + else { |
| 72 | + return (struct dirent *) NULL; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// return last part of a path. The last path being a filename. |
| 78 | +char *basename(char *path) |
| 79 | +{ |
| 80 | + char *pdest; |
| 81 | + |
| 82 | + if (!path) |
| 83 | + return "."; |
| 84 | + pdest = strrchr(path, '/'); |
| 85 | + if (pdest) |
| 86 | + return (pdest+1); |
| 87 | + pdest = strrchr(path, '\\'); |
| 88 | + if (pdest) |
| 89 | + return (pdest+1); |
| 90 | + |
| 91 | + return path; // path does not have a slash |
| 92 | +} |
| 93 | +// end of dirent functions in Windows |
0 commit comments