Page 1 of 2

apply a structure to memory data

Posted: Mon Jun 04, 2007 10:04 pm
by frederic
Hello, i don't know if this pb is linux specific but i would like to apply a structure to data memory.

The function uname_ return a pointer to some structured information and i want to work with this info easily with the structure "utsname" but how ?

For the moment, i use PeekS, but it stops at the end of first string (normal it's his job).

Code: Select all

Structure  utsname 
       sysname.s
       nodename.s
       release.s
       version.s
       machine.s
EndStructure

Global *test=AllocateMemory(SizeOf(utsname)) 

If uname_(*test) = 0

        Debug PeekS(*test)

Else
        Debug "error"
EndIf

Posted: Mon Jun 04, 2007 10:08 pm
by Kaeru Gaman
use other components than .s

.s is a 4byte long pointer to a string, not the string itself.

Posted: Mon Jun 04, 2007 10:10 pm
by netmaestro
I'm not 100% sure what it is you're asking, but this is how you might set/retrieve the strings in the structure:

Code: Select all

Structure  utsname 
       sysname.s 
       nodename.s 
       release.s 
       version.s 
       machine.s 
EndStructure 

Global *test.utsname = AllocateMemory(SizeOf(utsname)) 

With *test
  \sysname = "MySystem"
  \nodename = "MyNode"
  \release = "1.0"
  \version = "1"
  \machine = "MyMachine"
EndWith

With *test
  Debug \sysname 
  Debug \nodename 
  Debug \release 
  Debug \version 
  Debug \machine 
EndWith

Posted: Mon Jun 04, 2007 10:10 pm
by srod

Code: Select all

Structure  utsname 
       sysname.s 
       nodename.s 
       release.s 
       version.s 
       machine.s 
EndStructure 

Global *test.utsname=AllocateMemory(SizeOf(utsname)) 
*test\sysname = "Sys"
*test\nodename = "Node"

Debug *test\sysname
Beware though that when freeing the memory pointed to by *test, the memory allocated to the individual strings will not be freed.

There's a thread somewhere in these forums which discusses this and shows a workaround.

**EDIT: too slow by far! :)

Posted: Mon Jun 04, 2007 10:12 pm
by netmaestro
Ha! I did them all and still beatcha!

Posted: Mon Jun 04, 2007 10:14 pm
by srod
netmaestro wrote:Ha! I did them all and still beatcha!
It's this damn dialup internet connection I'm having to use at the moment!

That's my excuse anyhow!

Posted: Mon Jun 04, 2007 10:14 pm
by frederic
Your example doesn't works with the function uname_

I've an Invalid Memory Access on this line " Debug \sysname"

Code: Select all

Structure  utsname 
       sysname.s
       nodename.s
       release.s
       version.s
       machine.s
EndStructure

Global *test.utsname=AllocateMemory(SizeOf(utsname)) 

uname_(*test) 

With *test
  Debug \sysname
  Debug \nodename
  Debug \release
  Debug \version
  Debug \machine
EndWith 

Posted: Mon Jun 04, 2007 10:15 pm
by srod
What is uname_() ?

Posted: Mon Jun 04, 2007 10:17 pm
by netmaestro
The structure is probably wrong then, could they be fixedstrings or byte arrays? Something other than null-terminated strings, anyhow.

Posted: Mon Jun 04, 2007 10:17 pm
by frederic
uname_ is a linux function that retrieve some system information

Code: Select all

int uname(struct utsname *buf);  
DESCRIPTION
uname returns system information in the structure pointed to by buf. The utsname struct is defined in <sys/utsname.h>:

    struct utsname {
    char sysname[];
    char nodename[];
    char release[];
    char version[];
    char machine[];
    #ifdef _GNU_SOURCE
    char domainname[];
    #endif
    };

The length of the arrays in a struct utsname is unspecified; the fields are NUL-terminated.  
with this i've no error, but i don't know if it's correct

Code: Select all

Structure  utsname 
       sysname.s{255}
       nodename.s{255}
       release.s{255}
       version.s{255}
       machine.s{255}
EndStructure

Posted: Mon Jun 04, 2007 10:18 pm
by srod
Indeed, we can now see that this is an entirely different kind of structure.

Thinking...

Posted: Mon Jun 04, 2007 10:19 pm
by Kale
another way:

Code: Select all

Structure utsname
	sysname.s
	nodename.s
	release.s
	version.s
	machine.s
EndStructure

Procedure uname_()
	Static MyStruct.utsname
	MyStruct\sysname = "sysname"
	MyStruct\nodename = "nodename"
	MyStruct\release = "release"
	MyStruct\version = "version"
	MyStruct\machine = "machine"
	ProcedureReturn @MyStruct
EndProcedure

Global *test.utsname = uname_()

Debug *test\sysname
Debug *test\nodename
Debug *test\release
Debug *test\version
Debug *test\machine

Posted: Mon Jun 04, 2007 10:25 pm
by frederic
ok with a scan of memory it appears that the data are (some datas :)) :

Code: Select all

0807CA74  4C 69 6E 75 78 00 00 00 00 00 00 00 00 00 00 00  Linux...........
0807CA84  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CA94  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAA4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAB4  00 50 75 72 65 66 72 69 65 6E 64 73 00 00 00 00  .Purefriends....
0807CAC4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAD4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAE4  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CAF4  00 00 32 2E 36 2E 32 30 2D 31 36 2D 67 65 6E 65  ..2.6.20-16-gene
0807CB04  72 69 63 00 00 00 00 00 00 00 00 00 00 00 00 00  ric.............
0807CB14  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
0807CB24  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
so i must to count how many bytes there are for a string or "WE" know how many byte is a char xxxx[]; in linux language

Posted: Mon Jun 04, 2007 10:28 pm
by srod
Not sure you've followed the thread correctly kale. uname_() is an external function.

frederick, I'd be a little surprised if the fixed length strings work if the structure in question is supposed to contain variable length strings with a null terminator.

The thing is about PB structures, string fields are stored as pointers rather than an array of charcaters embedded within the structure. This explains the invalid memory access error etc.

Fixed length strings are moving in the right direction in that the characters are stored directly within the structure etc. but here we stumble because of the the fact that your structures needs variable length strings etc.

I reckon you'll just have to allocate a large buffer and write your own code to read and write the string fields etc. Should be easy enough.

Posted: Mon Jun 04, 2007 10:45 pm
by Fred
The first time ever i've seen a structure like that...