views:

374

answers:

4

In an assignment for college it was suggested to use the c readline function in an exercise. I have searched for its reference but still haven't found it. Does it really exist? In which header? Can you please post the link to the reference?

Thanks.

A: 

It doesn't exist.

They were mistaken and referred to gets() from stdio.h.

Also this is a very unsafe function due to no maximum size to read parameter, making it immediate security whole (lookup buffer overrun attack). You may use fgets() instead, like the angry comments below suggest.

Pavel Radzivilovsky
No, **don't** use `gets` -- ever!
Jerry Coffin
@Jerry +1... do you recommend using fgets ?
LB
To back Jerry up: `gets` doesn't let you specify a limit on the number of characters to read. This is bad, because you're reading into a character array, which has a fixed length. If you read text that's longer than the length of the array, you get a buffer overflow, which could result in your program crashing or memory that's not supposed to be touched being overwritten.
Samir Talwar
Yes, `fgets` is fine, if a bit of a pain. It normally leaves the '\n' on the end of the string. An easy (though odd) way to strip that off is `strtok(string, "\n");`.
Jerry Coffin
Or: if (buff[strlen(buff)-1] == '\n') buff[strlen(buff)-1] = '\0';
paxdiablo
It most decidedly _does_ exist, in fact two libraries offering the same API under different licenses are available to implement it. It offers neat stuff like command line editing, tab completion, history, etc ... , kind of essential for any CLI.
Tim Post
+1  A: 

C doesn't define such a thing. readline would be Pascalian -- the equivalent in C would be fgets and in C++ you'd normally use getline.

Jerry Coffin
+1  A: 

I don't think it's a standard function.

I simple implementation would be like this:

char *Readline(char *in) {
   char *cptr;

   if (cptr = fgets(in, MAX_LINE, stdin)) {
     /* kill preceding whitespace but leave \n so we're guaranteed to have something
     while(*cptr == ' ' || *cptr == '\t') {
       cptr++;
     }
     return cptr;    
    } else {
     return 0;
   }
 }

It uses fgets() to read up to MAX_LINE - 1 characters into the buffer 'in'. It strips preceding whitespace and returns a pointer to the first non-whitespace character.

Yassin
readline() offers a command history, tab completion, simple line editing,, etc. Its an external but wildly popular library.
Tim Post
@Tim: You're right, the Readline library does that, but I think he only wants a function to read a line as a C char array.
Yassin
@Yassin - I think he wanted links to the libraries.
Tim Post
+8  A: 

Readline exists in two places, libreadline and libedit (also called libeditline). Both have an identical interface. The difference is libreadline is licensed under the GPL, libedit is 3 clause BSD. Licensing is really not a concern for an assignment, at least I don't think it is. Either license allows you to use the code freely. If you link against readline, be sure to make the whole program GPL 2 or later which will satisfy whatever version of the GPL governs the system readline. It may be GPL2+ or GPL3+, depending on the age of the system. I'm not advocating either license, that's up to you.

Note, take care to install either / or and adjust linking as needed (-lreadline or -ledit or -leditline). Both are libraries and not a part of the standard C library.

Edit (afterthought):

If releasing a program to the wild, its a nice gesture to allow the user to configure it with their readline of choice. I.e. --with-readline or --with-libedit, etc. This allows a binary package that conforms to their choice of license, at least as far as readline is concerned.

Links: Readline and Edit/Editline.

Tim Post
which means, you want `libedit` cause BSD rules. :P
Earlz
Yes, I have seen `libedit` incarnate as `libeditline` on more than a few systems, though I can't remember what they were at the moment.
Tim Post
Also note, I did not link to either license. readline can be GPL2-or-later or GPL3 depending on the version you use, however the 3 clause BSD license remains static.
Tim Post