by Dan East » Dec 6, 2001 @ 11:44pm
The Windows API FindFirstFile, FindNextFile and FindClose will provide the solution to implementing both those functions. FindFirstFile and FindNextFile fill a struct (WIN32_FIND_DATA) with information about the found file (attributes, file times, size, etc). Check WIN32_FIND_DATA::dwFileAttributes for the bits FILE_ATTRIBUTE_READONLY to be set set for the access function. Ahh, heck, I'll just code _access for you:<br>[fixed]<br>int _access(const char *path, int mode) {<br> //Have to convert to UNICODE<br> TCHAR fname[MAX_PATH];<br> MultiByteToWideChar( CP_ACP, 0, path, -1,<br> fname, sizeof( fname )/sizeof( TCHAR ) );<br> <br> WIN32_FIND_DATA ffd;<br> //Get info on target file<br> HANDLE h=FindFirstFile( fname, &ffd );<br><br> if ( h==INVALID_HANDLE_VALUE )<br> return -1; //Can't find file<br> FindClose( h );<br><br> if (ffd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)<br> return 0; //Always return success if target is directory and exists<br><br> switch ( mode ) {<br> case 00: //Check existance<br> return 0;<br> case 06: //Check Read & Write permission<br> case 02: //Check Write permission<br> return ffd.dwFileAttributes&FILE_ATTRIBUTE_READONLY?-1:0;<br> case 04: //Check Read permission<br> return 0; //Assume always have read permission<br> } <br> //Bad mode value supplied, return failure<br> return -1;<br>}[/fixed]<br><br>That assumes a file can always be read, thus it obviously is not taking into account file locking due to access by an application. I doubt the true implementation of _access does that anyways - it probably goes by file permissions only.<br><br>Dan East