home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
tvos200.zip
/
CPL
/
WATSTUB.C
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-19
|
11KB
|
572 lines
/*
* watstub.c: WatCOM stub routines
* ================================
* (C)1995 by F.Jalvingh; All rights reserved.
*
*
* $Header: s:/prj/apl/ICCSTUB.C_V 1.3 93/05/10 13:28:39 JAL Exp Locker: JAL $
*
* Revision log:
* -------------
* $Log: ICCSTUB.C_V $
* Revision 1.3 93/05/10 13:28:39 JAL
* Misc changes.
*
* Revision 1.2 92/09/25 13:53:14 JAL
* New 32-bits stuff..
*
* Revision 1.1 92/08/24 14:22:20 JAL
* Initial revision
*
* Revision 1.2 92/06/18 14:22:33 HJB
* Error in getdisk() solved.
*
* Revision 1.1 91/10/20 20:44:13 JAL
* Initial revision
*
*
*/
#if defined(__WATCOMC__)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <io.h>
#include <errno.h>
static char rcsid[] = "$Header: s:/prj/apl/ICCSTUB.C_V 1.3 93/05/10 13:28:39 JAL Exp Locker: JAL $";
#define NUL '\0'
#define EQ ==
#define NE !=
#define UWORD unsigned short
#if defined(__WATCOMC__) && defined(__FLAT__) && !defined(__32BITS__)
# define __32BITS__
#endif
#ifdef __OS2__
#define INCL_BASE
#define INCL_DOSMISC
#define INCL_DOS
#define INCL_SUB
#define INCL_DOSERRORS
#define INCL_DOSFILEMGR
#define INCL_NOPMAPI
#if defined(__32BITS__)
#include <os2.h>
#else
#include <os2/os2.h>
#endif
#endif
char *searchpath(char *name)
{
static char buffer[82];
char *p, *pout, *pin, lastch;
p = getenv("PATH"); /* Try to find PATH= variable, */
if(p EQ NULL) return NULL; /* There's no path! */
/**** Now, for each subpath in the path try if the file passed can ****/
/**** be accessed.. ****/
while(*p NE NUL)
{
pout = buffer;
lastch = NUL;
while(*p NE ';' && *p NE NUL)
{
lastch = *p++;
if( (pout-buffer) < sizeof(buffer)-2)
*pout++ = lastch;
}
if(lastch != '\\' && lastch != ':')
*pout++ = '\\';
/**** Now copy user-name part.. ****/
pin = name;
while(*pin NE NUL)
{
if( (pout-buffer) < sizeof(buffer)-2)
{
*pout++ = *pin++;
}
else
break;
}
*pout = NUL;
if(access(buffer, 0) == 0) return buffer;
if(*p EQ ';') p++;
}
return NULL;
}
unsigned long coreleft(void)
{
#ifndef __OS2__
unsigned short seg;
_dos_allocmem(0, &seg);
return (unsigned long)seg * 16l;
#else
# if defined(__32BITS__)
ULONG c;
DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &c, sizeof(c));
return c;
# else
unsigned long c;
DosMemAvail(&c);
return c;
# endif
#endif
}
unsigned long farcoreleft(void)
{
return coreleft();
}
static void CopyIt(char *dst, const char *src, unsigned maxlen)
{
if (dst)
{
if(strlen(src) >= maxlen)
{
strncpy(dst, src, maxlen);
dst[maxlen] = 0;
}
else
strcpy(dst, src);
}
}
static int DotFound(char *pb)
{
if (*(pb-1) == '.')
pb--;
switch (*--pb)
{
case ':' : if (*(pb-2) != '\0') break;
case '/' :
case '\\' :
case '\0' : return 1;
}
return 0;
}
#define WILDCARDS 0x01
#define EXTENSION 0x02
#define FILENAME 0x04
#define DIRECTORY 0x08
#define DRIVE 0x10
#define MAXPATH 80
#define MAXDRIVE 3
#define MAXDIR 66
#define MAXFILE 9
#define MAXEXT 5
int fnsplit(const char *pathp, char *drivep, char *dirp, char *namep, char *extp)
{
register char *pb;
register int wrk;
int ret;
char buf[MAXPATH+2];
/**** Set all string to default value zero ****/
ret = 0;
if (drivep) *drivep = 0;
if (dirp) *dirp = 0;
if (namep) *namep = 0;
if (extp) *extp = 0;
/**** Copy filename into template up to MAXPATH characters ****/
pb = buf;
while (*pathp == ' ') pathp++;
if ((wrk = strlen(pathp)) > MAXPATH)
wrk = MAXPATH;
*pb++ = 0;
strncpy(pb, pathp, wrk);
*(pb += wrk) = 0;
/**** Split the filename and fill corresponding nonzero pointers ****/
wrk = 0;
for (;;)
{
switch (*--pb)
{
case '.' : if (!wrk && (*(pb+1) == '\0')) wrk = DotFound(pb);
if ((!wrk) && ((ret & EXTENSION) == 0))
{
ret |= EXTENSION;
CopyIt(extp, pb, MAXEXT - 1);
*pb = 0;
}
continue;
case ':' : if (pb != &buf[2]) continue;
case '\0' : if (wrk)
{
if (*++pb)
ret |= DIRECTORY;
CopyIt(dirp, pb, MAXDIR - 1);
*pb-- = 0;
break;
}
case '/' :
case '\\' : if (!wrk)
{
wrk++;
if (*++pb)
ret |= FILENAME;
CopyIt(namep, pb, MAXFILE - 1);
*pb-- = 0;
if (*pb == 0 || (*pb == ':' && pb == &buf[2]))
break;
}
continue;
case '*' :
case '?' : if (!wrk) ret |= WILDCARDS;
default : continue;
}
break;
}
if (*pb == ':')
{
if (buf[1])
ret |= DRIVE;
CopyIt(drivep, &buf[1], MAXDRIVE - 1);
}
return (ret);
}
void fnmerge(char *out, char *drive, char *dir, char *name, char *ext)
{
*out = '\0';
if(*drive != '\0') strcpy(out, drive);
if(*dir != '\0') strcat(out, dir);
if(*name != '\0') strcat(out, name);
if(*ext != '\0') strcat(out, ext);
}
#ifdef __OS2__
/*
* getftime() for OS/2..
*/
int getftime(int ha, struct ftime *ft)
{
#if defined(__32BITS__)
ULONG errc;
FILESTATUS3 fs;
errc= DosQueryFileInfo(ha, 1, &fs, sizeof(fs));
if(errc)
{
memset(ft, 0, sizeof(ft));
return -1;
}
* (UWORD *)ft = * (UWORD *) &fs.ftimeLastWrite;
((UWORD *)ft)[1] = *(UWORD *) &fs.fdateLastWrite;
#else
FILESTATUS fs;
typedef unsigned int UWORD;
DosQFileInfo(ha, 0x0001, &fs, sizeof(fs));
* (UWORD *)ft = * (UWORD *) &fs.ftimeLastWrite;
((UWORD *)ft)[1] = *(UWORD *) &fs.fdateLastWrite;
#endif
return 0;
}
/*
* setftime() for OS/2
*/
int setftime(int ha, struct ftime *ft)
{
FILESTATUS fs;
UWORD errc, *p;
memset(&fs, 0, sizeof(fs));
errc= DosQFileInfo(ha, 0x0001, &fs, sizeof(fs));
if(errc) return -1;
/**** Change the FILESTATUS structure.. ****/
p = (UWORD *) &fs.ftimeLastWrite;
*p = * (UWORD *)ft;
p = (UWORD *) &fs.fdateLastWrite;
*p = ((UWORD *)ft)[1];
errc= DosSetFileInfo(ha, 0x0001, &fs, sizeof(fs));
return errc == 0 ? 0 : -1;
}
#endif
/*
* farmalloc() does a MSC halloc..
*/
void *farmalloc(unsigned long size)
{
return malloc(size);
}
void farfree(void *s)
{
free(s);
}
#ifdef __OS2__
struct ffblk
{
char ff_reserved[21];
char ff_attrib;
unsigned ff_ftime;
unsigned ff_fdate;
long ff_fsize;
char ff_name[13];
};
/*
* findfirst() searches a '1st' file..
*/
int findfirst(char *path, struct ffblk *ff, int attrib)
{
#ifdef __32BITS__
FILEFINDBUF3 fb;
HDIR hdir = 0xffff; /* HDIR_SYSTEM */
ULONG count = 1, /* File count, */
err;
err = DosFindFirst(path, &hdir, attrib, &fb, sizeof(fb), &count, 1l);
if(err)
{
/**** Translate error.. ****/
switch(err)
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
case ERROR_NO_MORE_FILES: errno = EMFILE; break;
default: errno = EINVAL; break;
}
return -1;
}
#else
FILEFINDBUF fb;
HDIR hdir = 0xffff; /* HDIR_SYSTEM */
USHORT count = 1, /* File count, */
err;
err = DosFindFirst(path, &hdir, attrib, &fb, sizeof(fb), &count, 0l);
if(err)
{
/**** Translate error.. ****/
switch(err)
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
case ERROR_NO_MORE_FILES: errno = EMFILE; break;
default: errno = EINVAL; break;
}
return -1;
}
#endif
/**** Translate find buffer.. ****/
memcpy(ff->ff_reserved, &hdir, sizeof(hdir)); /* Save handle, */
ff->ff_attrib = (char)fb.attrFile; /* Copy file attributes, */
memcpy(&ff->ff_ftime, &fb.ftimeLastWrite, sizeof(ff->ff_ftime));
memcpy(&ff->ff_fdate, &fb.fdateLastWrite, sizeof(ff->ff_fdate));
ff->ff_fsize = fb.cbFile;
memcpy(ff->ff_name, fb.achName, sizeof(ff->ff_name));
return 0;
}
/*
* findnext() finds the next file..
*/
int findnext(struct ffblk *ff)
{
#ifdef __32BITS__
FILEFINDBUF3 fb;
HDIR hdir;
ULONG count = 1,
err;
/**** Get HDIR from reserved area, ****/
memcpy(&hdir, ff->ff_reserved, sizeof(hdir));
err = DosFindNext(hdir, &fb, sizeof(fb), &count);
if(err NE 0)
{
switch(err)
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
case ERROR_NO_MORE_FILES: errno = EMFILE; break;
default: errno = EINVAL; break;
}
return -1;
}
#else
FILEFINDBUF fb;
HDIR hdir;
USHORT count = 1,
err;
/**** Get HDIR from reserved area, ****/
memcpy(&hdir, ff->ff_reserved, sizeof(hdir));
err = DosFindNext(hdir, &fb, sizeof(fb), &count);
if(err NE 0)
{
switch(err)
{
case ERROR_PATH_NOT_FOUND: errno = ENOENT; break;
case ERROR_NO_MORE_FILES: errno = EMFILE; break;
default: errno = EINVAL; break;
}
return -1;
}
#endif
/**** Copy.. ****/
ff->ff_attrib = (char)fb.attrFile; /* Copy file attributes, */
memcpy(&ff->ff_ftime, &fb.ftimeLastWrite, sizeof(ff->ff_ftime));
memcpy(&ff->ff_fdate, &fb.fdateLastWrite, sizeof(ff->ff_fdate));
ff->ff_fsize = fb.cbFile;
memcpy(ff->ff_name, fb.achName, sizeof(ff->ff_name));
return 0;
}
int getcurdir(int drive, char *dir)
{
#ifdef __32BITS__
ULONG bytes;
bytes = 0;
DosQueryCurrentDir(drive, NULL, &bytes);
if(bytes > MAXPATH) return -1;
DosQCurDir(drive, dir, &bytes);
return 0;
#else
USHORT bytes;
bytes = 0;
DosQCurDir(drive, NULL, &bytes);
if(bytes > MAXPATH) return -1;
DosQCurDir(drive, dir, &bytes);
return 0;
#endif
}
int getdisk(void)
{
#ifdef __32BITS__
ULONG drive, numdrives;
DosQueryCurrentDisk(&drive, &numdrives);
#else
USHORT drive;
ULONG numdrives;
DosQCurDisk(&drive, &numdrives);
#endif
return (int)drive - 1;
}
int setdisk(int drive)
{
#if defined(__32BITS__)
return (int)DosSetDefaultDisk((ULONG)drive + 1);
#else
return (int)DosSelectDisk((USHORT)drive + 1); /* Bcc getdisk: 'A' = 0; MSC DosSelectdisk: 'A' = 1 */
#endif
}
char *getpass(char *prompt)
{
static char buf[20];
char *p;
puts(prompt);
fgets(buf, 8, stdin);
for(p = buf; *p NE NUL && *p NE 0x0a && *p NE 0x0d; ) p++;
*p = NUL;
return buf;
}
#define LK_UNLCK 0 /* unlock the file region */
#define LK_LOCK 1 /* lock the file region */
#define LK_NBLCK 2 /* non-blocking lock */
#define LK_RLCK 3 /* lock for writing */
#define LK_NBRLCK 4 /* non-blocking lock for writing */
int locking(int h, int mode, long bytes)
{
ULONG errc;
FILELOCK fl, fu;
if(mode == LK_LOCK)
{
fl.lOffset = tell(h);
fl.lRange = bytes;
fu.lOffset = 0;
fu.lRange = 0;
}
else if(mode == LK_UNLCK)
{
fu.lOffset = tell(h);
fu.lRange = bytes;
fl.lOffset = 0;
fl.lRange = 0;
}
else
{
puts("Illegal LOCK mode");
exit(30);
}
errc= DosSetFileLocks(h, &fu, &fl, 20000, 0);
if(errc != 0) return -1;
return 0;
}
#endif /* __OS2__ */
#endif