- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to put hostname in a variable in a C program ?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2002 11:18 PM
тАО07-24-2002 11:18 PM
In a C program, I would like to put the server hostname (and more generally the result of a shell command) in a variable. I can show it on stdout with the following piece of program but I can't store it:
main()
{
char nodeName[9];
system("/usr/bin/hostname");
exit(0);
}
M.Piguel
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2002 11:41 PM
тАО07-24-2002 11:41 PM
Re: How to put hostname in a variable in a C program ?
e.g.
man gethostent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-24-2002 11:48 PM
тАО07-24-2002 11:48 PM
Re: How to put hostname in a variable in a C program ?
I assume this is more involved in C (I'm no C hacker, only do things in Perl, but the principle, as Perl inherited almost all from the standard C libs, should be the same)
For this I guess you will have to fork, open a pipe, and do an exec* with the system command in the child's part, and read the output in the parent's part from the pipe's reader handle.
I could code this easily in Perl, but as said not in C, so please also have a look at the manpages of
fork
exec
pipe
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2002 12:03 AM
тАО07-25-2002 12:03 AM
Re: How to put hostname in a variable in a C program ?
#include
envvar=getenv("TERM");
printf("The value for the environment variable TERM is ");
if(envvar)
{ printf("%s\n",envvar); }
else { printf("not set.\n"); }
}
Unfortunately, HOSTNAME is in most cases not a environment variable, but maybe you can set it yourself: export HOSTNAME=`uname -n`.
Regards,
Ceesjan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-25-2002 03:46 AM