summaryrefslogtreecommitdiff
path: root/src/glut/dos/PC_HW/pc_hw.c
blob: 100b372165192919ab75bd4ba9f3b427dc489d29 (plain)
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * PC/HW routine collection v1.3 for DOS/DJGPP
 *
 *  Copyright (C) 2002 - Daniel Borca
 *  Email : dborca@yahoo.com
 *  Web   : http://www.geocities.com/dborca
 */


#include <dpmi.h>
#include <fcntl.h>
#include <sys/stat.h> /* for mode definitions */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "pc_hw.h"


/*
 * atexit
 */
#define MAX_ATEXIT 32

static volatile int atexitcnt;
static VFUNC atexittbl[MAX_ATEXIT];


static void __attribute__((destructor))
doexit (void)
{
   while (atexitcnt) atexittbl[--atexitcnt]();
}


int
pc_clexit (VFUNC f)
{
   int i;

   for (i = 0; i < atexitcnt; i++) {
      if (atexittbl[i] == f) {
         for (atexitcnt--; i < atexitcnt; i++) atexittbl[i] = atexittbl[i+1];
         atexittbl[i] = 0;
         return 0;
      }
   }
   return -1;
}


int
pc_atexit (VFUNC f)
{
   pc_clexit(f);
   if (atexitcnt < MAX_ATEXIT) {
      atexittbl[atexitcnt++] = f;
      return 0;
   }
   return -1;
}


/*
 * locked memory allocation
 */
void *
pc_malloc (size_t size)
{
   void *p = malloc(size);

   if (p) {
      if (_go32_dpmi_lock_data(p, size)) {
         free(p);
         return NULL;
      }
   }

   return p;
}


/*
 * standard redirection
 */
static char outname[L_tmpnam];
static int h_out, h_outbak;
static char errname[L_tmpnam];
static int h_err, h_errbak;


int
pc_open_stdout (void)
{
   tmpnam(outname);

   if ((h_out=open(outname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) {
      h_outbak = dup(STDOUT_FILENO);
      fflush(stdout);
      dup2(h_out, STDOUT_FILENO);
   }

   return h_out;
}


void
pc_close_stdout (void)
{
   FILE *f;
   char *line = alloca(512);

   if (h_out > 0) {
      dup2(h_outbak, STDOUT_FILENO);
      close(h_out);
      close(h_outbak);

      f = fopen(outname, "rt");
      while (fgets(line, 512, f)) {
         fputs(line, stdout);
      }
      fclose(f);

      remove(outname);
   }
}


int
pc_open_stderr (void)
{
   tmpnam(errname);

   if ((h_err=open(errname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) {
      h_errbak = dup(STDERR_FILENO);
      fflush(stderr);
      dup2(h_err, STDERR_FILENO);
   }

   return h_err;
}


void
pc_close_stderr (void)
{
   FILE *f;
   char *line = alloca(512);

   if (h_err > 0) {
      dup2(h_errbak, STDERR_FILENO);
      close(h_err);
      close(h_errbak);

      f = fopen(errname, "rt");
      while (fgets(line, 512, f)) {
         fputs(line, stderr);
      }
      fclose(f);

      remove(errname);
   }
}