home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tvos200.zip / CPL / ICCSTUB.C < prev    next >
C/C++ Source or Header  |  1994-11-03  |  11KB  |  614 lines

  1. /*
  2.  *    iccstub.c:    MS C stub routines -> missing MSC calls and conversions to TC.
  3.  *    ==========================================================================
  4.  *    (C)1991 by F.Jalvingh; All rights reserved.
  5.  *
  6.  *
  7.  *    $Header: s:/prj/apl/ICCSTUB.C_V 1.3 93/05/10 13:28:39 JAL Exp Locker: JAL $
  8.  *
  9.  *    Revision log:
  10.  *    -------------
  11.  *    $Log:    ICCSTUB.C_V $
  12.  *    Revision 1.3  93/05/10    13:28:39  JAL
  13.  *    Misc changes.
  14.  *
  15.  *    Revision 1.2  92/09/25    13:53:14  JAL
  16.  *    New 32-bits stuff..
  17.  *
  18.  *    Revision 1.1  92/08/24    14:22:20  JAL
  19.  *    Initial revision
  20.  *
  21.  *    Revision 1.2  92/06/18    14:22:33  HJB
  22.  *    Error in getdisk() solved.
  23.  *
  24.  *    Revision 1.1  91/10/20    20:44:13  JAL
  25.  *    Initial revision
  26.  *
  27.  *
  28.  */
  29. #include    <stdio.h>
  30. #include    <stdlib.h>
  31. #include    <malloc.h>
  32. #include    <string.h>
  33. #include    <io.h>
  34. #include    <errno.h>
  35.  
  36. static char rcsid[] = "$Header: s:/prj/apl/ICCSTUB.C_V 1.3 93/05/10 13:28:39 JAL Exp Locker: JAL $";
  37.  
  38. #define NUL         '\0'
  39. #define EQ            ==
  40. #define NE            !=
  41. #define UWORD        unsigned short
  42.  
  43. #if defined(__ICC__)
  44. # define    __32BITS__    1
  45. #endif
  46.  
  47.  
  48. #ifdef    __OS2__
  49. #define INCL_BASE
  50. #define INCL_DOSMISC
  51. #define INCL_DOS
  52. #define INCL_SUB
  53. #define INCL_DOSERRORS
  54. #define INCL_DOSFILEMGR
  55. #define INCL_NOPMAPI
  56.  
  57. #if defined(__32BITS__)
  58. #include    <os2.32/os2.h>
  59. #else
  60. #include    <os2/os2.h>
  61. #endif
  62. #endif
  63.  
  64. char *searchpath(char *name)
  65. {
  66.     static char buffer[82];
  67.     char        *p, *pout, *pin, lastch;
  68.  
  69.     p    = getenv("PATH");                       /* Try to find PATH= variable, */
  70.     if(p EQ NULL) return NULL;                    /* There's no path! */
  71.  
  72.     /**** Now, for each subpath in the path try if the file passed can    ****/
  73.     /**** be accessed..                                                 ****/
  74.     while(*p NE NUL)
  75.     {
  76.         pout    = buffer;
  77.         lastch    = NUL;
  78.         while(*p NE ';' && *p NE NUL)
  79.         {
  80.             lastch    = *p++;
  81.             if( (pout-buffer) < sizeof(buffer)-2)
  82.                 *pout++ = lastch;
  83.         }
  84.         if(lastch != '\\' && lastch != ':')
  85.             *pout++ = '\\';
  86.  
  87.         /**** Now copy user-name part.. ****/
  88.         pin = name;
  89.         while(*pin NE NUL)
  90.         {
  91.             if( (pout-buffer) < sizeof(buffer)-2)
  92.             {
  93.                 *pout++ = *pin++;
  94.             }
  95.             else
  96.                 break;
  97.         }
  98.         *pout = NUL;
  99.  
  100.         if(access(buffer, 0) == 0) return buffer;
  101.         if(*p EQ ';') p++;
  102.     }
  103.     return NULL;
  104. }
  105.  
  106.  
  107. unsigned long coreleft(void)
  108. {
  109. #ifndef __OS2__
  110.     unsigned short seg;
  111.  
  112.     _dos_allocmem(0, &seg);
  113.     return (unsigned long)seg * 16l;
  114. #else
  115. # if defined(__32BITS__)
  116.     ULONG        c;
  117.  
  118.     DosQuerySysInfo(QSV_TOTAVAILMEM, QSV_TOTAVAILMEM, &c, sizeof(c));
  119.     return c;
  120. # else
  121.     unsigned long    c;
  122.  
  123.     DosMemAvail(&c);
  124.     return c;
  125. # endif
  126. #endif
  127. }
  128.  
  129.  
  130. unsigned long farcoreleft(void)
  131. {
  132.     return coreleft();
  133. }
  134.  
  135.  
  136. static void CopyIt(char *dst, const char *src, unsigned maxlen)
  137. {
  138.     if (dst)
  139.     {
  140.         if(strlen(src) >= maxlen)
  141.         {
  142.             strncpy(dst, src, maxlen);
  143.             dst[maxlen] = 0;
  144.         }
  145.         else
  146.             strcpy(dst, src);
  147.     }
  148. }
  149.  
  150. static int DotFound(char *pb)
  151. {
  152.     if (*(pb-1) == '.')
  153.         pb--;
  154.  
  155.     switch (*--pb)
  156.     {
  157.         case ':'  : if (*(pb-2) != '\0') break;
  158.         case '/'  :
  159.         case '\\' :
  160.         case '\0' : return 1;
  161.     }
  162.  
  163.     return 0;
  164. }
  165.  
  166. #define WILDCARDS 0x01
  167. #define EXTENSION 0x02
  168. #define FILENAME  0x04
  169. #define DIRECTORY 0x08
  170. #define DRIVE      0x10
  171.  
  172. #define MAXPATH   80
  173. #define MAXDRIVE  3
  174. #define MAXDIR      66
  175. #define MAXFILE   9
  176. #define MAXEXT      5
  177.  
  178. int fnsplit(const char *pathp, char *drivep, char *dirp, char *namep, char *extp)
  179. {
  180.     register char    *pb;
  181.     register int    wrk;
  182.     int     ret;
  183.     char    buf[MAXPATH+2];
  184.  
  185.     /**** Set all string to default value zero ****/
  186.     ret = 0;
  187.     if (drivep) *drivep = 0;
  188.     if (dirp)    *dirp    = 0;
  189.     if (namep)    *namep    = 0;
  190.     if (extp)    *extp    = 0;
  191.  
  192.     /**** Copy filename into template up to MAXPATH characters ****/
  193.     pb = buf;
  194.     while (*pathp == ' ') pathp++;
  195.  
  196.     if ((wrk = strlen(pathp)) > MAXPATH)
  197.         wrk = MAXPATH;
  198.  
  199.     *pb++ = 0;
  200.     strncpy(pb, pathp, wrk);
  201.     *(pb += wrk) = 0;
  202.  
  203.     /**** Split the filename and fill corresponding nonzero pointers ****/
  204.     wrk = 0;
  205.     for (;;)
  206.     {
  207.         switch (*--pb)
  208.         {
  209.             case '.'  : if (!wrk && (*(pb+1) == '\0')) wrk = DotFound(pb);
  210.                         if ((!wrk) && ((ret & EXTENSION) == 0))
  211.                         {
  212.                             ret |= EXTENSION;
  213.                             CopyIt(extp, pb, MAXEXT - 1);
  214.                             *pb = 0;
  215.                         }
  216.                         continue;
  217.  
  218.             case ':'  : if (pb != &buf[2]) continue;
  219.  
  220.             case '\0' : if (wrk)
  221.                         {
  222.                             if (*++pb)
  223.                                 ret |= DIRECTORY;
  224.                             CopyIt(dirp, pb, MAXDIR - 1);
  225.                             *pb-- = 0;
  226.                             break;
  227.                         }
  228.  
  229.             case '/'  :
  230.             case '\\' : if (!wrk)
  231.                         {
  232.                             wrk++;
  233.                             if (*++pb)
  234.                                 ret |= FILENAME;
  235.                             CopyIt(namep, pb, MAXFILE - 1);
  236.                             *pb-- = 0;
  237.                             if (*pb == 0 || (*pb == ':' && pb == &buf[2]))
  238.                                 break;
  239.                         }
  240.                         continue;
  241.  
  242.             case '*'  :
  243.             case '?'  : if (!wrk) ret |= WILDCARDS;
  244.  
  245.             default   : continue;
  246.         }
  247.  
  248.         break;
  249.     }
  250.  
  251.     if (*pb == ':')
  252.     {
  253.         if (buf[1])
  254.             ret |= DRIVE;
  255.         CopyIt(drivep, &buf[1], MAXDRIVE - 1);
  256.     }
  257.  
  258.     return (ret);
  259. }
  260.  
  261.  
  262. void fnmerge(char *out, char *drive, char *dir, char *name, char *ext)
  263. {
  264.     *out    = '\0';
  265.     if(*drive != '\0') strcpy(out, drive);
  266.     if(*dir   != '\0') strcat(out, dir);
  267.     if(*name  != '\0') strcat(out, name);
  268.     if(*ext   != '\0') strcat(out, ext);
  269. }
  270.  
  271.  
  272. #ifdef    __OS2__
  273. /*
  274.  *        getftime() for OS/2..
  275.  */
  276. int getftime(int ha, struct ftime *ft)
  277. {
  278. #if defined(__32BITS__)
  279.     ULONG        errc;
  280.     FILESTATUS3 fs;
  281.  
  282.     errc= DosQueryFileInfo(ha, 1, &fs, sizeof(fs));
  283.     if(errc)
  284.     {
  285.         memset(ft, 0, sizeof(ft));
  286.         return -1;
  287.     }
  288.     * (UWORD *)ft = * (UWORD *) &fs.ftimeLastWrite;
  289.     ((UWORD *)ft)[1] = *(UWORD *) &fs.fdateLastWrite;
  290.  
  291. #else
  292.     FILESTATUS        fs;
  293.     typedef unsigned int UWORD;
  294.  
  295.     DosQFileInfo(ha, 0x0001, &fs, sizeof(fs));
  296.     * (UWORD *)ft = * (UWORD *) &fs.ftimeLastWrite;
  297.     ((UWORD *)ft)[1] = *(UWORD *) &fs.fdateLastWrite;
  298. #endif
  299.     return 0;
  300. }
  301.  
  302.  
  303. /*
  304.  *    setftime() for OS/2
  305.  */
  306. int setftime(int ha, struct ftime *ft)
  307. {
  308.     FILESTATUS        fs;
  309.     UWORD            errc, *p;
  310.  
  311.     memset(&fs, 0, sizeof(fs));
  312.     errc= DosQFileInfo(ha, 0x0001, &fs, sizeof(fs));
  313.     if(errc) return -1;
  314.  
  315.     /**** Change the FILESTATUS structure.. ****/
  316.     p    = (UWORD *) &fs.ftimeLastWrite;
  317.     *p    = * (UWORD *)ft;
  318.  
  319.     p    = (UWORD *) &fs.fdateLastWrite;
  320.     *p    = ((UWORD *)ft)[1];
  321.     errc= DosSetFileInfo(ha, 0x0001, &fs, sizeof(fs));
  322.     return errc == 0 ? 0 : -1;
  323. }
  324. #endif
  325.  
  326.  
  327. /*
  328.  *        farmalloc() does a MSC halloc..
  329.  */
  330. void *farmalloc(unsigned long size)
  331. {
  332. #ifdef    __MSC__
  333.     return halloc(1, size);
  334. #else
  335.     return malloc(size);
  336. #endif
  337. }
  338.  
  339.  
  340. void farfree(void *s)
  341. {
  342. #ifdef    __MSC__
  343.     hfree((void huge *)s);
  344. #else
  345.     free(s);
  346. #endif
  347. }
  348.  
  349.  
  350. #ifdef    __OS2__
  351.  
  352. struct    ffblk
  353. {
  354.     char        ff_reserved[21];
  355.     char        ff_attrib;
  356.     unsigned    ff_ftime;
  357.     unsigned    ff_fdate;
  358.     long        ff_fsize;
  359.     char        ff_name[13];
  360. };
  361.  
  362.  
  363. /*
  364.  *        findfirst() searches a '1st' file..
  365.  */
  366. int findfirst(char *path, struct ffblk *ff, int attrib)
  367. {
  368. #ifdef __32BITS__
  369.     FILEFINDBUF3    fb;
  370.     HDIR            hdir = 0xffff;        /* HDIR_SYSTEM */
  371.     ULONG            count = 1,            /* File count, */
  372.                     err;
  373.  
  374.     err = DosFindFirst(path, &hdir, attrib, &fb, sizeof(fb), &count, 1l);
  375.     if(err)
  376.     {
  377.         /**** Translate error.. ****/
  378.         switch(err)
  379.         {
  380.             case ERROR_PATH_NOT_FOUND:    errno = ENOENT; break;
  381.             case ERROR_NO_MORE_FILES:    errno = EMFILE; break;
  382.             default:                    errno = EINVAL; break;
  383.         }
  384.         return -1;
  385.     }
  386. #else
  387.     FILEFINDBUF     fb;
  388.     HDIR            hdir = 0xffff;        /* HDIR_SYSTEM */
  389.     USHORT            count = 1,            /* File count, */
  390.                     err;
  391.  
  392.     err = DosFindFirst(path, &hdir, attrib, &fb, sizeof(fb), &count, 0l);
  393.     if(err)
  394.     {
  395.         /**** Translate error.. ****/
  396.         switch(err)
  397.         {
  398.             case ERROR_PATH_NOT_FOUND:    errno = ENOENT; break;
  399.             case ERROR_NO_MORE_FILES:    errno = EMFILE; break;
  400.             default:                    errno = EINVAL; break;
  401.         }
  402.         return -1;
  403.     }
  404. #endif
  405.  
  406.     /**** Translate find buffer..    ****/
  407.     memcpy(ff->ff_reserved, &hdir, sizeof(hdir));    /* Save handle, */
  408.     ff->ff_attrib    = (char)fb.attrFile;        /* Copy file attributes, */
  409.     memcpy(&ff->ff_ftime, &fb.ftimeLastWrite, sizeof(ff->ff_ftime));
  410.     memcpy(&ff->ff_fdate, &fb.fdateLastWrite, sizeof(ff->ff_fdate));
  411.     ff->ff_fsize    = fb.cbFile;
  412.     memcpy(ff->ff_name, fb.achName, sizeof(ff->ff_name));
  413.     return 0;
  414. }
  415.  
  416.  
  417. /*
  418.  *        findnext() finds the next file..
  419.  */
  420. int findnext(struct ffblk *ff)
  421. {
  422. #ifdef __32BITS__
  423.     FILEFINDBUF3    fb;
  424.     HDIR            hdir;
  425.     ULONG            count = 1,
  426.                     err;
  427.  
  428.     /**** Get HDIR from reserved area,    ****/
  429.     memcpy(&hdir, ff->ff_reserved, sizeof(hdir));
  430.     err = DosFindNext(hdir, &fb, sizeof(fb), &count);
  431.     if(err NE 0)
  432.     {
  433.         switch(err)
  434.         {
  435.             case ERROR_PATH_NOT_FOUND:    errno = ENOENT; break;
  436.             case ERROR_NO_MORE_FILES:    errno = EMFILE; break;
  437.             default:                    errno = EINVAL; break;
  438.         }
  439.         return -1;
  440.     }
  441. #else
  442.     FILEFINDBUF     fb;
  443.     HDIR            hdir;
  444.     USHORT            count = 1,
  445.                     err;
  446.  
  447.     /**** Get HDIR from reserved area,    ****/
  448.     memcpy(&hdir, ff->ff_reserved, sizeof(hdir));
  449.     err = DosFindNext(hdir, &fb, sizeof(fb), &count);
  450.     if(err NE 0)
  451.     {
  452.         switch(err)
  453.         {
  454.             case ERROR_PATH_NOT_FOUND:    errno = ENOENT; break;
  455.             case ERROR_NO_MORE_FILES:    errno = EMFILE; break;
  456.             default:                    errno = EINVAL; break;
  457.         }
  458.         return -1;
  459.     }
  460. #endif
  461.  
  462.     /**** Copy..    ****/
  463.     ff->ff_attrib    = (char)fb.attrFile;        /* Copy file attributes, */
  464.     memcpy(&ff->ff_ftime, &fb.ftimeLastWrite, sizeof(ff->ff_ftime));
  465.     memcpy(&ff->ff_fdate, &fb.fdateLastWrite, sizeof(ff->ff_fdate));
  466.     ff->ff_fsize    = fb.cbFile;
  467.     memcpy(ff->ff_name, fb.achName, sizeof(ff->ff_name));
  468.     return 0;
  469. }
  470.  
  471.  
  472. int getcurdir(int drive, char *dir)
  473. {
  474. #ifdef __32BITS__
  475.     ULONG    bytes;
  476.  
  477.     bytes    = 0;
  478.     DosQueryCurrentDir(drive, NULL, &bytes);
  479.     if(bytes > MAXPATH) return -1;
  480.     DosQCurDir(drive, dir, &bytes);
  481.     return 0;
  482. #else
  483.     USHORT    bytes;
  484.  
  485.     bytes    = 0;
  486.     DosQCurDir(drive, NULL, &bytes);
  487.     if(bytes > MAXPATH) return -1;
  488.     DosQCurDir(drive, dir, &bytes);
  489.     return 0;
  490. #endif
  491. }
  492.  
  493.  
  494. int getdisk(void)
  495. {
  496. #ifdef    __32BITS__
  497.     ULONG    drive, numdrives;
  498.  
  499.     DosQueryCurrentDisk(&drive, &numdrives);
  500. #else
  501.     USHORT    drive;
  502.     ULONG    numdrives;
  503.  
  504.     DosQCurDisk(&drive, &numdrives);
  505. #endif
  506.     return (int)drive - 1;
  507.  
  508. }
  509.  
  510.  
  511. int setdisk(int drive)
  512. {
  513. #if defined(__32BITS__)
  514.     return (int)DosSetDefaultDisk((ULONG)drive + 1);
  515. #else
  516.     return (int)DosSelectDisk((USHORT)drive + 1);  /* Bcc getdisk: 'A' = 0; MSC DosSelectdisk: 'A' = 1 */
  517. #endif
  518. }
  519.  
  520.  
  521. char *getpass(char *prompt)
  522. {
  523.     static char buf[20];
  524.     char        *p;
  525.  
  526.     puts(prompt);
  527.  
  528.     fgets(buf, 8, stdin);
  529.     for(p = buf; *p NE NUL && *p NE 0x0a && *p NE 0x0d; ) p++;
  530.     *p = NUL;
  531.  
  532.     return buf;
  533. }
  534.  
  535.  
  536. #define LK_UNLCK    0    /* unlock the file region */
  537. #define LK_LOCK     1    /* lock the file region */
  538. #define LK_NBLCK    2    /* non-blocking lock */
  539. #define LK_RLCK     3    /* lock for writing */
  540. #define LK_NBRLCK    4    /* non-blocking lock for writing */
  541.  
  542.  
  543. int locking(int h, int mode, long bytes)
  544. {
  545.     ULONG        errc;
  546.     FILELOCK    fl, fu;
  547.  
  548.     if(mode == LK_LOCK)
  549.     {
  550.         fl.lOffset    = tell(h);
  551.         fl.lRange    = bytes;
  552.         fu.lOffset    = 0;
  553.         fu.lRange    = 0;
  554.     }
  555.     else if(mode == LK_UNLCK)
  556.     {
  557.         fu.lOffset    = tell(h);
  558.         fu.lRange    = bytes;
  559.         fl.lOffset    = 0;
  560.         fl.lRange    = 0;
  561.     }
  562.     else
  563.     {
  564.         puts("Illegal LOCK mode");
  565.         exit(30);
  566.     }
  567.  
  568.     errc= DosSetFileLocks(h, &fu, &fl, 20000, 0);
  569.     if(errc != 0) return -1;
  570.     return 0;
  571. }
  572.  
  573.  
  574. #endif /* __OS2__ */
  575.  
  576.  
  577.  
  578. #if defined(__MSDOS__) && defined(__MSC__)
  579.  
  580. int getdisk(void)
  581. {
  582.     unsigned    drv;
  583.  
  584.     _dos_getdrive(&drv);
  585.     return drv - 1;
  586. }
  587.  
  588. int setdisk(int drive)
  589. {
  590.     unsigned        ndr;
  591.  
  592.     _dos_setdrive(drive +1, &ndr);
  593.     return 0;
  594. }
  595.  
  596.  
  597. #endif
  598.  
  599.  
  600.  
  601. FILE* fsopen(
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.