Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with fgets

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi,
> i am writing a cgi-c program and the input from the browser is sent to
> stdin. i have to retrive the input from stdin and use it.
> here is my sample code.
> #include <stdio.h>
> #include <stdlib.h>
> #include <string.h>
> main()
> {
> float min,max;
> int len;
> char *input,*lenstr;
> //content_length gives the length of input..
>
> lenstr=getenv(&quot;CONTENT_LENGTH&quot;);
> printf(&quot;<content-type:text/html\n\n>&quot;);
> len=atoi(lenstr);
> fgets(input,len+1,stdin);
> sscanf(input,&quot;T1=%f&T2=%f&quot;,&min,&max);
> printf(&quot;<html><body>minimum=%f and maximum=%f&quot;,min,max);
> printf(&quot;</body></html>&quot;);
> }
> program works fine till it encounters fgets statment. then it says segementation
> fault, core dumped etc..i am working on unix platform and using gcc compiler.
> any help will be appreciated..
> lucifer...
 
You are trying to store the string in memory referenced by an uninitialized pointer. &quot;input&quot; must point to memory that you own. A segmentation faul occurs when you try to access virtual memory which you do not own. So, initialize &quot;input&quot; either statically:

char input[ MAX_STRING_SIZE ] ;

or dynamically:

char *input ;
input = (char *)malloc(sizeof (char) * expectedLength) ;

Hope this helps.

BTW, Lucifer will not always smile...Is. 14:15.
 
brudnakm is right on about the string. You may also want to be careful mixing members of the fgets() and scanf() tribes. Used together, you may get unexpected results (usually losing characters).
Might be better (especially when receiving user) input to use fgets() throughout and convert the data using atoi() and similar functions.
Just my two cents worth.
 
Yes the problem is in the allocation of the memory to the variable input. But what will happen if the return value of the getenv() is NULL.

Put a if statement and check the return value of the getenv() before processing.

Make sure that that the memory of 'input' is large enough.

Maniraja S
 
Hi Lucifer,

Your query has already been hit by correct responces, but I would like to address your problem with a precise solution. I have inserted some lines in BOLD red within your code. Try understanding it.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main()
{
float min,max;
int len;
char *input,*lenstr;
//content_length gives the length of input..

lenstr=getenv(&quot;CONTENT_LENGTH&quot;);
printf(&quot;<content-type:text/html\n\n>&quot;);
len=atoi(lenstr);

if (len) //i.e. if len is ZERO!
{
printf(&quot;Error reading Content Length, can't be Zero!&quot;);
//or however you may wanna handle this error.
}
else
{
input = (char *) malloc (sizeof(char) * (len+1));
fgets(input,len,stdin);
Code:
  sscanf(input,&quot;T1=%f&T2=%f&quot;,&min,&max);
  printf(&quot;<html><body>minimum=%f and maximum=%f&quot;,min,max);
  printf(&quot;</body></html>&quot;); [code][COLOR=red][b] 
   }[/b][/color][code]
}


I think it would certainly help.

Chow!

Jayanta Roy
user.gif

mail me : jayantaroy@email.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top