1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22
23 /*
24 * Copyright (c) 1994, by Sun Microsystems, Inc.
25 */
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <string.h>
33
34 #include <netdb.h>
35 #include "getent.h"
36
37 static int
putservent(const struct servent * sp,FILE * fp)38 putservent(const struct servent *sp, FILE *fp)
39 {
40 char **p;
41 int rc = 0;
42
43 if (sp == NULL) {
44 return (1);
45 }
46
47 if (fprintf(fp, "%-20s %d/%s",
48 sp->s_name, ntohs(sp->s_port), sp->s_proto) == EOF)
49 rc = 1;
50 for (p = sp->s_aliases; *p != 0; p++) {
51 if (fprintf(fp, " %s", *p) == EOF)
52 rc = 1;
53 }
54 if (putc('\n', fp) == EOF)
55 rc = 1;
56 return (rc);
57 }
58
59 /*
60 * getservbyname/addr - get entries from service database
61 * Accepts arguments as:
62 * port/protocol
63 * port
64 * name/protocol
65 * name
66 */
67 int
dogetserv(const char ** list)68 dogetserv(const char **list)
69 {
70 struct servent *sp;
71 int rc = EXC_SUCCESS;
72
73 if (list == NULL || *list == NULL) {
74 while ((sp = getservent()) != NULL)
75 (void) putservent(sp, stdout);
76 } else {
77 for (; *list != NULL; list++) {
78 int port;
79 char key[BUFSIZ];
80 const char *protocol = NULL;
81 char *cp;
82
83 /* Copy string to avoiding modifying the argument */
84 (void) strncpy(key, *list, sizeof (key));
85 key[sizeof (key) - 1] = '\0';
86 /* Split at a '/' to extract protocol number */
87 if ((cp = strchr(key, '/')) != NULL) {
88 *cp = '\0';
89 protocol = cp + 1;
90 }
91 port = htons(atoi(key));
92 if (port != 0)
93 sp = getservbyport(port, protocol);
94 else
95 sp = getservbyname(key, protocol);
96 if (sp == NULL)
97 rc = EXC_NAME_NOT_FOUND;
98 else
99 (void) putservent(sp, stdout);
100 }
101 }
102
103 return (rc);
104 }
105