/*
** Falling Tower KAI
**	Copyright (c) 2000	ROYALPANDA	All rights reserved
**
**	May 25, 2000 Ver 1.0
*/

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/types.h>
#include<pwd.h>

static long Seed;


int rnd( int range )
{
	long tmp,hi,lo;

	hi = Seed / 127773;
	lo = Seed % 127773;
	Seed = 16807 * lo - 2836 * hi;
	if( Seed <= 0 )
		Seed += 0x7fffffff;

	return Seed % range;
}


void srnd( int seed )
{
	Seed = seed;
}


void get_name( char *buf, int bufsiz, int uid )
{
	struct passwd *pw;
	int i;
	char *q,*r;
	char b[80];

	bufsiz--;

	if( ( pw = getpwuid(uid) ) == NULL ){
		sprintf(b,"noname(UID=%-5.5d)",uid);
		strncpy(buf,b,bufsiz);
	}else{
		i = 0;
		q = buf;
		r = pw->pw_gecos;
		while( i < bufsiz && *r != '\0' && *r != ',' ){
			*q++ = iscntrl(*r) ? ' ' : *r;
			r++;
			i++;
		}
		if( i > 0 )
			*q = '\0';
		else
			strncpy(buf,pw->pw_name,bufsiz);
	}
}
