Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions .github/workflows/windows-stdlib.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Windows environment regression

on: [push, pull_request]

permissions:
contents: read

jobs:
test:
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
include:
- sys: MINGW64
package: mingw-w64-x86_64-gcc
- sys: UCRT64
package: mingw-w64-ucrt-x86_64-gcc
steps:
- uses: actions/checkout@v4
- uses: msys2/setup-msys2@v2
with:
msystem: ${{ matrix.sys }}
install: ${{ matrix.package }}
- name: Compile Windows compatibility code with warnings as errors
shell: msys2 {0}
run: |
gcc -std=c99 -Wall -Wextra -Werror -I. \
test/test_win32_stdlib.c contrib/win32/stdlib.c \
-o test_win32_stdlib.exe
- name: Run without NFC hardware
shell: msys2 {0}
run: ./test_win32_stdlib.exe
29 changes: 14 additions & 15 deletions contrib/win32/stdlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,26 @@
* @brief Windows System compatibility
*/

// Handle platform specific includes
#include "contrib/windows.h"
#include <stdlib.h>
#include <string.h>

//There is no setenv()and unsetenv() in windows,but we can use putenv() instead.
// Use the Windows CRT without a fixed-size name=value buffer.
int setenv(const char *name, const char *value, int overwrite)
{
char *env = getenv(name);
if ((env && overwrite) || (!env)) {
char *str[32];
strcpy(str, name);
strcat(str, "=");
strcat(str, value);
return putenv(str);
if (!name || !*name || strchr(name, '=') || !value) {
return -1;
}
return -1;
if (!overwrite && getenv(name)) {
return 0;
}
return _putenv_s(name, value) == 0 ? 0 : -1;
}

void unsetenv(const char *name)
{
char *str[32];
strcpy(str, name);
strcat(str, "=");
putenv(str);
if (!name || !*name || strchr(name, '=')) {
return;
}
// An empty value removes the variable in the Windows CRT.
_putenv_s(name, "");
}
4 changes: 4 additions & 0 deletions libnfc/drivers/acr122_pcsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ acr122_pcsc_close(nfc_device *pnd)
{
pn53x_idle(pnd);

#if defined (_WIN32)
SCardDisconnect(DRIVER_DATA(pnd)->hCard, SCARD_UNPOWER_CARD);
#else
SCardDisconnect(DRIVER_DATA(pnd)->hCard, SCARD_LEAVE_CARD);
#endif
acr122_pcsc_free_scardcontext();

pn53x_data_free(pnd);
Expand Down
141 changes: 141 additions & 0 deletions test/test_win32_stdlib.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*-
* Regression checks for the Windows setenv()/unsetenv() replacements.
* Licensed under the GNU Lesser General Public License, version 3 or later.
*
* Run on Windows without NFC hardware or libusb:
* gcc -std=c99 -Wall -Wextra -Werror -I. test/test_win32_stdlib.c \
* contrib/win32/stdlib.c -o test_win32_stdlib.exe
* ./test_win32_stdlib.exe
*/

#ifndef _WIN32
#error These tests require the Windows CRT.
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int setenv(const char *name, const char *value, int overwrite);
void unsetenv(const char *name);

static int failures;
static int checks;

#define CHECK(condition) do { \
checks++; \
if (!(condition)) { \
fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
failures++; \
} \
} while (0)

static int
value_is(const char *name, const char *expected)
{
const char *value = getenv(name);
return value && strcmp(value, expected) == 0;
}

static void
test_overwrite(void)
{
const char *name = "NFC_ENV_TEST";
unsetenv(name);
CHECK(setenv(name, "first", 0) == 0);
CHECK(value_is(name, "first"));
CHECK(setenv(name, "second", 0) == 0);
CHECK(value_is(name, "first"));
CHECK(setenv(name, "second", 1) == 0);
CHECK(value_is(name, "second"));
CHECK(setenv(name, "third", -1) == 0);
CHECK(value_is(name, "third"));
unsetenv(name);
CHECK(getenv(name) == NULL);
unsetenv(name);
CHECK(getenv(name) == NULL);
}

static void
test_long_strings(void)
{
char name[81];
char value[1025];
memset(name, 'N', sizeof(name) - 1);
name[sizeof(name) - 1] = '\0';
memset(value, 'v', sizeof(value) - 1);
value[sizeof(value) - 1] = '\0';

CHECK(setenv(name, "short", 1) == 0);
CHECK(value_is(name, "short"));
unsetenv(name);
CHECK(getenv(name) == NULL);

CHECK(setenv("NFC_ENV_TEST", value, 1) == 0);
CHECK(value_is("NFC_ENV_TEST", value));
unsetenv("NFC_ENV_TEST");
CHECK(getenv("NFC_ENV_TEST") == NULL);

CHECK(setenv(name, value, 1) == 0);
CHECK(value_is(name, value));
CHECK(setenv(name, "ignored", 0) == 0);
CHECK(value_is(name, value));
unsetenv(name);
CHECK(getenv(name) == NULL);
}

static void
test_argument_copy(void)
{
char name[] = "NFC_ENV_COPY";
char value[] = "left=right";
CHECK(setenv(name, value, 1) == 0);
memset(name, 'x', sizeof(name) - 1);
memset(value, 'y', sizeof(value) - 1);
CHECK(value_is("NFC_ENV_COPY", "left=right"));
unsetenv("NFC_ENV_COPY");
CHECK(getenv("NFC_ENV_COPY") == NULL);
}

static void
test_invalid_arguments(void)
{
CHECK(setenv("NFC_ENV_TEST", "keep", 1) == 0);
CHECK(setenv(NULL, "value", 1) == -1);
CHECK(setenv("", "value", 1) == -1);
CHECK(setenv("NFC_ENV_TEST=bad", "value", 1) == -1);
CHECK(setenv("NFC_ENV_TEST", NULL, 1) == -1);
CHECK(setenv("NFC_ENV_TEST", NULL, 0) == -1);
unsetenv(NULL);
unsetenv("");
unsetenv("NFC_ENV_TEST=bad");
CHECK(value_is("NFC_ENV_TEST", "keep"));
unsetenv("NFC_ENV_TEST");
CHECK(getenv("NFC_ENV_TEST") == NULL);
}

static void
test_windows_empty_value(void)
{
// Preserve the Windows CRT convention: an empty value removes the entry.
CHECK(setenv("NFC_ENV_TEST", "keep", 1) == 0);
CHECK(setenv("NFC_ENV_TEST", "", 0) == 0);
CHECK(value_is("NFC_ENV_TEST", "keep"));
CHECK(setenv("NFC_ENV_TEST", "", 1) == 0);
CHECK(getenv("NFC_ENV_TEST") == NULL);
}

int
main(int argc, char **argv)
{
test_overwrite();
// This mode also runs safely against the previous 32-byte implementation.
if (!(argc == 2 && strcmp(argv[1], "--overwrite-only") == 0)) {
test_long_strings();
test_argument_copy();
test_invalid_arguments();
test_windows_empty_value();
}
printf("%d checks, %d failures\n", checks, failures);
return failures ? EXIT_FAILURE : EXIT_SUCCESS;
}