home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / gnu / misc / discuss / 2956 < prev    next >
Encoding:
Text File  |  1992-09-08  |  1.9 KB  |  60 lines

  1. Newsgroups: gnu.misc.discuss
  2. Path: sparky!uunet!cs.utexas.edu!tamsun.tamu.edu!bpb9204
  3. From: bpb9204@tamsun.tamu.edu (Brent)
  4. Subject: Gripe with Bison code (and a suggested fix...)
  5. Message-ID: <1992Sep9.041540.10477@tamsun.tamu.edu>
  6. Organization: Texas A&M Univ., Inc.
  7. Date: Wed, 9 Sep 1992 04:15:40 GMT
  8. Lines: 50
  9.  
  10.  
  11. I have a problem with some of the Bison code (version 1.18).  Not the
  12. generated code, but bison's source.
  13.  
  14. For instance, here is a little jewel of coding wizardy.  It can be also
  15. labeled as, "how to do variable args when you don't know how."
  16.  
  17. ----------------------  code from main.c
  18. void fatals(fmt,x1,x2,x3,x4,x5,x6,x7,x8)
  19. char *fmt;
  20. {
  21.   char buffer[some_size];
  22.   sprintf(buffer, fmt, x1,x2,x3,x4,x5,x6,x7,x8);
  23. }
  24. ---------------------- end of code
  25.  
  26. This is totally ridiculous!  Of course, my compiler is probably the only one
  27. that chokes on this (because x1-x8 have no type declared), but that's 
  28. irrelevant.  This coding style is what gives C such a bad name (OK,
  29. *one* of the things), not to mention all the extra parameters that
  30. have to be passed to sprintf every time it's called, even if they are zero.
  31.  
  32. Here is the fixed code:
  33.  
  34. ---------------------
  35. #include <stdarg.h>  /* or varargs.h or whatever your system uses */
  36.  
  37. void fatals( char *fmt, ...)
  38. {
  39.   char buffer[BUF_SIZE];
  40.   va_list ap; 
  41.  
  42.   va_start( ap, fmt);
  43.   (void)vsprintf( buffer, fmt, ap);  // ohhh, that's what the vprintf's do...
  44.   va_end(ap);
  45. }
  46. ---------------------------
  47.  
  48. Then, you would declare the function as "void fatals(char *, ...);" where-
  49. ever you used it.  It took me 5 minutes to learn how to *properly* use
  50. variable-argument functions.  When I see code like the first snippet,
  51. I naturally wonder about the other sections that my compiler doesn't
  52. reject.
  53.  
  54. I am going to go through and fix this function throughout the source.  
  55.  
  56. I am willing to distribute the fixed code, provided the FSF, et al will
  57. accept code modifications from a Macintosh. 
  58.  
  59. -Brent
  60.