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

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

Thread Previous
From:
Stas Bekman
Date:
March 9, 2003 17:47
Subject:
Re: [docs patch] replace gets() with fgets() in example
Message ID:
3E6BEE45.9030901@stason.org
Stas Bekman wrote:
> 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?).

inlined and attached:

--- pod/perlembed.pod.orig	2003-02-17 06:45:17.000000000 +1100
+++ pod/perlembed.pod	2003-03-10 12:44:24.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 Previous


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