Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code for rotate.c (in C not C++): /* rotate.c : map a->b, b->, ... z->a */ #incl

ID: 3861794 • Letter: C

Question

Code for rotate.c (in C not C++):

/* rotate.c : map a->b, b->, ... z->a */

#include <stdio.h>
#include <ctype.h>
#include <termios.h>

int main() {
int c;

struct termios original_mode;
struct termios ttystate;
// record initial terminal settings for later use
tcgetattr(0, &original_mode);
// change settings
tcgetattr( 0, &ttystate);         /* read curr. setting   */
ttystate.c_lflag &= ~ICANON;     /* no buffering         */
ttystate.c_cc[VMIN]     = 1;     /* get 1 char at a time */
tcsetattr( 0 , TCSANOW, &ttystate);     /* install settings     */

while ( (c=getchar() ) != EOF) {      
    if (c=='z')
      c = 'a';
    else if (islower(c))
      c++;
    putchar(c);
}

// restore initial terminal settings
tcsetattr(0, TCSANOW, &original_mode);

return 0;
}

The previous version of rotate.c still showed the keys as they were pressed. It will wait forever until it receives input. Fortunately, the terminal driver has the ability to stop waiting after a certain amount oftime (specified in units of.1 seconds). Modify the codes rotate.c to be able to do the following: (i) disable echoing (ii) set no minimum number of chars. (iii) wait up to 1 second. (iv) print dot ifyou type ctrl-d (v) type character 'Q' to exit the program Note that if your program quits without restoring the terminal settings, yourterminal may be in a bad state and you need to type stty echo again or possibly stty sane.

Explanation / Answer

#include <stdio.h>
#include <ctype.h>
#include <termios.h>

int main() {
int c;

struct termios original_mode;
struct termios ttystate;
// record initial terminal settings for later use
tcgetattr(0, &original_mode);
// change settings
tcgetattr( 0, &ttystate);         /* read curr. setting   */
ttystate.c_lflag &= ~ICANON;     /* no buffering         */
ttystate.c_cc[VMIN]     = 1;     /* get 1 char at a time */
tcsetattr( 0 , TCSANOW, &ttystate);     /* install settings     */

while ( (c=getchar() ) != EOF) {      
    if (c=='z')
      c = 'a';
    else if (islower(c))
      c++;
    putchar(c);
}

// restore initial terminal settings
tcsetattr(0, TCSANOW, &original_mode);

return 0;
}