// license:BSD-3-Clause // copyright-holders:Olivier Galibert, R. Belmont //============================================================ // // sdlptty_unix.c - SDL pseudo tty access functions // // SDLMAME by Olivier Galibert and R. Belmont // //============================================================ #if (!defined(SDLMAME_SOLARIS) && !(defined(SDLMAME_OS2))) #include #include #include #include #include #if defined(SDLMAME_FREEBSD) || defined(SDLMAME_DRAGONFLY) # include # include #elif defined(SDLMAME_NETBSD) || defined(SDLMAME_MACOSX) # include # include #elif defined(SDLMAME_OPENBSD) # include # include #elif defined(SDLMAME_LINUX) || defined(SDLMAME_EMSCRIPTEN) # include #elif defined(SDLMAME_HAIKU) # include #endif #include #include "sdlfile.h" #if defined(SDLMAME_MACOSX) const char *sdlfile_ptty_identifier = "/dev/pty"; #else const char *sdlfile_ptty_identifier = "/dev/pts"; #endif file_error sdl_open_ptty(const char *path, UINT32 openflags, osd_file **file, UINT64 *filesize) { int master; int aslave; struct termios tios; int oldflags; memset(&tios , 0 , sizeof(tios)); cfmakeraw(&tios); if (openpty(&master, &aslave, NULL, &tios, NULL) >= 0) { oldflags = fcntl(master, F_GETFL, 0); if (oldflags == -1) { close(master); return FILERR_FAILURE; } fcntl(master, F_SETFL, oldflags | O_NONBLOCK); close(aslave); (*file)->handle = master; *filesize = 0; } else { return FILERR_ACCESS_DENIED; } return FILERR_NONE; } file_error sdl_read_ptty(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { ssize_t result; result = read(file->handle, buffer, count); if (result < 0) { return error_to_file_error(errno); } if (actual != NULL ) { *actual = result; } return FILERR_NONE; } file_error sdl_write_ptty(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { ssize_t result; result = write(file->handle, buffer, count); if (result < 0) { return error_to_file_error(errno); } if (actual != NULL ) { *actual = result; } return FILERR_NONE; } file_error sdl_close_ptty(osd_file *file) { close(file->handle); osd_free(file); return FILERR_NONE; } file_error sdl_slave_name_ptty(osd_file *file , char *name , size_t name_len) { const char *slave_name = ptsname(file->handle); if (slave_name == NULL || strlen(slave_name) >= name_len) { return FILERR_INVALID_ACCESS; } strcpy(name , slave_name); return FILERR_NONE; } #else #include "sdlfile.h" const char *sdlfile_ptty_identifier = ""; file_error sdl_open_ptty(const char *path, UINT32 openflags, osd_file **file, UINT64 *filesize) { return FILERR_ACCESS_DENIED; } file_error sdl_read_ptty(osd_file *file, void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { return FILERR_ACCESS_DENIED; } file_error sdl_write_ptty(osd_file *file, const void *buffer, UINT64 offset, UINT32 count, UINT32 *actual) { return FILERR_ACCESS_DENIED; } file_error sdl_close_ptty(osd_file *file) { return FILERR_ACCESS_DENIED; } file_error sdl_slave_name_ptty(osd_file *file) { return FILERR_ACCESS_DENIED; } #endif st/SDL_test_random.c?id=4080e50b9e51bddbeb234a70dca19f6b64cc4ec7'>treecommitdiffstatshomepage
blob: c5baaa640f3a9160a91832594ac4819cdc36f78e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
  Simple DirectMedia Layer
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>

  This software is provided 'as-is', without any express or implied
  warranty.  In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute it
  freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not
     claim that you wrote the original software. If you use this software
     in a product, an acknowledgment in the product documentation would be
     appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be
     misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.
*/

/*

 A portable "32-bit Multiply with carry" random number generator.

 Used by the fuzzer component.
 Original source code contributed by A. Schiffler for GSOC project.

*/

#include "SDL_config.h"

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

#include "SDL_test.h"

/* Initialize random number generator with two integer variables */

void SDLTest_RandomInit(SDLTest_RandomContext * rndContext, unsigned int xi, unsigned int ci)
{
  if (rndContext==NULL) return;

  /*
   * Choose a value for 'a' from this list
   * 1791398085 1929682203 1683268614 1965537969 1675393560
   * 1967773755 1517746329 1447497129 1655692410 1606218150
   * 2051013963 1075433238 1557985959 1781943330 1893513180
   * 1631296680 2131995753 2083801278 1873196400 1554115554
   */
  rndContext->a = 1655692410;
  rndContext->x = 30903;
  rndContext->c = 0;
  if (xi != 0) {
      rndContext->x = xi;
  }
  rndContext->c = ci;
  rndContext->ah = rndContext->a >> 16;
  rndContext->al = rndContext->a & 65535;
}

/* Initialize random number generator from system time */

void SDLTest_RandomInitTime(SDLTest_RandomContext * rndContext)
{
  int a, b;

  if (rndContext==NULL) return;

  srand((unsigned int)time(NULL));
  a=rand();
  srand(clock());
  b=rand();
  SDLTest_RandomInit(rndContext, a, b);
}

/* Returns random numbers */

unsigned int SDLTest_Random(SDLTest_RandomContext * rndContext)
{
  unsigned int xh, xl;

  if (rndContext==NULL) return -1;

  xh = rndContext->x >> 16, xl = rndContext->x & 65535;
  rndContext->x = rndContext->x * rndContext->a + rndContext->c;
  rndContext->c =
    xh * rndContext->ah + ((xh * rndContext->al) >> 16) +
    ((xl * rndContext->ah) >> 16);
  if (xl * rndContext->al >= (~rndContext->c + 1))
    rndContext->c++;
  return (rndContext->x);
}