From: H.J. Lu (hlu@eecs.wsu.edu)
Date: 10/28/92


From: hlu@eecs.wsu.edu (H.J. Lu)
Subject: Re: what is mkstemp in libc
Date: 28 Oct 1992 21:20:10 GMT

In article <1992Oct28.203447.8432@utdallas.edu>, ramesh@utdallas.edu (R. Ramesh) writes:
|> I am trying to compile tvtwm and came across a missing libc function called
|> mkstemp. Is there a equivalent for it in libc (would that be mktmp?)
|>
|> Ramesh
|>

mkstemp is in alpha libc 4.2, which I just released for testing last night.

H.J.
=========
/* Copyright (C) 1991, 1992 Free Software Foundation, Inc.
This file is part of the GNU C Library.

The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.

The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with the GNU C Library; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 675 Mass Ave,
Cambridge, MA 02139, USA. */

#include <ansidecl.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <alloca.h>
#include <fcntl.h>
#include <sys/types.h>

#if 1
#define __access access
#define __open open
#endif

/* Return a file descriptor with a unique temporary file name from TEMPLATE.
   The last six characters of TEMPLATE must be "XXXXXX";
   they are replaced with a string that makes the filename unique.
   Return -1 if no suitable file could be created. */
int
DEFUN(mkstemp, (template), char *template)
{
  char *buffer;
  size_t len;

  len = strlen(template);
  if (len < 6 || strcmp(&template[len - 6], "XXXXXX")) {
    errno = EINVAL;
    return -1;
  }

  buffer = (char *) __alloca(len + 1);
  strcpy (buffer, template);

  while (mktemp (template) != NULL) {

    /* Make sure `template' doesn't exist. */
    if (__access (template, F_OK))
      return __open (template, O_RDWR | O_CREAT, 0666);

    strcpy (template, buffer);
  }

  return -1;
}