// written by Jesse Dutton <jessedutton@gmail.com>

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


// so pause() returns
void do_alarm(int i){
	return;
}

// make sure that there is one arg, and that it is a positive int
int check_usage(int argc, char *argv[]){
	int sec=0;

	if (argc == 2) sec=atoi(argv[1]);
	if (sec <= 0){
		printf("Usage: %s seconds\n", argv[0]);
		exit(1);
	}

	return sec;
}

int main(int argc, char *argv[]){
	time_t start, now;
	int seconds;
	size_t len;

	seconds = check_usage(argc, argv);
	signal(SIGALRM, &do_alarm);
	len = strlen(argv[1])+1;
	start = now = time(0);
	
	while (now-start < seconds){
		alarm(1);
		pause();
		now = time(0);
		snprintf(argv[1], len, "%li", seconds + start - now);
	}

	return 0;
}

