Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: getting perl and C working together in a way that makes sense

Reply
Thread Tools

Re: getting perl and C working together in a way that makes sense

 
 
Mark Bluemel
Guest
Posts: n/a
 
      02-01-2013
On 01/02/2013 07:19, Cal Dershowitz wrote:
> $ cat perl1.c
>
> #include <stdio.h>
>
> int
> main (int argc, char *argv[])
> {
>
> // write perl file to a string
> char text[1000];
> FILE *fp = fopen ("capture3.pl", "r");
> int i = 0;
> while (feof (fp))
> text[i++] = fgetc (fp);
> text[i] = '\0';


potential buffer overrun (twice)

>
> // output perl source to file
> FILE *fp;


duplicate declaration

> char *perlfile = "/tmp/doppelgaenger.pl"; // edit
>
> // add error checking to open/flose/system
> fp = fopen (perlfile, "w");
> fprintf (fp, "%s\n", perlsource);


missing variable perlsource

> fclose (fp);
>
> char cmd[100];
> sprintf (cmd, "perl %s %s", perlfile, argv[1]);


potential buffer overrun

> system (cmd);


http://xkcd.com/327/

> }

[Snip]

Well, apart from duplicate declarations, missing declarations, potential
buffer overruns, missplaced use of feof and lack of error checking,
this is a master piece of something or other.

>
> This is from a post in c.l.p.misc, and I thought I would play with it a
> bit, because I've got something similar up my sleeve.


I think you'd do well to get it out of your sleeve and into a dustbin asap.

> I think the intent of this program speaks for itself,


Really? The program appears to be a rather rough attempt at duplicating
a file and then executing it with perl.

Why the duplication seems necessary, I can't imagine.

What this has to do with the title of your post "getting perl and C
working together in a way that makes sense" is equally baffling.

> and it cuts across
> some of the greatest standing arguments in the C world: how do you
> declare and handle the input of a string whose size you don't know?


With great difficulty and it may not be possible at all. I think you
could probably find quite a few threads in comp.lang.c over the years
about this subject.

> I've taken all the guess work out of that:


Really?

> $ ls -l perl1.c
> -rw-rw-r-- 1 fred fred 578 Feb 1 00:03 perl1.c
> $
>
>
> This is one of these days where everything I've tried has failed in a
> way I don't understand, so I'd like to ask for some help straightening
> out my syntaxes.


You could start by fixing the obvious problems in your code so it
compiles - reading the compile errors helps.

> Where I'm going with this past this interesting riddle is that I want to
> pass data to fortran using the ISO C bindings, and the above method is
> way to primitive for what I have in mind.


It's hard to see what your sample program has to do with what you are
discussing here.

> That perl and C can communicate with each other as much as they need to
> is I think what drove the development of both languages. And none of it
> stops when Dennis Ritchie dies.


I'm fairly sure you think wrong. I doubt that any aspect of Perl has had
any influence on the development of C. The converse may not be the case.

> Anyways, how do I fix this program?


That depends - what are you trying to do?
 
Reply With Quote
 
 
 
 
Ken Brody
Guest
Posts: n/a
 
      02-01-2013
On 2/1/2013 5:08 AM, Mark Bluemel wrote:
> On 01/02/2013 07:19, Cal Dershowitz wrote:
>> $ cat perl1.c

[...]
>> FILE *fp = fopen ("capture3.pl", "r");
>> int i = 0;
>> while (feof (fp))
>> text[i++] = fgetc (fp);
>> text[i] = '\0';

>
> potential buffer overrun (twice)


Not really. Given that feof() will be false prior to actually reading
anything[1], the loop is a no-op, and text will end up being "".

Of course, given all the other errors in the code...

[...]

[1] Is this actually guaranteed? 7.19.10.2p3 only says "The feof function
returns nonzero if and only if the end-of-file indicator is set for
stream", and 7.19.1 only says the FILE contains "an end-of-file indicator
that records whether the end of the file has been reached". I assume that
one can conclude that the end-of-file hasn't been "reached" if no I/O has
yet occurred.

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      02-01-2013
On 02/01/2013 01:03 PM, Ken Brody wrote:
....
> Not really. Given that feof() will be false prior to actually reading
> anything[1], the loop is a no-op, and text will end up being "".
>
> Of course, given all the other errors in the code...
>
> [...]
>
> [1] Is this actually guaranteed? 7.19.10.2p3 only says "The feof function
> returns nonzero if and only if the end-of-file indicator is set for
> stream", and 7.19.1 only says the FILE contains "an end-of-file indicator
> that records whether the end of the file has been reached". I assume that
> one can conclude that the end-of-file hasn't been "reached" if no I/O has
> yet occurred.
>


7.21.5.3p8 (describing behavior of fopen()): "When opened, a stream is
fully buffered if and only if it can be determined not to refer to an
interactive device. The error and end-of-file indicators for the stream
are cleared."

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: getting perl and C working together in a way that makes sense Jens Schweikhardt C Programming 24 02-04-2013 08:12 PM
Re: getting perl and C working together in a way that makes sense Jorgen Grahn C Programming 0 02-02-2013 09:55 AM
Re: getting perl and C working together in a way that makes sense Malcolm McLean C Programming 5 02-02-2013 08:31 AM
Re: getting perl and C working together in a way that makes sense Keith Thompson C Programming 0 02-01-2013 04:14 PM
Re: getting perl and C working together in a way that makes sense Johann Klammer C Programming 0 02-01-2013 08:30 AM



Advertisments