Subversion Repositories shark

Rev

Rev 56 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 pj 1
/*
2
 * PC/HW routine collection v1.2 for DOS/DJGPP
3
 *
4
 *  Copyright (C) 2002 - Borca Daniel
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
 * atexit
21
 */
22
#define MAX_ATEXIT 32
23
 
24
static volatile int atexitcnt;
25
static VFUNC atexittbl[MAX_ATEXIT];
26
 
27
static void __attribute__((destructor)) doexit (void)
28
{
29
 while (atexitcnt) atexittbl[--atexitcnt]();
30
}
31
 
32
int pc_clexit (VFUNC f)
33
{
34
 int i;
35
 
36
 for (i=0;i<atexitcnt;i++) {
37
     if (atexittbl[i]==f) {
38
        for (atexitcnt--;i<atexitcnt;i++) atexittbl[i] = atexittbl[i+1];
39
        atexittbl[i] = 0;
40
        return 0;
41
     }
42
 }
43
 return -1;
44
}
45
 
46
int pc_atexit (VFUNC f)
47
{
48
 pc_clexit(f);
49
 if (atexitcnt<MAX_ATEXIT) {
50
    atexittbl[atexitcnt++] = f;
51
    return 0;
52
 }
53
 return -1;
54
}
55
 
56
/*
57
 * locked memory allocation
58
 */
59
void *pc_malloc (size_t size)
60
{
61
 void *p = malloc(size);
62
 
63
 if (p) {
64
    if (_go32_dpmi_lock_data(p, size)) {
65
       free(p);
66
       return NULL;
67
    }
68
 }
69
 
70
 return p;
71
}
72
 
73
/*
74
 * standard redirection
75
 */
76
static int h_out, h_outbak, h_err, h_errbak;
77
 
78
int pc_open_stdout (void)
79
{
80
 if ((h_out=open(tmpnam(NULL), O_WRONLY | O_CREAT | O_TRUNC | O_TEXT | O_TEMPORARY, S_IRUSR | S_IWUSR)) >= 0) {
81
    if ((h_outbak=dup(1)) != -1) {
82
       fflush(stdout);
83
       if (dup2(h_out, 1) != -1) {
84
          return 0;
85
       }
86
       close(h_outbak);
87
    }
88
    close(h_out);
89
 }
90
 return (h_out = -1);
91
}
92
 
93
void pc_close_stdout (void)
94
{
95
 FILE *f;
96
 char *line = alloca(512);
97
 
98
 if (h_out >= 0) {
99
    dup2(h_outbak, 1);
100
    close(h_outbak);
101
 
102
    if ((f=fdopen(h_out, "r")) != NULL) {
103
       fseek(f, 0, SEEK_SET);
104
       while (fgets(line, 512, f)) {
105
             fputs(line, stdout);
106
       }
107
       fclose(f);
108
    } else {
109
       close(h_out);
110
    }
111
 }
112
}
113
 
114
int pc_open_stderr (void)
115
{
116
 if ((h_err=open(tmpnam(NULL), O_WRONLY | O_CREAT | O_TRUNC | O_TEXT | O_TEMPORARY, S_IRUSR | S_IWUSR)) >= 0) {
117
    if ((h_errbak=dup(2)) != -1) {
118
       fflush(stderr);
119
       if (dup2(h_err, 2) != -1) {
120
          return 0;
121
       }
122
       close(h_errbak);
123
    }
124
    close(h_err);
125
 }
126
 return (h_err = -1);
127
}
128
 
129
void pc_close_stderr (void)
130
{
131
 FILE *f;
132
 char *line = alloca(512);
133
 
134
 if (h_err >= 0) {
135
    dup2(h_errbak, 2);
136
    close(h_errbak);
137
 
138
    if ((f=fdopen(h_err, "r")) != NULL) {
139
       fseek(f, 0, SEEK_SET);
140
       while (fgets(line, 512, f)) {
141
             fputs(line, stderr);
142
       }
143
       fclose(f);
144
    } else {
145
       close(h_err);
146
    }
147
 }
148
}