alarm.c 840 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * alarm() for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #include "syscalls.h"
  10. #include <unistd.h>
  11. libc_hidden_proto(alarm)
  12. #ifdef __NR_alarm
  13. #define __NR___alarm __NR_alarm
  14. _syscall1(unsigned int, alarm, unsigned int, seconds);
  15. #else
  16. #include <sys/time.h>
  17. libc_hidden_proto(setitimer)
  18. unsigned int alarm(unsigned int seconds)
  19. {
  20. struct itimerval old, new;
  21. unsigned int retval;
  22. new.it_value.tv_usec = 0;
  23. new.it_interval.tv_sec = 0;
  24. new.it_interval.tv_usec = 0;
  25. new.it_value.tv_sec = (long int) seconds;
  26. if (setitimer(ITIMER_REAL, &new, &old) < 0) {
  27. return 0;
  28. }
  29. retval = old.it_value.tv_sec;
  30. if (old.it_value.tv_usec) {
  31. ++retval;
  32. }
  33. return retval;
  34. }
  35. #endif
  36. libc_hidden_def(alarm)