Chapter Contents

Previous

Next
getservent

getservent



Gets Next Entry in Services File

Portability: UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
CAUTION
PORTABILITY
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS


SYNOPSIS

#include <netdb.h>

struct servent *getservent(void);


DESCRIPTION

getservent returns a pointer to the next sequential entry in the services file, that is, a file with the same format as the /etc/services file on a UNIX operating system. Refer to <netdb.h> for details on the servent structure.

Refer to Search Logic for information on the logic used to determine the location of the services file.


RETURN VALUE

If getservent succeeds, it returns a pointer to the servent structure. A null pointer indicates an error or an end-of-file.


CAUTION

The value that getservent returns points to a static structure within the library. You must copy the information from this structure before you make further getservbyname , getservbyport , or getservent calls.


PORTABILITY

getservent is portable to other environments, including most UNIX systems, that implement BSD sockets.


IMPLEMENTATION

getservent is ported directly from the BSD UNIX Socket Library.


EXAMPLE

This program demonstrates the socket calls: endservent , getservent , and setservent . GETSENT attempts to open a prefix.ETC.SERVICES file to obtain local configuration data; where prefix is described in Network Administration.

#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
main(int argc, char *argv[])
{
   struct servent *serv;

   if (argc < 3) {
      puts("Incorrect parameters. Use:");
      puts("   gsbnm service-name protocol-name");
      return EXIT_FAILURE;
   }

   /* getservbyname() - opens the etc.services file and returns the */
   /* values for the requested service and protocol.                */

   serv = getservbyname(argv[1], argv[2]);
   if (serv == NULL) {
      printf("Service "%s" not found for protocol "%s"\n",
         argv[1], argv[2]);
      return EXIT_FAILURE;
   }

   /* Print it. */
   printf("Name: %-15s  Port: %5d    Protocol: %-6s\n",
             serv->s_name,ntohs(serv->s_port),serv->s_proto);
   return EXIT_SUCCESS;
}


RELATED FUNCTIONS

endservent , setservent , getservbyname , getservbyport


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.