#if 0
 # /etc/services:
 ...
 echo        7/tcp
 echo        7/udp
 discard     9/tcp       sink null
 discard     9/udp       sink null
 ...
 daytime     13/tcp
 daytime     13/udp
 ...
 ftp-data    20/tcp
 ftp     21/tcp
 ...
 ssh     22/tcp              # SSH Remote Login Protocol
 ssh     22/udp 

/* 
 * reads the next line from the file /etc/services and returns a structure
 * servent containing  the  broken  out  fields  from  the  line.
 *      struct servent { 
 *          char  *s_name;      /* official service name */
 *          char **s_aliases;   /* alias list */ 
 *          int    s_port;      /* port number */ 
 *          char  *s_proto;     /* protocol to use */
 *       }
 * /
struct servent *getservent(void);

/* 
 * returns a servent structure for the line from /etc/services that matches
 * the  service name using protocol proto. eg.
 *    struct servent *sp = getservbyname ("telnet", "tcp");
 */
struct servent *getservbyname(const char *name, const char *proto);

/* 
 * returns a servent structure for the line that matches the port given
 * in network byte order using protocol proto. eg.
 *    struct servent *sp = getservbyname (13, "tcp");
 */
struct servent *getservbyport(int port, const char *proto);

/* opens and rewinds the /etc/services file. If stayopen is true (1),
 * then the file will not be closed  between calls to getservbyname()
 * and getservbyport().
 */
void setservent(int stayopen);

/* closes /etc/services */
void endservent(void);

#endif

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <netinet/in.h>

int main (int argc,char **argv) {
    struct servent *sp;
    int    i;

    while (sp = getservent ()) {
        printf("%s:\n\tPort: %d\n\tProtocol: %s\n\tAliases: ", sp->s_name,
            ntohs(sp->s_port), sp->s_proto);

        i = 0;
        for (i = 0; sp->s_aliases[i] != NULL; ++i)
            printf("%s ",sp->s_aliases[i]);
        printf ("\n");
    }
    return 0;
}
