1 /****************************************************************************
3 * SciTech OS Portability Manager Library
5 * ========================================================================
7 * The contents of this file are subject to the SciTech MGL Public
8 * License Version 1.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.scitechsoft.com/mgl-license.txt
12 * Software distributed under the License is distributed on an
13 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
22 * ========================================================================
27 * Description: Module containing code common to all platforms.
29 ****************************************************************************/
32 #include "drvlib/os/os.h"
33 #if defined(__WIN32_VXD__) || defined(__OS2_VDD__) || defined(__NT_DRIVER__)
34 #include "sdd/sddhelp.h"
41 /*---------------------------- Global variables ---------------------------*/
44 long _VARAPI ___drv_os_type = _OS_UNSUPPORTED;
45 static char localBPDPath[PM_MAX_PATH] = "";
47 /*----------------------------- Implementation ----------------------------*/
49 /****************************************************************************
51 path - Local path to the Nucleus BPD driver files.
54 This function is used by the application program to override the location
55 of the Nucleus driver files that are loaded. Normally the loader code
56 will look in the system Nucleus directories first, then in the 'drivers'
57 directory relative to the current working directory, and finally relative
58 to the MGL_ROOT environment variable. By default the local BPD path is
59 always set to the current directory if not initialised.
60 ****************************************************************************/
61 void PMAPI PM_setLocalBPDPath(
65 strncpy(localBPDPath,path,sizeof(localBPDPath));
66 localBPDPath[sizeof(localBPDPath)-1] = 0;
69 /****************************************************************************
71 bpdpath - Place to store the actual path to the file
72 cachedpath - Place to store the cached BPD driver path
73 trypath - Path to try to find the BPD file in
74 subpath - Optional sub path to append to trypath
75 dllname - Name of the Binary Portable DLL to load
78 True if found, false if not.
81 Trys the specified path to see if the BPD file can be found or not. If so,
82 the path used is returned in bpdpath and cachedpath.
83 ****************************************************************************/
94 strcpy(bpdpath, trypath);
95 PM_backslash(bpdpath);
96 strcat(bpdpath,subpath);
97 PM_backslash(bpdpath);
98 strcpy(filename,bpdpath);
99 strcat(filename,dllname);
100 if ((f = fopen(filename,"rb")) == NULL)
103 strcpy(cachedpath,bpdpath);
108 /****************************************************************************
110 True if local override enabled, false if not.
113 Tests to see if the local override option is enabled, and if so it will
114 look for the Nucleus drivers in the local application directories in
115 preference to the Nucleus system directories.
116 ****************************************************************************/
117 static ibool GetLocalOverride(void)
121 static ibool local_override = -1;
123 if (local_override == -1) {
124 local_override = false;
125 strcpy(filename,PM_getNucleusPath());
126 PM_backslash(filename);
127 strcat(filename,"graphics.ini");
128 if ((f = fopen(filename,"r")) != NULL) {
129 while (!feof(f) && fgets(filename,sizeof(filename),f)) {
130 if (strnicmp(filename,"uselocal",8) == 0) {
131 local_override = ((*(filename+9) - '0') == 1);
138 return local_override;
141 /****************************************************************************
143 Sets the location of the debug log file.
149 dllname - Name of the Binary Portable DLL to load
150 bpdpath - Place to store the actual path to the file
153 True if found, false if not.
156 Finds the location of a specific Binary Portable DLL, by searching all
157 the standard SciTech Nucleus driver locations.
158 ****************************************************************************/
159 ibool PMAPI PM_findBPD(
163 static char cachedpath[PM_MAX_PATH] = "";
165 /* On the first call determine the path to the Nucleus drivers */
166 if (cachedpath[0] == 0) {
167 /* First try in the global system Nucleus driver path if
168 * the local override setting is not enabled.
171 if (!GetLocalOverride()) {
172 if (TryPath(bpdpath,cachedpath,PM_getNucleusPath(),"",dllname))
176 /* Next try in the local application directory if available */
177 if (localBPDPath[0] != 0) {
178 if (TryPath(bpdpath,cachedpath,localBPDPath,"",dllname))
182 #if !defined(__WIN32_VXD__) && !defined(__NT_DRIVER__)
184 if ((mgl_root = getenv("MGL_ROOT")) != NULL) {
185 if (TryPath(bpdpath,cachedpath,mgl_root,"drivers",dllname))
189 PM_getCurrentPath(bpdpath,PM_MAX_PATH);
190 if (TryPath(bpdpath,cachedpath,bpdpath,"drivers",dllname))
194 /* Finally try in the global system path again so that we
195 * will still find the drivers in the global system path if
196 * the local override option is on, but the application does
197 * not have any local override drivers.
199 if (TryPath(bpdpath,cachedpath,PM_getNucleusPath(),"",dllname))
202 /* Whoops, we can't find the BPD file! */
206 /* Always try in the previously discovered path */
207 return TryPath(bpdpath,NULL,cachedpath,"",dllname);
210 /****************************************************************************
212 Copies a string into another, and returns dest + strlen(src).
213 ****************************************************************************/
214 static char *_stpcpy(
220 while ((*_dest++ = *_src++) != 0)
225 /****************************************************************************
227 Copies a string into another, stopping at the maximum length. The string
228 is properly terminated (unlike strncpy).
229 ****************************************************************************/
230 static void safe_strncpy(
236 if(strlen(src) >= maxlen) {
237 strncpy(dst, src, maxlen);
245 /****************************************************************************
247 Determins if the dot separator is present in the string.
248 ****************************************************************************/
266 /****************************************************************************
268 Make a full pathname from split components.
274 path - Place to store full path
275 drive - Drive component for path
276 dir - Directory component for path
277 name - Filename component for path
278 ext - Extension component for path
281 Function to make a full pathname from split components. Under Unix the
282 drive component will usually be empty. If the drive, dir, name, or ext
283 parameters are null or empty, they are not inserted in the path string.
284 Otherwise, if the drive doesn't end with a colon, one is inserted in the
285 path. If the dir doesn't end in a slash, one is inserted in the path.
286 If the ext doesn't start with a dot, one is inserted in the path.
288 The maximum sizes for the path string is given by the constant PM_MAX_PATH,
289 which includes space for the null-terminator.
293 ****************************************************************************/
294 void PMAPI PM_makepath(
301 if (drive && *drive) {
306 path = _stpcpy(path,dir);
307 if (*(path-1) != '\\' && *(path-1) != '/')
315 path = _stpcpy(path,name);
319 path = _stpcpy(path,ext);
324 /****************************************************************************
326 Split a full pathname into components.
332 path - Full path to split
333 drive - Drive component for path
334 dir - Directory component for path
335 name - Filename component for path
336 ext - Extension component for path
339 Flags indicating what components were parsed.
342 Function to split a full pathmame into separate components in the form
344 X:\DIR\SUBDIR\NAME.EXT
346 and splits path into its four components. It then stores those components
347 in the strings pointed to by drive, dir, name and ext. (Each component is
348 required but can be a NULL, which means the corresponding component will be
349 parsed but not stored).
351 The maximum sizes for these strings are given by the constants PM_MAX_DRIVE
352 and PM_MAX_PATH. PM_MAX_DRIVE is always 4, and PM_MAX_PATH is usually at
353 least 256 characters. Under Unix the dir, name and ext components may be
354 up to the full path in length.
358 ****************************************************************************/
359 int PMAPI PM_splitpath(
368 char buf[PM_MAX_PATH+2];
370 /* Set all string to default value zero */
372 if (drive) *drive = 0;
377 /* Copy filename into template up to PM_MAX_PATH characters */
379 if ((temp = strlen(path)) > PM_MAX_PATH)
382 strncpy(p, path, temp);
385 /* Split the filename and fill corresponding nonzero pointers */
390 if (!temp && (*(p+1) == '\0'))
392 if ((!temp) && ((ret & PM_HAS_EXTENSION) == 0)) {
393 ret |= PM_HAS_EXTENSION;
394 safe_strncpy(ext, p, PM_MAX_PATH - 1);
404 ret |= PM_HAS_DIRECTORY;
405 safe_strncpy(dir, p, PM_MAX_PATH - 1);
414 ret |= PM_HAS_FILENAME;
415 safe_strncpy(name, p, PM_MAX_PATH - 1);
417 if (*p == 0 || (*p == ':' && p == &buf[2]))
424 ret |= PM_HAS_WILDCARDS;
433 safe_strncpy(drive, &buf[1], PM_MAX_DRIVE - 1);
438 /****************************************************************************
440 Block until a specific time has elapsed since the last call
446 milliseconds - Number of milliseconds for delay
449 This function will block the calling thread or process until the specified
450 number of milliseconds have passed since the /last/ call to this function.
451 The first time this function is called, it will return immediately. On
452 subsquent calls it will block until the specified time has elapsed, or it
453 will return immediately if the time has already elapsed.
455 This function is useful to provide constant time functionality in a
456 program, such as a frame rate limiter for graphics applications etc.
460 ****************************************************************************/
461 void PMAPI PM_blockUntilTimeout(
464 ulong microseconds = milliseconds * 1000L,msDelay;
465 static LZTimerObject tm;
466 static ibool firstTime = true;
473 if ((msDelay = (microseconds - LZTimerLapExt(&tm)) / 1000L) > 0)
475 while (LZTimerLapExt(&tm) < microseconds)