Function: make-temp-file-internal

make-temp-file-internal is a function defined in fileio.c.

Signature

(make-temp-file-internal PREFIX DIR-FLAG SUFFIX TEXT)

Documentation

Generate a new file whose name starts with PREFIX, a string.

Return the name of the generated file. If DIR-FLAG is zero, do not create the file, just its name. Otherwise, if DIR-FLAG is non-nil, create an empty directory. The file name should end in SUFFIX. Do not expand PREFIX; a non-absolute PREFIX is relative to the Emacs working directory. If TEXT is a string, insert it into the newly created file.

Signal an error if the file could not be created.

This function does not grok magic file names.

Source Code

// Defined in /usr/src/emacs/src/fileio.c
{
  CHECK_STRING (prefix);
  CHECK_STRING (suffix);
  Lisp_Object encoded_prefix = ENCODE_FILE (prefix);
  Lisp_Object encoded_suffix = ENCODE_FILE (suffix);
  ptrdiff_t prefix_len = SBYTES (encoded_prefix);
  ptrdiff_t suffix_len = SBYTES (encoded_suffix);
  if (INT_MAX < suffix_len)
    args_out_of_range (prefix, suffix);
  int nX = 6;
  Lisp_Object val = make_uninit_string (prefix_len + nX + suffix_len);
  char *data = SSDATA (val);
  memcpy (data, SSDATA (encoded_prefix), prefix_len);
  memset (data + prefix_len, 'X', nX);
  memcpy (data + prefix_len + nX, SSDATA (encoded_suffix), suffix_len);
  int kind = (NILP (dir_flag) ? GT_FILE
	      : BASE_EQ (dir_flag, make_fixnum (0)) ? GT_NOCREATE
	      : GT_DIR);
  int fd = gen_tempname (data, suffix_len, O_BINARY | O_CLOEXEC, kind);
  bool failed = fd < 0;
  if (!failed)
    {
      specpdl_ref count = SPECPDL_INDEX ();
      record_unwind_protect_int (close_file_unwind, fd);
      val = DECODE_FILE (val);
      if (STRINGP (text) && SBYTES (text) != 0)
	write_region (text, Qnil, val, Qnil, Qnil, Qnil, Qnil, fd);
      failed = NILP (dir_flag) && emacs_close (fd) != 0;
      /* Discard the unwind protect.  */
      specpdl_ptr = specpdl_ref_to_ptr (count);
    }
  if (failed)
    {
      static char const kind_message[][32] =
	{
	  [GT_FILE] = "Creating file with prefix",
	  [GT_DIR] = "Creating directory with prefix",
	  [GT_NOCREATE] = "Creating file name with prefix"
	};
      report_file_error (kind_message[kind], prefix);
    }
  return val;
}