Date: Tue, 16 Jun 2009 10:02:44 -0700 From: Gary Kline <kline@thought.org> To: Mel Flynn <mel.flynn+fbsd.questions@mailing.thruhere.net> Cc: freebsd-questions@freebsd.org Subject: Re: feedback, comments on this php-delimiter scrubbing program? Message-ID: <20090616170244.GA40934@thought.org> In-Reply-To: <20090616153040.GA40540@thought.org> References: <20090616012114.GA38011@thought.org> <200906151857.45945.mel.flynn%2Bfbsd.questions@mailing.thruhere.net> <20090616153040.GA40540@thought.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Jun 16, 2009 at 08:30:40AM -0700, Gary Kline wrote: > On Mon, Jun 15, 2009 at 06:57:45PM -0800, Mel Flynn wrote: > > On Monday 15 June 2009 17:21:16 Gary Kline wrote: > > > > > Encl: dephp.c, test > > case '?': > > ch = getchar(); > > while (1) > > { > > if (ch == '?' && (ch = getchar()) == '>') > > { > > break; > > } > > else > > { > > ch = getchar(); > > } > > } > > break; > > > > As has been hinted before you're not handling the EOF case. Files like: > > <?php > > class foo > > { > > function __construct() { echo 'foo'; }; > > }; > > > > Are perfectly valid php files and actually preferred for included files, > > rather then a terminating ?>, because one can start filling the output by > > trailing whitespace before EOF and thus not set any header() anymore. The > > above code will wait indefinitely for the next char or spin like mad if you're > > using non-blocking IO. > > > YUP. > > I thought my initial getchar() != EOF would handle that. > But then there's that do-forever loop. I remember Jeffrey's > post and tried a case 'EOF' or case '-1'; thar gives me > compiler errors. > > Suggestions? > > > > > > You should really take the pointers from Jeffrey Goldberg and record states > > and decide based on the state, rather then inlined switch statements, if only > > for readability. > > > > You're also in trouble with <?xml, but that's an entirely different beast and > > you might actually be doing the right thing from your usage perspective. > > -- > > Mel this works, but still gives a warning. it's sloppy coding, but as a second version... gary #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { FILE *fp; int results=0; *argv++; if ((fp = fopen(*argv, "r")) == NULL) exit(printf("[%s] not found\n", *argv)); else results = foo(fp); printf("\nend of main(), %s, results = [%d]\n", __FILE__, results); } int foo(FILE *fp) { int ch; do { if ((ch = getc(fp)) != EOF) switch (ch) { case 'EOF': feof(fp); exit(0); case '>': putchar (ch); break; case '<': putchar (ch); switch ((ch = getc(fp)) ) { case '?': ch = getc(fp); while ( (ch = getc(fp)) != EOF) { if (ch == '?' && (ch = getc(fp)) == '>' ) { break; } else { ch = getc(fp); } } break; case '>': putchar (ch); break; default: putchar(ch); break; } break; default: putchar (ch); } } while (ch != EOF); return 0; }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090616170244.GA40934>