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_fseeko
  16. sim_ftell
  17. sim_fseeko
  18. sim_ftell
  19. sim_fseeko
  20. sim_ftell
  21. sim_fseek
  22. sim_set_fsize
  23. sim_set_fifo_nonblock
  24. sim_set_fsize
  25. 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 t_bool sim_taddr_64;                /* t_addr is > 32b and Large File Support available */
  81 t_bool sim_toffset_64;              /* Large File (>2GB) file I/O Support available */
  82 
  83 /*
  84  * OS-independent, endian independent binary I/O package
  85  *
  86  * For consistency, all binary data read and written by the simulator
  87  * is stored in little endian data order.  That is, in a multi-byte
  88  * data item, the bytes are written out right to left, low order byte
  89  * to high order byte.  On a big endian host, data is read and written
  90  * from high byte to low byte.  Consequently, data written on a little
  91  * endian system must be byte reversed to be usable on a big endian
  92  * system, and vice versa.
  93  *
  94  * These routines are analogs of the standard C runtime routines
  95  * fread and fwrite.  If the host is little endian, or the data items
  96  * are size char, then the calls are passed directly to fread or
  97  * fwrite.  Otherwise, these routines perform the necessary byte swaps.
  98  * Sim_fread swaps in place, sim_fwrite uses an intermediate buffer.
  99  */
 100 
 101 int32 sim_finit (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 102 {
 103 sim_end = DECLITEND;
 104 sim_toffset_64 = (sizeof(t_offset) > sizeof(int32));    /* Large File (>2GB) support */
 105 /*CONSTCOND*/
 106 sim_taddr_64 = sim_toffset_64 && (sizeof(t_addr) > sizeof(int32));
 107 return sim_end;
 108 }
 109 
 110 void sim_buf_swap_data (void *bptr, size_t size, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 111 {
 112 size_t j;
 113 size_t k;
 114 unsigned char by, *sptr, *dptr;
 115 
 116 if (sim_end || (count == 0) || (size == sizeof (char)))
 117     return;
 118 for (j = 0, dptr = sptr = (unsigned char *) bptr;       /* loop on items */
 119      j < count; j++) {
 120     for (k = (int32)(size - 1); k >= (((int32) size + 1) / 2); k--) {
 121         by = *sptr;                                     /* swap end-for-end */
 122         *sptr++ = *(dptr + k);
 123         *(dptr + k) = by;
 124         }
 125     sptr = dptr = dptr + size;                          /* next item */
 126     }
 127 }
 128 
 129 size_t sim_fread (void *bptr, size_t size, size_t count, FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 130 {
 131 size_t c;
 132 
 133 if ((size == 0) || (count == 0))                        /* check arguments */
 134     return 0;
 135 c = fread (bptr, size, count, fptr);                    /* read buffer */
 136 if (sim_end || (size == sizeof (char)) || (c == 0))     /* le, byte, or err? */
 137     return c;                                           /* done */
 138 sim_buf_swap_data (bptr, size, count);
 139 return c;
 140 }
 141 
 142 void sim_buf_copy_swapped (void *dbuf, const void *sbuf, size_t size, size_t count)
     /* [previous][next][first][last][top][bottom][index][help] */
 143 {
 144 size_t j;
 145 size_t k;
 146 const unsigned char *sptr = (const unsigned char *)sbuf;
 147 unsigned char *dptr = (unsigned char *)dbuf;
 148 
 149 if (sim_end || (size == sizeof (char))) {
 150     memcpy (dptr, sptr, size * count);
 151     return;
 152     }
 153 for (j = 0; j < count; j++) {                           /* loop on items */
 154     /* Unsigned countdown loop. Pre-decrement k before it's used inside the
 155        loop so that k == 0 in the loop body to process the last item, then
 156        terminate. Initialize k to size for the same reason: the pre-decrement
 157        gives us size - 1 in the loop body. */
 158     for (k = size; k > 0; /* empty */)
 159         *(dptr + --k) = *sptr++;
 160     dptr = dptr + size;
 161     }
 162 }
 163 
 164 size_t sim_fwrite (const void *bptr, size_t size, size_t count, FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 165 {
 166 size_t c, nelem, nbuf, lcnt, total;
 167 int32 i;
 168 const unsigned char *sptr;
 169 unsigned char *sim_flip;
 170 
 171 if ((size == 0) || (count == 0))                        /* check arguments */
 172     return 0;
 173 if (sim_end || (size == sizeof (char)))                 /* le or byte? */
 174     return fwrite (bptr, size, count, fptr);            /* done */
 175 sim_flip = (unsigned char *)malloc(FLIP_SIZE);
 176 if (!sim_flip)
 177     return 0;
 178 nelem = FLIP_SIZE / size;                               /* elements in buffer */
 179 nbuf = count / nelem;                                   /* number buffers */
 180 lcnt = count % nelem;                                   /* count in last buf */
 181 if (lcnt) nbuf = nbuf + 1;
 182 else lcnt = nelem;
 183 total = 0;
 184 sptr = (const unsigned char *) bptr;                    /* init input ptr */
 185 for (i = (int32)nbuf; i > 0; i--) {                     /* loop on buffers */
 186     c = (i == 1)? lcnt: nelem;
 187     sim_buf_copy_swapped (sim_flip, sptr, size, c);
 188     sptr = sptr + size * count;
 189     c = fwrite (sim_flip, size, c, fptr);
 190     if (c == 0) {
 191         FREE(sim_flip);
 192         return total;
 193         }
 194     total = total + c;
 195     }
 196 FREE(sim_flip);
 197 return total;
 198 }
 199 
 200 /* Forward Declaration */
 201 
 202 t_offset sim_ftell (FILE *st);
 203 
 204 /* Get file size */
 205 
 206 t_offset sim_fsize_ex (FILE *fp)
     /* [previous][next][first][last][top][bottom][index][help] */
 207 {
 208 t_offset pos, sz;
 209 
 210 if (fp == NULL)
 211     return 0;
 212 pos = sim_ftell (fp);
 213 if (sim_fseek (fp, 0, SEEK_END))
 214   return 0;
 215 sz = sim_ftell (fp);
 216 if (sim_fseeko (fp, pos, SEEK_SET))
 217   return 0;
 218 return sz;
 219 }
 220 
 221 t_offset sim_fsize_name_ex (const char *fname)
     /* [previous][next][first][last][top][bottom][index][help] */
 222 {
 223 FILE *fp;
 224 t_offset sz;
 225 
 226 if ((fp = sim_fopen (fname, "rb")) == NULL)
 227     return 0;
 228 sz = sim_fsize_ex (fp);
 229 fclose (fp);
 230 return sz;
 231 }
 232 
 233 uint32 sim_fsize_name (const char *fname)
     /* [previous][next][first][last][top][bottom][index][help] */
 234 {
 235 return (uint32)(sim_fsize_name_ex (fname));
 236 }
 237 
 238 uint32 sim_fsize (FILE *fp)
     /* [previous][next][first][last][top][bottom][index][help] */
 239 {
 240 return (uint32)(sim_fsize_ex (fp));
 241 }
 242 
 243 /* OS-dependent routines */
 244 
 245 /* Optimized file open */
 246 
 247 FILE *sim_fopen (const char *file, const char *mode)
     /* [previous][next][first][last][top][bottom][index][help] */
 248 {
 249 FILE *fsc = NULL;
 250 #if defined(USE_FCNTL) || defined(USE_FLOCK)
 251 # include <fcntl.h>
 252 # include <sys/stat.h>
 253 # include <sys/types.h>
 254 int writable = 0;
 255 int rc = 0;
 256 if (strstr(mode, "+") != NULL)
 257   writable = 1;
 258 if (strstr(mode, "w") != NULL)
 259   writable = 1;
 260 if (strstr(mode, "a") != NULL)
 261   writable = 1;
 262 #endif /* if defined(USE_FCNTL) || defined(USE_FLOCK) */
 263 #if (defined (__linux) || defined (__linux__) || defined (_AIX)) && !defined (DONT_DO_LARGEFILE)
 264 fsc = fopen64 (file, mode);
 265 #else
 266 fsc = fopen (file, mode);
 267 #endif
 268 #if defined(USE_FCNTL)
 269 struct flock lock;
 270 (void)memset (&lock, 0, sizeof(lock));
 271 lock.l_type = F_WRLCK;
 272 if (writable && !sim_nolock) {
 273   if (fsc != NULL)
 274     rc = fcntl (fileno(fsc), F_SETLK, &lock);
 275   if (rc < 0) {
 276     if (!sim_quiet) {
 277       sim_printf ("%s(%s): %s [Error %d]",
 278                   __func__, mode, xstrerror_l(errno), errno);
 279       if (fcntl(fileno(fsc), F_GETLK, &lock) == 0 && lock.l_pid > 0)
 280         sim_printf (" (locked by PID %lu)",
 281                    (unsigned long)lock.l_pid);
 282       sim_printf ("\r\n");
 283     }
 284     if (!sim_iglock) return NULL;
 285   }
 286 }
 287 #elif defined(USE_FLOCK) /* if defined(USE_FCNTL) */
 288 if (writable && !sim_nolock) {
 289   if (fsc != NULL)
 290     rc = flock (fileno(fsc), LOCK_EX | LOCK_NB);
 291   if (rc < 0) {
 292     if (!sim_quiet)
 293       sim_printf ("%s(%s): %s [Error %d] (locked?)\r\n",
 294                   __func__, mode, xstrerror_l(errno), errno);
 295     if (!sim_iglock) return NULL;
 296   }
 297 }
 298 #endif /* elif defined(USE_FLOCK) */
 299 return fsc;
 300 }
 301 
 302 #if !defined (DONT_DO_LARGEFILE)
 303 
 304 # if ((defined(__sun) || defined(__sun__)) && defined(_LARGEFILE_SOURCE))
 305 #  define S_SIM_IO_FSEEK_EXT_ 1
 306 int sim_fseeko (FILE *st, t_offset offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 307 {
 308 return fseeko (st, (off_t)offset, whence);
 309 }
 310 
 311 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 312 {
 313 return (t_offset)(ftello (st));
 314 }
 315 
 316 # endif
 317 
 318 /* Windows */
 319 
 320 # if defined (_WIN32)
 321 #  define S_SIM_IO_FSEEK_EXT_ 1
 322 #  include <sys/stat.h>
 323 
 324 int sim_fseeko (FILE *st, t_offset offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 325 {
 326 fpos_t fileaddr;
 327 struct _stati64 statb;
 328 
 329 switch (whence) {
 330     case SEEK_SET:
 331         fileaddr = (fpos_t)offset;
 332         break;
 333 
 334     case SEEK_END:
 335         if (_fstati64 (_fileno (st), &statb))
 336             return (-1);
 337         fileaddr = statb.st_size + offset;
 338         break;
 339     case SEEK_CUR:
 340         if (fgetpos (st, &fileaddr))
 341             return (-1);
 342         fileaddr = fileaddr + offset;
 343         break;
 344 
 345     default:
 346         errno = EINVAL;
 347         return (-1);
 348         }
 349 
 350 return fsetpos (st, &fileaddr);
 351 }
 352 
 353 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 354 {
 355 fpos_t fileaddr;
 356 if (fgetpos (st, &fileaddr))
 357     return (-1);
 358 return (t_offset)fileaddr;
 359 }
 360 
 361 # endif                                                  /* end Windows */
 362 
 363 /* Linux */
 364 
 365 # if defined (__linux) || defined (__linux__) || defined (_AIX)
 366 #  define S_SIM_IO_FSEEK_EXT_ 1
 367 int sim_fseeko (FILE *st, t_offset xpos, int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
 368 {
 369 return fseeko64 (st, (off64_t)xpos, origin);
 370 }
 371 
 372 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 373 {
 374 return (t_offset)(ftello64 (st));
 375 }
 376 
 377 # endif                                                  /* end Linux with LFS */
 378 
 379 /* Apple */
 380 
 381 # if defined (__APPLE__)          || \
 382     defined (__FreeBSD__)         || \
 383     defined (__NetBSD__)          || \
 384     defined (__OpenBSD__)         || \
 385     defined (__CYGWIN__)
 386 #  define S_SIM_IO_FSEEK_EXT_ 1
 387 int sim_fseeko (FILE *st, t_offset xpos, int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
 388 {
 389 return fseeko (st, (off_t)xpos, origin);
 390 }
 391 
 392 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 393 {
 394 return (t_offset)(ftello (st));
 395 }
 396 
 397 # endif  /* end Apple */
 398 #endif /* !DONT_DO_LARGEFILE */
 399 
 400 /* Default: no OS-specific routine has been defined */
 401 
 402 #if !defined (S_SIM_IO_FSEEK_EXT_)
 403 int sim_fseeko (FILE *st, t_offset xpos, int origin)
     /* [previous][next][first][last][top][bottom][index][help] */
 404 {
 405 return fseek (st, (long) xpos, origin);
 406 }
 407 
 408 t_offset sim_ftell (FILE *st)
     /* [previous][next][first][last][top][bottom][index][help] */
 409 {
 410 return (t_offset)(ftell (st));
 411 }
 412 #endif
 413 
 414 int sim_fseek (FILE *st, t_addr offset, int whence)
     /* [previous][next][first][last][top][bottom][index][help] */
 415 {
 416 return sim_fseeko (st, (t_offset)offset, whence);
 417 }
 418 
 419 #if defined(_WIN32)
 420 # include <io.h>
 421 int sim_set_fsize (FILE *fptr, t_addr size)
     /* [previous][next][first][last][top][bottom][index][help] */
 422 {
 423 return _chsize(_fileno(fptr), (long)size);
 424 }
 425 
 426 int sim_set_fifo_nonblock (FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 427 {
 428 return -1;
 429 }
 430 
 431 #else /* !defined(_WIN32) */
 432 # include <unistd.h>
 433 int sim_set_fsize (FILE *fptr, t_addr size)
     /* [previous][next][first][last][top][bottom][index][help] */
 434 {
 435 return ftruncate(fileno(fptr), (off_t)size);
 436 }
 437 
 438 # include <sys/types.h>
 439 # include <sys/stat.h>
 440 # include <fcntl.h>
 441 
 442 int sim_set_fifo_nonblock (FILE *fptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 443 {
 444 struct stat stbuf;
 445 
 446 if (!fptr || fstat (fileno(fptr), &stbuf))
 447     return -1;
 448 # if defined(S_IFIFO) && defined(O_NONBLOCK)
 449 if ((stbuf.st_mode & S_IFIFO)) {
 450     int flags = fcntl(fileno(fptr), F_GETFL, 0);
 451     return fcntl(fileno(fptr), F_SETFL, flags | O_NONBLOCK);
 452     }
 453 # endif
 454 return -1;
 455 }
 456 
 457 #endif

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