Merge commit '00f1a4f432b3d8aad1aa270e91c44c57f03ef407'
[unleashed.git] / usr / src / cmd / getent / dogetserv.c
blob9eac7ca1ed20a32b3e24bbe16752d8e116bb8b92
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright (c) 1994, by Sun Microsystems, Inc.
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <string.h>
33 #include <netdb.h>
34 #include "getent.h"
36 static int
37 putservent(const struct servent *sp, FILE *fp)
39 char **p;
40 int rc = 0;
42 if (sp == NULL) {
43 return (1);
46 if (fprintf(fp, "%-20s %d/%s",
47 sp->s_name, ntohs(sp->s_port), sp->s_proto) == EOF)
48 rc = 1;
49 for (p = sp->s_aliases; *p != 0; p++) {
50 if (fprintf(fp, " %s", *p) == EOF)
51 rc = 1;
53 if (putc('\n', fp) == EOF)
54 rc = 1;
55 return (rc);
59 * getservbyname/addr - get entries from service database
60 * Accepts arguments as:
61 * port/protocol
62 * port
63 * name/protocol
64 * name
66 int
67 dogetserv(const char **list)
69 struct servent *sp;
70 int rc = EXC_SUCCESS;
72 if (list == NULL || *list == NULL) {
73 while ((sp = getservent()) != NULL)
74 (void) putservent(sp, stdout);
75 } else {
76 for (; *list != NULL; list++) {
77 int port;
78 char key[BUFSIZ];
79 const char *protocol = NULL;
80 char *cp;
82 /* Copy string to avoiding modifying the argument */
83 (void) strncpy(key, *list, sizeof (key));
84 key[sizeof (key) - 1] = '\0';
85 /* Split at a '/' to extract protocol number */
86 if ((cp = strchr(key, '/')) != NULL) {
87 *cp = '\0';
88 protocol = cp + 1;
90 port = htons(atoi(key));
91 if (port != 0)
92 sp = getservbyport(port, protocol);
93 else
94 sp = getservbyname(key, protocol);
95 if (sp == NULL)
96 rc = EXC_NAME_NOT_FOUND;
97 else
98 (void) putservent(sp, stdout);
102 return (rc);