From: hcp@csx.cciw.ca (H.C. Pulley)
Subject: Re: core dump using mktemp()
Organization: Canada Centre for Inland Waters
Date: Sun, 13 Sep 1992 13:11:30 GMT
Keywords: coherent 4.0 mktemp core

In article <116@colorne.UUCP> dfilip@colorne.UUCP writes:
>Can someone please tell me what I am doing wrong ? The following code
>segment core dumps (segmentation violation) on me :

First of all, your code works without modification under Coherent 3.2.1a, IRIX,
EP/IX, and Ultrix.  I'm not sure that it should, though.  Your keywords above
suggest that you are running Coherent 4.0, which I can't test yet.

>#include <stdio.h>
>
>char msgtemp[33];
>
>extern char *mktemp();
>
>main()
>{
>	printf("before call to mktemp\n");
>
>	strcpy(msgtemp,mktemp("/tmp/sortXXXXXX"));

Paraphrased from the Coherent manual:
The parameter to mktemp is a pointer to char.  mktemp modifies your pattern
/tmp/sortXXXXXX and replaces the X's with a five-digit process ID and a letter
which is changed each time you call mktemp.  It returns the temporary file 
name in the pointer.

Quoting from 2nd edition C: A Reference Manual, by Harbison and Steele, 'A
string literal yields a constant of type "pointer to char"...' (p73).

It sounds like:
Your problem is that you have not given mktemp a string pointer variable.  The
mktemp function was trying to modify the constant string; this is a no-no which
causes core dumps.

If you change the above line to:

	strcpy(msgtemp,"/tmp/sortXXXXXX");

	mktemp(msgtemp);

it should work properly.

>	printf("after call to mktemp\n");
>
>	printf("msgtemp=%s\n",msgtemp);
>	exit(0);
>}
>
>BTW - if I remove the strcpy(.... and just call mktemp(... by itself, I
>get the same result. I can't figure out what I am doing wrong !

The problem is in the mktemp, not the strcpy.

My modified version and the original work under Coherent 3.2.1a.  I am still 
waiting (still within the number of days I was told to wait) for Coherent 4.0
so I cannot test it there.  Try it and see what happens.

I'm not sure whether your original version should have worked or not.  At any
rate it is considered bad programming style to have functions change strings
which do not have variable pointers attached to them. 

The string literal should be a constant pointer, not a pointer to constant
data.  You should only receive a core dump if mktemp() is actually trying to
modify the value of the pointer.  I don't think mktemp should need to modify
the pointer so I think this may be a problem.

Since your code works under Coherent 3.2.1a and the other operating systems I
have listed above I suspect that either a compiler-problem or a library 
problem (change to mktemp()) is present in Coherent 4.0.  It is bad style, and
it may well be against the rules (problem with C: rules are tough to find) but
it seems to be an 'ad hoc standard'.  This means there is probably code like 
this buried in some programs that people are trying to port.

Harry
-- 
          hcp@csx.cciw.ca           | This message    | It takes all kinds,
    hcpiv@grumpy.cis.uoguelph.ca    | released to the | and to each his own.
------------------------------------| PUBLIC DOMAIN.  | This thought in mind,
 Stay away from the DOS side, Luke! |                 | I walk alone.