home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / bsd / 8480 < prev    next >
Encoding:
Text File  |  1992-11-06  |  4.1 KB  |  105 lines

  1. Newsgroups: comp.unix.bsd
  2. Path: sparky!uunet!pmafire!mica.inel.gov!ux1!fcom.cc.utah.edu!cs.weber.edu!terry
  3. From: terry@cs.weber.edu (A Wizard of Earth C)
  4. Subject: Re: mktemp - Bus Error.
  5. Message-ID: <1992Nov5.180007.28471@fcom.cc.utah.edu>
  6. Sender: news@fcom.cc.utah.edu
  7. Organization: Weber State University  (Ogden, UT)
  8. References: <KHERA.92Nov3111245@thneed.cs.duke.edu> <1992Nov3.174359.19262@Princeton.EDU> <9231017.24452@mulga.cs.mu.OZ.AU>
  9. Date: Thu, 5 Nov 92 18:00:07 GMT
  10. Lines: 93
  11.  
  12. In article <9231017.24452@mulga.cs.mu.OZ.AU> ggr@nareen.acci.com.au (Greg Rose) writes:
  13. >In article <1992Nov3.174359.19262@Princeton.EDU> jsm@shade.Princeton.EDU (John Scott McCauley Jr.) writes:
  14. >>In article <KHERA.92Nov3111245@thneed.cs.duke.edu> khera@cs.duke.edu (Vivek Khera) writes:
  15. >>[on mktemp]
  16. >>>you don't need something so complicated.  how about this:
  17. >>>
  18. >>>    char tmpname[] = "/tmp/foo.XXXXXX";
  19. >>>    mktemp(tempname);
  20. >>[should be tmpname]
  21. >>>
  22. >>
  23. >>Careful -- it doesn't work for some C-compilers:
  24. >>"t.c", line 3: no automatic aggregate initialization
  25. >>"t.c", line 3: illegal lhs of assignment operator
  26. >>
  27. >>Program defensively! I'd rather have something complicated that works!
  28. >>
  29. >
  30. >I think there is a misunderstanding here. "line 3" is either legal C,
  31. >and always has been, or illegal C, and still is, depending on where
  32. >the line is.
  33. >
  34. >The word "automatic" in the error message refers to the storage class
  35. >of the variable "tmpname". If line 3 appears inside a function, the
  36. >storage class assumed is "auto", and no, you can't copy around arrays
  37. >like that in C. If line 3 is outside a function, the storage class
  38. >assumed is "extern", and the statement becomes an initialisation of
  39. >the array (not any sort of assignment), and has been legal C since at
  40. >least 1975. No C compiler could refuse it.
  41.  
  42.  
  43. Several compilers treat:
  44.  
  45.     char tmpname[] = "/tmp/foo.XXXXXX";
  46.  
  47. as if it were:
  48.  
  49.     char *tmpname = "/tmp/foo.XXXXXX";
  50.  
  51. ...the second is, of course, legal.  This is bad programming style, just as
  52. a construct of the form:
  53.  
  54.     char *foo = "Hello World" + 6;
  55.  
  56.     printf( "%s\n", foo);        /* prints "World\n"*/
  57.     printf( "%s\n", foo - 6);    /* depends on code generation*/
  58.  
  59. is bad style ("style" being synonymous with "practice" here).  If the
  60. compiler generates code the K&R/Portable C compile way, the second
  61. printf() will print "Hello World\n"; otherwise, what it does is not
  62. defined (for instance, Microsoft C).
  63.  
  64. If the code wasn't going to be reentered (ie: the mktemp() was called only
  65. once), then using a "char *x" or equivalent ("char x[]" on some compilers)
  66. is socially acceptable.
  67.  
  68. The fact that mktemp() returns a pointer to the modified string (this is
  69. *not* explicitly stated in the Sun man page, but is how it is coded on
  70. Sun and elsewhere, and documented elsewhere) means that you should be
  71. able to use:
  72.  
  73.     char    *p;
  74.  
  75.     p = mktemp( "/tmp/foo.XXXXXX");
  76.  
  77. without ill effect.  The fact that "/tmp/foo.XXXXXX" is considered a
  78. constant by GCC is an error in gcc, according to the rules of prottyping.
  79. This assumes you have an appropriate prototype for mktemp() before calling
  80. it so you can coerce the type of the argument's storage class declarater
  81. in the case of something with a default storage class of "const char"
  82. (ie: a literal string).
  83.  
  84. For portability, use "*foo" rather than "foo[]" as a pointer declarator
  85. and include all of the necessary header files when using ANSI-C.  Also
  86. prototype your own functions which may be passed "default const" values,
  87. since the compiler isn't smart enough to realize the way the parameters
  88. are used (or rather, the linker isn't smart enough).
  89.  
  90. If the mktemp() still fails, then there is a problem with type coercion
  91. in the compiler that needs to be fixed.
  92.  
  93.  
  94.                     Terry Lambert
  95.                     terry@icarus.weber.edu
  96.                     terry_lambert@novell.com
  97. ---
  98. Any opinions in this posting are my own and not those of my present
  99. or previous employers.
  100. -- 
  101. -------------------------------------------------------------------------------
  102.                                         "I have an 8 user poetic license" - me
  103.  Get the 386bsd FAQ from agate.berkeley.edu:/pub/386BSD/386bsd-0.1/unofficial
  104. -------------------------------------------------------------------------------
  105.