ReactOS 0.4.15-dev-7998-gdb93cb1
compat.h File Reference
#include "config.h"
#include "intsym.h"
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <inttypes.h>
#include <stdint.h>
#include <limits.h>
#include <string.h>
#include <strings.h>
#include <sys/time.h>
#include <fcntl.h>
#include "true.h"
Include dependency graph for compat.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define inline
 
#define SIZE_MAX   ((size_t)-1)
 
#define SSIZE_MAX   ((size_t)-1/2)
 
#define ULONG_MAX   ((unsigned long)-1)
 
#define atobigint   atoll
 
#define OFF_P   "li"
 
#define SIZE_P   "lu"
 
#define SSIZE_P   "li"
 

Typedefs

typedef unsigned char byte
 
typedef long off_p
 
typedef unsigned long size_p
 
typedef long ssize_p
 

Functions

voidsafe_realloc (void *ptr, size_t size)
 
charcompat_strdup (const char *s)
 
charcompat_getenv (const char *name)
 
int compat_open (const char *filename, int flags)
 
FILEcompat_fopen (const char *filename, const char *mode)
 
FILEcompat_fdopen (int fd, const char *mode)
 
int compat_close (int infd)
 
int compat_fclose (FILE *stream)
 
int win32_wide_utf8 (const wchar_t *const wptr, char **mbptr, size_t *buflen)
 
int win32_utf8_wide (const char *const mbptr, wchar_t **wptr, size_t *buflen)
 
charcompat_catpath (const char *prefix, const char *path)
 
int compat_isdir (const char *path)
 
struct compat_dircompat_diropen (char *path)
 
void compat_dirclose (struct compat_dir *)
 
charcompat_nextfile (struct compat_dir *)
 
charcompat_nextdir (struct compat_dir *)
 
size_t unintr_write (int fd, void const *buffer, size_t bytes)
 
size_t unintr_read (int fd, void *buffer, size_t bytes)
 
size_t unintr_fwrite (const void *ptr, size_t size, size_t nmemb, FILE *stream)
 

Variables

void(*)() catchsignal (int signum, void(*handler)())
 

Macro Definition Documentation

◆ atobigint

#define atobigint   atoll

Definition at line 99 of file compat.h.

◆ inline

#define inline

Definition at line 23 of file compat.h.

◆ OFF_P

#define OFF_P   "li"

Definition at line 131 of file compat.h.

◆ SIZE_MAX

#define SIZE_MAX   ((size_t)-1)

Definition at line 66 of file compat.h.

◆ SIZE_P

#define SIZE_P   "lu"

Definition at line 139 of file compat.h.

◆ SSIZE_MAX

#define SSIZE_MAX   ((size_t)-1/2)

Definition at line 69 of file compat.h.

◆ SSIZE_P

#define SSIZE_P   "li"

Definition at line 147 of file compat.h.

◆ ULONG_MAX

#define ULONG_MAX   ((unsigned long)-1)

Definition at line 72 of file compat.h.

Typedef Documentation

◆ byte

typedef unsigned char byte

Definition at line 104 of file compat.h.

◆ off_p

typedef long off_p

Definition at line 132 of file compat.h.

◆ size_p

typedef unsigned long size_p

Definition at line 140 of file compat.h.

◆ ssize_p

typedef long ssize_p

Definition at line 148 of file compat.h.

Function Documentation

◆ compat_catpath()

char * compat_catpath ( const char prefix,
const char path 
)

Definition at line 324 of file compat.c.

325{
326 char *ret = NULL;
327#ifdef WANT_WIN32_UNICODE
328 wchar_t *wprefix = NULL; /* Wide windows versions of */
329 wchar_t *wpath = NULL; /* input arguments. */
330 wchar_t *locwret = NULL; /* Tmp return value from LocalAlloc */
331 /*
332 This variation of combinepath can work with long and UNC paths, but
333 is not officially exposed in any DLLs, It also allocates all its buffers
334 internally via LocalAlloc, avoiding buffer overflow problems.
335 ThOr: I presume this hack is for supporting pre-8 Windows, as
336 from Windows 8 on, this is documented in the API.
337 */
338 HRESULT (__stdcall *mypac)( const wchar_t *in, const wchar_t* more
339 , unsigned long flags, wchar_t **out ) = NULL;
340 HMODULE pathcch = NULL;
341
342 if(!prefix && !path)
343 goto catpath_end;
344 wprefix = u2wpath(prefix);
345 wpath = u2wpath(path);
346 if((prefix && !wprefix) || (path && !wpath))
347 goto catpath_end;
348
349 /* Again: I presume this whole fun is to get at PathAllocCombine
350 even when pathcch.h is not available (like in MinGW32). */
351 if( (pathcch = GetModuleHandleA("kernelbase")) )
352 mypac = (void *)GetProcAddress(pathcch, "PathAllocCombine");
353 if(mypac) /* PATHCCH_ALLOW_LONG_PATH = 1 per API docs */
354 {
355 debug("Actually calling PathAllocCombine!");
356 mypac(wprefix, wpath, 1, &locwret);
357 }
358 else
359 {
360 /* Playing safe, if we'd care much about performance, this would be on
361 the stack. */
362 locwret = LocalAlloc(LPTR, sizeof(wchar_t)*MAX_PATH);
363 if(locwret)
364 PathCombineW(locwret, wprefix, wpath);
365 }
366 ret = w2upath(locwret);
367
368catpath_end:
369 LocalFree(locwret);
370 free(wprefix);
371 free(wpath);
372#else
373 size_t len, prelen, patlen;
374
375 if(path && path[0] == '/')
376 prefix = NULL; /* Absolute path stays as it is. */
377 prelen = prefix ? strlen(prefix) : 0;
378 patlen = path ? strlen(path) : 0;
379 /* Concatenate the two, put a / in between if both present. */
380 len = ((prefix && path) ? 1 : 0) + prelen + patlen;
381 ret = malloc(len+1);
382 if(ret)
383 {
384 size_t off=0;
385 memcpy(ret, prefix, prelen);
386 if(prefix && path)
387 ret[prelen+(off++)] = '/';
388 memcpy(ret+prelen+off, path, patlen);
389 ret[len] = 0;
390 }
391#endif
392 return ret;
393}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define free
Definition: debug_ros.c:5
#define malloc
Definition: debug_ros.c:4
#define NULL
Definition: types.h:112
#define GetProcAddress(x, y)
Definition: compat.h:753
#define MAX_PATH
Definition: compat.h:34
HMODULE WINAPI DECLSPEC_HOTPATCH GetModuleHandleA(LPCSTR lpModuleName)
Definition: loader.c:812
GLuint in
Definition: glext.h:9616
GLbitfield flags
Definition: glext.h:7161
GLenum GLsizei len
Definition: glext.h:6722
HLOCAL NTAPI LocalAlloc(UINT uFlags, SIZE_T dwBytes)
Definition: heapmem.c:1390
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1594
#define debug(msg)
Definition: key_call.c:71
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define PathCombineW
Definition: pathcch.h:317
static FILE * out
Definition: regtests2xml.c:44
#define __stdcall
Definition: typedefs.h:25
int ret
#define LPTR
Definition: winbase.h:381
#define HRESULT
Definition: msvc.h:7

◆ compat_close()

int compat_close ( int  infd)

Closing a file handle can be platform specific. This function takes a file descriptor that is to be closed.

Parameters
[in]infdFile descriptor to be closed.
Returns
0 if the file was successfully closed. A return value of -1 indicates an error.

Definition at line 253 of file compat.c.

254{
255#if (defined(WIN32) && !defined (__CYGWIN__)) /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
256 return _close(infd);
257#else
258 return close(infd);
259#endif
260}
#define close
Definition: acwin.h:98
_Check_return_opt_ _CRTIMP int __cdecl _close(_In_ int _FileHandle)

◆ compat_dirclose()

void compat_dirclose ( struct compat_dir cd)

Definition at line 481 of file compat.c.

482{
483 if(cd)
484 {
485 free(cd->path);
486#ifdef WANT_WIN32_UNICODE
487 FindClose(cd->ffn);
488#else
489 closedir(cd->dir);
490#endif
491 free(cd);
492 }
493}
InitDirComponents & cd
BOOL WINAPI FindClose(HANDLE hFindFile)
Definition: find.c:502
int __cdecl closedir(DIR *)

◆ compat_diropen()

struct compat_dir * compat_diropen ( char path)

Definition at line 431 of file compat.c.

432{
433 struct compat_dir *cd;
434 if(!path)
435 return NULL;
436 cd = malloc(sizeof(*cd));
437 if(!cd)
438 return NULL;
439#ifdef WANT_WIN32_UNICODE
440 cd->gotone = 0;
441 {
442 char *pattern;
443 wchar_t *wpattern;
445 wpattern = u2wlongpath(pattern);
446 if(wpattern)
447 {
448 cd->ffn = FindFirstFileW(wpattern, &(cd->d));
449 if(cd->ffn == INVALID_HANDLE_VALUE)
450 {
451 /* FindClose() only needed after successful first find, right? */
452 free(cd);
453 cd = NULL;
454 }
455 else
456 cd->gotone = 1;
457 }
458 free(wpattern);
459 free(pattern);
460 }
461#else
462 cd->dir = opendir(path);
463 if(!cd->dir)
464 {
465 free(cd);
466 cd = NULL;
467 }
468#endif
469 if(cd)
470 {
471 cd->path = compat_strdup(path);
472 if(!cd->path)
473 {
475 cd = NULL;
476 }
477 }
478 return cd;
479}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
HANDLE WINAPI FindFirstFileW(IN LPCWSTR lpFileName, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:320
GLubyte * pattern
Definition: glext.h:7787
DIR *__cdecl opendir(const char *)
#define compat_dirclose
Definition: intsym.h:23
#define compat_strdup
Definition: intsym.h:11
#define compat_catpath
Definition: intsym.h:20

◆ compat_fclose()

int compat_fclose ( FILE stream)

Definition at line 262 of file compat.c.

263{
264 return fclose(stream);
265}
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
Definition: parse.h:23

◆ compat_fdopen()

FILE * compat_fdopen ( int  fd,
const char mode 
)

Also fdopen to avoid having to define POSIX macros in various source files.

Definition at line 248 of file compat.c.

249{
250 return fdopen(fd, mode);
251}
GLenum mode
Definition: glext.h:6217
_Check_return_ _CRTIMP FILE *__cdecl fdopen(_In_ int _FileHandle, _In_z_ const char *_Format)
static int fd
Definition: io.c:51

◆ compat_fopen()

FILE * compat_fopen ( const char filename,
const char mode 
)

Definition at line 218 of file compat.c.

219{
220 FILE* stream = NULL;
221#ifdef WANT_WIN32_UNICODE
222 int cnt = 0;
223 wchar_t *wname = NULL;
224 wchar_t *wmode = NULL;
225
226 wname = u2wlongpath(filename);
227 if(!wname)
228 goto fopen_fallback;
229 cnt = win32_utf8_wide(mode, &wmode, NULL);
230 if( (wmode == NULL) || (cnt == 0))
231 goto fopen_fallback;
232
233 stream = _wfopen(wname, wmode);
234 if(stream) goto fopen_ok;
235
236fopen_fallback:
237#endif
239#ifdef WANT_WIN32_UNICODE
240
241fopen_ok:
242 free(wmode);
243 free(wname);
244#endif
245 return stream;
246}
_Check_return_ _CRTIMP FILE *__cdecl _wfopen(_In_z_ const wchar_t *_Filename, _In_z_ const wchar_t *_Mode)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
#define win32_utf8_wide
Definition: intsym.h:19
const char * filename
Definition: ioapi.h:137

◆ compat_getenv()

char * compat_getenv ( const char name)

Definition at line 52 of file compat.c.

53{
54 char *ret = NULL;
55#ifdef WANT_WIN32_UNICODE
56 wchar_t *env;
57 wchar_t *wname = NULL;
58 if(win32_utf8_wide(name, &wname, NULL) > 0)
59 {
60 env = _wgetenv(wname);
61 free(wname);
62 if(env)
64 }
65#else
66 ret = getenv(name);
67 if(ret)
69#endif
70 return ret;
71}
static LPCWSTR LPCWSTR LPCWSTR env
Definition: db.cpp:170
_Check_return_ _CRTIMP wchar_t *__cdecl _wgetenv(_In_z_ const wchar_t *_VarName)
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)
#define win32_wide_utf8
Definition: intsym.h:18
Definition: name.c:39

◆ compat_isdir()

int compat_isdir ( const char path)

Definition at line 395 of file compat.c.

396{
397 int ret = 0;
398#ifdef WANT_WIN32_UNICODE
399 wchar_t *wpath;
400 wpath = u2wlongpath(path);
401 if(wpath)
402 {
405 ret=1;
406 free(wpath);
407 }
408#else
409 struct stat sb;
410 if(path && !stat(path, &sb))
411 {
412 if(S_ISDIR(sb.st_mode))
413 ret=1;
414 }
415#endif
416 return ret;
417}
#define stat
Definition: acwin.h:99
#define S_ISDIR(mode)
Definition: various.h:18
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
Definition: fileinfo.c:652
superblock * sb
Definition: btrfs.c:4261
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FILE_ATTRIBUTE_DIRECTORY
Definition: nt_native.h:705
Definition: cookie.c:202
Definition: stat.h:55
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23

◆ compat_nextdir()

char * compat_nextdir ( struct compat_dir cd)

Definition at line 529 of file compat.c.

530{
531 if(!cd)
532 return NULL;
533#ifdef WANT_WIN32_UNICODE
534 while(cd->gotone || FindNextFileW(cd->ffn, &(cd->d)))
535 {
536 cd->gotone = 0;
537 if(cd->d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
538 {
539 char *ret;
540 win32_wide_utf8(cd->d.cFileName, &ret, NULL);
541 return ret;
542 }
543 }
544#else
545 {
546 struct dirent *dp;
547 while((dp = readdir(cd->dir)))
548 {
549 struct stat fst;
550 char *fullpath = compat_catpath(cd->path, dp->d_name);
551 if(fullpath && !stat(fullpath, &fst) && S_ISDIR(fst.st_mode))
552 {
553 free(fullpath);
554 return compat_strdup(dp->d_name);
555 }
556 free(fullpath);
557 }
558 }
559#endif
560 return NULL;
561}
BOOL WINAPI FindNextFileW(IN HANDLE hFindFile, OUT LPWIN32_FIND_DATAW lpFindFileData)
Definition: find.c:382
struct dirent *__cdecl readdir(DIR *)
Definition: fatfs.h:198
char * d_name
Definition: dirent.h:29

◆ compat_nextfile()

char * compat_nextfile ( struct compat_dir cd)

Definition at line 495 of file compat.c.

496{
497 if(!cd)
498 return NULL;
499#ifdef WANT_WIN32_UNICODE
500 while(cd->gotone || FindNextFileW(cd->ffn, &(cd->d)))
501 {
502 cd->gotone = 0;
503 if(!(cd->d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
504 {
505 char *ret;
506 win32_wide_utf8(cd->d.cFileName, &ret, NULL);
507 return ret;
508 }
509 }
510#else
511 {
512 struct dirent *dp;
513 while((dp = readdir(cd->dir)))
514 {
515 struct stat fst;
516 char *fullpath = compat_catpath(cd->path, dp->d_name);
517 if(fullpath && !stat(fullpath, &fst) && S_ISREG(fst.st_mode))
518 {
519 free(fullpath);
520 return compat_strdup(dp->d_name);
521 }
522 free(fullpath);
523 }
524 }
525#endif
526 return NULL;
527}
#define S_ISREG(mode)
Definition: various.h:17

◆ compat_open()

int compat_open ( const char filename,
int  flags 
)

Opening a file handle can be different. This function here is defined to take a path in native encoding (ISO8859 / UTF-8 / ...), or, when MS Windows Unicode support is enabled, an UTF-8 string that will be converted back to native UCS-2 (wide character) before calling the system's open function.

Parameters
[in]wptrPointer to wide string.
[in]mbptrPointer to multibyte string.
Returns
file descriptor (>=0) or error code.

Definition at line 181 of file compat.c.

182{
183 int ret;
184#if defined (WANT_WIN32_UNICODE)
185 wchar_t *frag = NULL;
186
187 frag = u2wlongpath(filename);
188 /* Fallback to plain open when ucs-2 conversion fails */
189 if(!frag)
190 goto open_fallback;
191
192 /*Try _wopen */
194 if(ret != -1 )
195 goto open_ok; /* msdn says -1 means failure */
196
197open_fallback:
198#endif
199
200#if (defined(WIN32) && !defined (__CYGWIN__))
201 /* MSDN says POSIX function is deprecated beginning in Visual C++ 2005 */
202 /* Try plain old _open(), if it fails, do nothing */
204#else
206#endif
207
208#if defined (WANT_WIN32_UNICODE)
209open_ok:
210 free(frag);
211#endif
212
213 return ret;
214}
#define open
Definition: acwin.h:95
#define _O_BINARY
Definition: cabinet.h:51
#define _S_IWRITE
Definition: cabinet.h:33
#define _S_IREAD
Definition: cabinet.h:34
#define S_IROTH
Definition: propsheet.h:53
#define S_IRGRP
Definition: propsheet.h:41
#define S_IWOTH
Definition: propsheet.h:57
#define S_IRUSR
Definition: propsheet.h:29
#define S_IWUSR
Definition: propsheet.h:33
#define S_IWGRP
Definition: propsheet.h:45
_CRTIMP int __cdecl _wopen(const wchar_t *_Filename, int _OpenFlag,...)
Definition: file.c:2020
_CRTIMP int __cdecl _open(const char *_Filename, int _OpenFlag,...)
Definition: file.c:2001

◆ compat_strdup()

char * compat_strdup ( const char s)

Definition at line 32 of file compat_str.c.

33{
34 char *dest = NULL;
35 if(src)
36 {
37 size_t len;
38 len = strlen(src)+1;
39 if((dest = malloc(len)))
40 memcpy(dest, src, len);
41 }
42 return dest;
43}
GLenum src
Definition: glext.h:6340
static char * dest
Definition: rtl.c:135

◆ safe_realloc()

void * safe_realloc ( void ptr,
size_t  size 
)

Definition at line 16 of file compat_str.c.

17{
18 if(ptr == NULL) return malloc(size);
19 else return realloc(ptr, size);
20}
#define realloc
Definition: debug_ros.c:6
GLsizeiptr size
Definition: glext.h:5919
static PVOID ptr
Definition: dispmode.c:27

◆ unintr_fwrite()

size_t unintr_fwrite ( const void ptr,
size_t  size,
size_t  nmemb,
FILE stream 
)

◆ unintr_read()

size_t unintr_read ( int  fd,
void buffer,
size_t  bytes 
)

Definition at line 631 of file compat.c.

632{
633 size_t got = 0;
634 while(bytes)
635 {
636 ssize_t part = read(fd, (char*)buffer+got, bytes);
637 if(part < 0 && errno != EINTR)
638 break;
639 bytes -= part;
640 got += part;
641 }
642 return got;
643}
#define EINTR
Definition: acclib.h:80
#define read
Definition: acwin.h:96
static unsigned char bytes[4]
Definition: adnsresfilter.c:74
GLuint buffer
Definition: glext.h:5915
int ssize_t
Definition: rosdhcp.h:48
#define errno
Definition: errno.h:18

◆ unintr_write()

size_t unintr_write ( int  fd,
void const buffer,
size_t  bytes 
)

Definition at line 616 of file compat.c.

617{
618 size_t written = 0;
619 while(bytes)
620 {
621 ssize_t part = write(fd, (char*)buffer+written, bytes);
622 if(part < 0 && errno != EINTR)
623 break;
624 bytes -= part;
625 written += part;
626 }
627 return written;
628}
#define write
Definition: acwin.h:97

◆ win32_utf8_wide()

int win32_utf8_wide ( const char *const  mbptr,
wchar_t **  wptr,
size_t buflen 
)

win32_mbc2uni Converts a null terminated UTF-8 string to a UCS-2 equivalent. Caller is supposed to free allocated buffer.

Parameters
[out]mbptrPointer to multibyte string.
[in]wptrPointer to wide string.
[out]buflenOptional parameter for length of allocated buffer.
Returns
status of WideCharToMultiByte conversion.

MultiByteToWideChar - http://msdn.microsoft.com/en-us/library/dd319072(VS.85).aspx

◆ win32_wide_utf8()

int win32_wide_utf8 ( const wchar_t *const  wptr,
char **  mbptr,
size_t buflen 
)

win32_uni2mbc Converts a null terminated UCS-2 string to a multibyte (UTF-8) equivalent. Caller is supposed to free allocated buffer.

Parameters
[in]wptrPointer to wide string.
[out]mbptrPointer to multibyte string.
[out]buflenOptional parameter for length of allocated buffer.
Returns
status of WideCharToMultiByte conversion.

WideCharToMultiByte - http://msdn.microsoft.com/en-us/library/dd374130(VS.85).aspx

Variable Documentation

◆ catchsignal

void(*)() catchsignal(int signum, void(*handler)()) ( int  signum,
void(*)()  handler 
)

Definition at line 291 of file compat.h.