LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-12-2004, 12:40 PM   #1
gearoid
Member
 
Registered: Nov 2003
Location: Ireland
Distribution: Red Hat 9
Posts: 49

Rep: Reputation: 15
accessing system variables in c++


hey!!

how do I access system variables (e.g. username, date, kernel version, etc. etc.) from a c++ program? Or does anyone know of any good sites I could check to see more on this?? I just want to be able to issue such shell commands as uname and who, but from a c++ program. Or could I just fork and exec these shell commands from my c++ program?? Ok, I'm rambling.... Can anyone help me out??

Thanks,
Gearóid
 
Old 05-12-2004, 12:50 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: accessing system variables in c++

Quote:
Originally posted by gearoid
how do I access system variables (e.g. username, date, kernel version, etc. etc.) from a c++ program?
You can get this info mainly from the files and directories in /proc. All programs like ps, uname, top, free, vmstat, etc. read their information from the files in /proc.

Just read and parse the information from the files in /proc, or e.g. to see who is the owner of PID 850, call stat() on the file /proc/850.

See "man proc" for more info about the format of the files in /proc.
Maybe also read "man utmp" for info about users logging in and out.
Quote:
Or could I just fork and exec these shell commands from my c++ program??
Yes, that would work also. But it's ugly and inefficient. Use popen() (or some C++ equivalent if it exists) instead of fork() and the exec() family of functions.

Last edited by Hko; 05-12-2004 at 01:00 PM.
 
Old 05-12-2004, 05:37 PM   #3
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
'man 2 uname' for the system call uname.
 
Old 05-13-2004, 01:56 AM   #4
gearoid
Member
 
Registered: Nov 2003
Location: Ireland
Distribution: Red Hat 9
Posts: 49

Original Poster
Rep: Reputation: 15
hey, thanks.

So I can just open these system files and read the contents as if they were just regular text files? So I'll mostly be using the fstream library and reading from all of these various system files to get whatever system variables I need?

Cool, thanks for the help.
 
Old 05-13-2004, 05:55 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Yes
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
accessing variables inside a function bahadur Programming 1 03-27-2005 04:41 AM
Setting global system variables AzidStar Linux - Newbie 5 03-11-2005 09:03 AM
How to set System Enviroment Variables cathodion Linux - General 2 06-22-2004 05:05 PM
How to keep enviroment variables after reboot the system ..? Hechizebrio Linux - General 2 09-26-2003 05:30 PM
System wide Environment variables Steave Linux - General 1 02-08-2002 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration