develooper Front page | perl.perl5.porters | Postings from February 2003

[docs patch] replace gets() with fgets() in example

Thread Next
From:
Stas Bekman
Date:
February 28, 2003 19:10
Subject:
[docs patch] replace gets() with fgets() in example
Message ID:
3E602427.3030100@stason.org
I was trying an example from perlembed.pod. It uses gets(), which on my linux 
box won't start the app:

/home/stas/tmp/ccUOSy3k.o: In function `main':
/home/stas/tmp/ccUOSy3k.o(.text+0x110): the `gets' function is dangerous and 
should not be used.

So I've fixed the example to use fgets instead.

Though I'm not sure about newline stripping where it's not \012 or \015 but 
\012\015 (mac?).

--- pod/perlembed.pod.orig	2003-03-01 13:51:24.000000000 +1100
+++ pod/perlembed.pod	2003-03-01 14:02:58.000000000 +1100
@@ -750,6 +750,8 @@
   #define DO_CLEAN 0
   #endif

+ #define BUFFER_SIZE 1024
+
   static PerlInterpreter *my_perl = NULL;

   int
@@ -757,7 +759,7 @@
   {
       char *embedding[] = { "", "persistent.pl" };
       char *args[] = { "", DO_CLEAN, NULL };
-     char filename [1024];
+     char filename[BUFFER_SIZE];
       int exitstatus = 0;
       STRLEN n_a;

@@ -772,8 +774,10 @@
       if(!exitstatus) {
          exitstatus = perl_run(my_perl);

-        while(printf("Enter file name: ") && gets(filename)) {
+        while(printf("Enter file name: ") &&
+              fgets(filename, BUFFER_SIZE, stdin)) {

+            filename[strlen(filename)-1] = '\0'; /* strip \n */
              /* call the subroutine, passing it the filename as an argument */
              args[0] = filename;
              call_argv("Embed::Persistent::eval_file",


__________________________________________________________________
Stas Bekman            JAm_pH ------> Just Another mod_perl Hacker
http://stason.org/     mod_perl Guide ---> http://perl.apache.org
mailto:stas@stason.org http://use.perl.org http://apacheweek.com
http://modperlbook.org http://apache.org   http://ticketmaster.com


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About