/* 
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*	Copyright (c) 1987 AT&T	*/
/*	  All Rights Reserved  	*/

/*	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T	*/
/*	The copyright notice above does not evidence any   	*/
/*	actual or intended publication of such source code.	*/

#ident "$Header: form_lang.c 1.1 90/01/09 $@(#)form_lang.c	1.1 89/12/01"

/*
 * form_lang -- split up an environ variable
 *
 *
 * int form_lang(env_var, lang, terr, code)
 *	char *env_var;		 environ variable text   (size NAME_MAX + 1)
 *	char *lang;		 language name string	 (size NAME_MAX + 1)
 *	char *terr;		 territory name string   (size NAME_MAX + 1)
 *	char *code;		 codeset name string	 (size NAME_MAX + 1)
 *
 * DESCRIPTION:
 *	splits up the given name into it's components
 *	the values are checked for correct length
 *	the buffers have to be as long as described above
 *
 * RETURNS:
 *    lang:	language name string
 *    terr:	territory name string
 *    code:	codeset name string
 *
 * RETURNVALUES:
 *     0:		all is OK
 *    -1:		given env_var not correct (to long or not set)
 */

#include "limits.h"


extern char *index();

/*
 * for portability recognise that strchr sometimes is called index
 */
int
form_lang(env_var, lang, terr, code)
char *env_var;		/* environ variable text   (size NAME_MAX + 1)  */
char *lang;		/* language name string	 (size NAME_MAX + 1)	*/
char *terr;		/* territory name string (size NAME_MAX + 1)    */
char *code;		/* codeset name string	 (size NAME_MAX + 1)	*/
{
    if (env_var == (char *)0 || strlen(env_var) > NAME_MAX) {
	code[0] = '\0';
	terr[0] = '\0';
	lang[0] = '\0';
	return -1;
    }

    strcpy(lang, env_var);

    /* split 'LANG_VAR' into lang, terr, code */

    if ((env_var = index(lang, '.')) != (char *)0) {
	*env_var++ = '\0';
	strcpy(code, env_var);
    }
    else {
	code[0] = '\0';
    }

    if ((env_var = index(lang, '_')) != (char *)0) {
	*env_var++ = '\0';
	strcpy(terr, env_var);
    }
    else {
	terr[0] = '\0';
    }

    if (lang[0] == '\0') {
	return -1;
    }

    return 0;
}
