Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4626054
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:21:45+00:00 2026-05-22T03:21:45+00:00

Can anybody point me to the code that implements mkstemp() (C/C++) on Win32, or

  • 0

Can anybody point me to the code that implements mkstemp() (C/C++) on Win32, or very close analog?

Must be race-free.

It’s supposed to look like

#include <windows.h>
#include <io.h>

// port of mkstemp() to win32. race-free.
// behaviour as described in http://linux.die.net/man/3/mkstemp
// 
int mkstemp(char *template) {
     ...
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:21:46+00:00Added an answer on May 22, 2026 at 3:21 am

    You can use the following function which is extracted from wcecompat library (from file src/stdlib_extras.cpp)

    /* mkstemp extracted from libc/sysdeps/posix/tempname.c.  Copyright
       (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc.
    
       The GNU C Library is free software; you can redistribute it and/or
       modify it under the terms of the GNU Lesser General Public
       License as published by the Free Software Foundation; either
       version 2.1 of the License, or (at your option) any later version.  */
    
    static const char letters[] =
    "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    
    /* Generate a temporary file name based on TMPL.  TMPL must match the
       rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
       does not exist at the time of the call to mkstemp.  TMPL is
       overwritten with the result.  */
    int
    mkstemp (char *tmpl)
    {
      int len;
      char *XXXXXX;
      static unsigned long long value;
      unsigned long long random_time_bits;
      unsigned int count;
      int fd = -1;
      int save_errno = errno;
    
      /* A lower bound on the number of temporary files to attempt to
         generate.  The maximum total number of temporary file names that
         can exist for a given template is 62**6.  It should never be
         necessary to try all these combinations.  Instead if a reasonable
         number of names is tried (we define reasonable as 62**3) fail to
         give the system administrator the chance to remove the problems.  */
    #define ATTEMPTS_MIN (62 * 62 * 62)
    
      /* The number of times to attempt to generate a temporary file.  To
         conform to POSIX, this must be no smaller than TMP_MAX.  */
    #if ATTEMPTS_MIN < TMP_MAX
      unsigned int attempts = TMP_MAX;
    #else
      unsigned int attempts = ATTEMPTS_MIN;
    #endif
    
      len = strlen (tmpl);
      if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX"))
        {
          errno = EINVAL;
          return -1;
        }
    
    /* This is where the Xs start.  */
      XXXXXX = &tmpl[len - 6];
    
      /* Get some more or less random data.  */
      {
        SYSTEMTIME      stNow;
        FILETIME ftNow;
    
        // get system time
        GetSystemTime(&stNow);
        stNow.wMilliseconds = 500;
        if (!SystemTimeToFileTime(&stNow, &ftNow))
        {
            errno = -1;
            return -1;
        }
    
        random_time_bits = (((unsigned long long)ftNow.dwHighDateTime << 32)
                            | (unsigned long long)ftNow.dwLowDateTime);
      }
      value += random_time_bits ^ (unsigned long long)GetCurrentThreadId ();
    
      for (count = 0; count < attempts; value += 7777, ++count)
        {
          unsigned long long v = value;
    
          /* Fill in the random bits.  */
          XXXXXX[0] = letters[v % 62];
          v /= 62;
          XXXXXX[1] = letters[v % 62];
          v /= 62;
          XXXXXX[2] = letters[v % 62];
          v /= 62;
          XXXXXX[3] = letters[v % 62];
          v /= 62;
          XXXXXX[4] = letters[v % 62];
          v /= 62;
          XXXXXX[5] = letters[v % 62];
    
          fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, _S_IREAD | _S_IWRITE);
          if (fd >= 0)
        {
          errno = save_errno;
          return fd;
        }
          else if (errno != EEXIST)
        return -1;
        }
    
      /* We got out of the loop because we ran out of combinations to try.  */
      errno = EEXIST;
      return -1;
    }
    

    It defines O_EXCL as;

    #define _O_EXCL         0x0400
    #define O_EXCL          _O_EXCL
    

    You can rip out mkstemp support out of it easily.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody point me to a sample SPA application (source code) that is constructed
Can anybody point me to a simple example about how to implement form based
Can anybody point me in the right direction to be able to encrypt a
can anybody point me in the right direction as to how I would go
Can anybody point me to a good beginner's guide for making Facebook apps?
I just wondered if anybody can point me in the right direction: I'm looking
can anybody tell me what's the point if any for a javascript function like
Can anybody point me in a direction to a great tutorial or how-to for
Ok, I realize that's not a very clear title, so hopefully this can clarify
Can anybody point me in the right direction to get Ninject working with WCF

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.