Solved

Translate code from PERL to C - image files

Posted on 1997-06-03
8
286 Views
Last Modified: 2010-04-15
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

------------------------------------------------
#!/usr/bin/perl

$pic_path='/usr/local/etc/httpd/cgi-bin/';

  $picno=$ARGV[0];
  $pic=$picno.".gif";

&DisplayPicture;

exit;

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

    open(GIF,"$pic_path$pic");
        select(GIF);$|=1;binmode(GIF);
        select(STDOUT);$|=1;binmode(STDOUT);
        while(<GIF>){
           print;
        }
    close(GIF);
}
------------------------------------------------
0
Comment
Question by:neis
  • 3
  • 3
  • 2
8 Comments
 
LVL 1

Author Comment

by:neis
Comment Utility
Edited text of question
0
 

Expert Comment

by:beckers012097
Comment Utility
Yes I will code it for you, but that's more worth than 100 points...
0
 
LVL 1

Author Comment

by:neis
Comment Utility
Ok, how much?
0
 

Expert Comment

by:beckers012097
Comment Utility
250 incl. the source - because you have to compile it on your system. I think it's a UNIX-box (Apache)?!?
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 5

Accepted Solution

by:
julio011597 earned 100 total points
Comment Utility
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);

  fclose(pgif);
  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
0
 

Expert Comment

by:beckers012097
Comment Utility
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.
0
 
LVL 5

Expert Comment

by:julio011597
Comment Utility
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
0
 
LVL 1

Author Comment

by:neis
Comment Utility
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.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now