3 /* _PDCLIB_open( char const * const, int )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
9 /* This is a stub implementation of open.
16 #include <_PDCLIB_glue.h>
19 void _PDCLIB_w32errno(void);
20 HANDLE _PDCLIB_open( char const * const filename, unsigned int mode )
23 DWORD creationDisposition;
25 switch(mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND
28 case _PDCLIB_FREAD: /* "r" */
29 desiredAccess = GENERIC_READ;
30 creationDisposition = OPEN_EXISTING;
32 case _PDCLIB_FWRITE: /* "w" */
33 desiredAccess = GENERIC_WRITE;
34 creationDisposition = CREATE_ALWAYS;
36 case _PDCLIB_FAPPEND: /* "a" */
37 desiredAccess = GENERIC_WRITE;
38 creationDisposition = OPEN_ALWAYS;
40 case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
41 desiredAccess = GENERIC_READ | GENERIC_WRITE;
42 creationDisposition = OPEN_EXISTING;
44 case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
45 desiredAccess = GENERIC_WRITE | GENERIC_READ;
46 creationDisposition = CREATE_ALWAYS;
48 case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
49 desiredAccess = GENERIC_WRITE | GENERIC_READ;
50 creationDisposition = OPEN_ALWAYS;
52 default: /* Invalid mode */
57 HANDLE fd = CreateFileA(filename, desiredAccess,
58 FILE_SHARE_READ | FILE_SHARE_DELETE,
59 NULL, creationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
61 if(fd == INVALID_HANDLE_VALUE) {
63 DWORD dw = GetLastError();
66 FORMAT_MESSAGE_ALLOCATE_BUFFER |
67 FORMAT_MESSAGE_FROM_SYSTEM |
68 FORMAT_MESSAGE_IGNORE_INSERTS,
71 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
75 fprintf(stderr, "Error: %s\n", msgBuf);
81 if(mode & _PDCLIB_FAPPEND) {
84 BOOL ok = SetFilePointerEx(fd, offs, NULL, FILE_END);
98 #include <_PDCLIB_test.h>