About Monkey 2 › Forums › Monkey 2 Programming Help › A Few Questions About: libc
This topic contains 12 replies, has 3 voices, and was last updated by
scurty 2 years, 4 months ago.
-
AuthorPosts
-
December 3, 2016 at 9:35 am #5557
I have a few questions in regards to the libc Module. I’m trying to covert a “libc.char_t *” (from returned from “libc.getenv()” ) to a String.
I know that a “libc.char_t *” is a pointer, so I tried just referencing it to maybe get a value, but I got a memory access error for some reason.
This seems to make sense to me but it’s obviously not valid. xD
Monkey12Local USERNAME:libc.char_t Ptr = libc.getenv("USER")Print(Cast<String>(USERNAME[0]))Error: Can’t convert “char_t” to a “String”
How would I get retrieve environment variables from libc?
Any help would be greatly appreciated. xD Thanks.Edit: I’m currently getting this values from a simple
Monkey1libc.system("echo $USER > data.txt")December 3, 2016 at 10:38 am #5559Mark just added String.FromChars a few days ago. It’s in the develop branch on github. I suppose it’s for that purpose..
https://github.com/blitz-research/monkey2/commit/b2976c674ad445a321b8f9c9c175c4025336b664
December 3, 2016 at 10:46 am #5560That only seems to only convert an Array of Int’s to their to Unicode equivalents…
December 3, 2016 at 11:19 am #5561[/crayon]Monkey12345[crayon-5cba8b0ae4e95283956521 inline="true" ]#import "<libc>"Function Main()Local USERNAME:libc.char_t Ptr = libc.getenv("USER")Print(String.FromCString(USERNAME))EndDecember 3, 2016 at 11:23 am #5562indeed…
There is the FromCString for that purpose..?
[/crayon]Monkey1234567891011[crayon-5cba8b0ae7096346344262 inline="true" ]#Import "<std>"Using std..Function Main()Local USERNAME:= libc.getenv("USER")Local s:=String.FromCString(USERNAME)Print sEndBut the argument of get env is wrong:
Function getenv:char_t Ptr( name:@cstring )So “USER” has to be converted to a cstring (don’t know what the @ means!)
from the docs:
monkey:monkey.types.String.ToCString
Method ToCString:Void( buf:Void Ptr, bufSize:Int )
Converts the string to a CString.If there is enough room in the memory buffer, a null terminating ‘0’ is appended to the CString.
Parameters
buf Memory buffer to write the CString to.
bufSize Size of the memory buffer in bytes.December 3, 2016 at 11:45 am #5563Monkey12Local USERNAME:libc.char_t Ptr = libc.getenv("USERNAME")Print(String.FromCString(USERNAME))Sorry I didn’t realize I used the wrong Environment Variable to retrieve. I meant “USERNAME” xD
“String.FromCString” is what I need! Thanks so much!
December 3, 2016 at 2:15 pm #5568@scurty are you on windows ’cause USER works for me! whereas USERNAME does not…
maybe check both and see which one isn’t empty?
December 3, 2016 at 2:22 pm #5569USERNAME is for Linux. xD Sorry forgot to mention that. xD
December 3, 2016 at 2:24 pm #5570narp not here! its USER (Void Linux) let me reboot and see Antix (non systemd debian variant)… (brb)
Antix Linux also uses USER … NOT USERNAME
its login that sets the variable see also LOGNAME… just for extra confusion!
December 3, 2016 at 2:41 pm #5571I’ve always wanted to make my own Distro, and try and fix a few things I’ve noticed such as the Environment Vars. Pre-load it with a nice looking, light weight GUI(Openbox or XFCE/LXDE) with the some of the user-space written in mostly in Monkey2. No way I could do it myself. xD
Addition: I’m running the new Zorin OS 12 Core. And I don’t have a “USER” just a “USERNAME” and a “LOGNAME”
I’ll switch it to “LOGNAME” since both of our distros support it.Monkey12345678Function GetEnv:String(value)Local variable:libc.char_t Ptr = libc.getenv(value)return String.FromCString(variable)End FunctionFunction Main:Void()Print(GetEnv("LOGNAME"))End FunctionDecember 3, 2016 at 3:31 pm #5572So “USER” has to be converted to a cstring
looks like it’s implicitly converting on this side
On Windows “USERNAME” is working..
Note that
[/crayon]Monkey123[crayon-5cba8b0af1bee290110532 inline="true" ]Local variable:libc.char_t Ptr = libc.getenv(value)can be declared simply by
[/crayon]Monkey123[crayon-5cba8b0af1bf3549486858 inline="true" ]Local variable:= libc.getenv(value)December 3, 2016 at 4:03 pm #5573December 3, 2016 at 4:51 pm #5574Duplicate variables are in principle still not good. I just love K.I.S.S.
-
AuthorPosts
You must be logged in to reply to this topic.