1272343Sngie/* $NetBSD: h_servent.c,v 1.2 2011/04/07 18:14:09 jruoho Exp $ */
2272343Sngie
3272343Sngie/*-
4272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26272343Sngie * POSSIBILITY OF SUCH DAMAGE.
27272343Sngie */
28272343Sngie
29272343Sngie#include <netdb.h>
30272343Sngie#include <stdint.h>
31272343Sngie#include <stdlib.h>
32272343Sngie#include <unistd.h>
33272343Sngie#include <netinet/in.h>
34272343Sngie#include <sys/types.h>
35272343Sngie#include <stdio.h>
36272343Sngie
37272343Sngiestatic void
38272343Sngiepserv(const struct servent *svp)
39272343Sngie{
40272343Sngie	char **pp;
41272343Sngie
42272343Sngie	printf("name=%s, port=%d, proto=%s, aliases=",
43272343Sngie	    svp->s_name, ntohs((uint16_t)svp->s_port), svp->s_proto);
44272343Sngie	for (pp = svp->s_aliases; *pp; pp++)
45272343Sngie		printf("%s ", *pp);
46272343Sngie	printf("\n");
47272343Sngie}
48272343Sngie
49272343Sngiestatic void
50272343Sngieusage(void)
51272343Sngie{
52272343Sngie	(void)fprintf(stderr, "Usage: %s\n"
53272343Sngie	    "\t%s -p <port> [-P <proto>]\n"
54272343Sngie	    "\t%s -n <name> [-P <proto>]\n", getprogname(), getprogname(),
55272343Sngie	    getprogname());
56272343Sngie	exit(1);
57272343Sngie}
58272343Sngie
59272343Sngieint
60272343Sngiemain(int argc, char *argv[])
61272343Sngie{
62272343Sngie	struct servent *svp;
63272343Sngie	const char *port = NULL, *proto = NULL, *name = NULL;
64272343Sngie	int c;
65272343Sngie
66272343Sngie	while ((c = getopt(argc, argv, "p:n:P:")) != -1) {
67272343Sngie		switch (c) {
68272343Sngie		case 'n':
69272343Sngie			name = optarg;
70272343Sngie			break;
71272343Sngie		case 'p':
72272343Sngie			port = optarg;
73272343Sngie			break;
74272343Sngie		case 'P':
75272343Sngie			proto = optarg;
76272343Sngie			break;
77272343Sngie		default:
78272343Sngie			usage();
79272343Sngie		}
80272343Sngie	}
81272343Sngie
82272343Sngie	if (port && name)
83272343Sngie		usage();
84272343Sngie	if (port) {
85272343Sngie		if ((svp = getservbyport(htons(atoi(port)), proto)) != NULL)
86272343Sngie			pserv(svp);
87272343Sngie		return 0;
88272343Sngie	}
89272343Sngie	if (name) {
90272343Sngie		if ((svp = getservbyname(name, proto)) != NULL)
91272343Sngie			pserv(svp);
92272343Sngie		return 0;
93272343Sngie	}
94272343Sngie
95272343Sngie	setservent(0);
96272343Sngie	while ((svp = getservent()) != NULL)
97272343Sngie		pserv(svp);
98272343Sngie	endservent();
99272343Sngie	return 0;
100272343Sngie}
101