From owner-freebsd-questions@FreeBSD.ORG Tue Jun 9 23:19:24 2009 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 727581065728 for ; Tue, 9 Jun 2009 23:19:24 +0000 (UTC) (envelope-from jeffrey@goldmark.org) Received: from out2.smtp.messagingengine.com (out2.smtp.messagingengine.com [66.111.4.26]) by mx1.freebsd.org (Postfix) with ESMTP id 467E68FC12 for ; Tue, 9 Jun 2009 23:19:24 +0000 (UTC) (envelope-from jeffrey@goldmark.org) Received: from compute1.internal (compute1.internal [10.202.2.41]) by out1.messagingengine.com (Postfix) with ESMTP id 9AA5D35EBBA; Tue, 9 Jun 2009 19:19:23 -0400 (EDT) Received: from heartbeat2.messagingengine.com ([10.202.2.161]) by compute1.internal (MEProxy); Tue, 09 Jun 2009 19:19:23 -0400 X-Sasl-enc: HVGT/tEmUfd91GhtXsDrBw8ZF4Wf3KuSxIEBJ/u5W6ng 1244589563 Received: from hagrid.ewd.goldmark.org (n114.ewd.goldmark.org [72.64.118.114]) by mail.messagingengine.com (Postfix) with ESMTPSA id 292613A704; Tue, 9 Jun 2009 19:19:23 -0400 (EDT) Message-Id: <0F5E9DB6-2342-46E0-B5AB-7BB70837FEF6@goldmark.org> From: Jeffrey Goldberg To: Gary Kline In-Reply-To: <20090609001529.GA7166@thought.org> Content-Type: text/plain; charset=US-ASCII; format=flowed; delsp=yes Content-Transfer-Encoding: 7bit Mime-Version: 1.0 (Apple Message framework v935.3) Date: Tue, 9 Jun 2009 18:19:21 -0500 References: <20090609001529.GA7166@thought.org> X-Mailer: Apple Mail (2.935.3) Cc: FreeBSD Questions Subject: Re: flaw found [in my own program] X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Jun 2009 23:19:24 -0000 On Jun 8, 2009, at 7:15 PM, Gary Kline wrote: > not surprisingly, i found a fla w in my getc(fp) program that > tried to read past "" ... the example i added to my > test file was simply the 2 bytes "<" and "?". so if you have a > stray > > " > with a matching close case, the binary hangs on a read. > so, again, can anybody suggest a better example, in C, to get > past two delimiters? Back in the days when I taught introductory C programming, one the the early homework assignments was to write a filter that would strip C- style comments. As a follow-up they had to do this allowing for nested comments. I don't think I can recover things from the back-up tapes that I have for that corse material, but the approach I directed people toward was to have a variable, let's call it status that records one of four states OUTSIDE /* just reading normally, not in the material to be striped */ AFTER_LT /* You've read in a '<' and are looking for a '?' */ INSIDE /* You are in the material to be stripped */ AFTER_Q /* You are in the material to be stripped and have just read a '?' */ then use a switch statement on the character you are reading in. switch(c) { case '<': ... case '?': ... case '>': ... case EOF: ... default: ... } In each case, you look at the current state, decide whether the write 'c' to output and what state to change to. The most common mistake students would make would be to forget the EOF case. I suspect that you may have done the same. -j