*BSD News Article 7475


Return to BSD News archive

Newsgroups: comp.unix.bsd
Path: sserve!manuel.anu.edu.au!munnari.oz.au!spool.mu.edu!caen!hellgate.utah.edu!fcom.cc.utah.edu!cs.weber.edu!terry
From: terry@cs.weber.edu (A Wizard of Earth C)
Subject: Re: mktemp - Bus Error.
Message-ID: <1992Nov5.180007.28471@fcom.cc.utah.edu>
Sender: news@fcom.cc.utah.edu
Organization: Weber State University  (Ogden, UT)
References: <KHERA.92Nov3111245@thneed.cs.duke.edu> <1992Nov3.174359.19262@Princeton.EDU> <9231017.24452@mulga.cs.mu.OZ.AU>
Date: Thu, 5 Nov 92 18:00:07 GMT
Lines: 93

In article <9231017.24452@mulga.cs.mu.OZ.AU> ggr@nareen.acci.com.au (Greg Rose) writes:
>In article <1992Nov3.174359.19262@Princeton.EDU> jsm@shade.Princeton.EDU (John Scott McCauley Jr.) writes:
>>In article <KHERA.92Nov3111245@thneed.cs.duke.edu> khera@cs.duke.edu (Vivek Khera) writes:
>>[on mktemp]
>>>you don't need something so complicated.  how about this:
>>>
>>>	char tmpname[] = "/tmp/foo.XXXXXX";
>>>	mktemp(tempname);
>>[should be tmpname]
>>>
>>
>>Careful -- it doesn't work for some C-compilers:
>>"t.c", line 3: no automatic aggregate initialization
>>"t.c", line 3: illegal lhs of assignment operator
>>
>>Program defensively! I'd rather have something complicated that works!
>>
>
>I think there is a misunderstanding here. "line 3" is either legal C,
>and always has been, or illegal C, and still is, depending on where
>the line is.
>
>The word "automatic" in the error message refers to the storage class
>of the variable "tmpname". If line 3 appears inside a function, the
>storage class assumed is "auto", and no, you can't copy around arrays
>like that in C. If line 3 is outside a function, the storage class
>assumed is "extern", and the statement becomes an initialisation of
>the array (not any sort of assignment), and has been legal C since at
>least 1975. No C compiler could refuse it.


Several compilers treat:

	char tmpname[] = "/tmp/foo.XXXXXX";

as if it were:

	char *tmpname = "/tmp/foo.XXXXXX";

....the second is, of course, legal.  This is bad programming style, just as
a construct of the form:

	char *foo = "Hello World" + 6;

	printf( "%s\n", foo);		/* prints "World\n"*/
	printf( "%s\n", foo - 6);	/* depends on code generation*/

is bad style ("style" being synonymous with "practice" here).  If the
compiler generates code the K&R/Portable C compile way, the second
printf() will print "Hello World\n"; otherwise, what it does is not
defined (for instance, Microsoft C).

If the code wasn't going to be reentered (ie: the mktemp() was called only
once), then using a "char *x" or equivalent ("char x[]" on some compilers)
is socially acceptable.

The fact that mktemp() returns a pointer to the modified string (this is
*not* explicitly stated in the Sun man page, but is how it is coded on
Sun and elsewhere, and documented elsewhere) means that you should be
able to use:

	char	*p;

	p = mktemp( "/tmp/foo.XXXXXX");

without ill effect.  The fact that "/tmp/foo.XXXXXX" is considered a
constant by GCC is an error in gcc, according to the rules of prottyping.
This assumes you have an appropriate prototype for mktemp() before calling
it so you can coerce the type of the argument's storage class declarater
in the case of something with a default storage class of "const char"
(ie: a literal string).

For portability, use "*foo" rather than "foo[]" as a pointer declarator
and include all of the necessary header files when using ANSI-C.  Also
prototype your own functions which may be passed "default const" values,
since the compiler isn't smart enough to realize the way the parameters
are used (or rather, the linker isn't smart enough).

If the mktemp() still fails, then there is a problem with type coercion
in the compiler that needs to be fixed.


					Terry Lambert
					terry@icarus.weber.edu
					terry_lambert@novell.com
---
Any opinions in this posting are my own and not those of my present
or previous employers.
-- 
-------------------------------------------------------------------------------
                                        "I have an 8 user poetic license" - me
 Get the 386bsd FAQ from agate.berkeley.edu:/pub/386BSD/386bsd-0.1/unofficial
-------------------------------------------------------------------------------