Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing system call output using printf...

Status
Not open for further replies.

GooMooPunch

Programmer
Jan 26, 2004
5
US
I'm quite new to the C language, and i'm sure this is a simple question...

I'm trying to create a prompt that includes the output from the command uname(), but I'm having trouble displaying it properly.

My question is how do I capture the output of the uname() command so I can include it in a printf line?
 
Instead of using the uname command, use the uname function instead
Code:
#include <sys/utsname.h>
int uname(struct utsname *buf);

--
 
Thank you for the reply Salem,

Follow up question:

How can I capture the string data from calling the uname() function? It returns an integer, so I'm not quite sure how to do this...

Thanks
 
The string data is in the utsname struct that is passed to the function as in the following code:

Code:
#include <stdio.h>
#include <sys/utsname.h>

int main() {

  struct utsname name;

  uname(&name);
  printf(&quot;sysname: %s\n&quot;,name.sysname);
  printf(&quot;nodename: %s\n&quot;,name.nodename);
  printf(&quot;release: %s\n&quot;,name.release);
  printf(&quot;version: %s\n&quot;,name.version);
  printf(&quot;machine: %s\n&quot;,name.machine);

}

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top