Chapter Contents |
Previous |
Next |
gethostname |
Portability: | UNIX compatible |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
PORTABILITY | |
IMPLEMENTATION | |
PORTABILITY | |
EXAMPLE | |
RELATED FUNCTIONS |
SYNOPSIS |
int gethostname(char *name, int namelen);
DESCRIPTION |
gethostname
returns the host name for the current processor.
namelen
is the size of the
name
array. The returned host
name is null-terminated unless there is not enough space in
namelen
.
RETURN VALUE |
If
gethostname
succeeds, it returns a
0
. Otherwise, it returns a
-1
, and sets
errno
to indicate the type of error.
PORTABILITY |
gethostname
is portable to other environments, including most UNIX
systems, that implement BSD sockets. These other socket library implementations
do not require the inclusion of the
<netdb.h>
header file. The SAS/C
<netdb.h>
header file contains
#pragma map
statements to create unique eight-character identifiers
for the OS/390 and CMS linking utilities.
IMPLEMENTATION |
The value that
gethostname
returns is assigned by the local host's TCP/IP software,
which must be running in order for
gethostid
to succeed.
PORTABILITY |
gethostname
is portable to other environments, including most UNIX
systems, that implement BSD sockets. To reduce incompatibilities caused by
failure to include
<netdb.h>
in existing source code, a
#pragma map
statement for this function is also available in
<sys/types.h>
.
EXAMPLE |
This program uses the socket call,
gethostname
to return the hostname
as defined to the local host. The local host must have an operational TCPIP
stack.
#include <stdlib.h> #include <netdb.h> #include <stdio.h> main() { char buf[128]; /* gethostname returns the hostname in the buf array. If */ /* gethostname() succeeds it returns a 0, otherwise a -1 and */ /* errno is set accordingly. */ if (gethostname(buf,sizeof(buf))) { perror("Can't retrive host name"); return EXIT_FAILURE; } printf("%s\n",buf); return EXIT_SUCCESS; }
RELATED FUNCTIONS |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.