/**
* Function: To sleep for a given number of milliseconds.
* The standard Linux sleep() function can only sleep 
* in seconds. This function can be changed also to 
* sleep for micro seconds.
*
* Author: Asanga Udugama (adu@comnets.uni-bremen.de)
* License: GPL
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char *argv[]) 
{
	struct timeval tv;
	int retval, milisec;

	if(argc != 2) {
		printf("usage mysleep milisec\n");
		exit(1);
	}
	
	if(sscanf(argv[1], "%d", &milisec) != 1) {
		printf("invalid milsec\n");
		exit(1);
	} 
	
	/* Wait given milli seconds. */
	tv.tv_sec = (milisec - (milisec % 1000)) / 1000;
	tv.tv_usec = (milisec - (tv.tv_sec * 1000)) * 1000; 

	retval = select(0, NULL, NULL, NULL, &tv);

	if(retval == -1)
		perror("select()");
	else
		printf("Slept for %d milliseconds.\n", milisec);

	return 0;
}
