xref: /illumos-gate/usr/src/cmd/getent/dogetserv.c (revision 142d813a)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, by Sun Microsystems, Inc.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/socket.h>
317c478bd9Sstevel@tonic-gate #include <netinet/in.h>
327c478bd9Sstevel@tonic-gate #include <string.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <netdb.h>
357c478bd9Sstevel@tonic-gate #include "getent.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate static int
putservent(const struct servent * sp,FILE * fp)387c478bd9Sstevel@tonic-gate putservent(const struct servent *sp, FILE *fp)
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	char **p;
417c478bd9Sstevel@tonic-gate 	int rc = 0;
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
447c478bd9Sstevel@tonic-gate 		return (1);
457c478bd9Sstevel@tonic-gate 	}
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (fprintf(fp, "%-20s %d/%s",
48*142d813aSToomas Soome 	    sp->s_name, ntohs(sp->s_port), sp->s_proto) == EOF)
497c478bd9Sstevel@tonic-gate 		rc = 1;
507c478bd9Sstevel@tonic-gate 	for (p = sp->s_aliases; *p != 0; p++) {
517c478bd9Sstevel@tonic-gate 		if (fprintf(fp, " %s", *p) == EOF)
527c478bd9Sstevel@tonic-gate 			rc = 1;
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate 	if (putc('\n', fp) == EOF)
557c478bd9Sstevel@tonic-gate 		rc = 1;
567c478bd9Sstevel@tonic-gate 	return (rc);
577c478bd9Sstevel@tonic-gate }
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*
607c478bd9Sstevel@tonic-gate  * getservbyname/addr - get entries from service database
617c478bd9Sstevel@tonic-gate  * Accepts arguments as:
627c478bd9Sstevel@tonic-gate  *	port/protocol
637c478bd9Sstevel@tonic-gate  *	port
647c478bd9Sstevel@tonic-gate  *	name/protocol
657c478bd9Sstevel@tonic-gate  *	name
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate int
dogetserv(const char ** list)687c478bd9Sstevel@tonic-gate dogetserv(const char **list)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate 	struct servent *sp;
717c478bd9Sstevel@tonic-gate 	int rc = EXC_SUCCESS;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	if (list == NULL || *list == NULL) {
747c478bd9Sstevel@tonic-gate 		while ((sp = getservent()) != NULL)
757c478bd9Sstevel@tonic-gate 			(void) putservent(sp, stdout);
767c478bd9Sstevel@tonic-gate 	} else {
777c478bd9Sstevel@tonic-gate 		for (; *list != NULL; list++) {
787c478bd9Sstevel@tonic-gate 			int port;
797c478bd9Sstevel@tonic-gate 			char key[BUFSIZ];
807c478bd9Sstevel@tonic-gate 			const char *protocol = NULL;
817c478bd9Sstevel@tonic-gate 			char *cp;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 			/* Copy string to avoiding modifying the argument */
847c478bd9Sstevel@tonic-gate 			(void) strncpy(key, *list, sizeof (key));
85*142d813aSToomas Soome 			key[sizeof (key) - 1] = '\0';
867c478bd9Sstevel@tonic-gate 			/* Split at a '/' to extract protocol number */
877c478bd9Sstevel@tonic-gate 			if ((cp = strchr(key, '/')) != NULL) {
88*142d813aSToomas Soome 				*cp = '\0';
897c478bd9Sstevel@tonic-gate 				protocol = cp + 1;
907c478bd9Sstevel@tonic-gate 			}
917c478bd9Sstevel@tonic-gate 			port = htons(atoi(key));
927c478bd9Sstevel@tonic-gate 			if (port != 0)
937c478bd9Sstevel@tonic-gate 				sp = getservbyport(port, protocol);
947c478bd9Sstevel@tonic-gate 			else
957c478bd9Sstevel@tonic-gate 				sp = getservbyname(key, protocol);
967c478bd9Sstevel@tonic-gate 			if (sp == NULL)
977c478bd9Sstevel@tonic-gate 				rc = EXC_NAME_NOT_FOUND;
987c478bd9Sstevel@tonic-gate 			else
997c478bd9Sstevel@tonic-gate 				(void) putservent(sp, stdout);
1007c478bd9Sstevel@tonic-gate 		}
1017c478bd9Sstevel@tonic-gate 	}
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	return (rc);
1047c478bd9Sstevel@tonic-gate }
105