#ident	"@(#)yp_getalias.c	1.6	96/01/11 SMI"
/*
 *    Copyright (c) 1986-1995  Sun Microsystems, Inc
 *      All rights reserved.
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/statvfs.h>
#include "ypsym.h"
/* this is 14 less the space for temps pids and .pag more or less */

#ifdef DEBUG
#define	YPDBDIR		"/var/ypnew"
#define	ALIASLIST	"/var/ypnew/aliases"
#else
#define	YPDBDIR		"/var/yp"
#define	ALIASLIST	"/var/yp/aliases"
#endif
#define	issep(c) (c == ' ' || c == '\t')

#ifdef SYSVCONFIG
#define	isvar_sysv() (wasitsysv)

static int wasitsysv = TRUE;
static int first_time = TRUE;
static listofnames *list = NULL;
#endif

void sysv_exit();
extern listofnames *names();
extern void exit();
extern void free_listofnames();

/*
 * Setup alias file, check /var/yp filesystem type
 * Note: The *never* checks for aliases under Solaris, so there is no need
 *	 for this function.  As of 1.1 beta 4, I will ifdef it out (cause
 *	 you never know...).  Should go away completely soon.
 */

#ifdef SYSVCONFIG
void
sysvconfig(void)
{
	struct statvfs statbuf;

	sigset(SIGCHLD, SIG_IGN);
	/*
	 * if neccesary free previous list, then read in aliaslist
	 */

	if (!first_time)
		free_listofnames(list);
	else
		first_time = FALSE;

	list = names(ALIASLIST);

	/*
	 *	Check if YP database directory is in a system v filesystem
	 */

	if (statvfs(YPDBDIR, &statbuf) != 0) {
		fprintf(stderr, "Cannot stat %s\n", YPDBDIR);
		exit(-1);
	} else {
		/* if (strcmp(statbuf.f_basetype,"s5")) (doesn't work in k13) */
		if (statbuf.f_namemax == 14)
			wasitsysv = TRUE;
		else
			wasitsysv = FALSE;
	}
	sigset(SIGCHLD, (void (*)())sysvconfig);
}
#endif

/*
 * Match key to alias
 */
int
yp_getalias(key, key_alias, maxlen)
	char *key;
	char *key_alias;
	int maxlen;
{
	listofnames *entry;
	char *longname;
	char *alias;
	char name[256];

#ifndef SYSVCONFIG
	strcpy(key_alias, key);
	return (0);
#else
	/* sysvconfig must be run before this routine */
	if (key == NULL || first_time)
		return (-1);

	if (!isvar_sysv()) {
		strcpy(key_alias, key);
		return (0);
	}

	for (entry = list, strcpy(name, entry->name); entry;
		entry = entry->nextname, strcpy(name, entry->name)) {

		longname = strtok(name, " \t");
		alias = strtok(NULL, " \t\n");
		if (longname == NULL || alias == NULL) {
			continue;
		}
		if (strcmp(longname, key) == 0) {
			if ((int)strlen(alias) > (maxlen)) {
				strncpy(key_alias, alias, (maxlen));
				key_alias[maxlen] = '\0';
			} else {
				strcpy(key_alias, alias);
			}
			return (0);
		}
	}
	/* alias not found */
	return (-1);
#endif
}

/*
 * Match alias to key
 */
int
yp_getkey(key_alias, key, maxlen)
	char *key_alias;
	char *key;
	int maxlen;
{
	listofnames *entry;
	char *longname;
	char *alias;
	char name[256];

#ifndef SYSVCONFIG
	strcpy(key, key_alias);
	return (0);
#else
	if (key_alias == NULL || first_time) {
		return (-1);
	}

	if (!isvar_sysv()) {
		strcpy(key, key_alias);
		return (0);
	}

	for (entry = list, strcpy(name, entry->name);
		entry; entry = entry->nextname, strcpy(name, entry->name)) {

		longname = strtok(name, " \t");
		alias = strtok(NULL, " \t\n");
		if (alias == NULL || longname == NULL) {
			continue;
		}
		if ((strcmp(alias, key_alias) == 0) ||
		    (strncmp(alias, key_alias, maxlen) == 0)) {
			strcpy(key, longname);
			return (0);
		}
	}
	/* key not found */
	return (-1);
#endif
}
