I am trying to write my own shell. I am having problems with the user input part. I have made an array (char inputBuff[512]) and I want to read in user input. I am using ffputs() and ffgets() but I want to limit the person to the 512 size, and have it say it was an error and have them try again. I can not get the size restriction to work right.

fputs("Enter command: ", stdout);
fgets (prompt, sizeof inputBuff, stdin);

I had more than that, but I keep deleting and trying other ideas. I thought ffgets second argument would limit the input, but it doesn't. Any help is appreciated.

Edited 6 Years Ago by chade25: n/a

I am trying to write my own shell. I am having problems with the user input part. I have made an array (char inputBuff[512]) and I want to read in user input. I am using ffputs() and ffgets() but I want to limit the person to the 512 size, and have it say it was an error and have them try again. I can not get the size restriction to work right.

fputs("Enter command: ", stdout);
fgets (prompt, sizeof inputBuff, stdin);

I had more than that, but I keep deleting and trying other ideas. I thought ffgets second argument would limit the input, but it doesn't. Any help is appreciated.

Omg, I think I got it. I am kind of embarrased. Let me post how I fixed it, and tell me if there is a more "correct" way.

fputs("Enter command: ", stdout);
fgets (inputBuff, sizeof inputBuff, stdin);
int k = strlen(inputBuff) -1;

if(k > 512) 
{
    //Do stuff for
}
else
{
       //Do other stuff, fflush stdin and stdout and loop
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

fgets() DOES limit the amount of chars you can put into your buffer, but you never made your collection of chars in your buffer, into a legitimate string - it has no end of string marker.

#include <stdio.h>

int main() {
  int i; 
  char buff[25];
  buff[sizeof(buff)-1]='\0';   //setting the end of string marker

  fgets(buff, sizeof(buff)-1, stdin);

  printf("\n\n%s", buff);

  printf("\n\n\t\t\t     press enter when ready");

  i = getchar();
  return 0;
}

When you print a collection of char's that aren't terminated with an end of string marker, you can get a lot more char's than what are actually in your buffer. Tricky, eh? ;)

Welcome to the forum, and PLEASE surround your code with CODE tags (click on the [CODE] icon in the editing window, to get a pair).

Edited 3 Years Ago by Dani: Formatting fixed

I thought ffgets second argument would limit the input, but it doesn't. Any help is appreciated.

fgets *does* limit the input. But only for the buffer argument to fgets. To print an error if there is more input than the buffer can hold, check for a new-line character at the end of the buffer. If there is not one new-line then there are more characters in the input stream.

#include <stdio.h>
#include <string.h>

void main() {
    char buffer[10+2]; // 10 data + new-line & nul
    if (fgets(buffer, sizeof(buffer), stdin)) {
        if (buffer[strlen(buffer)-1] != '\n')
            fprintf(stderr, "Input string too long\n");
        else
            fprintf(stderr, "Input OK\n");
    }
}

Thanks for your help. I did some print statements and noticed that the sizeof shows how much space is left in buffer, but what I have come up with is:

while(flag == 1)
{
		fputs("Enter command: ", stdout);
		fgets (inputBuff, sizeof inputBuff, stdin);
		int buffCheck =(strlen(inputBuff) -1);
		printf("%d \n", buffCheck);

	if(buffCheck > 4) 
	{
		fprintf(stderr, "exceeds the allowed size. \n");
		fflush(stdout);
		fflush(stdin);
		return buffCheck;
	}
	else
	{
		fflush(stdout);
		fflush(stdin);
		return buffCheck;;
	}
}// end while
This question has already been answered. Start a new discussion instead.