xref: /original-bsd/lib/libc/gen/ualarm.c (revision 89a39cb6)
1 /*
2  * Copyright (c) 1985 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)ualarm.c	5.4 (Berkeley) 06/01/90";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/time.h>
13 
14 #define	USPS	1000000		/* # of microseconds in a second */
15 
16 /*
17  * Generate a SIGALRM signal in ``usecs'' microseconds.
18  * If ``reload'' is non-zero, keep generating SIGALRM
19  * every ``reload'' microseconds after the first signal.
20  */
21 unsigned
22 ualarm(usecs, reload)
23 	register unsigned usecs;
24 	register unsigned reload;
25 {
26 	struct itimerval new, old;
27 
28 	new.it_interval.tv_usec = reload % USPS;
29 	new.it_interval.tv_sec = reload / USPS;
30 
31 	new.it_value.tv_usec = usecs % USPS;
32 	new.it_value.tv_sec = usecs / USPS;
33 
34 	if (setitimer(ITIMER_REAL, &new, &old) == 0)
35 		return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
36 	/* else */
37 		return (-1);
38 }
39