Ticket #1171: vformat_win32.patch

File vformat_win32.patch, 3.2 KB (added by henrik, 3 years ago)
  • tests/support.c

     
    11#include "support.h" 
    22 
     3#ifdef WIN32 
     4 
     5/*  Testing of vformat requires a temporary directory. 
     6 *  On Windows, there is no mkdtemp, 
     7 *  and GLib only provides g_mkstemp. 
     8 *  Hence we have to define our own version of mkdtemp. 
     9 *  The code below is based on g_mkstemp_full from GLib. 
     10 *  Copyright 2000 Red Hat, Inc. 
     11 */ 
     12 
     13#include <errno.h> 
     14gint 
     15mkdtemp_full (gchar *tmpl,  
     16                int    mode) 
     17{ 
     18  char *XXXXXX; 
     19  int count, fd; 
     20  static const char letters[] = 
     21    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
     22  static const int NLETTERS = sizeof (letters) - 1; 
     23  glong value; 
     24  GTimeVal tv; 
     25  static int counter = 0; 
     26 
     27  g_return_val_if_fail (tmpl != NULL, -1); 
     28 
     29 
     30  /* find the last occurrence of "XXXXXX" */ 
     31  XXXXXX = g_strrstr (tmpl, "XXXXXX"); 
     32 
     33  if (!XXXXXX || strncmp (XXXXXX, "XXXXXX", 6)) 
     34    { 
     35      errno = EINVAL; 
     36      return -1; 
     37    } 
     38 
     39  /* Get some more or less random data.  */ 
     40  g_get_current_time (&tv); 
     41  value = (tv.tv_usec ^ tv.tv_sec) + counter++; 
     42 
     43  for (count = 0; count < 100; value += 7777, ++count) 
     44    { 
     45      glong v = value; 
     46 
     47      /* Fill in the random bits.  */ 
     48      XXXXXX[0] = letters[v % NLETTERS]; 
     49      v /= NLETTERS; 
     50      XXXXXX[1] = letters[v % NLETTERS]; 
     51      v /= NLETTERS; 
     52      XXXXXX[2] = letters[v % NLETTERS]; 
     53      v /= NLETTERS; 
     54      XXXXXX[3] = letters[v % NLETTERS]; 
     55      v /= NLETTERS; 
     56      XXXXXX[4] = letters[v % NLETTERS]; 
     57      v /= NLETTERS; 
     58      XXXXXX[5] = letters[v % NLETTERS]; 
     59 
     60      /* tmpl is in UTF-8 on Windows, thus use g_open() */ 
     61      fd = g_mkdir (tmpl, mode); 
     62 
     63      if (fd >= 0) 
     64        return fd; 
     65      else if (errno != EEXIST) 
     66        /* Any other error will apply also to other names we might 
     67         *  try, and there are 2^32 or so of them, so give up now. 
     68         */ 
     69        return -1; 
     70    } 
     71 
     72  /* We got out of the loop because we ran out of combinations to try.  */ 
     73  errno = EEXIST; 
     74  return -1; 
     75} 
     76 
     77gint 
     78mkdtemp (gchar *tmpl) 
     79{ 
     80  return mkdtemp_full (tmpl, 0700); 
     81} 
     82 
     83#endif 
     84 
     85// ------------------------------------------------------------ 
     86 
    387char *olddir = NULL; 
    488 
    589static void reset_env(void) 
    690{ 
    7         unsetenv("CONNECT_ERROR"); 
    8         unsetenv("CONNECT_TIMEOUT"); 
    9         unsetenv("INIT_NULL"); 
    10         unsetenv("GET_CHANGES_ERROR"); 
    11         unsetenv("GET_CHANGES_TIMEOUT"); 
    12         unsetenv("GET_CHANGES_TIMEOUT2"); 
    13         unsetenv("COMMIT_ERROR"); 
    14         unsetenv("COMMIT_TIMEOUT"); 
    15         unsetenv("SYNC_DONE_ERROR"); 
    16         unsetenv("SYNC_DONE_TIMEOUT"); 
    17         unsetenv("DISCONNECT_ERROR"); 
    18         unsetenv("DISCONNECT_TIMEOUT"); 
     91        g_unsetenv("CONNECT_ERROR"); 
     92        g_unsetenv("CONNECT_TIMEOUT"); 
     93        g_unsetenv("INIT_NULL"); 
     94        g_unsetenv("GET_CHANGES_ERROR"); 
     95        g_unsetenv("GET_CHANGES_TIMEOUT"); 
     96        g_unsetenv("GET_CHANGES_TIMEOUT2"); 
     97        g_unsetenv("COMMIT_ERROR"); 
     98        g_unsetenv("COMMIT_TIMEOUT"); 
     99        g_unsetenv("SYNC_DONE_ERROR"); 
     100        g_unsetenv("SYNC_DONE_TIMEOUT"); 
     101        g_unsetenv("DISCONNECT_ERROR"); 
     102        g_unsetenv("DISCONNECT_TIMEOUT"); 
    19103} 
    20  
    21104char *setup_testbed(char *fkt_name) 
    22105{ 
    23106         
     107#ifndef WIN32 
     108        // Run testbed as "nobody" 
    24109        setuid(65534); 
     110#endif 
    25111        char *testbed = g_strdup_printf("%s/testbed.XXXXXX", g_get_tmp_dir()); 
    26112        mkdtemp(testbed); 
    27113