-
Notifications
You must be signed in to change notification settings - Fork 63
/
alarm.c
34 lines (31 loc) · 855 Bytes
/
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
/*
* alarm.c
*
* Simple synchronous alarm program. This is used as a
* reference for progressive examples of asynchronous
* alarm programs.
*/
#include "errors.h"
int main (int argc, char *argv[])
{
int seconds;
char line[128];
char message[64];
while (1) {
printf ("Alarm> ");
if (fgets (line, sizeof (line), stdin) == NULL) exit (0);
if (strlen (line) <= 1) continue;
/*
* Parse input line into seconds (%d) and a message
* (%64[^\n]), consisting of up to 64 characters
* separated from the seconds by whitespace.
*/
if (sscanf (line, "%d %64[^\n]",
&seconds, message) < 2) {
fprintf (stderr, "Bad command\n");
} else {
sleep (seconds);
printf ("(%d) %s\n", seconds, message);
}
}
}