xref: /freebsd/contrib/pkgconf/tests/win-shim.h (revision 592efe252472a3385acf36b1f49ecf710a7f3d9c)
1 /*
2  * win-shim.h
3  * Windows function/define shims
4  *
5  * SPDX-License-Identifier: pkgconf
6  *
7  * Copyright (c) 2025 pkgconf authors (see AUTHORS).
8  *
9  * Permission to use, copy, modify, and/or distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * This software is provided 'as is' and without any warranty, express or
14  * implied.  In no event shall the authors be liable for any damages arising
15  * from the use of this software.
16  */
17 
18 #ifndef WIN_SHIM_H
19 #define WIN_SHIM_H
20 
21 #ifdef _WIN32
22 
23 #include <direct.h>
24 #include <io.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 
28 // Shims shared by both MSVC and MSYS2
29 #define mkdir(p, m) _mkdir(p)
30 #define setenv(n, v, o) _putenv_s(n, v)
31 #define unsetenv(n) _putenv_s(n, "")
32 
33 #ifndef PATH_MAX
34 #	define PATH_MAX MAX_PATH
35 #endif // !PATH_MAX
36 
37 // MSVC-specific shims
38 #ifdef _MSC_VER
39 #	define getcwd _getcwd
40 #	define chdir _chdir
41 #	define rmdir _rmdir
42 #	define lstat _lstat
43 #	define unlink _unlink
44 #	define popen _popen
45 #	define pclose _pclose
46 
47 static inline char *
mkdtemp(char * tmpl)48 mkdtemp(char *tmpl)
49 {
50 	if (_mktemp_s(tmpl, strlen(tmpl) + 1) != 0)
51 		return NULL;
52 	if (_mkdir(tmpl) != 0)
53 		return NULL;
54 	return tmpl;
55 }
56 
57 static inline int
mkstemp(char * tmpl)58 mkstemp(char *tmpl)
59 {
60 	if (_mktemp_s(tmpl, strlen(tmpl) + 1) != 0)
61 		return -1;
62 	return _open(tmpl, _O_CREAT | _O_EXCL | _O_RDWR | _O_BINARY, _S_IREAD | _S_IWRITE);
63 }
64 #endif // _MSC_VER
65 
66 #endif // _WIN32
67 #endif // WIN_SHIM_H
68