• C

Translate code from PERL to C - image files

I have the following PERL script that works fine. It sends a
picture to a browser as a response to a
<img src="http://www.***.com/cgi-bin/sendpic.pl?pic_no">
tag with the picture number is passed as shown.

Can you please show me how can one do the same thing using a
CGI script in C that can be compiled. Just in case you need,
here is info about the server:

server software is Apache/1.2b10
Perl version is 5.003
server protocol is HTTP/1.0
Common Gateway Interface (CGI) version is CGI/1.1.

Also, how do I go about compiling?

Thank you for your help.
Sincerely yours,
Laurentiu Traineanu






sub DisplayPicture
    print "Content-type: image/gif\r\n";
    print "\r\n";

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

neisAuthor Commented:
Edited text of question
Yes I will code it for you, but that's more worth than 100 points...
neisAuthor Commented:
Ok, how much?
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

250 incl. the source - because you have to compile it on your system. I think it's a UNIX-box (Apache)?!?
Sorry for the intrusion, but i think 100 points are enough:)

Here is the code:

/* sendpic.c */

#include <stdlib.h>
#include <stdio.h>

#define PIC_PATH "/usr/local/etc/httpd/cgi-bin"

int main(int argc, char **argv)
  char buff[BUFSIZ];
  FILE *pgif;

  /* look at fread(3): give items_read the type */
  /* returned by fread (size_t on my system) */
  register size_t items_read;

  /* let's print the header */
  fputs("Content-Type: image/gif\n\n", stdout);

  if(argc == 1) {
    /* in C, first argument is the command run */
    /* so, handle no parameter passed: */
    /* maybe show a default picture */
    /* then exit saying: the cgi didn't fail! */
    return EXIT_SUCCESS;

  /* let's put file name in buff */
  sprintf(buff, "%s/%s.gif", PIC_PATH, *(argv+1));

  if(!(pgif = fopen(buff, "r"))) {
    /* gif not found: again, maybe show a default image */
    return EXIT_SUCCESS;

  while(items_read = fread((void *)buff, sizeof(char), BUFSIZ, pgif))
    fwrite((void *)buff, sizeof(char), items_read, stdout);

  return EXIT_SUCCESS;

Compile it with:

$ cc -o sendpic.cgi sendpic.c

Note that in C/Unix you handle binary files with fread() and fwrite() - don't need to specify binary mode.
Also, you needn't unbuffer stdout in this case, because you are sending a self-standing buch of data; also, unbuffering makes things slower.

Rgds, julio

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Give him the points, learn C and you will not have to give points away for such easy problems. To julio: I only wanted to clear this section from complete-program-searchers. This is for special C-problems.
Hello beckers,

i may agree with you, this forum is mainly intended for advice, not for ready products.

Anyway, there's not such a strong rule about this, so, to show my position:

i never ask homeworks;
i sometimes give complete programs, because - say - Mr.neis doesn't care about becoming a programmer: he's just 'paying' for some help.

BTW, you would have answered this for 250 points, so what's indeed your point? ;)

Cheers, julio
neisAuthor Commented:
Thank you julio for your help. It is exactly what I was looking for. You are right, I don't care to become a C programmer, I just need to replace the PERL program with a C one.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.