view pcsc-test/pcsc-test1.c @ 127:141489d31667

fc-simtool: a38 command implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 29 Jan 2021 03:40:20 +0000
parents 1d7d8615d628
children
line wrap: on
line source

#include <sys/types.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <pcsclite.h>
#include <winscard.h>

main(argc, argv)
	char **argv;
{
	SCARDCONTEXT hContext;
	LONG rv;
	DWORD dwReaders;
	LPSTR mszReaders;
	char *cp;

	rv = SCardEstablishContext(SCARD_SCOPE_SYSTEM, NULL, NULL, &hContext);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardEstablishContext: %s\n",
			pcsc_stringify_error(rv));
		exit(1);
	}
	rv = SCardListReaders(hContext, NULL, NULL, &dwReaders);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardListReaders 1st call: %s\n",
			pcsc_stringify_error(rv));
		SCardReleaseContext(hContext);
		exit(1);
	}
	printf("dwReaders = %u\n", (unsigned) dwReaders);
	if (dwReaders < 1) {
		fprintf(stderr,
	"error: dwReaders returned by SCardListReaders() is less than 1\n");
		SCardReleaseContext(hContext);
		exit(1);
	}
	mszReaders = malloc(dwReaders);
	if (!mszReaders) {
		perror("malloc for readers list");
		SCardReleaseContext(hContext);
		exit(1);
	}
	*mszReaders = '\0';
	rv = SCardListReaders(hContext, NULL, mszReaders, &dwReaders);
	if (rv != SCARD_S_SUCCESS) {
		fprintf(stderr, "SCardListReaders 2nd call: %s\n",
			pcsc_stringify_error(rv));
		SCardReleaseContext(hContext);
		exit(1);
	}
	for (cp = mszReaders; *cp; ) {
		printf("Reader: %s\n", cp);
		cp += strlen(cp) + 1;
	}
	printf("End of list of readers\n");
	SCardReleaseContext(hContext);
	exit(0);
}