I can limit user input to 5 characters using the GNU readline :
#include <readline/readline.h>
#include <stdio.h>
#include <stdlib.h>
static int limit_rl(FILE *f)
{
if (rl_end > 5) {
return '\b';
}
return rl_getc(f);
}
int main(void)
{
char *str;
rl_getc_function = limit_rl;
str = readline("> ");
printf("%s\n", str);
free(str);
return 0;
}
But how to read input with a default value (and not with a hint), for example:
> ummy
^ cursor here
if user types dand Enterreturn "dummy"
if the user types DELand Enterreturns "mmy"
source
share