A Few Questions About: libc

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.

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #5557

    scurty
    Participant

    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

    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

    #5559

    abakobo
    Participant

    Mark 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

    #5560

    scurty
    Participant

    That only seems to only convert an Array of Int’s to their to Unicode equivalents…

    #5561

    codifies
    Participant
    [/crayon]
    #5562

    abakobo
    Participant

    indeed…

    There is the FromCString for that purpose..?

    [/crayon]

    But 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.

    #5563

    scurty
    Participant

    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!

    #5568

    codifies
    Participant

    @scurty are you on windows ’cause USER works for me! whereas USERNAME does not…

    maybe check both and see which one isn’t empty?

    #5569

    scurty
    Participant

    USERNAME is for Linux. xD Sorry forgot to mention that. xD

    #5570

    codifies
    Participant

    narp 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!

    #5571

    scurty
    Participant

    I’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.

    #5572

    abakobo
    Participant

    So “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]

    can be declared simply by

    [/crayon]
    #5573

    codifies
    Participant

    @scurty I love standards – there are so many to choose from… problem is what you fix others will probably consider broken…

    @abakobo personally I’m not keep on implicit declarations – makes code less readable, explicit declaration might mean more typing but its worth it in the long run!

    #5574

    scurty
    Participant

    Duplicate variables are in principle still not good. I just love K.I.S.S.

Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.