1272343Sngie/* $NetBSD: h_protoent.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 protoent *prp) 39272343Sngie{ 40272343Sngie char **pp; 41272343Sngie 42272343Sngie printf("name=%s, proto=%d, aliases=", 43272343Sngie prp->p_name, prp->p_proto); 44272343Sngie for (pp = prp->p_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 <proto>\n" 54272343Sngie "\t%s -n <name>\n", getprogname(), getprogname(), 55272343Sngie getprogname()); 56272343Sngie exit(1); 57272343Sngie} 58272343Sngie 59272343Sngieint 60272343Sngiemain(int argc, char *argv[]) 61272343Sngie{ 62272343Sngie struct protoent *prp; 63272343Sngie const char *proto = NULL, *name = NULL; 64272343Sngie int c; 65272343Sngie 66272343Sngie while ((c = getopt(argc, argv, "p:n:")) != -1) { 67272343Sngie switch (c) { 68272343Sngie case 'n': 69272343Sngie name = optarg; 70272343Sngie break; 71272343Sngie case 'p': 72272343Sngie proto = optarg; 73272343Sngie break; 74272343Sngie default: 75272343Sngie usage(); 76272343Sngie } 77272343Sngie } 78272343Sngie 79272343Sngie if (proto && name) 80272343Sngie usage(); 81272343Sngie if (proto) { 82272343Sngie if ((prp = getprotobynumber(atoi(proto))) != NULL) 83272343Sngie pserv(prp); 84272343Sngie return 0; 85272343Sngie } 86272343Sngie if (name) { 87272343Sngie if ((prp = getprotobyname(name)) != NULL) 88272343Sngie pserv(prp); 89272343Sngie return 0; 90272343Sngie } 91272343Sngie 92272343Sngie setprotoent(0); 93272343Sngie while ((prp = getprotoent()) != NULL) 94272343Sngie pserv(prp); 95272343Sngie endprotoent(); 96272343Sngie return 0; 97272343Sngie} 98