home *** CD-ROM | disk | FTP | other *** search
- /* Wide AREA INFORMATION SERVER SOFTWARE
- No guarantees or restrictions. See the readme file for the full standard
- disclaimer.
-
- 3.26.90 Harry Morris, morris@think.com
- 4.11.90 HWM - generalized conditional includes (see c-dialect.h)
- */
- #define _C_C_util_
-
- #include <dos.h>
- #include <windows.h>
-
- #undef TRUE
- #undef FALSE
- #undef IGNORE
-
- #include "cdialect.h"
- #include "cutil.h"
- #include "panic.h"
-
- #include <string.h>
- #include <varargs.h>
-
- extern unsigned int vb_data;
- extern long allocated_memory;
- int total_segs;
-
- typedef WORD HANDLE32;
- #define MAXMEMSEGS 5
- HANDLE hSegment[MAXMEMSEGS];
-
- int fs_meminit(numsegs)
- int numsegs;
- {
- BOOL bRetVal;
- LPSTR lp;
- WORD wSeg, wSize;
- int x;
-
- bRetVal = 0;
- total_segs = MIN(numsegs, MAXMEMSEGS);
-
- for(x = 0; x < total_segs; x++)
- {
- if(!(hSegment[x] = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, (long)(63L*1024L))))
- return FALSE;
- lp = GlobalLock(hSegment[x]);
- wSeg = HIWORD(lp);
- wSize = (WORD)GlobalSize(hSegment[x]) - 16;
- bRetVal |= LocalInit(wSeg, 0, wSize);
- GlobalUnlock(hSegment[x]); // undo localinit's global lock, but since we're
- } // always in prot mode, leave the segment locked
- return bRetVal;
- }
-
- void* fs_malloc(size)
- long size;
- {
- HANDLE hMem, hSeg;
- LPSTR lp;
- PSTR p;
- WORD wSeg;
- static int which = -1;
- register void *ptr;
-
- if (!size) // retrofit from b5
- return((void*)NULL);
-
- if(size > 256L)
- {
- hSeg = GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, size);
- lp = GlobalLock(hSeg);
- memset((void*)lp, 0, size);
- return((void*)lp);
- }
-
- p = 0;
-
- if(++which == total_segs)
- which = 0;
-
- lp = GlobalLock(hSegment[which]); // get the segment address
- wSeg = HIWORD(lp);
- asm push ds;
- asm mov ax, wSeg;
- asm mov ds, ax;
- hMem = LocalAlloc(LMEM_DISCARDABLE | LMEM_MOVEABLE, size);
- if(hMem)
- p = LocalLock(hMem);
- asm pop ds;
- GlobalUnlock(hSegment[which]);
- if(!hMem || !p)
- MessageBox(GetFocus(), "Memory allocation failed", "WAIS Error", MB_OK);
- ptr = (void*)MAKELONG(p, wSeg);
- memset(ptr, 0, size);
- return(ptr);
- }
-
- void fs_free(ptr)
- void* ptr;
- {
- HANDLE hMem, hSeg;
- LPSTR lp;
- WORD w, wSeg;
- int x;
-
- if (ptr == NULL)
- return;
-
- w = LOWORD(ptr);
- hSeg = LOWORD(GlobalHandle(HIWORD(ptr)));
- if(w == vb_data || hSeg == vb_data)
- MessageBox(GetFocus(), "ATTEMPT TO FREE VB DATA", "WAIS Error", MB_OK);
- for(x = 0; x < total_segs; x++)
- {
- if(hSeg == hSegment[x])
- {
- lp = GlobalLock(hSeg);
- wSeg = HIWORD(lp);
- asm push ds;
- asm mov ax, wSeg;
- asm mov ds, ax;
- hMem = LocalHandle(w);
- if(hMem)
- {
- LocalUnlock(hMem);
- LocalFree(hMem);
- }
- asm pop ds;
- GlobalUnlock(hSeg);
- if(!hMem)
- MessageBox(GetFocus(), "Memory release failed", "WAIS Error", MB_OK);
- return;
- }
- }
-
- GlobalUnlock(hSeg);
- GlobalFree(hSeg);
- return;
- }
-
- void* fs_realloc(ptr,size)
- void* ptr;
- long size;
- {
- HANDLE hMem, hNewMem, hSeg;
- LPSTR lp;
- PSTR p;
- WORD w, wSeg;
- int x;
-
- if (ptr == NULL)
- return((void*)s_malloc((long)size));
-
- hMem = hNewMem = NULL;
- p = 0;
-
- w = LOWORD(ptr);
- hSeg = LOWORD(GlobalHandle(HIWORD(ptr)));
- for(x = 0; x < total_segs; x++)
- {
- if(hSeg == hSegment[x])
- {
- lp = GlobalLock(hSeg);
- wSeg = HIWORD(lp);
- asm push ds;
- asm mov ax, wSeg;
- asm mov ds, ax;
- hMem = LocalHandle(w);
- if(hMem)
- {
- LocalUnlock(hMem);
- hNewMem = LocalReAlloc(hMem, size, LMEM_DISCARDABLE | LMEM_MOVEABLE);
- if(hNewMem)
- p = LocalLock(hNewMem);
- }
- asm pop ds;
- GlobalUnlock(hSeg);
- if(!hMem || !hNewMem || !p)
- MessageBox(GetFocus(), "Memory reallocation failed", "WAIS Error", MB_OK);
- return((void*)MAKELONG(p, wSeg));
- }
- }
-
- GlobalUnlock(hSeg);
- hSeg = GlobalReAlloc(hSeg, size, GMEM_MOVEABLE | GMEM_ZEROINIT);
- return((void*)GlobalLock(hSeg));
- }
-
- void fs_memterm()
- {
- int x;
- int memsegs = total_segs;
-
- for(x = 0; x < memsegs; x++)
- {
- GlobalUnlock(hSegment[x]);
- GlobalFree(hSegment[x]);
- }
- }
-
-
- void fs_checkPtr(ptr)
- void* ptr;
- {
- if (ptr == NULL)
- MessageBox(GetFocus(), "Memory reallocation (>1K) failed", "WAIS Error", MB_OK);
- }
-
- char* s_strdup(s)
- char* s;
-
- /* return a copy of s. This is identical to the standard library routine
- strdup(), except that it is safe. If s == NULL or malloc fails,
- appropriate action is taken.
- */
- {
- unsigned long len;
- char* copy = NULL;
-
- if (s == NULL) /* saftey check to postpone stupid errors */
- return(NULL);
-
- len = strlen(s); /* length of string - terminator */
-
- copy = (char*)s_malloc((long)((long)sizeof(char)*(len + 1L)));
- strncpy(copy,s,len + 1);
- return(copy);
- }
-
- char* fs_strncat(dst,src,maxToAdd,maxTotal)
- char* dst;
- char* src;
- long maxToAdd;
- long maxTotal;
-
- /* like strncat, except the fourth argument limits the maximum total
- length of the resulting string
- */
- {
- long dstSize = strlen(dst);
- long srcSize = strlen(src);
-
- if (dstSize + srcSize < maxTotal) /* use regular old strncat */
- return(strncat(dst,src,maxToAdd));
- else
- { long truncateTo = maxTotal - dstSize - 1;
- char saveChar = src[truncateTo];
- char* result = NULL;
- src[truncateTo] = '\0';
- result = strncat(dst,src,maxToAdd);
- src[truncateTo] = saveChar;
- return(result);
- }
- }
-
- /*----------------------------------------------------------------------*/
-
- typedef long (longfunc) _AP((long c));
-
- char*
- strtokf(s1,isDelimiter)
- char* s1;
- longfunc *isDelimiter; /* really *isDelimiter() */
-
- /* This function is exactly like strtok, except that instead of passing a
- delimiter string, you pass a function that decides if a character is
- a delimiter or not, returning IS_DELIMITER or NOT_DELIMITER respecively.
- Note that passing a NULL delimiter function will cause the last delimiter
- to be used.
- */
- {
- static char* searchStr = NULL;
- static longfunc *delimiterFunc;
- long i;
- char* startTok = NULL;
-
- if (s1 != NULL) /* passing s1 = NULL says use the last pos */
- searchStr = s1;
-
- if (isDelimiter != NULL)
- delimiterFunc = isDelimiter;
-
- if (searchStr == NULL || searchStr[0] == '\0')
- return(NULL); /* nothing left to search */
-
- if (delimiterFunc == NULL)
- return(NULL); /* no delimiter to search with */
-
- /* find the start of the next token */
- for (i = 0; searchStr[i] != '\0'; i++)
- { if ((*delimiterFunc)((long)searchStr[i]) == NOT_DELIMITER)
- break;
- }
-
- if (searchStr[i] == '\0')
- return(NULL); /* read to end of search string */
- else
- startTok = searchStr + i; /* remember the starting point for this token*/
-
- /* find the end of the next token */
- for (; searchStr[i] != '\0'; i++)
- { if ((*delimiterFunc)((long)searchStr[i]) == IS_DELIMITER)
- break;
- }
-
- /* if the end is a delimiter (and not just the end of the search string)
- replace it with '\0', and put searchStr just beyond it, otherwise
- put searchStr at the terminator. */
- if (searchStr[i] != '\0')
- { searchStr[i] = '\0';
- searchStr = searchStr + i + 1;
- }
- else
- searchStr = searchStr + i;
-
- return(startTok);
- }
-
- /*----------------------------------------------------------------------*/
-
- extern char* log_file_name;
- extern FILE* logfile;
-
- /*----------------------------------------------------------------------*/
-
- void
- warn(message)
- char* message;
-
- {
- printf("%s\n<press return to continue>\n",message);
- getchar();
- }
-
- /*----------------------------------------------------------------------*/
- boolean substrcmp(string1,string2)
- char *string1, *string2;
- {
- /* compares the strings up until one of then ends.
- * returns true if they are the same, false if not.
- */
- long count = 0;
- while(true){
- if(count >= strlen(string1) ||
- count >= strlen(string2)){
- return(true);
- }
- else if(string1[count] != string2[count]){
- return(false);
- }
- count++;
- }
- }
-
- /*----------------------------------------------------------------------*/
- extern char *ctime();
- char *printable_time()
- {
- static char *string;
-
- time_t tptr;
- time(&tptr);
- string = ctime(&tptr);
- if(string){
- if(string[strlen(string)-1] == '\n')
- string[strlen(string)-1] = '\0';
- return(string);
- }
- else
- return("Time Unknown");
- }
-
- /*----------------------------------------------------------------------*/
-
- char char_downcase(long_ch)
- unsigned long long_ch;
- {
- unsigned char ch = long_ch & 0xFF; /* just want one byte */
- /* when ansi is the way of the world, this can be tolower */
- return (((ch >= 'A') && (ch <= 'Z')) ? (ch + 'a' -'A') : ch);
- }
-
- char *string_downcase(w)
- char *w;
- {
- long i = 0;
- while(w[i] != '\0'){
- w[i] = char_downcase((unsigned long)w[i]);
- i++;
- }
- return(w);
- }
-
- /*----------------------------------------------------------------------*/
-
-
- /* parsing arguments functions */
-
- char *next_arg(argc,argv)
- int *argc;
- char ***argv;
-
- /* Returns NULL when it is out of arguments,
- This side effects both argc and argv. argc always contains the number
- of arguments left.
- The first returned is the command name.
- */
- {
- if((*argc)-- > 0)
- return(*((*argv)++));
- else
- return(NULL);
- }
-
- /*----------------------------------------------------------------------*/
-
- char *peek_arg(argc,argv)
- int *argc;
- char ***argv;
-
- /* Returns the next argument without popping it.
- Returns NULL when it is out of arguments.
- */
- {
- if((*argc) > 0)
- return(**argv);
- else
- return(NULL);
- }
-
- /*----------------------------------------------------------------------*/
-
- void beFriendly()
- {
- MSG msg;
-
- while(PeekMessage((LPMSG)&msg, 0, 0, 0, PM_REMOVE))
- {
- if(msg.message == WM_PAINT) /* leave WM_PAINT's in the queue */
- GetMessage((LPMSG)&msg, 0, 0, 0);
- TranslateMessage((LPMSG)&msg);
- DispatchMessage((LPMSG)&msg);
- }
- }
-