Question: Help with C & Linux Environments Task 0: Task 1: #include #include #include #include #include /* Simple example of using gnu readline to get lines

Help with C & Linux Environments

Task 0:

Help with C & Linux Environments Task 0: Task 1: #include #include

Task 1:

#include #include #include /* Simple example of using gnu readline to get

lines of input from a user. Needs to be linked with -lreadline

#include #include #include #include #include /* Simple example of using gnu readline to get lines of input from a user. Needs to be linked with -lreadline -lcurses add_history tells the readline library to add the line to it's internal history, so that using up-arrow (or ^p) will allows the user to see/edit previous lines. */ int main(int argc, char **argv) { char *s; while (s=readline("prompt> ")) { add_history(s); /* adds the line to the readline history buffer */ free(s); /* clean up! */ } return(0); }

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 -lreadline -lcurses 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()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!