-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext_alarm.c
79 lines (50 loc) · 1.36 KB
/
text_alarm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*******************************************************************************
*
* my text-mode alarm clock program
*
******************************************************************************/
#include <time.h>
#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int target_hour, target_min;
void
alarm_handler (arg)
int arg;
{
printf ("It's time %d:%d:00\n", target_hour, target_min);
system ("play /usr/share/sounds/phone.wav");
}//alarm_handler()
main(argc, argv)
int argc;
char *argv[];
{
time_t cur_time;
struct tm *cur_time_tm;
int cur_sec, target_sec;
int wait_sec;
/* setup the signal handler */
signal (SIGALRM, alarm_handler);
/* get alarm time in second (related to 00:00:00 today) */
sscanf (argv[1], "%d:%d", &target_hour, &target_min);
target_sec = 3600*target_hour + 60*target_min;
/* get current time in second (related to 00:00:00 today) */
time (&cur_time);
cur_time_tm = localtime (&cur_time);
cur_sec = cur_time_tm->tm_hour*3600 + cur_time_tm->tm_min*60 +
cur_time_tm->tm_sec;
/* setup the alarm signal */
wait_sec = target_sec - cur_sec;
if (wait_sec<0) return 0;
alarm (wait_sec);
printf ("It's now %d:%d:%d and I'll wake you up at %d:%d:00\n",
cur_time_tm->tm_hour,
cur_time_tm->tm_min,
cur_time_tm->tm_sec,
target_hour,
target_min
);
pause ();
return 0;
}//main()