Skip to content

Instantly share code, notes, and snippets.

@mik9
Created October 1, 2012 19:49
Show Gist options
  • Select an option

  • Save mik9/3814025 to your computer and use it in GitHub Desktop.

Select an option

Save mik9/3814025 to your computer and use it in GitHub Desktop.
Morning Alarms
#include <stdio.h>
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
/*
* This expects the new RTC class driver framework, working with
* clocks that will often not be clones of what the PC-AT had.
* Use the command line to specify another RTC if you need one.
*/
static const char default_rtc[] = "/dev/rtc0";
int days_per_month[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int main(int argc, char **argv)
{
int fd, retval;
struct tm *tm;
time_t now;
const char *rtc = default_rtc;
/* Get current time */
now = time(0);
tm = localtime(&now);
if ((tm->tm_year + 1900) % 4 != 0) {
days_per_month[1]--;
}
if (tm->tm_hour > 7) {
tm->tm_mday++;
}
tm->tm_hour = 6;
tm->tm_min = 59;
tm->tm_sec = 30;
if (tm->tm_mday > days_per_month[tm->tm_mon]) {
tm->tm_mday = 1;
tm->tm_mon++;
}
if (tm->tm_mon > 12) {
tm->tm_mon = 0;
tm->tm_year++;
}
fd = open(rtc, O_RDONLY);
if (fd == -1) {
perror(rtc);
exit(errno);
}
retval = ioctl(fd, RTC_ALM_SET, tm);
if (retval == -1) {
perror("RTC_ALM_SET ioctl");
exit(errno);
}
/* Enable alarm interrupts */
retval = ioctl(fd, RTC_AIE_ON, 0);
if (retval == -1) {
perror("RTC_AIE_ON ioctl");
exit(errno);
}
/* Read the current alarm settings */
retval = ioctl(fd, RTC_ALM_READ, tm);
if (retval == -1) {
perror("RTC_ALM_READ ioctl");
exit(errno);
}
fprintf(stdout, "Alarm time now set to %d-%d-%d, %02d:%02d:%02d.\n",
tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900,
tm->tm_hour, tm->tm_min, tm->tm_sec);
setgid(1000); setuid(1000);
system("at 7:00 -f /home/mik/play");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment