Having trouble reading from STDIN using fread(). The environment variable CONTENT_LENGTH is used to tell the program the length of the input in bytes. I can use atoi(getenv("CONTENT_LENGTH")) just fine. Where I'm having trouble is with fread()...
Code:
#include <stdio.h>
#include <string.h>
int main(){
char *s=getenv("CONTENT_LENGTH");
int i=atoi(getenv("CONTENT_LENGTH"));
printf("Content-type: text/html\n\n");
printf("%s\n<br />",s); //Shows you CONTENT_LENGTH works
printf("%d\n<br />",i); //Shows you it was converted to int
char *tmp;
fread(tmp,i,1,stdin); //read from stdin something of i bytes to tmp
printf("%s\n<br />",tmp);
return 0;
}
Obviously this won't run on the command line. But for some reason it won't work on the server, either. I can only guess, but I think it's always a segmentation fault because the script ends "prematurely" according to the error log:
[Thu Sep 15 07:18:26 2011] [error] [client 127.0.0.1] Premature end of script headers: a.out, referer: http://localhost/home.html
What's actually happening?