Fixed some issues found by winapi_check.
[wine] / win32 / newfns.c
1 /*
2  * Win32 miscellaneous functions
3  *
4  * Copyright 1995 Thomas Sandford (tdgsandf@prds-grn.demon.co.uk)
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /* Misc. new functions - they should be moved into appropriate files
22 at a later date. */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdio.h>
28 #include <string.h>
29 #ifdef HAVE_SYS_TIME_H
30 # include <sys/time.h>
31 #endif
32 #ifdef HAVE_UNISTD_H
33 # include <unistd.h>
34 #endif
35 #include "windef.h"
36 #include "winbase.h"
37 #include "winnls.h"
38 #include "winerror.h"
39 #include "wine/debug.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(win32);
42 WINE_DECLARE_DEBUG_CHANNEL(debug);
43
44
45 static BOOL     QUERYPERF_Initialized           = 0;
46 #if defined(__i386__) && defined(__GNUC__)
47 static BOOL     QUERYPERF_RDTSC_Use             = 0;
48 static LONGLONG QUERYPERF_RDTSC_Frequency       = 0;
49 #endif
50
51 static void QUERYPERF_Init(void)
52 {
53 #if defined(__i386__) && defined(__GNUC__)
54     /* We are running on i386 and compiling on GCC.
55      * Do a runtime check to see if we have the rdtsc instruction available
56      */
57     FILE                *fp;
58     char                line[256], *s, *value;
59     double              cpuMHz;
60
61     TRACE("()\n");
62
63     if (IsProcessorFeaturePresent( PF_RDTSC_INSTRUCTION_AVAILABLE ))
64     {
65         /* rdtsc is available.  However, in order to use it
66          * we also need to be able to get the processor's
67          * speed.  Currently we do this by reading /proc/cpuinfo
68          * which makes it Linux-specific.
69          */
70
71         TRACE("rdtsc available\n");
72
73         fp = fopen( "/proc/cpuinfo", "r" );
74         if (fp)
75         {
76             while(fgets( line, sizeof(line), fp ))
77             {
78                 /* NOTE: the ':' is the only character we can rely on */
79                 if (!(value = strchr( line, ':' )))
80                     continue;
81
82                 /* terminate the valuename */
83                 *value++ = '\0';
84                 /* skip any leading spaces */
85                 while (*value == ' ') value++;
86                 if ((s = strchr( value, '\n' )))
87                     *s = '\0';
88
89                 if (!strncasecmp( line, "cpu MHz", strlen( "cpu MHz" ) ))
90                 {
91                     if (sscanf( value, "%lf", &cpuMHz ) == 1)
92                     {
93                         QUERYPERF_RDTSC_Frequency = (LONGLONG)(cpuMHz * 1000000.0);
94                         QUERYPERF_RDTSC_Use = TRUE;
95                         TRACE("using frequency: %lldHz\n", QUERYPERF_RDTSC_Frequency);
96                         break;
97                     }
98                 }
99             }
100             fclose(fp);
101         }
102     }
103 #endif
104     QUERYPERF_Initialized = TRUE;
105 }
106
107
108 /****************************************************************************
109  *              QueryPerformanceCounter (KERNEL32.@)
110  */
111 BOOL WINAPI QueryPerformanceCounter(PLARGE_INTEGER counter)
112 {
113     struct timeval tv;
114
115     if (!QUERYPERF_Initialized)
116         QUERYPERF_Init();
117
118 #if defined(__i386__) && defined(__GNUC__)
119     if (QUERYPERF_RDTSC_Use)
120     {
121         /* i586 optimized version */
122         __asm__ __volatile__ ( "rdtsc"
123                                : "=a" (counter->s.LowPart), "=d" (counter->s.HighPart) );
124         return TRUE;
125     }
126     /* fall back to generic routine (ie, for i386, i486) */
127 #endif
128
129     /* generic routine */
130     gettimeofday( &tv, NULL );
131     counter->QuadPart = (LONGLONG)tv.tv_usec + (LONGLONG)tv.tv_sec * 1000000;
132     return TRUE;
133 }
134
135 /****************************************************************************
136  *              QueryPerformanceFrequency (KERNEL32.@)
137  */
138 BOOL WINAPI QueryPerformanceFrequency(PLARGE_INTEGER frequency)
139 {
140     if (!QUERYPERF_Initialized)
141         QUERYPERF_Init();
142
143 #if defined(__i386__) && defined(__GNUC__)
144     if (QUERYPERF_RDTSC_Use)
145     {
146         frequency->QuadPart = QUERYPERF_RDTSC_Frequency;
147         return TRUE;
148     }
149 #endif
150
151     frequency->s.LowPart        = 1000000;
152     frequency->s.HighPart       = 0;
153     return TRUE;
154 }
155
156 /****************************************************************************
157  *              FlushInstructionCache (KERNEL32.@)
158  */
159 BOOL WINAPI FlushInstructionCache(HANDLE hProcess, LPCVOID lpBaseAddress, SIZE_T dwSize)
160 {
161     if (GetVersion() & 0x80000000) return TRUE; /* not NT, always TRUE */
162     FIXME_(debug)("(0x%08lx,%p,0x%08lx): stub\n",(DWORD)hProcess, lpBaseAddress, dwSize);
163     return TRUE;
164 }
165
166 /***********************************************************************
167  *           GetSystemPowerStatus      (KERNEL32.@)
168  */
169 BOOL WINAPI GetSystemPowerStatus(LPSYSTEM_POWER_STATUS sps_ptr)
170 {
171     return FALSE;   /* no power management support */
172 }
173
174
175 /***********************************************************************
176  *           SetSystemPowerState      (KERNEL32.@)
177  */
178 BOOL WINAPI SetSystemPowerState(BOOL suspend_or_hibernate,
179                                   BOOL force_flag)
180 {
181     /* suspend_or_hibernate flag: w95 does not support
182        this feature anyway */
183
184     for ( ;0; )
185     {
186         if ( force_flag )
187         {
188         }
189         else
190         {
191         }
192     }
193     return TRUE;
194 }
195
196
197 /******************************************************************************
198  * CreateMailslotA [KERNEL32.@]
199  */
200 HANDLE WINAPI CreateMailslotA( LPCSTR lpName, DWORD nMaxMessageSize,
201                                    DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa)
202 {
203     FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_a(lpName),
204           nMaxMessageSize, lReadTimeout, sa);
205     return (HANDLE)1;
206 }
207
208
209 /******************************************************************************
210  * CreateMailslotW [KERNEL32.@]  Creates a mailslot with specified name
211  *
212  * PARAMS
213  *    lpName          [I] Pointer to string for mailslot name
214  *    nMaxMessageSize [I] Maximum message size
215  *    lReadTimeout    [I] Milliseconds before read time-out
216  *    sa              [I] Pointer to security structure
217  *
218  * RETURNS
219  *    Success: Handle to mailslot
220  *    Failure: INVALID_HANDLE_VALUE
221  */
222 HANDLE WINAPI CreateMailslotW( LPCWSTR lpName, DWORD nMaxMessageSize,
223                                    DWORD lReadTimeout, LPSECURITY_ATTRIBUTES sa )
224 {
225     FIXME("(%s,%ld,%ld,%p): stub\n", debugstr_w(lpName),
226           nMaxMessageSize, lReadTimeout, sa);
227     return (HANDLE)1;
228 }
229
230
231 /******************************************************************************
232  * GetMailslotInfo [KERNEL32.@]  Retrieves info about specified mailslot
233  *
234  * PARAMS
235  *    hMailslot        [I] Mailslot handle
236  *    lpMaxMessageSize [O] Address of maximum message size
237  *    lpNextSize       [O] Address of size of next message
238  *    lpMessageCount   [O] Address of number of messages
239  *    lpReadTimeout    [O] Address of read time-out
240  *
241  * RETURNS
242  *    Success: TRUE
243  *    Failure: FALSE
244  */
245 BOOL WINAPI GetMailslotInfo( HANDLE hMailslot, LPDWORD lpMaxMessageSize,
246                                LPDWORD lpNextSize, LPDWORD lpMessageCount,
247                                LPDWORD lpReadTimeout )
248 {
249     FIXME("(%04x): stub\n",hMailslot);
250     if (lpMaxMessageSize) *lpMaxMessageSize = (DWORD)NULL;
251     if (lpNextSize) *lpNextSize = (DWORD)NULL;
252     if (lpMessageCount) *lpMessageCount = (DWORD)NULL;
253     if (lpReadTimeout) *lpReadTimeout = (DWORD)NULL;
254     return TRUE;
255 }
256
257
258 /******************************************************************************
259  * GetCompressedFileSizeA [KERNEL32.@]
260  *
261  * NOTES
262  *    This should call the W function below
263  */
264 DWORD WINAPI GetCompressedFileSizeA(
265     LPCSTR lpFileName,
266     LPDWORD lpFileSizeHigh)
267 {
268     FIXME("(...): stub\n");
269     return 0xffffffff;
270 }
271
272
273 /******************************************************************************
274  * GetCompressedFileSizeW [KERNEL32.@]
275  *
276  * RETURNS
277  *    Success: Low-order doubleword of number of bytes
278  *    Failure: 0xffffffff
279  */
280 DWORD WINAPI GetCompressedFileSizeW(
281     LPCWSTR lpFileName,     /* [in]  Pointer to name of file */
282     LPDWORD lpFileSizeHigh) /* [out] Receives high-order doubleword of size */
283 {
284     FIXME("(%s,%p): stub\n",debugstr_w(lpFileName),lpFileSizeHigh);
285     return 0xffffffff;
286 }
287
288
289 /******************************************************************************
290  * SetComputerNameA [KERNEL32.@]
291  */
292 BOOL WINAPI SetComputerNameA( LPCSTR lpComputerName )
293 {
294     BOOL ret;
295     DWORD len = MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, NULL, 0 );
296     LPWSTR nameW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
297
298     MultiByteToWideChar( CP_ACP, 0, lpComputerName, -1, nameW, len );
299     ret = SetComputerNameW( nameW );
300     HeapFree( GetProcessHeap(), 0, nameW );
301     return ret;
302 }
303
304
305 /******************************************************************************
306  * SetComputerNameW [KERNEL32.@]
307  *
308  * PARAMS
309  *    lpComputerName [I] Address of new computer name
310  *
311  * RETURNS STD
312  */
313 BOOL WINAPI SetComputerNameW( LPCWSTR lpComputerName )
314 {
315     FIXME("(%s): stub\n", debugstr_w(lpComputerName));
316     return TRUE;
317 }
318
319 /******************************************************************************
320  *              CreateIoCompletionPort (KERNEL32.@)
321  */
322 HANDLE WINAPI CreateIoCompletionPort(HANDLE hFileHandle,
323 HANDLE hExistingCompletionPort, DWORD dwCompletionKey,
324 DWORD dwNumberOfConcurrentThreads)
325 {
326     FIXME("(%04x, %04x, %08lx, %08lx): stub.\n", hFileHandle, hExistingCompletionPort, dwCompletionKey, dwNumberOfConcurrentThreads);
327     return (HANDLE)NULL;
328 }
329
330 /******************************************************************************
331  *              GetQueuedCompletionStatus (KERNEL32.@)
332  */
333 BOOL WINAPI GetQueuedCompletionStatus(
334     HANDLE CompletionPort, LPDWORD lpNumberOfBytesTransferred,
335     LPDWORD lpCompletionKey, LPOVERLAPPED *lpOverlapped, DWORD dwMilliseconds
336 ) {
337     FIXME("(%x,%p,%p,%p,%ld), stub!\n",CompletionPort,lpNumberOfBytesTransferred,lpCompletionKey,lpOverlapped,dwMilliseconds);
338     SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
339     return FALSE;
340 }