Sync usage with man page.
[netbsd-mini2440.git] / regress / lib / libc / servent / servent.c
blobebed482483ca5ae323af9d21c566852cfdb0126d
1 #include <netdb.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <netinet/in.h>
6 #include <sys/types.h>
7 #include <stdio.h>
9 static void
10 pserv(const struct servent *svp)
12 char **pp;
14 printf("name=%s, port=%d, proto=%s, aliases=",
15 svp->s_name, ntohs((uint16_t)svp->s_port), svp->s_proto);
16 for (pp = svp->s_aliases; *pp; pp++)
17 printf("%s ", *pp);
18 printf("\n");
21 static void
22 usage(void)
24 (void)fprintf(stderr, "Usage: %s\n"
25 "\t%s -p <port> [-P <proto>]\n"
26 "\t%s -n <name> [-P <proto>]\n", getprogname(), getprogname(),
27 getprogname());
28 exit(1);
31 int
32 main(int argc, char *argv[])
34 struct servent *svp;
35 const char *port = NULL, *proto = NULL, *name = NULL;
36 int c;
38 while ((c = getopt(argc, argv, "p:n:P:")) != -1) {
39 switch (c) {
40 case 'n':
41 name = optarg;
42 break;
43 case 'p':
44 port = optarg;
45 break;
46 case 'P':
47 proto = optarg;
48 break;
49 default:
50 usage();
54 if (port && name)
55 usage();
56 if (port) {
57 if ((svp = getservbyport(htons(atoi(port)), proto)) != NULL)
58 pserv(svp);
59 return 0;
61 if (name) {
62 if ((svp = getservbyname(name, proto)) != NULL)
63 pserv(svp);
64 return 0;
67 setservent(0);
68 while ((svp = getservent()) != NULL)
69 pserv(svp);
70 endservent();
71 return 0;