Submit your solution as a plain-text file with a .c extension in the name. Name
ID: 3790710 • Letter: S
Question
Submit your solution as a plain-text file with a .c extension in the name.
Name
timer - counts down to zero from a user supplied number.
Description
Displays a count down to zero from a number supplied by the user at the command line. Supports
pausing and unpausing the countdown. Countdown is done by ones, one second at a time. While
the program is runnng, typing p will pause the countdown and typing u will unpause it. echoing of
characters is disabled while the program is running.
Sample Usage
timer 60 begins the countdown
Hints
man 3 sleep
uses struct termios
uses VSTART and VSTOP
Explanation / Answer
#include<stdio.h>
#include<termios.h>
#include<stdlib.h>
int main(void)
{
int n;
struct termios oldio,newio;
tcgetattr(0,&oldio);
printf("Enter n:");
scanf("%d",&n);
printf("Starting countdown... ");
newio.c_iflag &= ~IXOFF;
newio.c_cc[VSTOP]='p';
newio.c_cc[VSTART]='u';
if(tcsetattr(1,TCSANOW,&newio)==-1)
{
printf("Error setting up terminal attributes ");
return 0;
}
while(n>=0)
{
printf("%d ",n);
n--;
sleep(1);
system("clear");
}
tcsetattr(1,TCSAFLUSH,&oldio);
return 0;
}