[Haskell-beginners] Re: ffi woes

Dean Herington heringtonlacey at mindspring.com
Sat Feb 20 03:04:42 EST 2010


At 11:15 PM +0100 2/19/10, Jose A. Ortega Ruiz wrote:
>Stephen Tetley <stephen.tetley at gmail.com> writes:
>
>>  Hi
>>
>>  I think I've found a version 30 from Debian's archive - the essid
>>  string is fixed length, rather than sized by essid_len (even though
>>  essid_len is now in the struct). Is this the same as the version you
>>  have?
>
>Yes, it is. I've tried to read the essid field both using peekCString,
>as in
>
>    s <- (#peek struct wireless_info, essid)
>    peekCString s
>
>and providing explicitly the maximum length of essid (34) to
>peekCStringLen. Do you mean that for a struct field that is a of type
>char[LEN] those peek functions are innappropriate?
>
>(I've checked using C that essid_len actually gets the correct value,
>and that the contents of essid is null-terminated, i.e.
>wi.essid[wi.essid_len] == '\0' is always true).
>
>Thanks,
>jao
>
>>
>>
>>  /* Structure for storing all wireless information for each device
>>   * This is a cut down version of the one above, containing only
>>   * the things *truly* needed to configure a card.
>>   * Don't add other junk, I'll remove it... */
>>  typedef struct wireless_config
>>  {
>>    char		name[IFNAMSIZ + 1];	/* Wireless/protocol name */
>>    int		has_nwid;
>>    iwparam	nwid;			/* Network ID */
>>    int		has_freq;
>>    double	freq;			/* Frequency/channel */
>>    int		freq_flags;
>>    int		has_key;
>>    unsigned char	key[IW_ENCODING_TOKEN_MAX];	/* Encoding key used */
>>    int		key_size;		/* Number of bytes */
>>    int		key_flags;		/* Various flags */
>>    int		has_essid;
>>    int		essid_on;
>>    char		essid[IW_ESSID_MAX_SIZE + 2];	/* ESSID 
>>(extended network) */
>>    int		essid_len;
>>    int		has_mode;
>>    int		mode;			/* Operation mode */
>>  } wireless_config;
>>
>>  Best wishes
>>
>  > Stephen

Though I'm not an FFI expert, I'll take a stab in case it might help you.

It seems to me that

>    s <- (#peek struct wireless_info, essid)
>    peekCString s

would read a pointer from the essid field, which is clearly not what 
you want.  I think you'd want to use #ptr.  Maybe something like the 
following (untested!):

     l <- (#peek struct wireless_config, essid_len) wc
     p <- (#ptr struct wireless_info, essid) wc
     -- wireless_config.essid is a char[MAX_LEN]
     peekCStringLen (p, fromIntegral (l :: CInt))

Dean


More information about the Beginners mailing list