LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
Posts: 216

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);
}
 
Old 07-17-2019, 10:06 AM   #4
JMerkle
LQ Newbie
 
Registered: Aug 2015
Location: Dallas, TX
Distribution: Debian & Ubuntu
Posts: 1

Rep: Reputation: Disabled
Using Raspberry Pi 3B

1) If you have a Raspberry Pi with desktop image, open Geany IDE.
2) Copy the following into a new file:

// file: hostname.cpp

#include <unistd.h>
#include <stdio.h>
#include <limits.h>

int main(int argc, char *argv[])
{
char hostname[HOST_NAME_MAX];
int status = gethostname(hostname,sizeof(hostname));
printf("HOST_NAME_MAX: %u, Hostname: "%s", status: %d\n",HOST_NAME_MAX,hostname,status);

}

3) save file to "hostname.cpp"
4) click the "Build the current file" button
5) click the "run or view the current file" button

Resulting output:
HOST_NAME_MAX: 64, Hostname: "raspberrypi.frontiernet.net", status: 0

The API works great, but I thought I had already given my device a different hostname... Something else to investigate...
 
Old 07-17-2019, 12:58 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,327
Blog Entries: 24

Rep: Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269Reputation: 4269
Welcome to LQ JMerkle!

You have posted to a thread which has been inactive for 13 years, often called necroposting and generally frowned upon.

If you have a question of your own it is best to open your own thread which will give it better exposure among currently active members.

Please review the Site FAQ for guidance in posting your questions and general forum usage.

Good luck!
 
  


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 02:02 PM.

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