/*
** alarm v2.0
**
** Start program with alarm timer set
**
** Copyright (C) 30.9.1992 by Andreas Ley <ley@rz.uni-karlsruhe.de>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**
** This program has been tested on a HP9000/715 with HP-UX A.09.07
** In this environment, neither lint -u nor gcc -Wall -pedantic -fsyntax-only
** produce any messages. If you encounter any errors or need to make any
** changes to port it to another platform, please contact me.
**
** Version history
**
** Version 2.0 - 5.12.1997
**	Rewrite, correct error handling
**
** Version 1.0 - 30.9.1992
**	Initial version
*/

static char copyright[] = "@(#)Copyright (C) 1992-1997 by Andreas Ley (ley@rz.uni-karlsruhe.de)";
static char sccsid[] = "@(#)alarm v2.0 - Start program with alarm timer set";


#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#ifndef FALSE
#define	FALSE	(0)		/* This is the naked Truth */
#define	TRUE	(1)		/* and this is the Light */
#endif


char	*image;
int	debug=0;


static void usage()
{
	(void)fprintf(stderr,"Usage: %s [-h] [-v] timeout command [arguments...]\n",image);
	exit(1);
}


int main(argc,argv)
int	argc;
char	*argv[];
{
	int		c;
	extern char	*optarg;
	extern int	optind;

	if ((image=strrchr(argv[0],'/')))
		image++;
	else
		image=argv[0];

	while ((c=getopt(argc,argv,"Dvh?"))!=EOF) {
		if (debug)
			(void)fprintf(stderr,"getopt: %d %c\n",optind,c);
		switch ((char)c) {
		case 'D':
			debug++;
			break;
		case 'v':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
			exit(0);
		case 'h':
			(void)fprintf(stderr,"%s\n",sccsid+4);
			(void)fprintf(stderr,"%s\n",copyright+4);
		case '?':
			usage();
		}
	}

	if (optind+2>argc)
		usage();

	(void)alarm(atoi(argv[optind++]));
	if (execvp(argv[optind],&argv[optind])) {
		(void)fprintf(stderr,"%s: can't execvp ",image);
		perror(argv[optind]);
		exit(1);
	}
	return(0);
}

