gallium/draw: initial code to properly support llvm in the draw module
[mesa.git] / src / glut / dos / PC_HW / pc_hw.c
1 /*
2 * PC/HW routine collection v1.3 for DOS/DJGPP
3 *
4 * Copyright (C) 2002 - Daniel Borca
5 * Email : dborca@yahoo.com
6 * Web : http://www.geocities.com/dborca
7 */
8
9
10 #include <dpmi.h>
11 #include <fcntl.h>
12 #include <sys/stat.h> /* for mode definitions */
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include "pc_hw.h"
18
19
20 /*
21 * atexit
22 */
23 #define MAX_ATEXIT 32
24
25 static volatile int atexitcnt;
26 static VFUNC atexittbl[MAX_ATEXIT];
27
28
29 static void __attribute__((destructor))
30 doexit (void)
31 {
32 while (atexitcnt) atexittbl[--atexitcnt]();
33 }
34
35
36 int
37 pc_clexit (VFUNC f)
38 {
39 int i;
40
41 for (i = 0; i < atexitcnt; i++) {
42 if (atexittbl[i] == f) {
43 for (atexitcnt--; i < atexitcnt; i++) atexittbl[i] = atexittbl[i+1];
44 atexittbl[i] = 0;
45 return 0;
46 }
47 }
48 return -1;
49 }
50
51
52 int
53 pc_atexit (VFUNC f)
54 {
55 pc_clexit(f);
56 if (atexitcnt < MAX_ATEXIT) {
57 atexittbl[atexitcnt++] = f;
58 return 0;
59 }
60 return -1;
61 }
62
63
64 /*
65 * locked memory allocation
66 */
67 void *
68 pc_malloc (size_t size)
69 {
70 void *p = malloc(size);
71
72 if (p) {
73 if (_go32_dpmi_lock_data(p, size)) {
74 free(p);
75 return NULL;
76 }
77 }
78
79 return p;
80 }
81
82
83 /*
84 * standard redirection
85 */
86 static char outname[L_tmpnam];
87 static int h_out, h_outbak;
88 static char errname[L_tmpnam];
89 static int h_err, h_errbak;
90
91
92 int
93 pc_open_stdout (void)
94 {
95 tmpnam(outname);
96
97 if ((h_out=open(outname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) {
98 h_outbak = dup(STDOUT_FILENO);
99 fflush(stdout);
100 dup2(h_out, STDOUT_FILENO);
101 }
102
103 return h_out;
104 }
105
106
107 void
108 pc_close_stdout (void)
109 {
110 FILE *f;
111 char *line = alloca(512);
112
113 if (h_out > 0) {
114 dup2(h_outbak, STDOUT_FILENO);
115 close(h_out);
116 close(h_outbak);
117
118 f = fopen(outname, "rt");
119 while (fgets(line, 512, f)) {
120 fputs(line, stdout);
121 }
122 fclose(f);
123
124 remove(outname);
125 }
126 }
127
128
129 int
130 pc_open_stderr (void)
131 {
132 tmpnam(errname);
133
134 if ((h_err=open(errname, O_WRONLY | O_CREAT | O_TEXT | O_TRUNC, S_IREAD | S_IWRITE)) > 0) {
135 h_errbak = dup(STDERR_FILENO);
136 fflush(stderr);
137 dup2(h_err, STDERR_FILENO);
138 }
139
140 return h_err;
141 }
142
143
144 void
145 pc_close_stderr (void)
146 {
147 FILE *f;
148 char *line = alloca(512);
149
150 if (h_err > 0) {
151 dup2(h_errbak, STDERR_FILENO);
152 close(h_err);
153 close(h_errbak);
154
155 f = fopen(errname, "rt");
156 while (fgets(line, 512, f)) {
157 fputs(line, stderr);
158 }
159 fclose(f);
160
161 remove(errname);
162 }
163 }