1 /* pcscio.c -- input/output through PCSClite */
10 #include <sys/select.h>
17 int pthread_detach __P((pthread_t pth)) {
18 fprintf (stderr, "ERROR: pthread_detach called\n");
23 int main (int argc, char *argv []) {
30 if (SCardEstablishContext (SCARD_SCOPE_SYSTEM, NULL, NULL, &ctx) != SCARD_S_SUCCESS) {
31 fprintf (stderr, "Failed to contact pcscd\n");
36 LPTSTR readers=NULL, reader=NULL;
37 fprintf (stderr, "Usage: %s readername\n", argv [0]);
38 if ((SCardListReaders (ctx, NULL, NULL, &listsz) == SCARD_S_SUCCESS)
39 && (readers=malloc (listsz))
40 && (SCardListReaders (ctx, NULL, readers, &listsz) == SCARD_S_SUCCESS)) {
41 fprintf (stderr, "Currently attached readers:\n");
44 fprintf (stderr, " - %s\n", reader);
45 reader = reader + strlen (reader);
52 SCardReleaseContext (ctx);
55 err = SCardConnect (ctx, argv [1], SCARD_SHARE_EXCLUSIVE, SCARD_PROTOCOL_T1, &card, &actproto);
56 if (err != SCARD_S_SUCCESS) {
57 fprintf (stderr, "Failed to access the smart card\n");
60 fprintf (stderr, "Yippy!\n");
62 BYTE txbuf [275], rxbuf [275];
65 int txlen = read (0, txbuf, sizeof (txbuf));
67 perror ("Error on stdin");
69 } else if (txlen == 0) {
73 req.dwProtocol = actproto;
74 req.cbPciLength = sizeof (req);
75 rxlen = sizeof (rxbuf);
76 err = SCardTransmit (card, &req, txbuf, txlen, &req, rxbuf, &rxlen);
77 if (err != SCARD_S_SUCCESS) {
78 fprintf (stderr, "Error writing to smart card\n");
81 if (write (1, txbuf, rxlen) < rxlen) {
82 perror ("Partial output");
88 SCardReleaseContext (ctx);