getservbyname Subroutine

Purpose

Gets service entry by name.

Library

Standard C Library (libc.a)

Syntax

#include <netdb.h>
struct servent *getservbyname ( Name,  Protocol)
char *Name, *Protocol;

Description

The getservbyname subroutine is threadsafe. However, the return value points to static data that is overwritten by subsequent calls. This data must be copied to be saved for use by subsequent calls.

The getservbyname subroutine retrieves an entry from the /etc/services file using the service name as a search key.

An application program can use the getservbyname subroutine to access a service, service aliases, the protocol for the service, and a protocol port number for the service.

The getservbyname subroutine searches the /etc/services file sequentially from the start of the file until it finds one of the following:

  • Matching name and protocol number
  • Matching name when the Protocol parameter is set to 0
  • End of the file

Upon locating a matching name and protocol, the getservbyname subroutine returns a pointer to the servent structure, which contains fields for a line of information from the /etc/services file. The netdb.h file defines the servent structure and structure fields.

Use the endservent subroutine to close the /etc/services file.

All applications containing the getservbyname subroutine must be compiled with the _BSD macro set to a specific value. Acceptable values are 43 and 44. In addition, all socket applications must include the BSD libbsd.a library.

Parameters

Item Description
Name Specifies the name of a service.
Protocol Specifies a protocol for use with the specified service.

Return Values

The getservbyname subroutine returns a pointer to a servent structure when a successful match occurs. Entries in this structure are in network byte order.

If an error occurs or the end of the file is reached, the getservbyname subroutine returns a null pointer.

Files

Item Description
/etc/services Contains service names.