]> pd.if.org Git - pdclib.old/blob - platform/win32/functions/_PDCLIB/_PDCLIB_open.c
PDCLIB-3 Change _PDCLIB_wcinfo_t to use wint_t
[pdclib.old] / platform / win32 / functions / _PDCLIB / _PDCLIB_open.c
1 /* $Id$ */
2
3 /* _PDCLIB_open( char const * const, int )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 /* This is a stub implementation of open.
10 */
11
12 #include <stdio.h>
13 #include <errno.h>
14
15 #ifndef REGTEST
16 #include <_PDCLIB_glue.h>
17 #include <windows.h>
18
19 extern const _PDCLIB_fileops_t _PDCLIB_fileops;
20
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 )
24 {
25     DWORD desiredAccess;
26     DWORD creationDisposition;
27
28     switch(mode & ( _PDCLIB_FREAD | _PDCLIB_FWRITE | _PDCLIB_FAPPEND 
29                     | _PDCLIB_FRW ))
30     {
31     case _PDCLIB_FREAD: /* "r" */
32         desiredAccess = GENERIC_READ;
33         creationDisposition = OPEN_EXISTING;
34         break;
35     case _PDCLIB_FWRITE: /* "w" */
36         desiredAccess = GENERIC_WRITE;
37         creationDisposition = CREATE_ALWAYS;
38         break;
39     case _PDCLIB_FAPPEND: /* "a" */
40         desiredAccess = GENERIC_WRITE;
41         creationDisposition = OPEN_ALWAYS;
42         break;
43     case _PDCLIB_FREAD | _PDCLIB_FRW: /* "r+" */
44         desiredAccess = GENERIC_READ | GENERIC_WRITE;
45         creationDisposition = OPEN_EXISTING;
46         break;
47     case _PDCLIB_FWRITE | _PDCLIB_FRW: /* "w+" */
48         desiredAccess = GENERIC_WRITE | GENERIC_READ;
49         creationDisposition = CREATE_ALWAYS;
50         break;
51     case _PDCLIB_FAPPEND | _PDCLIB_FRW: /* "a+" */
52         desiredAccess = GENERIC_WRITE | GENERIC_READ;
53         creationDisposition = OPEN_ALWAYS;
54         break;
55     default: /* Invalid mode */
56         errno = EINVAL;
57         return false;
58     }
59
60     HANDLE fd = CreateFileA(filename, desiredAccess, 
61         FILE_SHARE_READ | FILE_SHARE_DELETE,
62         NULL, creationDisposition, FILE_ATTRIBUTE_NORMAL, NULL);
63
64     if(fd == INVALID_HANDLE_VALUE) {
65 #if 0
66         DWORD dw = GetLastError();
67         char* msgBuf;
68         FormatMessage(
69             FORMAT_MESSAGE_ALLOCATE_BUFFER | 
70             FORMAT_MESSAGE_FROM_SYSTEM |
71             FORMAT_MESSAGE_IGNORE_INSERTS,
72             NULL,
73             dw,
74             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
75             (LPSTR) &msgBuf,
76             0, NULL );
77
78         fprintf(stderr, "Error: %s\n", msgBuf);
79 #endif
80         _PDCLIB_w32errno();
81         return false;
82     }
83
84     if(mode & _PDCLIB_FAPPEND) {
85         LARGE_INTEGER offs;
86         offs.QuadPart = 0;
87         BOOL ok = SetFilePointerEx(fd, offs, NULL, FILE_END);
88         if(!ok) {
89             _PDCLIB_w32errno();
90             CloseHandle(fd);
91             return false;
92         }
93     }
94
95     pFd->pointer = fd;
96     *pOps = &_PDCLIB_fileops;
97     return true;
98 }
99
100 #endif
101
102 #ifdef TEST
103 #include <_PDCLIB_test.h>
104
105 #include <stdlib.h>
106 #include <string.h>
107
108 int main( void )
109 {
110     return TEST_RESULTS;
111 }
112
113 #endif
114