xref: /netbsd-src/tests/lib/libc/net/h_servent.c (revision 02dfa85f0005a1198af8f1d9a841035068b93429)
1*02dfa85fSjruoho /* $NetBSD: h_servent.c,v 1.2 2011/04/07 18:14:09 jruoho Exp $ */
22517c83dSpgoyette 
32517c83dSpgoyette /*-
42517c83dSpgoyette  * Copyright (c) 2011 The NetBSD Foundation, Inc.
52517c83dSpgoyette  * All rights reserved.
62517c83dSpgoyette  *
72517c83dSpgoyette  * Redistribution and use in source and binary forms, with or without
82517c83dSpgoyette  * modification, are permitted provided that the following conditions
92517c83dSpgoyette  * are met:
102517c83dSpgoyette  * 1. Redistributions of source code must retain the above copyright
112517c83dSpgoyette  *    notice, this list of conditions and the following disclaimer.
122517c83dSpgoyette  * 2. Redistributions in binary form must reproduce the above copyright
132517c83dSpgoyette  *    notice, this list of conditions and the following disclaimer in the
142517c83dSpgoyette  *    documentation and/or other materials provided with the distribution.
152517c83dSpgoyette  *
162517c83dSpgoyette  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
172517c83dSpgoyette  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
182517c83dSpgoyette  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
192517c83dSpgoyette  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
202517c83dSpgoyette  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
212517c83dSpgoyette  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
222517c83dSpgoyette  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
232517c83dSpgoyette  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
242517c83dSpgoyette  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
252517c83dSpgoyette  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
262517c83dSpgoyette  * POSSIBILITY OF SUCH DAMAGE.
272517c83dSpgoyette  */
282517c83dSpgoyette 
292517c83dSpgoyette #include <netdb.h>
302517c83dSpgoyette #include <stdint.h>
312517c83dSpgoyette #include <stdlib.h>
322517c83dSpgoyette #include <unistd.h>
332517c83dSpgoyette #include <netinet/in.h>
342517c83dSpgoyette #include <sys/types.h>
352517c83dSpgoyette #include <stdio.h>
362517c83dSpgoyette 
372517c83dSpgoyette static void
pserv(const struct servent * svp)382517c83dSpgoyette pserv(const struct servent *svp)
392517c83dSpgoyette {
402517c83dSpgoyette 	char **pp;
412517c83dSpgoyette 
422517c83dSpgoyette 	printf("name=%s, port=%d, proto=%s, aliases=",
432517c83dSpgoyette 	    svp->s_name, ntohs((uint16_t)svp->s_port), svp->s_proto);
442517c83dSpgoyette 	for (pp = svp->s_aliases; *pp; pp++)
452517c83dSpgoyette 		printf("%s ", *pp);
462517c83dSpgoyette 	printf("\n");
472517c83dSpgoyette }
482517c83dSpgoyette 
492517c83dSpgoyette static void
usage(void)502517c83dSpgoyette usage(void)
512517c83dSpgoyette {
522517c83dSpgoyette 	(void)fprintf(stderr, "Usage: %s\n"
532517c83dSpgoyette 	    "\t%s -p <port> [-P <proto>]\n"
542517c83dSpgoyette 	    "\t%s -n <name> [-P <proto>]\n", getprogname(), getprogname(),
552517c83dSpgoyette 	    getprogname());
562517c83dSpgoyette 	exit(1);
572517c83dSpgoyette }
582517c83dSpgoyette 
592517c83dSpgoyette int
main(int argc,char * argv[])602517c83dSpgoyette main(int argc, char *argv[])
612517c83dSpgoyette {
622517c83dSpgoyette 	struct servent *svp;
632517c83dSpgoyette 	const char *port = NULL, *proto = NULL, *name = NULL;
642517c83dSpgoyette 	int c;
652517c83dSpgoyette 
662517c83dSpgoyette 	while ((c = getopt(argc, argv, "p:n:P:")) != -1) {
672517c83dSpgoyette 		switch (c) {
682517c83dSpgoyette 		case 'n':
692517c83dSpgoyette 			name = optarg;
702517c83dSpgoyette 			break;
712517c83dSpgoyette 		case 'p':
722517c83dSpgoyette 			port = optarg;
732517c83dSpgoyette 			break;
742517c83dSpgoyette 		case 'P':
752517c83dSpgoyette 			proto = optarg;
762517c83dSpgoyette 			break;
772517c83dSpgoyette 		default:
782517c83dSpgoyette 			usage();
792517c83dSpgoyette 		}
802517c83dSpgoyette 	}
812517c83dSpgoyette 
822517c83dSpgoyette 	if (port && name)
832517c83dSpgoyette 		usage();
842517c83dSpgoyette 	if (port) {
852517c83dSpgoyette 		if ((svp = getservbyport(htons(atoi(port)), proto)) != NULL)
862517c83dSpgoyette 			pserv(svp);
872517c83dSpgoyette 		return 0;
882517c83dSpgoyette 	}
892517c83dSpgoyette 	if (name) {
902517c83dSpgoyette 		if ((svp = getservbyname(name, proto)) != NULL)
912517c83dSpgoyette 			pserv(svp);
922517c83dSpgoyette 		return 0;
932517c83dSpgoyette 	}
942517c83dSpgoyette 
952517c83dSpgoyette 	setservent(0);
962517c83dSpgoyette 	while ((svp = getservent()) != NULL)
972517c83dSpgoyette 		pserv(svp);
982517c83dSpgoyette 	endservent();
992517c83dSpgoyette 	return 0;
1002517c83dSpgoyette }
101