ualarm.c revision 8870
1147075Sbrooks/*
250476Speter * Copyright (c) 1985, 1993
3147075Sbrooks *	The Regents of the University of California.  All rights reserved.
4147075Sbrooks *
5147075Sbrooks * Redistribution and use in source and binary forms, with or without
6147075Sbrooks * modification, are permitted provided that the following conditions
7147075Sbrooks * are met:
8147075Sbrooks * 1. Redistributions of source code must retain the above copyright
9147075Sbrooks *    notice, this list of conditions and the following disclaimer.
10147075Sbrooks * 2. Redistributions in binary form must reproduce the above copyright
11147075Sbrooks *    notice, this list of conditions and the following disclaimer in the
12147075Sbrooks *    documentation and/or other materials provided with the distribution.
13147075Sbrooks * 3. All advertising materials mentioning features or use of this software
14147075Sbrooks *    must display the following acknowledgement:
15147075Sbrooks *	This product includes software developed by the University of
16147075Sbrooks *	California, Berkeley and its contributors.
17147075Sbrooks * 4. Neither the name of the University nor the names of its contributors
18147075Sbrooks *    may be used to endorse or promote products derived from this software
19147075Sbrooks *    without specific prior written permission.
20147075Sbrooks *
21147075Sbrooks * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22147075Sbrooks * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23147075Sbrooks * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24147075Sbrooks * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25147075Sbrooks * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26147075Sbrooks * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27147075Sbrooks * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28147075Sbrooks * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29147075Sbrooks * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30147075Sbrooks * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31147075Sbrooks * SUCH DAMAGE.
32147075Sbrooks */
3343855Sobrien
34147075Sbrooks#if defined(LIBC_SCCS) && !defined(lint)
35147075Sbrooksstatic char sccsid[] = "@(#)ualarm.c	8.1 (Berkeley) 6/4/93";
36147075Sbrooks#endif /* LIBC_SCCS and not lint */
3791306Smurray
38147075Sbrooks#include <sys/time.h>
39147087Sbrooks#include <unistd.h>
40147596Sru
41147596Sru#define	USPS	1000000		/* # of microseconds in a second */
4291306Smurray
43198236Sru/*
44198236Sru * Generate a SIGALRM signal in ``usecs'' microseconds.
45147075Sbrooks * If ``reload'' is non-zero, keep generating SIGALRM
46 * every ``reload'' microseconds after the first signal.
47 */
48unsigned
49ualarm(usecs, reload)
50	register unsigned usecs;
51	register unsigned reload;
52{
53	struct itimerval new, old;
54
55	new.it_interval.tv_usec = reload % USPS;
56	new.it_interval.tv_sec = reload / USPS;
57
58	new.it_value.tv_usec = usecs % USPS;
59	new.it_value.tv_sec = usecs / USPS;
60
61	if (setitimer(ITIMER_REAL, &new, &old) == 0)
62		return (old.it_value.tv_sec * USPS + old.it_value.tv_usec);
63	/* else */
64		return (-1);
65}
66