C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Accessing system variables in Linux from C code Posted by azidstar on 10 Feb 2005 at 9:48 AM
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
Report
Re: Accessing system variables in Linux from C code Posted by DB1 on 10 Feb 2005 at 10:28 AM
: 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 <stdlib.h> there are a few environment variable functions.. setenv(), getenv(), putenv(), clearenv(), unsetenv(), and there's also "extern char **environ" (declared in <unistd.h>, or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.


To understand recursive, first you need to understand recursive

Report
Re: Accessing system variables in Linux from C code Posted by azidstar on 10 Feb 2005 at 1:40 PM
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 <stdlib.h> there are a few
environment variable functions.. setenv(), getenv(), putenv(),
clearenv(), unsetenv(), and there's also "extern char **environ" (declared in <unistd.h>, or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
:
:
: To understand recursive, first you need to understand recursive
:
:

Report
Re: Accessing system variables in Linux from C code Posted by DB1 on 10 Feb 2005 at 4:23 PM
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 <stdlib.h> there are a few
: environment variable functions.. setenv(), getenv(), putenv(),
: clearenv(), unsetenv(), and there's also "extern char **environ" (declared in <unistd.h>, or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
: :
: :
: : To understand recursive, first you need to understand recursive
: :
: :
:
:

To understand recursive, first you need to understand recursive

Report
Re: Accessing system variables in Linux from C code Posted by dennisparker on 10 Feb 2005 at 5:57 PM
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 <stdlib.h> there are a few
: : environment variable functions.. setenv(), getenv(), putenv(),
: : clearenv(), unsetenv(), and there's also "extern char **environ" (declared in <unistd.h>, or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
: : :
: : :
: : : To understand recursive, first you need to understand recursive
: : :
: : :
: :
: :
:
: To understand recursive, first you need to understand recursive
:
:

Report
Re: Accessing system variables in Linux from C code Posted by DB1 on 11 Feb 2005 at 12:49 AM
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 <stdlib.h> there are a few
: : : environment variable functions.. setenv(), getenv(), putenv(),
: : : clearenv(), unsetenv(), and there's also "extern char **environ" (declared in <unistd.h>, or you can declare it yourself) which lets you access the environment variables as an array of strings in the form of `name=value'.
: : : :
: : : :
: : : : To understand recursive, first you need to understand recursive
: : : :
: : : :
: : :
: : :
: :
: : To understand recursive, first you need to understand recursive
: :
: :
:
:

To understand recursive, first you need to understand recursive

Report
Re: Accessing system variables in Linux from C code Posted by azidstar on 11 Feb 2005 at 6:13 AM
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



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.