root/src/dps8/utfile.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. hash32s
  2. utfile_mkstemps

   1 /*
   2  * vim: filetype=c:tabstop=4:ai:expandtab
   3  * SPDX-License-Identifier: ICU
   4  * scspell-id: 485f7d3b-f630-11ec-953b-80ee73e9b8e7
   5  *
   6  * ---------------------------------------------------------------------------
   7  *
   8  * Copyright (c) 2021-2025 The DPS8M Development Team
   9  *
  10  * This software is made available under the terms of the ICU License.
  11  * See the LICENSE.md file at the top-level directory of this distribution.
  12  *
  13  * ---------------------------------------------------------------------------
  14  */
  15 
  16 #include <ctype.h>
  17 #include <signal.h>
  18 #include <errno.h>
  19 #include <fcntl.h>
  20 #include <stdio.h>
  21 #include <stdlib.h>
  22 #include <string.h>
  23 #if defined(__sunos) && defined(SYSV)
  24 # include <sys/param.h>
  25 #endif /* if defined(__sunos) && defined(SYSV) */
  26 #include <sys/stat.h>
  27 #include <sys/types.h>
  28 #include <time.h>
  29 #include <sys/time.h>
  30 #include <stdint.h>
  31 #include <unistd.h>
  32 #include "dps8_sir.h"
  33 
  34 #define MAX_MKSTEMPS_TRIES 10000
  35 
  36 #if defined(__MINGW64__) || defined(__MINGW32__)
  37 # include "bsd_random.h"
  38 # define random  bsd_random
  39 # define srandom bsd_srandom
  40 #endif /* if defined(__MINGW64__) || defined(__MINGW32__) */
  41 
  42 #if defined(FREE)
  43 # undef FREE
  44 #endif /* if defined(FREE) */
  45 #define FREE(p) do  \
  46   {                 \
  47     free((p));      \
  48     (p) = NULL;     \
  49   } while(0)
  50 
  51 static char valid_file_name_chars[]
  52   = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  53 
  54 static inline uint32_t
  55 hash32s(const void *buf, size_t len, uint32_t h)
     /* [previous][next][first][last][top][bottom][index][help] */
  56 {
  57   const unsigned char *p = buf;
  58 
  59   for (size_t i = 0; i < len; i++)
  60     h = h * 31 + p[i];
  61 
  62   h ^= h >> 17;
  63   h *= UINT32_C(0xed5ad4bb);
  64   h ^= h >> 11;
  65   h *= UINT32_C(0xac4c1b51);
  66   h ^= h >> 15;
  67   h *= UINT32_C(0x31848bab);
  68   h ^= h >> 14;
  69 
  70   return h;
  71 }
  72 
  73  /*
  74   * This is a minimal portable replacement for the mkstemps
  75   * function, which is not available on all platforms.
  76   */
  77 
  78 int
  79 utfile_mkstemps(char *request_pattern, int suffix_length)
     /* [previous][next][first][last][top][bottom][index][help] */
  80 {
  81   long pattern_length;
  82   int st1ret;
  83   char *mask_pointer;
  84   struct timespec st1;
  85 
  86   char *pattern = request_pattern;
  87   if (!pattern)
  88     return -1;
  89 
  90   pattern_length = (long) strlen(pattern);
  91 
  92   st1ret = clock_gettime(SIR_WALLCLOCK, &st1);
  93   if (st1ret != 0)
  94     {
  95       (void)fprintf (stderr, "\rFATAL: clock_gettime failure! Aborting at %s[%s:%d]\r\n",
  96                      __func__, __FILE__, __LINE__);
  97 #if defined(USE_BACKTRACE)
  98 # if defined(SIGUSR2)
  99       (void)raise(SIGUSR2);
 100       /*NOTREACHED*/ /* unreachable */
 101 # endif /* if defined(SIGUSR2) */
 102 #endif /* if defined(USE_BACKTRACE) */
 103       abort();
 104     }
 105 
 106   uint32_t h = 0;  /* initial hash value */
 107 #if __STDC_VERSION__ < 201112L
 108   /* LINTED E_OLD_STYLE_FUNC_DECL */
 109   void *(*mallocptr)() = malloc;
 110   h = hash32s(&mallocptr, sizeof(mallocptr), h);
 111 #endif /* if __STDC_VERSION__ < 201112L */
 112   void *ptr = &ptr;
 113   h = hash32s(&ptr, sizeof(ptr), h);
 114   time_t t = time(0);
 115   h = hash32s(&t, sizeof(t), h);
 116 #if !defined(_AIX) && !defined(__managarm__)
 117   for (int i = 0; i < 1000; i++)
 118     {
 119       unsigned long counter = 0;
 120       clock_t start = clock();
 121       while (clock() == start)
 122         {
 123           counter++;
 124         }
 125       h = hash32s(&start, sizeof(start), h);
 126       h = hash32s(&counter, sizeof(counter), h);
 127     }
 128 #endif /* if !defined(_AIX) */
 129   int mypid = (int)_sir_getpid();
 130   h = hash32s(&mypid, sizeof(mypid), h);
 131   char rnd[4];
 132   FILE *f = fopen("/dev/urandom", "rb");
 133   if (f)
 134     {
 135       if (fread(rnd, sizeof(rnd), 1, f))
 136         {
 137           h = hash32s(rnd, sizeof(rnd), h);
 138         }
 139       fclose(f);
 140     }
 141   srandom(h);
 142 
 143   if (( (long) pattern_length - 6 ) < (long) suffix_length)
 144     return ( -1 );
 145 
 146   long mask_offset = (long) pattern_length - ( 6 + (long) suffix_length );
 147 
 148   if (strncmp(&pattern[mask_offset], "XXXXXX", 6))
 149     return ( -1 );
 150 
 151   mask_pointer = &pattern[mask_offset];
 152 
 153   long valid_char_count = (long) strlen(valid_file_name_chars);
 154 
 155   if (valid_char_count < 1)
 156     return ( -1 );
 157 
 158   for (int count = 0; count < MAX_MKSTEMPS_TRIES; count++)
 159   {
 160     for (int mask_index = 0; mask_index < 6; mask_index++)
 161     {
 162       mask_pointer[mask_index]
 163         = valid_file_name_chars[random() % valid_char_count];
 164     }
 165 
 166     int fd = open(pattern, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
 167 
 168     if (fd >= 0)
 169       return ( fd );
 170 
 171     /*
 172      * If the error is not "file already exists",
 173      * or is a directory, then we just bail out.
 174      */
 175 
 176     if (( errno != EEXIST ) && ( errno != EISDIR ))
 177     {
 178       break;
 179     }
 180   }
 181 
 182   /*
 183    * If we get here, we were unable to create
 184    * a unique file name, despite many tries.
 185    */
 186 
 187   return ( -1 );
 188 }

/* [previous][next][first][last][top][bottom][index][help] */