djb@stoney.mitre.org (David J. Braunegg) (12/01/90)
I figured out how to pass strings from a C function into Lisp in AKCL.
I give it below in #1 for the interested.
I want to write the Lisp function machine-instance and I am trying to
use the C system function gethostname(2) (see #2 below). However, the
call to gethostname goes bad (#3 below). However, the equivalent C
code compiled with cc *does* work (#4 below). If anyone has any
clues as to why the Clines is failing, please let me know.
Thanks,
Dave
1) Passing strings (this works):
(in-package 'user)
(Clines
"
#include <strings.h>
object abc()
{
char *abc_string;
abc_string = (char *)malloc(4*(sizeof(char)));
strcpy(abc_string, \"abc\");
return(make_simple_string(abc_string));
}")
(defentry abc-lisp () (object abc))
(defun test ()
(abc-lisp))
(test)
"abc"
2) I tried the same thing using gethostname (which does work if I
write and compile the equivalent C code). The code and error are
shown below:
(in-package 'user)
(Clines
"
#include <sys/param.h>
#include <syscall.h>
object foobar()
{
char *hostname_string;
hostname_string = (char *)malloc(MAXHOSTNAMELEN*(sizeof(char)));
gethostname(hostname_string, MAXHOSTNAMELEN);
return(make_simple_string(hostname_string));
}")
(defentry foo-bar () (object foobar))
(defun test ()
(foo-bar))
3) The error I get has to do with gethostname.
>(compile-file "clines")
Compiling clines.lsp.
End of Pass 1.
End of Pass 2.
OPTIMIZE levels: Safety=0 (No runtime error checking), Space=0, Speed=3
Finished compiling clines.
#"clines.o"
>(load "clines")
Loading clines.o
undefined _gethostname symbolstart address -T 2815bc Finished loading clines.o
240
>(test)
Error: NIL is not a VECTOR
Error signalled by TEST.
Broken at TEST. Type :H for Help.
>>:q
4) The following C code, however, runs fine using cc:
#include <sys/param.h>
#include <syscall.h>
char *foobar()
{
char *hostname_string;
hostname_string = (char *)malloc(MAXHOSTNAMELEN*(sizeof(char)));
gethostname(hostname_string, MAXHOSTNAMELEN);
return(hostname_string);
}
main(argc, argv)
int argc;
char *argv[];
{
printf("%s\n", foobar());
}
Then
a.out
stoney (the machine name)