Accessing system variables in Linux from C code - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Accessing system variables in Linux from C code

azidstarazidstar Posts: 10Member
hello everyone.

Here is an easy question.

How do I access (read and write) system variables in linux from a C program?
For example $HOSTNAME.

I've googled... searched this site but to no luck.

Thnx in advance for any help or pointers.

Sincerly Kristoffer Nordstrm

Comments

  • DB1DB1 Posts: 1,142Member
    : hello everyone.
    :
    : Here is an easy question.
    :
    : How do I access (read and write) system variables in linux from a C program?
    : For example $HOSTNAME.
    :
    : I've googled... searched this site but to no luck.
    :
    : Thnx in advance for any help or pointers.
    :
    : Sincerly Kristoffer Nordstrm
    :

    Not sure if this is what you want, but in there are a few environment variable functions.. setenv(), getenv(), putenv(), clearenv(), unsetenv(), and there's also "extern char **environ" (declared in , or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.


    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • azidstarazidstar Posts: 10Member
    Thnx a million.

    That was exactly the functions I was looking for.
    Now I will be able to pass some data between my processes.

    At least I hope so... Im just worried how "global" these variables get .... but as I said thats exactly the answer I was looking for.
    Thnx for the speedy reply.

    Sincerly Kristoffer Nordstrm


    : Not sure if this is what you want, but in there are a few
    environment variable functions.. setenv(), getenv(), putenv(),
    clearenv(), unsetenv(), and there's also "extern char **environ" (declared in , or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
    :
    :
    : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    :
    :

  • DB1DB1 Posts: 1,142Member
    Isn't that what pipe() is used for??



    : Thnx a million.
    :
    : That was exactly the functions I was looking for.
    : Now I will be able to pass some data between my processes.
    :
    : At least I hope so... Im just worried how "global" these variables get .... but as I said thats exactly the answer I was looking for.
    : Thnx for the speedy reply.
    :
    : Sincerly Kristoffer Nordstrm
    :
    :
    : : Not sure if this is what you want, but in there are a few
    : environment variable functions.. setenv(), getenv(), putenv(),
    : clearenv(), unsetenv(), and there's also "extern char **environ" (declared in , or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
    : :
    : :
    : : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    : :
    : :
    :
    :

    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • dennisparkerdennisparker Posts: 478Member
    I believe that pipe() has use in conjunction with fork() which my text tells me creates a duplicate process as the calling process, but with a different process id.

    If you had completely different processes that you wanted to trade information between, I am not sure that you could do it using pipe().



    ;

    : Isn't that what pipe() is used for??
    :
    :
    :
    : : Thnx a million.
    : :
    : : That was exactly the functions I was looking for.
    : : Now I will be able to pass some data between my processes.
    : :
    : : At least I hope so... Im just worried how "global" these variables get .... but as I said thats exactly the answer I was looking for.
    : : Thnx for the speedy reply.
    : :
    : : Sincerly Kristoffer Nordstrm
    : :
    : :
    : : : Not sure if this is what you want, but in there are a few
    : : environment variable functions.. setenv(), getenv(), putenv(),
    : : clearenv(), unsetenv(), and there's also "extern char **environ" (declared in , or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
    : : :
    : : :
    : : : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    : : :
    : : :
    : :
    : :
    :
    : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    :
    :

  • DB1DB1 Posts: 1,142Member
    Yea you're right. Looks like named pipes would work, but there are better ways. http://www.linuxhq.com/guides/LPG/node7.html

    : I believe that pipe() has use in conjunction with fork() which my text tells me creates a duplicate process as the calling process, but with a different process id.
    :
    : If you had completely different processes that you wanted to trade information between, I am not sure that you could do it using pipe().
    :
    :
    :
    : ;
    :
    : : Isn't that what pipe() is used for??
    : :
    : :
    : :
    : : : Thnx a million.
    : : :
    : : : That was exactly the functions I was looking for.
    : : : Now I will be able to pass some data between my processes.
    : : :
    : : : At least I hope so... Im just worried how "global" these variables get .... but as I said thats exactly the answer I was looking for.
    : : : Thnx for the speedy reply.
    : : :
    : : : Sincerly Kristoffer Nordstrm
    : : :
    : : :
    : : : : Not sure if this is what you want, but in there are a few
    : : : environment variable functions.. setenv(), getenv(), putenv(),
    : : : clearenv(), unsetenv(), and there's also "extern char **environ" (declared in , or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
    : : : :
    : : : :
    : : : : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    : : : :
    : : : :
    : : :
    : : :
    : :
    : : [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]
    : :
    : :
    :
    :

    [italic][blue]To understand recursive, first you need to understand recursive[/blue][/italic]

  • azidstarazidstar Posts: 10Member
    Thnx for the answers to both of you.

    Teh situation I have is two entirely different processes with different memory and everything.
    I will definatly look at the article mentioned previuosly .... another thought is to use /proc for this.

    Anyway thnx for the time and effort.

    Sincerly Kristoffer Nordstrm
Sign In or Register to comment.