Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!sdd.hp.com!spool.mu.edu!cs.umn.edu!kksys!wd0gol!newave!john From: john@newave.UUCP (John A. Weeks III) Newsgroups: comp.lang.c Subject: Re: Extension to solution given to a novice Message-ID: <798@newave.UUCP> Date: 1 May 91 05:28:24 GMT References: <1140@gtenmc.UUCP> Reply-To: john@newave.mn.org (John A. Weeks III) Organization: NeWave Communications Ltd, Eden Prairie, MN Lines: 48 In article <1140@gtenmc.UUCP> csp@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 + #include + #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@newave.mn.org NeWave Communications ...uunet!tcnet!wd0gol!newave!john