zcaudate
zcaudate

Reputation: 14258

how to define a char array in chicken scheme

I'm looking to follow the http://www.tcpdump.org/pcap.html example using chicken scheme but am stuck when looking to translate this to use the ffi:

#include <stdio.h>
#include <pcap.h>

int main(int argc, char *argv[])
{
    char *dev, errbuf[PCAP_ERRBUF_SIZE];

    dev = pcap_lookupdev(errbuf);
    if (dev == NULL) {
        fprintf(stderr, "Couldn't find default device: %s\n", errbuf);
        return(2);
    }
    printf("Device: %s\n", dev);
    return(0);
}

specifically, I wish to define a variable errbuf[PCAP_ERRBUF_SIZE] and call pcap_lookupdev using the ffi interface.

Any pointers will be appreciated.

Upvotes: 1

Views: 232

Answers (1)

wasamasa
wasamasa

Reputation: 320

The following code has been developed against CHICKEN 5.2.0 and compiled with csc -L -lpcap pcap-test.scm:

(module pcap-test ()

(import scheme)
(import (chicken base))
(import (chicken blob))
(import (chicken foreign))
(import (chicken format))

#>
#include <string.h>
#include <pcap.h>
<#

(define stderr (current-error-port))

(define pcap_lookupdev (foreign-lambda c-string "pcap_lookupdev" blob))
(define strdup (foreign-lambda c-string* "strdup" blob))

(define errbuf (make-blob (foreign-value "PCAP_ERRBUF_SIZE" int)))
(define dev (pcap_lookupdev errbuf))

(when (not dev)
  (fprintf stderr "Couldn't find default device: ~a\n" (strdup errbuf))
  (exit 2))

(printf "Device: ~a\n" dev)
(exit 0)

)

Upvotes: 1

Related Questions