#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/time.h>


extern int		optind;
extern char	   *optarg;

long			random_seed = 0;
int				fixed_seed = 0;

char		   *charset = "abcdefghijklmnopqrstuvwxyz"
						  "ABCDEFGHIJKLMNOPQRSTYVWXYZ"
						  "0123456789"
						  "!@#$%^&*()_-=+{}[]|:;,.?/~ ";
int				charset_ilen;
double			charset_len;

static void	miscinit(void);
static void usage(char *progname);


int
main (int argc, char *argv[])
{
	int		opt;
	int		errors = 0;
	int		idx;

	while ((opt = getopt(argc, argv, "s:h")) != EOF)
	{
		switch (opt)
		{
			case 's':	random_seed = strtol(optarg, NULL, 10);
						fixed_seed = 1;
						break;

			case 'h':	errors++;
						break;

			default:	fprintf(stderr, "unknown option %c\n", opt);
						errors++;
						break;
		}
	}
	if (errors)
		usage(argv[0]);

	miscinit();

	for (;;)
	{
		idx = (int)(drand48() * charset_len);
		if (idx < charset_ilen)
			putchar(charset[idx]);
	}
}


static void
usage(char *progname)
{
	fprintf(stderr, "usage: %s [-s seed]\n", progname);
	exit(1);
}


static void
miscinit(void)
{
	struct timeval	tv;

	if (!fixed_seed)
	{
		gettimeofday(&tv, NULL);
		random_seed = tv.tv_usec;
	}
	srand48(random_seed);

	charset_ilen = strlen(charset);
	charset_len = (double)strlen(charset);
}


