LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 01-22-2006, 12:30 PM   #1
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint Debian Edition (LMDE)
Posts: 215

Rep: Reputation: 31
Proper Use Of gethostname?


First, here's the function signature:
Code:
int gethostname(char *name, size_t len);
What shall we use for name and len? I've seen code that looks like:
Code:
#include <limits.h>
char hostname[HOST_NAME_MAX+1];
gethostname(hostname, sizeof(hostname));
According to the POSIX standard, HOST_NAME_MAX is a "possibly indeterminate" definition meaning that it

Quote:
...shall be omitted from <limits.h> on specific implementations where the corresponding value is equal to or greater than the stated minimum, but is unspecified.
HOST_NAME_MAX is not defined on my Linux box, probably because the kernel developers don't want to specify a maximum host name. I suppose I could throw in a

Code:
#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 256
#endif
but that doesn't seem right to me. So, what length for a host name do you specify on a system that has no defined maximum host name?
 
Old 01-22-2006, 02:27 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
try MAXHOSTNAMELEN

My copy of "Unix Network Programming" (Stevens, 2nd ed) recommends using MAXHOSTNAMELEN, defined in sys/param.h. I tried it; it seems to work OK on my current Linux:
Code:
#include <stdio.h>
#include <unistd.h>
#include <sys/param.h>

int
main (int argc, char *argv[])
{
  char host[MAXHOSTNAMELEN];
  gethostname (host, sizeof host);
  return printf ("host: %s\n", host);
}
 
Old 01-22-2006, 02:39 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Oh yeah - here's a version #ifdef'ed for Windows and *nix:
Code:
#include <stdio.h>
#if defined(WIN32)
  #include <windows.h>
#else
  #include <unistd.h>
  #include <sys/param.h>
#endif

int
main (int argc, char *argv[])
{
#if defined(WIN32)
  char host[MAX_COMPUTERNAME_LENGTH+1];
  int ilen = sizeof (host);
  GetComputerName (host, &ilen);
#else
  char host[MAXHOSTNAMELEN];
  gethostname (host, sizeof host);
#endif
  return printf ("host: %s\n", host);
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Specify proper x server banner Linux - Newbie 1 04-03-2005 04:38 PM
proper use of wine UGT001 Linux - Software 4 11-28-2004 02:25 PM
Finding a proper OS devit Linux - Distributions 2 06-22-2004 10:18 PM
Proper use of permissions ? wmcase Linux - General 4 02-04-2004 03:41 PM
Proper way to shutdown? snocked Linux - Newbie 12 02-11-2003 08:26 AM

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

All times are GMT -5. The time now is 04:40 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration