root/src/simh/sim_fio.c

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

DEFINITIONS

This source file includes following definitions.
  1. sim_finit
  2. sim_buf_swap_data
  3. sim_fread
  4. sim_buf_copy_swapped
  5. sim_fwrite
  6. sim_fsize_ex
  7. sim_fsize_name_ex
  8. sim_fsize_name
  9. sim_fsize
  10. sim_fopen
  11. sim_fseeko
  12. sim_ftell
  13. sim_fseeko
  14. sim_ftell
  15. sim_fseek
  16. sim_set_fsize
  17. sim_set_fifo_nonblock
  18. sim_set_fsize
  19. sim_set_fifo_nonblock

   1 /*
   2  * sim_fio.c: simulator file I/O library
   3  *
   4  * vim: filetype=c:tabstop=4:ai:expandtab
   5  * SPDX-License-Identifier: MIT
   6  * scspell-id: bc8c09f8-f62a-11ec-9723-80ee73e9b8e7
   7  *
   8  * ---------------------------------------------------------------------------
   9  *
  10  * Copyright (c) 1993-2008 Robert M. Supnik
  11  * Copyright (c) 2021-2024 The DPS8M Development Team
  12  *
  13  * Permission is hereby granted, free of charge, to any person obtaining a
  14  * copy of this software and associated documentation files (the "Software"),
  15  * to deal in the Software without restriction, including without limitation
  16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  17  * and/or sell copies of the Software, and to permit persons to whom the
  18  * Software is furnished to do so, subject to the following conditions:
  19  *
  20  * The above copyright notice and this permission notice shall be included
  21  * in all copies or substantial portions of the Software.
  22  *
  23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  26  * ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  27  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
  28  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29  * SOFTWARE.
  30  *
  31  * Except as contained in this notice, the name of Robert M. Supnik shall not
  32  * be used in advertising or otherwise to promote the sale, use or other
  33  * dealings in this Software without prior written authorization from
  34  * Robert M. Supnik.
  35  *
  36  * ---------------------------------------------------------------------------
  37  */
  38 
  39 /*
  40  * This library includes:
  41  *
  42  * sim_finit            -       initialize package
  43  * sim_fopen            -       open file
  44  * sim_fread            -       endian independent read (formerly fxread)
  45  * sim_write            -       endian independent write (formerly fxwrite)
  46  * sim_fseek            -       conditionally extended (>32b) seek (
  47  * sim_fseeko           -       extended seek (>32b if available)
  48  * sim_fsize            -       get file size
  49  * sim_fsize_name       -       get file size of named file
  50  * sim_fsize_ex         -       get file size as a t_offset
  51  * sim_fsize_name_ex    -       get file size as a t_offset of named file
  52  * sim_buf_copy_swapped -       copy data swapping elements along the way
  53  * sim_buf_swap_data    -       swap data elements inplace in buffer
  54  */
  55 
  56 #include <fcntl.h>
  57 #include <sys/file.h>
  58 #include <sys/types.h>
  59 #include <sys/stat.h>
  60 
  61 #include "sim_defs.h"
  62 
  63 #include "../decNumber/decContext.h"
  64 #include "../decNumber/decNumberLocal.h"
  65 
  66 #if !defined(DECLITEND)
  67 # error Unknown platform endianness
  68 #endif /* if !defined(DECLITEND) */
  69 
  70 #if defined(FREE)
  71 # undef FREE
  72 #endif /* if defined(FREE) */
  73 #define FREE(p) do  \
  74   {                 \
  75     free((p));      \
  76     (p) = NULL;     \
  77   } while(0)
  78 
  79 t_bool sim_end;                     /* TRUE = little endian, FALSE = big endian */
  80 
  81 /*
  82  * OS-independent, endian independent binary I/O package
  83  *
  84  * For consistency, all binary data read and written by the simulator
  85  * is stored in little endian data order.  That is, in a multi-byte
  86  * data item, the bytes are written out right to left, low order byte
  87  * to high order byte.  On a big endian host, data is read and written
  88  * from high byte to low byte.  Consequently, data written on a little
  89  * endian system must be byte reversed to be usable on a big endian
  90  * system, and vice versa.
  91  *
  92  * These routines are analogs of the standard C runtime routines
  93  * fread and fwrite.  If the host is little endian, or the data items
  94  * are size char, then the calls are passed directly to fread or
  95  * fwrite.  Otherwise, these routines perform the necessary byte swaps.
  96  * Sim_fread swaps in place, sim_fwrite uses an intermediate buffer.
  97  */
  98 
  99 int32 sim_finit (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101 sim_end = DECLITEND;
 102 return sim_end;
 103 }
 104 
 105 void sim_buf_swap_data (void *bptr, size_t size, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 106 {
 107 size_t j;
 108 size_t k;
 109 unsigned char by, *sptr, *dptr;
 110 
 111 if (sim_end || (count == 0) || (size == sizeof (char)))
 112     return;
 113 for (j = 0, dptr = sptr = (unsigned char *) bptr;       /* loop on items */
 114      j < count; j++) {
 115     for (k = (int32)(size - 1); k >= (((int32) size + 1) / 2); k--) {
 116         by = *sptr;                                     /* swap end-for-end */
 117         *sptr++ = *(dptr + k);
 118         *(dptr + k) = by;
 119         }
 120     sptr = dptr = dptr + size;                          /* next item */
 121     }
 122 }
 123 
 124 size_t sim_fread (void *bptr, size_t size, size_t count, FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 125 {
 126 size_t c;
 127 
 128 if ((size == 0) || (count == 0))                        /* check arguments */
 129     return 0;
 130 c = fread (bptr, size, count, fptr);                    /* read buffer */
 131 if (sim_end || (size == sizeof (char)) || (c == 0))     /* le, byte, or err? */
 132     return c;                                           /* done */
 133 sim_buf_swap_data (bptr, size, count);
 134 return c;
 135 }
 136 
 137 void sim_buf_copy_swapped (void *dbuf, const void *sbuf, size_t size, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 138 {
 139 size_t j;
 140 size_t k;
 141 const unsigned char *sptr = (const unsigned char *)sbuf;
 142 unsigned char *dptr = (unsigned char *)dbuf;
 143 
 144 if (sim_end || (size == sizeof (char))) {
 145     memcpy (dptr, sptr, size * count);
 146     return;
 147     }
 148 for (j = 0; j < count; j++) {                           /* loop on items */
 149     /* Unsigned countdown loop. Pre-decrement k before it's used inside the
 150        loop so that k == 0 in the loop body to process the last item, then
 151        terminate. Initialize k to size for the same reason: the pre-decrement
 152        gives us size - 1 in the loop body. */
 153     for (k = size; k > 0; /* empty */)
 154         *(dptr + --k) = *sptr++;
 155     dptr = dptr + size;
 156     }
 157 }
 158 
 159 size_t sim_fwrite (const void *bptr, size_t size, size_t count, FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 160 {
 161 size_t c, nelem, nbuf, lcnt, total;
 162 int32 i;
 163 const unsigned char *sptr;
 164 unsigned char *sim_flip;
 165 
 166 if ((size == 0) || (count == 0))                        /* check arguments */
 167     return 0;
 168 if (sim_end || (size == sizeof (char)))                 /* le or byte? */
 169     return fwrite (bptr, size, count, fptr);            /* done */
 170 sim_flip = (unsigned char *)malloc(FLIP_SIZE);
 171 if (!sim_flip)
 172     return 0;
 173 nelem = FLIP_SIZE / size;                               /* elements in buffer */
 174 nbuf = count / nelem;                                   /* number buffers */
 175 lcnt = count % nelem;                                   /* count in last buf */
 176 if (lcnt) nbuf = nbuf + 1;
 177 else lcnt = nelem;
 178 total = 0;
 179 sptr = (const unsigned char *) bptr;                    /* init input ptr */
 180 for (i = (int32)nbuf; i > 0; i--) {                     /* loop on buffers */
 181     c = (i == 1)? lcnt: nelem;
 182     sim_buf_copy_swapped (sim_flip, sptr, size, c);
 183     sptr = sptr + size * count;
 184     c = fwrite (sim_flip, size, c, fptr);
 185     if (c == 0) {
 186         FREE(sim_flip);
 187         return total;
 188         }
 189     total = total + c;
 190     }
 191 FREE(sim_flip);
 192 return total;
 193 }
 194 
 195 /* Forward Declaration */
 196 
 197 t_offset sim_ftell (FILE *st);
 198 
 199 /* Get file size */
 200 
 201 t_offset sim_fsize_ex (FILE *fp)
     /* [previous][next][first][last][top][bottom][index][help] */
 202 {
 203 t_offset pos, sz;
 204 
 205 if (fp == NULL)
 206     return 0;
 207 pos = sim_ftell (fp);
 208 if (sim_fseek (fp, 0, SEEK_END))
 209   return 0;
 210 sz = sim_ftell (fp);
 211 if (sim_fseeko (fp, pos, SEEK_SET))
 212   return 0;
 213 return sz;
 214 }
 215 
 216 t_offset sim_fsize_name_ex (const char *fname)
     /* [previous][next][first][last][top][bottom][index][help] */
 217 {
 218 FILE *fp;
 219 t_offset sz;
 220 
 221 if ((fp = sim_fopen (fname, "rb")) == NULL)
 222     return 0;
 223 sz = sim_fsize_ex (fp);
 224 fclose (fp);
 225 return sz;
 226 }
 227 
 228 uint32 sim_fsize_name (const char *fname)
     /* [previous][next][first][last][top][bottom][index][help] */
 229 {
 230 return (uint32)(sim_fsize_name_ex (fname));
 231 }
 232 
 233 uint32 sim_fsize (FILE *fp)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235 return (uint32)(sim_fsize_ex (fp));
 236 }
 237 
 238 /* OS-dependent routines */
 239 
 240 /* Optimized file open */
 241 
 242 FILE *sim_fopen (const char *file, const char *mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 243 {
 244 FILE *fsc = NULL;
 245 #if defined(USE_FCNTL) || defined(USE_FLOCK)
 246 # include <fcntl.h>
 247 # include <sys/stat.h>
 248 # include <sys/types.h>
 249 int writable = 0;
 250 int rc = 0;
 251 if (strstr(mode, "+") != NULL)
 252   writable = 1;
 253 if (strstr(mode, "w") != NULL)
 254   writable = 1;
 255 if (strstr(mode, "a") != NULL)
 256   writable = 1;
 257 #endif /* if defined(USE_FCNTL) || defined(USE_FLOCK) */
 258 fsc = fopen (file, mode);
 259 #if defined(USE_FCNTL)
 260 struct flock lock;
 261 (void)memset (&lock, 0, sizeof(lock));
 262 lock.l_type = F_WRLCK;
 263 if (writable && !sim_nolock) {
 264   if (fsc != NULL)
 265     rc = fcntl (fileno(fsc), F_SETLK, &lock);
 266   if (rc < 0) {
 267     if (!sim_quiet) {
 268       sim_printf ("%s(%s): %s [Error %d]",
 269                   __func__, mode, xstrerror_l(errno), errno);
 270       if (fcntl(fileno(fsc), F_GETLK, &lock) == 0 && lock.l_pid > 0)
 271         sim_printf (" (locked by PID %lu)",
 272                    (unsigned long)lock.l_pid);
 273       sim_printf ("\r\n");
 274     }
 275     if (!sim_iglock) {
 276       fclose(fsc);
 277       return NULL;
 278     }
 279   }
 280 }
 281 #elif defined(USE_FLOCK) /* if defined(USE_FCNTL) */
 282 if (writable && !sim_nolock) {
 283   if (fsc != NULL)
 284     rc = flock (fileno(fsc), LOCK_EX | LOCK_NB);
 285   if (rc < 0) {
 286     if (!sim_quiet)
 287       sim_printf ("%s(%s): %s [Error %d] (locked?)\r\n",
 288                   __func__, mode, xstrerror_l(errno), errno);
 289     if (!sim_iglock) return NULL;
 290   }
 291 }
 292 #endif /* elif defined(USE_FLOCK) */
 293 return fsc;
 294 }
 295 
 296 #if defined (_WIN32)
 297 # include <sys/stat.h>
 298 
 299 int sim_fseeko (FILE *st, t_offset offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 300 {
 301 fpos_t fileaddr;
 302 struct _stati64 statb;
 303 
 304 switch (whence) {
 305     case SEEK_SET:
 306         fileaddr = (fpos_t)offset;
 307         break;
 308 
 309     case SEEK_END:
 310         if (_fstati64 (_fileno (st), &statb))
 311             return (-1);
 312         fileaddr = statb.st_size + offset;
 313         break;
 314     case SEEK_CUR:
 315         if (fgetpos (st, &fileaddr))
 316             return (-1);
 317         fileaddr = fileaddr + offset;
 318         break;
 319 
 320     default:
 321         errno = EINVAL;
 322         return (-1);
 323         }
 324 
 325 return fsetpos (st, &fileaddr);
 326 }
 327 
 328 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 329 {
 330 fpos_t fileaddr;
 331 if (fgetpos (st, &fileaddr))
 332     return (-1);
 333 return (t_offset)fileaddr;
 334 }
 335 
 336 #else
 337 
 338 int sim_fseeko (FILE *st, t_offset xpos, int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
 339 {
 340 return fseeko (st, (off_t)xpos, origin);
 341 }
 342 
 343 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 344 {
 345 return (t_offset)(ftello (st));
 346 }
 347 
 348 #endif
 349 
 350 int sim_fseek (FILE *st, t_addr offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 351 {
 352 return sim_fseeko (st, (t_offset)offset, whence);
 353 }
 354 
 355 #if defined(_WIN32)
 356 # include <io.h>
 357 int sim_set_fsize (FILE *fptr, t_addr size)
     /* [previous][next][first][last][top][bottom][index][help] */
 358 {
 359 return _chsize(_fileno(fptr), (long)size);
 360 }
 361 
 362 int sim_set_fifo_nonblock (FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 363 {
 364 return -1;
 365 }
 366 
 367 #else /* !defined(_WIN32) */
 368 # include <unistd.h>
 369 int sim_set_fsize (FILE *fptr, t_addr size)
     /* [previous][next][first][last][top][bottom][index][help] */
 370 {
 371 return ftruncate(fileno(fptr), (off_t)size);
 372 }
 373 
 374 # include <sys/types.h>
 375 # include <sys/stat.h>
 376 # include <fcntl.h>
 377 
 378 int sim_set_fifo_nonblock (FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 379 {
 380 struct stat stbuf;
 381 
 382 if (!fptr || fstat (fileno(fptr), &stbuf))
 383     return -1;
 384 # if defined(S_IFIFO) && defined(O_NONBLOCK)
 385 if ((stbuf.st_mode & S_IFIFO)) {
 386     int flags = fcntl(fileno(fptr), F_GETFL, 0);
 387     return fcntl(fileno(fptr), F_SETFL, flags | O_NONBLOCK);
 388     }
 389 # endif
 390 return -1;
 391 }
 392 
 393 #endif

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