Zéychin
Zéychin

Reputation: 4205

Limit Console Input Length in C:

I am beginning to design a shell application to run within a Linux terminal for a class I am taking.

This, of course, will involve reading variable-length input strings (commands) from the user. I know that I can simply read each command into a buffer of a size that I would consider appropriate, but this has the potential to either a) truncate the command or b) cause a buffer overflow.

If possible, how can way limit the length of user input to the console?

Say, if I set the command length to 3, 123 would be allowed, but if 123 were already present in the input string (before the user has pressed enter) and the user attempted to add 4, no character would print to the console, perhaps even with an 'error ping'.

I realize that I could design such functionality, but if that is needed, I am not sure where to start to do such a thing.

Either a pre-existing solution or advice on implementing my own solution would be greatly appreciated.

Edit:

I suppose a cheap and easy solution would be to read a command on character at a time until an enter signal is reached or the maximum length is reached. Would problems arise with a solution of this sort?

Upvotes: 1

Views: 3348

Answers (4)

John Bode
John Bode

Reputation: 123468

Without digging into platform-specific controls, you cannot limit how many characters a used may type in a console before hitting "Enter".

What you can do is check for the presence of a newline character in your input buffer; if it isn't there, then the user typed in more characters than you're prepared to deal with. You can reject that input, and then read stdin repeatedly until you see the newline.

Example:

 #include <stdio.h>
 #include <string.h>
 ...
 char buf[SIZE];
 ...
 printf("Gimme something: ");
 fflush(stdout);

 if (fgets(buf, sizeof buf, stdin))
 {
   char *newline = strchr(buf, '\n');
   if (!newline)
   {
     printf("Input too long: \"%s\"\n", buf);
     while (!newline && fgets(buf, sizeof buf, stdin))
       newline = strchr(buf, '\n');
   }
   else
   {
     // do something with buf
   }
}

Upvotes: 1

Dave
Dave

Reputation: 11162

In response to your edit, terminals are usually line-buffered, allowing users to enter as much as they want before hitting enter without you even knowing about it. You could set the terminal to raw or cbreak mode, but then you're entering platform-specific territory.

Instead, I would suggest that you avoid this problem, and accept that a terminal is a silly vestige from 2 million years ago. Most platforms define LINE_MAX to be the maximum line size any program needs to handle. Beyond that, you can simply assume your user is messing with you, and truncate.

Upvotes: 0

cnicutar
cnicutar

Reputation: 182639

I have little experience with readline, but here's what you could try:

  • Write a function that checks rl_end (the number of characters in rl_line_buffer)
    • If you want to allow more, just return rl_getc
    • If not, you can use rl_ding
  • Set the rl_getc_function to call your function as described above

As a side note, if you do use readline, you don't need to limit the input at all (the library manages its memory as it goes). Another (simpler) function you might be interested in is getline.

Upvotes: 2

unwind
unwind

Reputation: 399833

That kind of low-level control of the console is not something that's included in C's rather basic built-in I/O model.

You need to look into something platform-specific, such as ncurses for Unix-like systems.

Upvotes: 2

Related Questions