Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 2 Oct 1995 12:48:05 -0600
From:      Nate Williams <nate@rocky.sri.MT.net>
To:        olsenc@ichips.intel.com
Cc:        questions@freebsd.org
Subject:   Re: If not gets(), then what?
Message-ID:  <199510021848.MAA29706@rocky.sri.MT.net>
In-Reply-To: <9510021814.AA35311@dtt034.intel.com>
References:  <9510021814.AA35311@dtt034.intel.com>

next in thread | previous in thread | raw e-mail | index | archive | help
> I have a program that uses gets(), and FreeBSD complains about it.
> I read the manpage on gets(), and it says it's dangerous, but yet
> it doesn't mention any alternatives!

Well, it's not conmpletely obvious, but the other routine mentioned in
the same man-page is the better solution.

Instead of:

{
   char foo[255];
   char *ptr;

#if defined(BAD_CODE)
   gets(foo);
#else
   fgets(foo, 255, stdin);
   c = index(foo, '\n');
   if ( c != NULL )
      *c = '\0';
#endif

}
   
The use of the index call is necessary to strip off the trailing '\n'.


Nate



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199510021848.MAA29706>