home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: gnu.misc.discuss
- Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!bpb9204
- From: bpb9204@tamsun.tamu.edu (Brent)
- Subject: Gripe with Bison code (and a suggested fix...)
- Message-ID: <1992Sep9.041540.10477@tamsun.tamu.edu>
- Organization: Texas A&M Univ., Inc.
- Date: Wed, 9 Sep 1992 04:15:40 GMT
- Lines: 50
-
-
- I have a problem with some of the Bison code (version 1.18). Not the
- generated code, but bison's source.
-
- For instance, here is a little jewel of coding wizardy. It can be also
- labeled as, "how to do variable args when you don't know how."
-
- ---------------------- code from main.c
- void fatals(fmt,x1,x2,x3,x4,x5,x6,x7,x8)
- char *fmt;
- {
- char buffer[some_size];
- sprintf(buffer, fmt, x1,x2,x3,x4,x5,x6,x7,x8);
- }
- ---------------------- end of code
-
- This is totally ridiculous! Of course, my compiler is probably the only one
- that chokes on this (because x1-x8 have no type declared), but that's
- irrelevant. This coding style is what gives C such a bad name (OK,
- *one* of the things), not to mention all the extra parameters that
- have to be passed to sprintf every time it's called, even if they are zero.
-
- Here is the fixed code:
-
- ---------------------
- #include <stdarg.h> /* or varargs.h or whatever your system uses */
-
- void fatals( char *fmt, ...)
- {
- char buffer[BUF_SIZE];
- va_list ap;
-
- va_start( ap, fmt);
- (void)vsprintf( buffer, fmt, ap); // ohhh, that's what the vprintf's do...
- va_end(ap);
- }
- ---------------------------
-
- Then, you would declare the function as "void fatals(char *, ...);" where-
- ever you used it. It took me 5 minutes to learn how to *properly* use
- variable-argument functions. When I see code like the first snippet,
- I naturally wonder about the other sections that my compiler doesn't
- reject.
-
- I am going to go through and fix this function throughout the source.
-
- I am willing to distribute the fixed code, provided the FSF, et al will
- accept code modifications from a Macintosh.
-
- -Brent
-