Endymion Ponsonby-Withermoor III wrote:
[Picking a post at random]
char *p = " can not modify";
p[0] ='b' ;is not allowed,
[Various people say that it shouldn't/won't work]
I have very vague recollections that there was (is?)a
Unix library function (probably several) for generating
temporary filenames:
extern void mktemp(char *s);
The idea was that you gave it a base name, such as
"abcdef0000000" and it would replace all the terminal
digits with a sequential number, or something. Anyway,
all the examples of its use that I saw, passed it
a quoted (i.e. constant) string. Calling the function
then overwrote that string.
Did I remember this right ?
This, I do not know. I just use C99's tmpnam() and
tmpfile() functions (which gives you a valid file
name/path which clashes with nothing or opens a
temporary file which will be removed when closed).
What would happen if a file fragment had two different
string variables pointing to different (but identical)
strings, like this:
#include <stdio.h>
static void f1(void)
{
char *aa = "Bugger";
fprintf(stdout,"aa=%d\n",(int)aa);
mktemp(aa);
}
static void f2(void)
{
char *bb = "Bugger";
fprintf(stdout,"bb=%d\n",(int)bb);
mktemp(bb);
}
extern void main(void)
{
f1();
f2();
}
I thought all identical string constants were replaced by
a single copy at compile time, so the two printfs above
would yield the same number.
This is not determined by the standard. A good compiler
probably will do this. Apart from that, it is a Bad Idea
to use a mere (int) cast for pointer output.
And void is no longer a valid return type for main().
It seems you have been out of business for over fifteen
years.
<OT>
Just had a look at 'man 3 mktemp'; your above code
will not work and is dangerous. Maybe this helps you:
--------------------------------------------------------
mktemp - make a unique temporary file name
SYNOPSIS
#include <stdlib.h>
char *mktemp(char *template);
DESCRIPTION
The mktemp() function generates a unique temporary file name
from template. The last
six characters of template must be XXXXXX and these are replaced
with a string that
makes the filename unique. Since it will be modified, template
must not be a string constant, but should be declared as a
character array.
RETURN VALUE
The mktemp() function returns NULL on error (template did not
end in XXXXXX) and template otherwise. If the call was
successful, the last six bytes of template will have
been modified in such a way that the resulting name is unique
(does not exist already).
If the call was unsuccessful, template is made an empty string.
ERRORS
EINVAL The last six characters of template were not XXXXXX.
CONFORMING TO
BSD 4.3. POSIX dictates tmpnam(3).
NOTE
The prototype is in <unistd.h> for libc4, libc5, glibc1; glibc2
follows the Single Unix Specification and has the prototype in
<stdlib.h>.
BUGS
Never use mktemp(). Some implementations follow BSD 4.3 and
replace XXXXXX by the current process id and a single letter,
so that at most 26 different names can be returned.
Since on the one hand the names are easy to guess, and on the
other hand there is a race between testing whether the name
exists and opening the file, every use of mktemp() is a
security risk. The race is avoided by mkstemp(3).
--------------------------------------------------------
</OT>
Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.