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 extern const _PDCLIB_fileops_t _PDCLIB_fileops;
21 void _PDCLIB_w32errno(void);
22 bool _PDCLIB_open( _PDCLIB_fd_t * pFd, const _PDCLIB_fileops_t ** pOps,
23 char const * const filename, unsigned int mode )
26 DWORD creationDisposition;
28 switch(mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND
31 case _PDCLIB_FREAD: /* "r" */
32 desiredAccess = GENERIC_READ;
33 creationDisposition = OPEN_EXISTING;
35 case _PDCLIB_FWRITE: /* "w" */
36 desiredAccess = GENERIC_WRITE;
37 creationDisposition = CREATE_ALWAYS;
39 case _PDCLIB_FAPPEND: /* "a" */
40 desiredAccess = GENERIC_WRITE;
41 creationDisposition = OPEN_ALWAYS;
43 case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
44 desiredAccess = GENERIC_READ | GENERIC_WRITE;
45 creationDisposition = OPEN_EXISTING;
47 case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
48 desiredAccess = GENERIC_WRITE | GENERIC_READ;
49 creationDisposition = CREATE_ALWAYS;
51 case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
52 desiredAccess = GENERIC_WRITE | GENERIC_READ;
53 creationDisposition = OPEN_ALWAYS;
55 default: /* Invalid mode */
60 HANDLE fd = CreateFileA(filename, desiredAccess,
61 FILE_SHARE_READ | FILE_SHARE_DELETE,
62 NULL, creationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
64 if(fd == INVALID_HANDLE_VALUE) {
66 DWORD dw = GetLastError();
69 FORMAT_MESSAGE_ALLOCATE_BUFFER |
70 FORMAT_MESSAGE_FROM_SYSTEM |
71 FORMAT_MESSAGE_IGNORE_INSERTS,
74 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
78 fprintf(stderr, "Error: %s\n", msgBuf);
84 if(mode & _PDCLIB_FAPPEND) {
87 BOOL ok = SetFilePointerEx(fd, offs, NULL, FILE_END);
96 *pOps = &_PDCLIB_fileops;
103 #include <_PDCLIB_test.h>