-
Notifications
You must be signed in to change notification settings - Fork 34
/
utility.c
91 lines (82 loc) · 2.08 KB
/
utility.c
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
#include <dpmi/dpmi.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
// http://www.techhelpmanual.com/346-dos_environment.html
int setenv(const char *name, const char *value, int rewrite)
{
int namelen;
int vallen;
if(name == NULL || value == NULL || (namelen=strlen(name)) == 0 || (vallen=strlen(value)) == 0)
return -1;
//find PSP of COMMAND.COM
uint32_t psp = 0;
uint32_t parent = 0;
{
DPMI_REG r = {0};
r.h.ah = 0x62;
DPMI_CallRealModeINT(0x21, &r);
parent = psp = r.w.bx;
}
do
{
psp = parent;
//printf("PSP:%x\n",psp);
parent = DPMI_LoadW((psp<<4)+0x16);
}while(parent != psp);
uint16_t env = DPMI_LoadW((parent<<4)+0x2C);
uint16_t mcb = env - 1;
uint16_t size = DPMI_LoadW((mcb<<4) + 0x03) << 4;
//printf("size:%d\n",size);
char* buf = (char*)malloc(size+namelen+1+vallen+1);
memset(buf, 0, size+namelen+1+vallen+1);
DPMI_CopyLinear(DPMI_PTR2L(buf), env<<4, size);
char* s;
s = buf;
do
{
#if DEBUG && 0
s += printf("%s\n", s);
#else
s += strlen(s) + 1;
#endif
}while(*s);
if((s-buf+1) + (namelen+1)+1+(vallen+1) > size) //not enough space. TODO: allocate new
{
free(buf);
return -1;
}
s = buf;
do
{
int len = strlen(s);
if(memicmp(s, name, namelen) == 0 && s[namelen] == '=')
{
if(rewrite)
{
memmove(s, s+len+1, size-(s-buf+len+1));
if(!*s) break; //bugfix (exposed in FreeDOS)
len = strlen(s);
}
else
{
free(buf);
return -1;
}
}
s += len+1;
}while(*s);
*(s + sprintf(s, "%s=%s", name, value)+1)='\0';
//size = namelen + vallen + 3;
#if DEBUG && 0
s = buf;
do
{
s += printf("%s\n", s);
}while(*s);
#endif
//DPMI_StoreW((mcb<<4), (size+15)>>4);
DPMI_CopyLinear(env<<4, DPMI_PTR2L(buf), size);
free(buf);
return 0;
}