/*****************************************************************
random numbers in 1st column

$Id: randhead.c,v 1.6 2003/01/19 23:17:09 purnhage Exp $

Source file: randhead.c

(c) Heiko Purnhagen 1994-2003
e-mail: purnhage@tnt.uni-hannover.de
WWW:    http://www.tnt.uni-hannover.de/~purnhage/

This program is available under the terms of the GNU General Public
License. See file COPYING for further details. All usual disclaimers
apply!

Authors:
HP    Heiko Purnhagen, Uni Hannover <purnhage@tnt.uni-hannover.de>

Changes:
18-mar-94   HP      first version written at NTH
13-oct-98   HP      own random, randomize, seed
13.jan-99   HP      randomize also for Solaris/IRIX
**********************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef MSVC
#include <time.h>
#else
#include <sys/times.h>
#endif


/* ---------- declarations ---------- */

#define PROGVER "randhead V0.4 11-jan-99"
#define CVSID "$Id: randhead.c,v 1.6 2003/01/19 23:17:09 purnhage Exp $"
#ifndef DISTVER		/* set in Makefile */
#define DISTVER "<unknown>"
#endif

#define MAXLEN 256

#define MY_RAND_MAX 0x7fffffff


/* ---------- variables ---------- */

char prog[] = "randhead";

FILE *infile;
int linenum = 0;
char linebuf[MAXLEN+1];
int addmode;

long randx = 1;		/* default seed */


/* ---------- local functions ---------- */

/* random1() */
/* Generate random long (uniform distribution 0..RND_MAX). */

long myrandom (void)
{
  randx = (randx * 1103515245) + 12345;
  return (randx & MY_RAND_MAX);
}


/* ---------- main ---------- */

int main (int argc, char *argv[])
{
  int i;
#ifndef MSVC
  struct tms timestms;
#endif

  if (!((argc==2 || argc==3) &&
	(!strcmp(argv[1],"-") || !strcmp(argv[1],"+") ||
	 (argv[1][0]>='0' && argv[1][0]<='9')))) {
    fprintf (stderr,
	     "%s: %s\n"
	      "%s\n\n"
	     "Usage: %s mode [infile]\n"
	     " mode '+' : add random numbers as 1st column (random seed)\n"
	     " mode 'nn': add random numbers as 1st column (seed nn)\n"
	     " mode '-' : remove 1st column\n"
	     "Example: "
	     "randhead + ordered.list | sort | randhead - > randomised.list\n"
	     "\n",
	     prog,PROGVER "   " DISTVER,CVSID,prog);
    exit (1);
  }

  if ((addmode=strcmp(argv[1],"-"))) {
    if (!strcmp(argv[1],"+")) {
      /* Seed the random-number generator with current time */
#ifdef MSVC
      randx = (unsigned)time(NULL);
#else
      randx = (long)times(&timestms);
#endif
    }
    else
      randx = atoi(argv[1]);
  }
  if (argc == 3) {
    if ((infile = fopen(argv[2],"r")) == NULL) {
      fprintf (stderr,"%s: can't open %s\n",prog,argv[2]);
      exit (2);
    }
  }
  else
    infile = stdin;

  while (fgets(linebuf,MAXLEN,infile) != NULL) {
    linenum++;
    if (addmode) {
      printf ("%7ld %s",myrandom() % 10000000,linebuf);
    }
    else {
      i = 0;
      while (linebuf[i] == ' ' || linebuf[i] == '\t')
	i++;
      while (linebuf[i] != ' ' && linebuf[i] != '\t' &&
	     linebuf[i] != '\0' && linebuf[i] != '\n')
	i++;
      while (linebuf[i] == ' ' || linebuf[i] == '\t')
	i++;
      printf ("%s",linebuf+i);
    }
  }

  fclose (infile);
  exit (0);
}

/* end of randhead.c */
