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