xref: /onnv-gate/usr/src/cmd/ypcmd/ypalias.c (revision 702:9495c7c1ed3a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  *
22*702Sth160488  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
270Sstevel@tonic-gate /*	  All Rights Reserved   */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
310Sstevel@tonic-gate  * under license from the Regents of the University of
320Sstevel@tonic-gate  * California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <limits.h>
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <sys/statvfs.h>
420Sstevel@tonic-gate #include "ypsym.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate #ifdef SYSVCONFIG
450Sstevel@tonic-gate extern void sysvconfig();
460Sstevel@tonic-gate #endif
470Sstevel@tonic-gate 
480Sstevel@tonic-gate extern int yp_getalias();
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /*
510Sstevel@tonic-gate  *	Given a domain name, return its system v alias.
520Sstevel@tonic-gate  *	If there is no alias name in the alias file,
530Sstevel@tonic-gate  *	create one. Rule of creation is to take the 1st
540Sstevel@tonic-gate  *	NAME_MAX-4 characters and concatenate the last 4 characters.
550Sstevel@tonic-gate  *	If the alias in the file is too long, trim off the end.
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate 
580Sstevel@tonic-gate static void
mkdomain_alias(name,result)590Sstevel@tonic-gate mkdomain_alias(name, result)
600Sstevel@tonic-gate char *name, *result;
610Sstevel@tonic-gate {
620Sstevel@tonic-gate 	int retval;
630Sstevel@tonic-gate 	char tmpbuf[MAXNAMLEN] = {NULL};
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	retval = yp_getalias(name, result, NAME_MAX);
660Sstevel@tonic-gate 	if (retval == -1) {
670Sstevel@tonic-gate 		if ((int)strlen(name) > NAME_MAX) {
680Sstevel@tonic-gate 			strncpy(result, name, NAME_MAX-4);
690Sstevel@tonic-gate 			strncpy(&result[NAME_MAX-4],
700Sstevel@tonic-gate 			    &name[strlen(name)-4], 4);
710Sstevel@tonic-gate 			result[NAME_MAX] = '\0';
720Sstevel@tonic-gate 		} else
730Sstevel@tonic-gate 			strcpy(result, name);
740Sstevel@tonic-gate 	} else if ((retval) && (int)strlen(result) > NAME_MAX) {
750Sstevel@tonic-gate 		strncpy(tmpbuf, result, NAME_MAX);
760Sstevel@tonic-gate 		strcpy(result, tmpbuf);
770Sstevel@tonic-gate 	}
780Sstevel@tonic-gate }
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  *	Given a map name, return its system v alias .
820Sstevel@tonic-gate  *	If there is no alias name in the alias file,
830Sstevel@tonic-gate  *	create one. Rule of creation is to take the 1st
840Sstevel@tonic-gate  *	MAXALIASLEN-4 characters and concatenate the last 4 characters.
850Sstevel@tonic-gate  *	If the alias in the file is too long, trim off the end.
860Sstevel@tonic-gate  */
870Sstevel@tonic-gate static void
mkmap_alias(name,result)880Sstevel@tonic-gate mkmap_alias(name, result)
890Sstevel@tonic-gate char *name, *result;
900Sstevel@tonic-gate {
910Sstevel@tonic-gate 	int retval;
920Sstevel@tonic-gate 	char tmpbuf[MAXNAMLEN] = {NULL};
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	retval = yp_getalias(name, result, MAXALIASLEN);
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	if (retval == -1) {
970Sstevel@tonic-gate 		if ((int)strlen(name) > MAXALIASLEN) {
980Sstevel@tonic-gate 			(void) strncpy(result, name, MAXALIASLEN-4);
990Sstevel@tonic-gate 			(void) strncpy(&result[MAXALIASLEN-4],
1000Sstevel@tonic-gate 			    &name[strlen(name)-4], 4);
1010Sstevel@tonic-gate 			result[MAXALIASLEN] = '\0';
1020Sstevel@tonic-gate 		} else
1030Sstevel@tonic-gate 			(void) strcpy(result, name);
1040Sstevel@tonic-gate 	} else if ((retval) && (int)strlen(result) > MAXALIASLEN) {
1050Sstevel@tonic-gate 		(void) strncpy(tmpbuf, result, MAXALIASLEN);
1060Sstevel@tonic-gate 		(void) strcpy(result, tmpbuf);
1070Sstevel@tonic-gate 	}
1080Sstevel@tonic-gate }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate #ifdef MAIN
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * executed only for the command ypalias
1140Sstevel@tonic-gate  * and not when ypbind or ypserv make use
1150Sstevel@tonic-gate  * of this file.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static char usage[] =
1190Sstevel@tonic-gate "Usage:\n\
1200Sstevel@tonic-gate 	ypalias -d domainname\n\
1210Sstevel@tonic-gate 	ypalias mapname\n";
1220Sstevel@tonic-gate 
123*702Sth160488 int
main(argc,argv)1240Sstevel@tonic-gate main(argc, argv)
1250Sstevel@tonic-gate char **argv;
1260Sstevel@tonic-gate {
1270Sstevel@tonic-gate 	char result[MAXNAMLEN] = {NULL};
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate #ifdef SYSVCONFIG
1300Sstevel@tonic-gate 	sysvconfig();
1310Sstevel@tonic-gate #endif
1320Sstevel@tonic-gate 	if (argc <= 1)
1330Sstevel@tonic-gate 		goto err;
1340Sstevel@tonic-gate 	if (strcmp(argv[1], "-d") == 0)
1350Sstevel@tonic-gate 		if (argc == 3) mkdomain_alias(argv[2], (char *)&result);
1360Sstevel@tonic-gate 		else goto err;
1370Sstevel@tonic-gate 	else if (argc == 2)
1380Sstevel@tonic-gate 		mkmap_alias(argv[1], (char *)&result);
1390Sstevel@tonic-gate 	else
1400Sstevel@tonic-gate 		goto err;
1410Sstevel@tonic-gate 	(void) printf("%s", result);
1420Sstevel@tonic-gate 	return (0);
1430Sstevel@tonic-gate err:
1440Sstevel@tonic-gate 	(void) fprintf(stderr, usage);
1450Sstevel@tonic-gate 	return (1);
1460Sstevel@tonic-gate }
1470Sstevel@tonic-gate #endif /* MAIN */
148