*BSD News Article 7187


Return to BSD News archive

Path: sserve!manuel.anu.edu.au!munnari.oz.au!metro!otc!alexk
From: alexk@otc.otca.oz.au (Alex Kowalenko)
Newsgroups: comp.unix.bsd
Subject: Re: mktemp - Bus Error. SUMMARY
Message-ID: <6634@otc.otca.oz>
Date: 29 Oct 92 22:54:21 GMT
Sender: news@otc.otca.oz
Organization: R&D, OTC Sydney, Australia
Lines: 88


-- 

> I'm having problems with mktemp failing due to a Bus Error.
> Even a simple program like the following will fail:

> #include<stdio.h>
>  
> main()
> {
>         printf("%s\n",mktemp("XXXX"));
> }


**** jtc@gain.com (J.T. Conklin) said :

>From the GCC Manual:
   * GNU CC normally makes string constants read-only.  If several
     identical-looking string constants are used, GNU CC stores only
     one copy of the string.

     One consequence is that you cannot call `mktemp' with a string
     constant argument.  The function `mktemp' always alters the
     string its argument points to.

     Another consequence is that `sscanf' does not work on some systems
     when passed a string constant as its format control string or
     input.  This is because `sscanf' incorrectly tries to write into
     the string constant.  Likewise `fscanf' and `scanf'.

     The best solution to these problems is to change the program to
     use `char'-array variables with initialization strings for these
     purposes instead of string constants.  But if this is not
     possible, you can use the `-fwritable-strings' flag, which
     directs GNU CC to handle string constants the same way most C
     compilers do.  `-traditional' also has this effect, among others.

Since ANSI C allows a compiler to make string constants read-only, and
the changes are usually easy, I tend to fix the code:

main()
{
	char buf[16];

	strcpy(buf, "/tmp/fooXXXXXX");
	printf("%s\n",mktemp(buf));
}

**** michaelm@pike.ee.mcgill.ca (Michael Moscovitch) said something similar

I have come accross this problem. I looked at the source to mktemp() and
found the problem. I don't remember the exact reason, but mktemp() doesn't
work with string constants as parameters. I think it assumes that you
are passing it an array of characters with more space in it. If I remember
correctly, it assumes it can modify the string you pass it. You can get
around it by using an intermediate character array to pass the value.

****  Paul Nulsen <pejn@cc.uow.edu.au> said:

>According to the manual entries for both sys V and bsd systems I have access
>to at the moment (my 386bsd system is at home), the string you give to
>mktemp must have 6 trailing X's. I guess that mktemp is trying to read or
>write beyond the end of your template string - maybe not ideal behaviour,
>but not unreasonable in the circumstances.

Are you reading the Sun man pages or BSD's. From the BSD manual page on mktemp

     		...	The template may be any file name with
     some number of `Xs' appended to it, for example /tmp/temp.XXXX. The
     trailing `Xs' are replaced with the current process number and/or a
     unique letter combination.  ...

Source code shows this also.

**** kym@bingsuns.cc.binghamton.edu (R. Kym Horsell)

>SIX X's old bean!!!

RTFMP Spring Chicken!!




    Alex Kowalenko                 ACSnet: alexk@otc.otca.oz
    R&D Contractor                   UUCP: {uunet,mcvax}!otc.otca.oz!alexk
    |||| OTC ||                       Fax: +6 2 287 3299
                                    Phone: +6 2 287 3131
                                    Snail: GPO Box 7000, Sydney 2001, Australia