Ignore:
Timestamp:
Oct 23, 2001, 11:25:46 PM (24 years ago)
Author:
umoeller
Message:

Misc updates.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/helpers/dosh.c

    r108 r113  
    5151#define INCL_DOSSESMGR
    5252#define INCL_DOSQUEUES
     53#define INCL_DOSSEMAPHORES
    5354#define INCL_DOSMISC
    5455#define INCL_DOSDEVICES
     
    6263#include <string.h>
    6364#include <stdio.h>
     65#include <stdarg.h>
    6466
    6567#include "setup.h"                      // code generation and debugging options
    6668
    6769#include "helpers\dosh.h"
     70#include "helpers\standards.h"
    6871
    6972#pragma hdrstop
     
    286289 *
    287290 ********************************************************************/
     291
     292/*
     293 *@@ Allocate:
     294 *      wrapper around malloc() which automatically
     295 *      sets ERROR_NOT_ENOUGH_MEMORY.
     296 *
     297 *@@added V0.9.16 (2001-10-19) [umoeller]
     298 */
     299
     300PVOID doshMalloc(ULONG cb,
     301                 APIRET *parc)
     302{
     303    PVOID pv;
     304    *parc = NO_ERROR;
     305    if (!(pv = malloc(cb)))
     306        *parc = ERROR_NOT_ENOUGH_MEMORY;
     307
     308    return (pv);
     309}
    288310
    289311/*
     
    12811303 *      Use doshQueryPathSize to query the size of
    12821304 *      any file.
    1283  */
    1284 
    1285 ULONG doshQueryFileSize(HFILE hFile)
    1286 {
     1305 *
     1306 *@@changed V0.9.16 (2001-10-19) [umoeller]: now returning APIRET
     1307 */
     1308
     1309APIRET doshQueryFileSize(HFILE hFile,
     1310                         PULONG pulSize)
     1311{
     1312    APIRET arc;
    12871313    FILESTATUS3 fs3;
    1288     if (DosQueryFileInfo(hFile, FIL_STANDARD, &fs3, sizeof(fs3)))
    1289         return (0);
    1290     else
    1291         return (fs3.cbFile);
     1314    if (!(arc = DosQueryFileInfo(hFile, FIL_STANDARD, &fs3, sizeof(fs3))))
     1315        *pulSize = fs3.cbFile;
     1316    return (arc);
    12921317}
    12931318
     
    12991324 *      Use doshQueryFileSize instead to query the
    13001325 *      size if you have a HFILE.
    1301  */
    1302 
    1303 ULONG doshQueryPathSize(PSZ pszFile)
    1304 {
     1326 *
     1327 *@@changed V0.9.16 (2001-10-19) [umoeller]: now returning APIRET
     1328 */
     1329
     1330APIRET doshQueryPathSize(PCSZ pcszFile,
     1331                         PULONG pulSize)
     1332{
     1333    APIRET arc;
    13051334    FILESTATUS3 fs3;
    1306     if (DosQueryPathInfo(pszFile, FIL_STANDARD, &fs3, sizeof(fs3)))
    1307         return (0);
    1308     else
    1309         return (fs3.cbFile);
     1335    if (!(arc = DosQueryPathInfo((PSZ)pcszFile, FIL_STANDARD, &fs3, sizeof(fs3))))
     1336        *pulSize = fs3.cbFile;
     1337    return (arc);
    13101338}
    13111339
     
    14991527
    15001528/*
     1529 * doshOpen:
     1530 *      wrapper around DosOpen for simpler opening
     1531 *      of files.
     1532 *
     1533 *      ulOpenMode determines the mode to open the
     1534 *      file in:
     1535 *
     1536 +      +-------------------------+------+-----------+-----------+
     1537 +      |                         |      |           |           |
     1538 +      |  ulOpenMode             | mode | if exists | if new    |
     1539 +      +-------------------------+------+-----------+-----------+
     1540 +      |  XOPEN_READ_EXISTING    | read | opens     | fails     |
     1541 +      +-------------------------+------+-----------+-----------+
     1542 +      |  XOPEN_READWRITE_APPEND | r/w  | opens,    | creates   |
     1543 +      |                         |      | appends   |           |
     1544 +      +-------------------------+------+-----------+-----------+
     1545 +      |  XOPEN_READWRITE_NEW    | r/w  | replaces  | creates   |
     1546 +      +-------------------------+------+-----------+-----------+
     1547 *
     1548 *      *ppFile receives a new XFILE structure describing
     1549 *      the open file, if NO_ERROR is returned.
     1550 *
     1551 *@@added V0.9.16 (2001-10-19) [umoeller]
     1552 */
     1553
     1554APIRET doshOpen(const char *pcszFilename,   // in: filename to open
     1555                ULONG ulOpenMode,       // in: XOPEN_* mode
     1556                PULONG pcbFile,         // in: new file size (if new file is created)
     1557                                        // out: file size
     1558                PXFILE *ppFile)
     1559{
     1560    APIRET arc = NO_ERROR;
     1561
     1562    ULONG   fsOpenFlags = 0,
     1563            fsOpenMode =    OPEN_FLAGS_FAIL_ON_ERROR
     1564                          | OPEN_FLAGS_NO_LOCALITY
     1565                          | OPEN_FLAGS_NOINHERIT;
     1566
     1567    switch (ulOpenMode)
     1568    {
     1569        case XOPEN_READ_EXISTING:
     1570            fsOpenFlags =   OPEN_ACTION_FAIL_IF_NEW
     1571                          | OPEN_ACTION_OPEN_IF_EXISTS;
     1572            fsOpenMode |=   OPEN_SHARE_DENYWRITE
     1573                          | OPEN_ACCESS_READONLY;
     1574        break;
     1575
     1576        case XOPEN_READWRITE_APPEND:
     1577            fsOpenFlags =   OPEN_ACTION_CREATE_IF_NEW
     1578                          | OPEN_ACTION_OPEN_IF_EXISTS;
     1579            fsOpenMode |=   OPEN_SHARE_DENYREADWRITE
     1580                          | OPEN_ACCESS_READWRITE;
     1581        break;
     1582
     1583        case XOPEN_READWRITE_NEW:
     1584            fsOpenFlags =   OPEN_ACTION_CREATE_IF_NEW
     1585                          | OPEN_ACTION_REPLACE_IF_EXISTS;
     1586            fsOpenMode |=   OPEN_SHARE_DENYREADWRITE
     1587                          | OPEN_ACCESS_READWRITE;
     1588        break;
     1589    }
     1590
     1591    if (pcszFilename && fsOpenFlags && pcbFile && ppFile)
     1592    {
     1593        PXFILE pFile;
     1594        if (pFile = NEW(XFILE))
     1595        {
     1596            ULONG ulAction;
     1597
     1598            ZERO(pFile);
     1599
     1600            if (!(arc = DosOpen((PSZ)pcszFilename,
     1601                                &pFile->hf,
     1602                                &ulAction,
     1603                                *pcbFile,
     1604                                FILE_ARCHIVED,
     1605                                fsOpenFlags,
     1606                                fsOpenMode,
     1607                                NULL)))       // EAs
     1608            {
     1609                // alright, got the file:
     1610
     1611                if (    (ulAction == FILE_EXISTED)
     1612                     && (ulOpenMode == XOPEN_READWRITE_APPEND)
     1613                   )
     1614                    // get its size and set ptr to end for append
     1615                    arc = DosSetFilePtr(pFile->hf,
     1616                                        0,
     1617                                        FILE_END,
     1618                                        pcbFile);
     1619                else
     1620                    arc = doshQueryFileSize(pFile->hf,
     1621                                            pcbFile);
     1622                    // file ptr is at beginning
     1623
     1624                // store file size
     1625                pFile->cbInitial
     1626                = pFile->cbCurrent
     1627                = *pcbFile;
     1628            }
     1629
     1630            if (arc)
     1631                doshClose(&pFile);
     1632            else
     1633                *ppFile = pFile;
     1634        }
     1635        else
     1636            arc = ERROR_NOT_ENOUGH_MEMORY;
     1637    }
     1638    else
     1639        arc = ERROR_INVALID_PARAMETER;
     1640
     1641    return (arc);
     1642}
     1643
     1644/*
     1645 *@@ doshLockFile:
     1646 *
     1647 *@@added V0.9.16 (2001-10-19) [umoeller]
     1648 */
     1649
     1650APIRET doshLockFile(PXFILE pFile)
     1651{
     1652    if (!pFile)
     1653        return (ERROR_INVALID_PARAMETER);
     1654
     1655    if (!pFile->hmtx)
     1656        // first call:
     1657        return (DosCreateMutexSem(NULL,
     1658                                  &pFile->hmtx,
     1659                                  0,
     1660                                  TRUE));        // request!
     1661
     1662    return (DosRequestMutexSem(pFile->hmtx, SEM_INDEFINITE_WAIT));
     1663}
     1664
     1665/*
     1666 *@@ doshUnlockFile:
     1667 *
     1668 *@@added V0.9.16 (2001-10-19) [umoeller]
     1669 */
     1670
     1671APIRET doshUnlockFile(PXFILE pFile)
     1672{
     1673    if (pFile)
     1674        return (DosReleaseMutexSem(pFile->hmtx));
     1675
     1676    return (ERROR_INVALID_PARAMETER);
     1677}
     1678
     1679/*
     1680 *@@ doshWrite:
     1681 *      writes the specified data to the file.
     1682 *      If (cb == 0), this runs strlen on pcsz
     1683 *      to find out the length.
     1684 *
     1685 *@@added V0.9.16 (2001-10-19) [umoeller]
     1686 */
     1687
     1688APIRET doshWrite(PXFILE pFile,
     1689                 PCSZ pcsz,
     1690                 ULONG cb)
     1691{
     1692    APIRET arc;
     1693    if (!pcsz)
     1694        arc = ERROR_INVALID_PARAMETER;
     1695    else
     1696    {
     1697        if (!cb)
     1698            cb = strlen(pcsz);
     1699
     1700        if (!cb)
     1701            arc = ERROR_INVALID_PARAMETER;
     1702        else
     1703            if (!(arc = doshLockFile(pFile)))   // this checks for pFile
     1704            {
     1705                ULONG cbWritten;
     1706                if (!(arc = DosWrite(pFile->hf,
     1707                                     (PSZ)pcsz,
     1708                                     cb,
     1709                                     &cbWritten)))
     1710                    pFile->cbCurrent += cbWritten;
     1711
     1712                doshUnlockFile(pFile);
     1713            }
     1714    }
     1715
     1716    return (arc);
     1717}
     1718
     1719/*
     1720 *@@ doshWriteLogEntry
     1721 *      writes a log string to an XFILE, adding a
     1722 *      leading timestamp before the line.
     1723 *
     1724 *      The internal string buffer is limited to 2000
     1725 *      characters. \n is NOT translated to \r\n before
     1726 *      writing.
     1727 */
     1728
     1729APIRET doshWriteLogEntry(PXFILE pFile,
     1730                         const char* pcszFormat,
     1731                         ...)
     1732{
     1733    APIRET arc;
     1734
     1735    DATETIME dt;
     1736    CHAR szTemp[2000];
     1737    ULONG   ulLength;
     1738
     1739    DosGetDateTime(&dt);
     1740    if (ulLength = sprintf(szTemp,
     1741                           "%04d-%02d-%02d %02d:%02d:%02d:%02d ",
     1742                           dt.year, dt.month, dt.day,
     1743                           dt.hours, dt.minutes, dt.seconds, dt.hundredths))
     1744    {
     1745        if (!(arc = doshWrite(pFile,
     1746                              szTemp,
     1747                              ulLength)))
     1748        {
     1749            va_list arg_ptr;
     1750            va_start(arg_ptr, pcszFormat);
     1751            ulLength = vsprintf(szTemp, pcszFormat, arg_ptr);
     1752            va_end(arg_ptr);
     1753
     1754            szTemp[ulLength++] = '\r';
     1755            szTemp[ulLength++] = '\n';
     1756
     1757            arc = doshWrite(pFile,
     1758                            (PVOID)szTemp,
     1759                            ulLength);
     1760        }
     1761    }
     1762
     1763    return (arc);
     1764}
     1765
     1766/*
     1767 * doshClose:
     1768 *
     1769 *@@added V0.9.16 (2001-10-19) [umoeller]
     1770 */
     1771
     1772APIRET doshClose(PXFILE *ppFile)
     1773{
     1774    APIRET arc = NO_ERROR;
     1775    PXFILE pFile;
     1776
     1777    if (    (ppFile)
     1778         && (pFile = *ppFile)
     1779       )
     1780    {
     1781        // request the mutex so that we won't be
     1782        // taking the file away under someone's butt
     1783        if (!(arc = doshLockFile(pFile)))
     1784        {
     1785            HMTX hmtx = pFile->hmtx;
     1786            pFile->hmtx = NULLHANDLE;
     1787
     1788            // now that the file is locked,
     1789            // set the ptr to NULL
     1790            *ppFile = NULL;
     1791
     1792            if (pFile->hf)
     1793            {
     1794                DosSetFileSize(pFile->hf, pFile->cbCurrent);
     1795                DosClose(pFile->hf);
     1796                pFile->hf = NULLHANDLE;
     1797            }
     1798
     1799            doshUnlockFile(pFile);
     1800            DosCloseMutexSem(pFile->hmtx);
     1801        }
     1802
     1803        free(pFile);
     1804    }
     1805    else
     1806        arc = ERROR_INVALID_PARAMETER;
     1807
     1808    return (arc);
     1809}
     1810
     1811/*
    15011812 *@@ doshLoadTextFile:
    15021813 *      reads a text file from disk, allocates memory
     
    15211832    PSZ     pszContent = NULL;
    15221833
    1523     APIRET arc = DosOpen((PSZ)pcszFile,
    1524                          &hFile,
    1525                          &ulAction,                      // action taken
    1526                          5000L,                          // primary allocation size
    1527                          FILE_ARCHIVED | FILE_NORMAL,    // file attribute
    1528                          OPEN_ACTION_OPEN_IF_EXISTS,     // open flags
    1529                          OPEN_FLAGS_NOINHERIT
    1530                             | OPEN_SHARE_DENYNONE
    1531                             | OPEN_ACCESS_READONLY,      // read-only mode
    1532                          NULL);                          // no EAs
    1533 
    1534     if (arc == NO_ERROR)
    1535     {
    1536         ulSize = doshQueryFileSize(hFile);
    1537         pszContent = (PSZ)malloc(ulSize+1);
    1538         arc = DosSetFilePtr(hFile,
    1539                             0L,
    1540                             FILE_BEGIN,
    1541                             &ulLocal);
    1542         arc = DosRead(hFile,
    1543                       pszContent,
    1544                       ulSize,
    1545                       &ulBytesRead);
     1834    APIRET arc;
     1835
     1836    *ppszContent = 0;
     1837
     1838    if (!(arc = DosOpen((PSZ)pcszFile,
     1839                        &hFile,
     1840                        &ulAction,                      // action taken
     1841                        5000L,                          // primary allocation size
     1842                        FILE_ARCHIVED | FILE_NORMAL,    // file attribute
     1843                        OPEN_ACTION_OPEN_IF_EXISTS,     // open flags
     1844                        OPEN_FLAGS_NOINHERIT
     1845                           | OPEN_SHARE_DENYNONE
     1846                           | OPEN_ACCESS_READONLY,      // read-only mode
     1847                        NULL)))                         // no EAs
     1848    {
     1849        if (!(arc = doshQueryFileSize(hFile, &ulSize)))
     1850        {
     1851            pszContent = (PSZ)malloc(ulSize+1);
     1852
     1853            if (!(arc = DosSetFilePtr(hFile,
     1854                                      0L,
     1855                                      FILE_BEGIN,
     1856                                      &ulLocal)))
     1857                if (!(arc = DosRead(hFile,
     1858                                    pszContent,
     1859                                    ulSize,
     1860                                    &ulBytesRead)))
     1861                {
     1862                    *(pszContent+ulBytesRead) = 0;
     1863                    // set output buffer pointer
     1864                    *ppszContent = pszContent;
     1865                }
     1866
     1867            if (arc)
     1868                free(pszContent);
     1869        }
    15461870        DosClose(hFile);
    1547         *(pszContent+ulBytesRead) = 0;
    1548 
    1549         // set output buffer pointer
    1550         *ppszContent = pszContent;
    1551     }
    1552     else
    1553         *ppszContent = 0;
     1871    }
    15541872
    15551873    return (arc);
     
    15771895    ULONG   ulCount = 1;
    15781896    CHAR    szCount[5];
     1897    ULONG   ulDummy;
    15791898
    15801899    strcpy(szFilename, pszExisting);
     
    15881907        strcpy(pszLastDot, szCount);
    15891908        ulCount++;
    1590     } while (doshQueryPathSize(szFilename) != 0);
     1909    } while (!doshQueryPathSize(szFilename, &ulDummy));
    15911910
    15921911    return (strdup(szFilename));
     
    18032122        }
    18042123
    1805         arc = DosOpen((PSZ)pszFile,
    1806                       &hFile,
    1807                       &ulAction,                      // action taken
    1808                       ulSize,                         // primary allocation size
    1809                       FILE_ARCHIVED | FILE_NORMAL,    // file attribute
    1810                       OPEN_ACTION_CREATE_IF_NEW
    1811                          | OPEN_ACTION_REPLACE_IF_EXISTS,  // open flags
    1812                       OPEN_FLAGS_NOINHERIT
    1813                          | OPEN_FLAGS_SEQUENTIAL         // sequential, not random access
    1814                          | OPEN_SHARE_DENYWRITE          // deny write mode
    1815                          | OPEN_ACCESS_WRITEONLY,        // write mode
    1816                       NULL);                          // no EAs
    1817 
    1818         if (arc == NO_ERROR)
     2124        if (!(arc = DosOpen((PSZ)pszFile,
     2125                            &hFile,
     2126                            &ulAction,                      // action taken
     2127                            ulSize,                         // primary allocation size
     2128                            FILE_ARCHIVED | FILE_NORMAL,    // file attribute
     2129                            OPEN_ACTION_CREATE_IF_NEW
     2130                               | OPEN_ACTION_REPLACE_IF_EXISTS,  // open flags
     2131                            OPEN_FLAGS_NOINHERIT
     2132                               | OPEN_FLAGS_SEQUENTIAL         // sequential, not random access
     2133                               | OPEN_SHARE_DENYWRITE          // deny write mode
     2134                               | OPEN_ACCESS_WRITEONLY,        // write mode
     2135                            NULL)))                         // no EAs
    18192136        {
    1820             arc = DosSetFilePtr(hFile,
    1821                                 0L,
    1822                                 FILE_BEGIN,
    1823                                 &ulLocal);
    1824             if (arc == NO_ERROR)
    1825             {
    1826                 arc = DosWrite(hFile,
    1827                                (PVOID)pszContent,
    1828                                ulSize,
    1829                                &ulWritten);
    1830                 if (arc == NO_ERROR)
     2137            if (!(arc = DosSetFilePtr(hFile,
     2138                                      0L,
     2139                                      FILE_BEGIN,
     2140                                      &ulLocal)))
     2141                if (!(arc = DosWrite(hFile,
     2142                                     (PVOID)pszContent,
     2143                                     ulSize,
     2144                                     &ulWritten)))
    18312145                    arc = DosSetFileSize(hFile, ulSize);
    1832             }
    18332146
    18342147            DosClose(hFile);
     
    18402153
    18412154    return (arc);
    1842 }
    1843 
    1844 /*
    1845  *@@ doshOpenLogFile:
    1846  *      this opens a log file in the root directory of
    1847  *      the boot drive; it is titled pszFilename, and
    1848  *      the file handle is returned.
    1849  */
    1850 
    1851 HFILE doshOpenLogFile(const char* pcszFilename)
    1852 {
    1853     APIRET  rc;
    1854     CHAR    szFileName[CCHMAXPATH];
    1855     HFILE   hfLog;
    1856     ULONG   ulAction;
    1857     ULONG   ibActual;
    1858 
    1859     sprintf(szFileName, "%c:\\%s", doshQueryBootDrive(), pcszFilename);
    1860     rc = DosOpen(szFileName,
    1861                  &hfLog,
    1862                  &ulAction,
    1863                  0,             // file size
    1864                  FILE_NORMAL,
    1865                  OPEN_ACTION_CREATE_IF_NEW | OPEN_ACTION_OPEN_IF_EXISTS,
    1866                  OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYWRITE,
    1867                  (PEAOP2)NULL);
    1868     if (rc == NO_ERROR)
    1869     {
    1870         DosSetFilePtr(hfLog, 0, FILE_END, &ibActual);
    1871         return (hfLog);
    1872     }
    1873     else
    1874         return (0);
    1875 }
    1876 
    1877 /*
    1878  * doshWriteToLogFile
    1879  *      writes a string to a log file, adding a
    1880  *      leading timestamp.
    1881  */
    1882 
    1883 APIRET doshWriteToLogFile(HFILE hfLog, const char* pcsz)
    1884 {
    1885     if (hfLog)
    1886     {
    1887         DATETIME dt;
    1888         CHAR szTemp[2000];
    1889         ULONG   cbWritten;
    1890         DosGetDateTime(&dt);
    1891         sprintf(szTemp,
    1892                 "Time: %02d:%02d:%02d %s",
    1893                 dt.hours, dt.minutes, dt.seconds,
    1894                 pcsz);
    1895         return (DosWrite(hfLog, (PVOID)szTemp, strlen(szTemp), &cbWritten));
    1896     }
    1897     else return (ERROR_INVALID_HANDLE);
    18982155}
    18992156
Note: See TracChangeset for help on using the changeset viewer.