00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <cal.h>
00023 #include <time.h>
00024 #include <alarmd/alarm_data.h>
00025
00026 typedef enum {
00027 ALARM_ERR = -1,
00028
00029
00030 ALARM_NORMAL = 0,
00031 ALARM_ACTDEAD = 1,
00032 ALARM_FUTURE = 2
00033 } alarmretval;
00034
00035 #define TIME_T_24H 24 * 60 * 60
00036
00037
00038
00039
00040
00041
00042
00043
00044 static int check_alarm_status(void)
00045 {
00046 struct cal *cal_data = NULL;
00047 time_t alarm_time = (time_t)-1;
00048 time_t now;
00049 int olderrno = errno;
00050 int status = ALARM_ERR;
00051 alarmaction action = ALARM_ACTION_INVALID;
00052
00053
00054 if ((now = time(NULL)) == -1) {
00055 errno = olderrno;
00056 goto EXIT;
00057 }
00058
00059
00060 if (cal_init(&cal_data) == 0) {
00061 void *ptr = NULL;
00062 unsigned long len;
00063
00064 if (cal_read_block(cal_data, "alarm", &ptr, &len,
00065 CAL_FLAG_USER) == 0) {
00066 struct alarm_data *alarmdata = (struct alarm_data *)ptr;
00067
00068 alarm_time = alarm_data->alarm_time;
00069 action = alarm_data->action;
00070 } else {
00071 goto EXIT;
00072 }
00073
00074 cal_finish(cal_data);
00075 } else {
00076 goto EXIT;
00077 }
00078
00079
00080 if (alarm_time == -1) {
00081 status = ALARM_ERR;
00082 goto EXIT;
00083 } else if ((alarm_time - now) > TIME_T_24H) {
00084 status = ALARM_FUTURE;
00085
00086 } else {
00087 if (action == ALARM_ACTION_NORMAL)
00088 status = ALARM_NORMAL;
00089 else if (action == ALARM_ACTION_ACTDEAD)
00090 status = ALARM_ACTDEAD;
00091 else
00092 status = ALARM_ERR;
00093 }
00094
00095 EXIT:
00096 return status;
00097 }