CGI Buffering issue

Posted by Punit on Stack Overflow See other posts from Stack Overflow or by Punit
Published on 2010-06-02T21:50:58Z Indexed on 2010/06/02 22:14 UTC
Read the original article Hit count: 246

Filed under:
|

I have a server side C based CGI code as:

cgiFormFileSize("UPDATEFILE", &size);   //UPDATEFILE = file being uploaded
cgiFormFileName("UPDATEFILE", file_name, 1024);
cgiFormFileContentType("UPDATEFILE", mime_type, 1024);
buffer = malloc(sizeof(char) * size);

if (cgiFormFileOpen("UPDATEFILE", &file) != cgiFormSuccess) {
    exit(1);
}
output = fopen("/tmp/cgi.tar.gz", "w+");

printf("The size of file is: %d bytes", size);
inc = size/(1024*100);

while (cgiFormFileRead(file, b, sizeof(b), &got_count) == cgiFormSuccess)
{
    fwrite(b,sizeof(char),got_count,output);
    i++;
    if(i == inc && j<=100)
    {
        ***inc_pb*** = j;
        i = 0;
        j++;   // j is the progress bar increment value
    }
}

cgiFormFileClose(file);
retval = system("mkdir /tmp/update-tmp;\
                 cd /tmp/update-tmp;\
                 tar -xzf ../cgi.tar.gz;\
                 bash -c /tmp/update-tmp/update.sh");

However, this doesn't work the way as is seen above. Instead of printing 1,2,...100 to progress_bar.txt one by one it prints at ONE GO, seems it buffers and then writes to the file. fflush() also didn't work.

Any clue/suggestion would be really appreciated.

© Stack Overflow or respective owner

Related posts about c

    Related posts about cgi