Debian Codegraph @

terps/tads/tads2/msdos/osdosex.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/msdos/OSDOSEX.C,v 1.2 1999/05/17 02:52:18 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1997, 2002 Michael J. Roberts. All Rights Reserved.
8 *
9 * Please see the accompanying license file, LICENSE.TXT, for information
10 * on using and copying this software.
11 */
12 /*
13 Name
14 osdosex.c - external function implementation for DOS
15 Function
16
17 Notes
18
19 Modified
20 11/02/97 MJRoberts - Creation
21 */
22
23 #include <string.h>
24 #ifndef DJGPP
25 #include <stdio.h>
26 #include <stddef.h>
27 #include <stdlib.h>
28 #include <ctype.h>
29 #include <share.h>
30 #endif /* DJGPP */
31
32 #include "os.h"
33 #include <assert.h>
34
35 #if defined(__DPMI16__) || defined(T_WIN32)
36 # include <windows.h>
37 #endif
38
39 /* forward declarations */
40 static const char *oss_get_exe(const char *argv0);
41
42
43 #ifdef DJGPP
44
45 /*
46 * os_exeseek - opens the given .EXE file and seeks to the end of the
47 * executable part of it, on the presumption that a datafile is to be
48 * found there.
49 */
50 osfildef *os_exeseek(const char *exefile, const char *typ)
51 {
52 return((osfildef *)0);
53 }
54
55 /* retrieve the full name of the program, given argv[0] */
56 static const char *oss_get_exe(const char *argv0)
57 {
58 /*
59 * djgpp reliably passes the fully-qualified path to the executable in
60 * argv[0], so we can simply return argv[0]
61 */
62 return argv0;
63 }
64
65 #else /* DJGPP */
66
67 /*
68 * os_exeseek - opens the given .EXE file and seeks to the end of the
69 * executable part of it, on the presumption that a datafile is to be
70 * found there.
71 */
72 osfildef *os_exeseek(const char *argv0, const char *typ)
73 {
74 const char *exefile;
75 osfildef *fp;
76 int tags_found;
77 unsigned ofs, segcnt;
78 unsigned long seekpt;
79 unsigned long check;
80 unsigned long startofs;
81 unsigned long end;
82 char typbuf[4];
83 static int no_data_present = FALSE;
84 static int tried_old_way = FALSE;
85
86 /*
87 * if we've already looked here and found no data, don't bother trying
88 * again
89 */
90 if (no_data_present)
91 return 0;
92
93 /* get the name of the executable file from the given argv0 */
94 exefile = oss_get_exe(argv0);
95
96 /* open the file */
97 if (!(fp = _fsopen(exefile, "rb", SH_DENYWR)))
98 return((FILE *)0);
99
100 /* seek to the end of the file */
101 fseek(fp, 0L, SEEK_END);
102
103 /* look through tagged blocks until we find the type we want */
104 for (tags_found = 0 ; ; ++tags_found)
105 {
106 /* seek back past the descriptor block */
107 fseek(fp, -12L, SEEK_CUR);
108 seekpt = ftell(fp);
109
110 /* read the elements of the descriptor block */
111 if (fread(&check, sizeof(check), 1, fp) != 1
112 || fread(typbuf, sizeof(typbuf), 1, fp) != 1
113 || fread(&startofs, sizeof(startofs), 1, fp) != 1)
114 break;
115
116 /* check the signature to make sure we're looking at a valid block */
117 if (check != ~seekpt)
118 break;
119
120 /* seek to the start of the data for this resource */
121 fseek(fp, startofs, SEEK_SET);
122
123 /* if this is the one we want, return it, otherwise keep looking */
124 if (!memcmp(typ, typbuf, sizeof(typbuf)))
125 {
126 /* check the header to make sure it matches */
127 if (fread(&check, sizeof(check), 1, fp) != 1
128 || fread(typbuf, sizeof(typbuf), 1, fp) != 1
129 || check != ~startofs
130 || memcmp(typ, typbuf, sizeof(typbuf)))
131 break;
132 return fp;
133 }
134 }
135
136 /*
137 * We didn't find it - if the caller is asking for the game file
138 * segment ("TGAM"), try the old way, in case this got built with the
139 * old maketrx program. If we've already tried this before in this
140 * session, don't bother doing so again, since nothing's going to
141 * change.
142 */
143 if (!tried_old_way)
144 {
145 /* note that we've tried this now */
146 tried_old_way = TRUE;
147
148 /* read the header */
149 fseek(fp, 0L, SEEK_SET);
150 if (fread(&ofs, sizeof(ofs), 1, fp) != 1 ||
151 fread(&ofs, sizeof(ofs), 1, fp) != 1 ||
152 fread(&segcnt, sizeof(segcnt), 1, fp) != 1)
153 goto no_data;
154
155 seekpt = ((unsigned long)segcnt - 1)*512L + (unsigned long)ofs;
156 if (fseek(fp, seekpt, SEEK_SET)) goto no_data;
157
158 /* check for the signature, to make sure it's not the symbol table */
159 if (fread(&end, sizeof(end), 1, fp) != 1
160 || fseek(fp, 0L, SEEK_END)
161 || (unsigned long)ftell(fp) != end
162 || fseek(fp, seekpt + sizeof(end), SEEK_SET))
163 goto no_data;
164
165 /* success! */
166 return(fp);
167 }
168
169 no_data:
170 /*
171 * if we didn't find any tags at all, note that there's no data
172 * present, so we don't try reading fruitlessly again
173 */
174 if (tags_found == 0)
175 no_data_present = TRUE;
176
177 /* we didn't find anything - close the file and return failure */
178 fclose(fp);
179 return((FILE *)0);
180 }
181
182 /* ------------------------------------------------------------------------ */
183 #ifdef MICROSOFT
184
185 /*
186 * retrieve the full name of the program, given argv[0]
187 */
188 static const char *oss_get_exe(const char *argv0)
189 {
190 /*
191 * The MSVC run-time library keeps the full filename of the executable
192 * in the global variable '_pgmptr'.
193 */
194 return _pgmptr;
195 }
196
197 #else
198
199 /*
200 * retrieve the full name of the program, given argv[0]
201 */
202 static const char *oss_get_exe(const char *argv0)
203 {
204 /* static path buffer, in case we need to build the path ourselves */
205 static char path[OSFNMAX];
206 char *p;
207
208 /*
209 * For non-microsoft compilers, we don't know of any RTL mechanisms for
210 * getting the full exe path. Some compilers always give us a
211 * fully-qualified path in argv[0], so check that first - if it looks
212 * like an absolute path, just return it.
213 */
214 if (os_is_file_absolute(argv0))
215 return argv0;
216
217 /*
218 * Okay, the RTL startup routine didn't give us any help - it looks
219 * like we have a relative filename, so the RTL probably just passed us
220 * the first token on the DOS/cmd.exe command line. The shell will
221 * have searched for the given filename first in the current directory,
222 * then in each directory listed in the PATH environment variable. So,
223 * first build out the full absolute path to the file as though it were
224 * in the current working directory; if the resulting file exists,
225 * return that path.
226 */
227 os_get_abs_filename(path, sizeof(path), argv0);
228 if (!osfacc(path))
229 return path;
230
231 /*
232 * The file doesn't exist in the working directory, so it must be
233 * somewhere in the PATH list. Search the path.
234 */
235 #ifdef TURBO
236 /*
237 * borland version - RTL searchpath() searches the PATH and returns a
238 * static buffer with the full path, if we found it
239 */
240 if ((p = searchpath(argv0)) != 0)
241 {
242 /*
243 * Found it. Build out the full absolute path, since the PATH
244 * variable could itself have relative path references.
245 */
246 os_get_abs_filename(path, sizeof(path), p);
247 return path;
248 }
249 #endif
250
251 /*
252 * We didn't find the file on the path. Give up and just return the
253 * original filenamem.
254 */
255 return argv0;
256 }
257
258 #endif /* MICROSOFT */
259
260 #endif /* DJGPP */
261
262 /* ------------------------------------------------------------------------ */
263 /*
264 * Get the executable filename given the main program's argv[0]. On DOS
265 * and Windows, argv[0] always contains the full path to the executable
266 * file, regardless of what the user typed on the command line - the shell
267 * always expands relative names to absolute paths, and inserts the PATH
268 * variable expansion as needed. So, we simply copy the argv[0] value
269 * into the caller's buffer.
270 */
271 int os_get_exe_filename(char *buf, size_t buflen, const char *argv0)
272 {
273 /*
274 * If the name string is too long, fail. Note that the name is too
275 * long if its strlen is so much as equal to the buffer length,
276 * because we need one extra byte for a null terminator in the copied
277 * result.
278 */
279 if (strlen(oss_get_exe(argv0)) >= buflen)
280 return FALSE;
281
282 /* copy it and return success */
283 strcpy(buf, oss_get_exe(argv0));
284 return TRUE;
285 }
286
287 /*
288 * Get a special path.
289 */
290 void os_get_special_path(char *buf, size_t buflen, const char *argv0, int id)
291 {
292 char tmp[OSFNMAX];
293
294 /* determine which path we're retrieving */
295 switch(id)
296 {
297 case OS_GSP_T3_RES:
298 case OS_GSP_T3_APP_DATA:
299 case OS_GSP_T3_SYSCONFIG:
300 case OS_GSP_LOGFILE:
301 /*
302 * For DOS/Windows, we keep these items in the root install
303 * directory. This is the same directory that contains the
304 * executable, so we simply need to get the path name of the
305 * executable.
306 */
307 os_get_path_name(buf, buflen, oss_get_exe(argv0));
308
309 /*
310 * canonicalize the path by converting it to all lower-case (since
311 * DOS/Win file systems are not sensitive to case, this won't
312 * change the meaning of the filename, but it will ensure that it's
313 * stable for comparisons to the same path obtained by other means)
314 */
315 strlwr(buf);
316 return;
317
318 case OS_GSP_T3_LIB:
319 /*
320 * For DOS/Windows, we keep TADS 3 library source files in the
321 * "lib" subdirectory of the install directory.
322 */
323 os_get_path_name(tmp, sizeof(tmp), oss_get_exe(argv0));
324 os_build_full_path(buf, buflen, tmp, "lib");
325
326 /* return in all lower-case for consistency */
327 strlwr(buf);
328 return;
329
330 case OS_GSP_T3_INC:
331 /*
332 * For DOS/Windows, we keep TADS 3 library header files in the
333 * "include" subdirectory of the install directory.
334 */
335 os_get_path_name(tmp, sizeof(tmp), oss_get_exe(argv0));
336 os_build_full_path(buf, buflen, tmp, "include");
337
338 /* return in all lower-case for consistency */
339 strlwr(buf);
340 return;
341
342 case OS_GSP_T3_USER_LIBS:
343 /*
344 * For DOS/Windows, the user library path is taken from the
345 * environment variable TADSLIB. If this environment variable
346 * isn't set, there's no default value.
347 */
348 {
349 char *p;
350
351 /* try getting the environment variable */
352 p = getenv("TADSLIB");
353
354 /* if it exists, set it; otherwise, return an empty string */
355 if (p != 0)
356 {
357 size_t copy_len;
358
359 /* copy as much as we can fit in the buffer */
360 copy_len = strlen(p);
361 if (copy_len > buflen - 1)
362 copy_len = buflen - 1;
363
364 /*
365 * copy the (possibly truncated) string, and null terminate
366 * the result
367 */
368 memcpy(buf, p, copy_len);
369 buf[copy_len] = '\0';
370 }
371 else
372 {
373 /* the variable isn't set - return empty */
374 buf[0] = '\0';
375 }
376 }
377 return;
378
379 default:
380 /*
381 * If we're called with another identifier, it must mean that
382 * we're out of date. Fail with an assertion.
383 */
384 assert(FALSE);
385 break;
386 }
387 }