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

C with readline library and linux commands You are to write a program that inter

ID: 3853265 • Letter: C

Question

C with readline library and linux commands

You are to write a program that interacts with the user via a command prompt (your program prompts for a command, reads a command as a line of text and prints out any results). Your program must use the GNU readline library to get input from the user. The readline library provides functions that make it possible for the user to scroll back through previous commands, search through previous commands, edit previous commands, etc (readline is used by many programs including the bash shell to handle user input, this is why you can hit up-arrow to recall the previous command entered). You can get the details of the functions provided by the readline library by issuing the command "man readline" at the UNIX prompt. Basically the readline library provides a function named readline () that will read input from standard input and allow the user to poke through any history that has been given to the readline library. readline () returns a char * pointing to the user input, or a NULL pointer indicating that it has found EOF. The string returned by readline is null terminated and has been allocated from the heap - this means you need to free this memory when you are done with it. To use the readline library you must tell the linker to include the readline and curses libraries, this means you need to add -Ireadline -Icurses to your compile line (see below for an example). The code shown below is a simple example of using the readline library, including the insertion of each line entered into the readline history. This program doesn't do anything with each line it gets, it just shows how to use readline ().

Explanation / Answer

#include<stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>

void main(){

int main()
{
        char *input;

        int count = 0;

        while ( count < 5 )
        {
                input = readline("Enter Command: ");
                add_history(input);
                system(input);               
                ++count;
        }

        return 0;

}