Accesing struct! after routine! exec
[1/4] from: jvargas::whywire::net at: 2-Jun-2003 12:12
Hello All. I've been having problems with the following script
when trying to access a subroutine in libc. I am using openbsd.
Everything executes well but rebol crashes with a coredump
when I try to access the fields of the struct! after calling the
external function.
REBOL[]
stdclib: %/usr/lib/libc.so.29.0
clib: load/library stdclib
c-string: func [
"Returns a null terminated c-string of size strsize"
strsize [integer!] "The size of the string"
/fill blank [char!] "Character to fill the blanks"
/local str
][
if none? fill [ blank: #"." ]
; fill the blancs
str: make string! strsize
loop strsize [
insert str blank
]
; insert null to terminate the c-string
poke str strsize #"^(null)"
return str
]
utsname: make struct! [
sysname [string!]
nodename [string!]
release [string!]
version [string!]
machine [string!]
] reduce [
c-string 256
c-string 256
c-string 256
c-string 256
c-string 256
c-string 256
]
uname: make routine! compose/deep [
name [struct! [(first utsname)]]
return: [integer!]
] clib "_uname"
uname utsname
probe utsname
Here are the c definitions:
int uname (struct utsname *name );
#define SYS_NMLN 256
struct utsname {
char sysname[SYS_NMLN]; /* Name of this OS. */
char nodename[SYS_NMLN]; /* Name of this network node. */
char release[SYS_NMLN]; /* Release level. */
char version[SYS_NMLN]; /* Version level. */
char machine[SYS_NMLN]; /* Hardware type. */
};
Ideas anyone??? Thanks for your help, Jaime
-- The best way to predict the future is to invent it -- Steve Jobs
[2/4] from: nitsch-lists:netcologne at: 3-Jun-2003 3:54
Jaime Vargas wrote:
>Hello All. I've been having problems with the following script
>when trying to access a subroutine in libc. I am using openbsd.
<<quoted lines omitted: 45>>
>struct utsname {
> char
sysname[SYS_NMLN];
/* Name of this OS. */
> char
nodename[SYS_NMLN];
/* Name of this network node. */
> char
release[SYS_NMLN];
/* Release level. */
> char
version[SYS_NMLN];
/* Version level. */
> char
machine[SYS_NMLN];
/* Hardware type. */
>};
>
Trying to remember c,
the rebol-definition means
struct utsname {
char * sysname;
/* Name of this OS. */
char * nodename;
/* Name of this network node. */
char * release;
/* Release level. */
char * version;
/* Version level. */
char * machine;
/* Hardware type. */
};
which means a struct with pointers.
while the c-defenition is a struct with char-arrays.
if i am right, eventually this works:
string: insert/dup "" " " 5 * SYS_NMLN ; make a filled string
sysname: string
nodename: skip string SYS_NMLN
release: skip nodename SYS_NMLN
...
and pass the string as argument.
sysname: copy/part sysname find sysname #"^(null)"
to get it back.
>Ideas anyone??? Thanks for your help, Jaime
>
>-- The best way to predict the future is to invent it -- Steve Jobs
>
-Volker
[3/4] from: jvargas:whywire at: 3-Jun-2003 10:37
On Monday, June 2, 2003, at 09:54 PM, Volker Nitsch wrote:
> Trying to remember c,
> the rebol-definition means
<<quoted lines omitted: 22>>
> to get it back.
> -Volker
Thanks Volker, It worked like a charm. Is there any other way to do
it using struct! ? I like it because the code is more compact and
explicit.
However, I will use your trick in the meantime. Cheers, Jaime
[4/4] from: greggirwin:mindspring at: 3-Jun-2003 10:14
Hi Jaime,
JV> Thanks Volker, It worked like a charm. Is there any other way to do
JV> it using struct! ? I like it because the code is more compact and
JV> explicit.
I don't know of any other way to include fixed size elements in calls
like that. Sometimes I'll use a generator routine that will build a
struct! definition so at least the code is still small, even though it
generates a lot behind the scenes.
Also, in your c-string routine, think about using insert/dup rather
than looping.
-- Gregg
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted