Extension to solution given to a novice
John A. Weeks III
john at newave.UUCP
Wed May 1 15:28:24 AEST 1991
In article <1140 at gtenmc.UUCP> csp at gtenmc.UUCP () writes:
> > This proves without doubt 'Ignorance is NOT bliss'.
> But if you insist here is a routine which will handle the input will
> some rigor.
Is it just me, living in a sheltered world, or is the following
routine gross?
+ #include <stdio.h>
+ #include <malloc.h>
+ #define MEM_ERR 1
+ extern err_no;
+ char *mygets();
+ char *mygets(l,t)
+ unsigned l;
+ char t; /* String terminator */
+ {
+ char *ptr,c;
+ c = getchar();
+ if (( int )c != -1 && c != t )
+ *( ptr = mygets( l + 1 , t )) = c;
+ else
+ {
+ if (( ptr = malloc( l + 1 )) == ( char * ) NULL )
+ return( err_no = MEM_ERR , ( char * ) NULL );
+ *( ptr += l ) = 0;
+ }
+ return( ptr - 1 * ( l > 0 ));
+ }
You are using recursion to read a string character by character? What
if you get a 1k string--have you ever worked on a machine/OS with
limited stack space? And what is the (int) c != -1? I think you want
EOF here. And the cast of c in this check, ever work with a machine
with 8 bit characters, especially signed 8 bit chars? You could easily
get symbols that map to integer -1 (like in DOS).
I think you are trying to hit a two pound problem with a 5 pound hammer.
No wonder we need risks and sparcs to do jobs that were once done on PDP's.
-john-
--
=============================================================================
John A. Weeks III (612) 942-6969 john at newave.mn.org
NeWave Communications ...uunet!tcnet!wd0gol!newave!john
More information about the Comp.lang.c
mailing list