Working with files

Return to homepage

Working with devices

  1. Review the following example:
                                                
    #include <termio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 120
    
    int main()
    {
    	struct termio d_str,d_nov;
    	char username[SIZE];
    	char password[SIZE];
    	printf("Enter user name:");
    	// reading from the input
    	scanf("%s",username);
    	// readout of terminal device configuration
    	ioctl(0, TCGETA, &d_str);
    	// backup of the terminal configuration
    	d_nov=d_str;
    	// turning of print of input characters
    	
    	
    	d_nov.c_lflag &= ~ECHO;
    	ioctl(0,TCSETA,&d_nov);
    	printf("Enter password:\n");
    	// reading from the input (ECHO is turned off)
    	scanf("%s", password);
    	// Restoring the previous configuration
    	ioctl(0, TCSETA, &d_str);
    	printf("Password: %s\n", password);
    	return 0;
    }
                                                
                                            
  2. Review program demostrating differences between canonical and non-canonical terminal mode: termios_demo.c

Homework 3

For this homework you have two exercises.
  1. use the following code to implement program which asks user for username and password but when entering password, echoing of input characters will be turned off. Be sure to preserve all the other terminal settings. Usefull manual page for this task is man 3 termios.
  2.                                         
    #include <termio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 120
    
    int main()
    {
        struct termio d_str,d_nov;
        char username[SIZE];
        char password[SIZE];
        printf("Enter user name:");
        scanf("%s",username);
        ioctl(0, TCGETA, &d_str);
        d_nov=d_str;
        
        //todo turn of echo while preserving other terminal settings
        
        ioctl(0,TCSETA,&d_nov);
        printf("Enter password:\n");
        scanf("%s", password);
        ioctl(0, TCSETA, &d_str);
        printf("Password: %s\n", password);
        return 0;
    }
                                            
                                        
  3. In the following code set interrupt signal of current terminal from default "ctrl + c" to "ctrl + k" before the infinite loop starts. Usefull resorce for completion of this task is man 3 termios and termios & stty
  4.                                             
    #include <termio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <unistd.h>
    
    int main(int argc, char** argv)
    {
        struct termios d_str,d_nov;
        
        tcgetattr(STDIN_FILENO, &d_str);
        d_nov=d_str;
        
        //todo set ctrl + k as interrupt character
        
        if (tcsetattr(STDIN_FILENO, TCSANOW, &d_nov) < 0 )
        {
            perror("Could not set terminal attributes");
            return 1;
        }
        while(1)
        {
            printf("hello\n");
            usleep((useconds_t) 1000000);
        }
    }