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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/* Functions that emulate UNIX catgets */
/* Kitten version 2021 by Tom Ehlert, */
/*
This software is free software; free to use,
modify, pass to others, whatever
use it at your own risk
*/
/* Minor modifications to use internal static buffer to keep API compatible
* with regular kitten. */
#ifndef NO_KITTEN
#include <stdio.h> /* sprintf */
#include <stdlib.h> /* getenv */
#include <io.h> /* sprintf */
#include <fcntl.h> /* sprintf */
#include <string.h> /* sprintf */
#include "kitten.h"
#include "kittenc.h"
/* Globals */
static char global_nls_buffer[4096] = {0};
/* Functions */
/**
* On success, catgets() returns a pointer to an internal
* buffer area containing the null-terminated message string.
* On failure, catgets() returns the value 'message'.
*/
const char *
_kittengets (int msgID, const char *message)
{
struct message_header *message_header; // if this is != 0 then init was done
if (global_nls_buffer[0] == 0)
return message;
message_header = (struct message_header *)global_nls_buffer;
for ( ; message_header->len != 0; message_header = (struct message_header *)((char*)message_header + message_header->len))
{
if (message_header->id== msgID)
{
return (const char*)message_header + sizeof(struct message_header);
}
}
return message;
}
/**
* Initialize kitten for program (name).
*/
#define _toupper(c) (c & ~0x20)
nl_catd
kittenopen (const char *name)
{
int fd;
struct message_end message_end;
struct content content[KITTEN_MAX_RESOURCES];
char *language;
int i;
long len;
char exename[_MAX_PATH];
long fileendnow, seekoffset;
//printf("kittenopen %s\n", name);
if ((fd = open(name, _O_RDONLY | _O_BINARY)) < 0)
{
sprintf(exename, "%s.exe", name);
if ((fd = open(exename, _O_RDONLY | _O_BINARY)) < 0)
{
printf("can't open %s\n", name); // should never happen
return 0;
}
}
fileendnow = lseek(fd, 0, SEEK_END);
lseek(fd, - (int)sizeof(struct message_end), SEEK_END);
read(fd, &message_end, sizeof(struct message_end));
if (memcmp(message_end.ID, "KITTENC", 8) != 0)
{
//printf("no KITTENC record found\n");
return 0;
}
if (message_end.resource_count > KITTEN_MAX_RESOURCES)
{
printf("resource > %d\n", KITTEN_MAX_RESOURCES);
return 0;
}
seekoffset = fileendnow - message_end.fileend_orig;
if (lseek(fd, message_end.filepos+seekoffset, SEEK_SET) != message_end.filepos+seekoffset)
{
printf("can't seek 1\n");
return 0;
}
read(fd, &content, sizeof(content[0]) * message_end.resource_count);
language = getenv("LANG");
// printf("using language %s\n", language);
if (message_end.resource_count == 1)
{
if (language &&
_toupper(language[0]) == 'E' &&
_toupper(language[0]) == 'N')
{
// set LANG=EN switches to internal strings
close(fd);
return 0;
}
// otherwise use the only compiled language
i = 0;
goto found_my_language;
}
if (language == NULL) // language not found
{ // use internal strings
close(fd);
return 0;
}
// see if we find our language in the existing resources
for (i = 0; i < message_end.resource_count; i++)
{
if(_toupper(content[i].language[0]) == _toupper(language[0]) &&
_toupper(content[i].language[1]) == _toupper(language[1]))
{
goto found_my_language;
}
}
// language not found
close(fd);
return 0;
found_my_language:
// found our language
// printf(" language %s found between %lx and %lx\n", language, content[i].filepos_start, content[i].filepos_end);
// printf("seek %lu --> %lu\n", content[i].filepos_start, content[i].filepos_start+seekoffset);
lseek(fd, content[i].filepos_start+seekoffset, SEEK_SET);
len = content[i].filepos_end - content[i].filepos_start;
if (len > sizeof(global_nls_buffer))
{
printf("nls_buffer too small(%d). we need at least %l\n",
sizeof(global_nls_buffer), len);
close(fd);
return 1;
}
read(fd, global_nls_buffer, (int)len);
close(fd);
return 0;
}
#endif /*NO_KITTEN */
|