Changeset 3710


Ignore:
Timestamp:
Mar 17, 2011, 5:22:45 PM (14 years ago)
Author:
bird
Message:

_getdcwd: When the buffer is NULL, the size is just a minimum. Refernces #194.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libc/src/libc/misc/_getdcwd.c

    r2254 r3710  
    5050 * @param   pszBuffer   Where to store the current directory of the drive.
    5151 *                      If NULL a buffer will be malloc'ed. The size of the malloc'ed
    52  *                      buffer will be cchBuffer and if cchBuffer is 0 it will be
    53  *                      as big as necessary.
    54  * @param   cchBuffer   Size of the buffer.
     52 *                      buffer will be at least @a cbBuffer bytes.
     53 * @param   cbBuffer    The size of the buffer @a pszBuffer points to.  If
     54                        @a pszbuffer is NULL, it specifies the minimum buffer
     55                        size of the allocated buffer.
    5556 */
    56 char *_getdcwd(int iDrive, char *pszBuffer, int cchBuffer)
     57char *_getdcwd(int iDrive, char *pszBuffer, int cbBuffer)
    5758{
    58     LIBCLOG_ENTER("iDrive=%d pszBuffer=%p cchBuffer=%d\n", iDrive, (void *)pszBuffer, cchBuffer);
     59    LIBCLOG_ENTER("iDrive=%d pszBuffer=%p cbBuffer=%d\n", iDrive, (void *)pszBuffer, cbBuffer);
    5960    char chDrive = iDrive ? iDrive + 'A' - 1 : 0;
    6061
     
    6263    if (!pszBuffer)
    6364    {
    64         LIBCLOG_MSG("Allocating buffer, %d bytes.\n", cchBuffer ? cchBuffer : PATH_MAX);
    65         size_t cch = cchBuffer ? cchBuffer : PATH_MAX;
    66         pszBuffer = malloc(cch);
     65        size_t cbAlloced = cbBuffer > PATH_MAX ? cbBuffer : PATH_MAX + 1;
     66        LIBCLOG_MSG("Allocating buffer, %zd bytes.\n", cbAlloced);
     67        pszBuffer = malloc(cbAlloced);
    6768        if (pszBuffer)
    6869        {
    69             rc = __libc_Back_fsDirCurrentGet(pszBuffer, cchBuffer, chDrive, 0);
     70            rc = __libc_Back_fsDirCurrentGet(pszBuffer, cbAlloced, chDrive, 0);
    7071            if (!rc)
    7172            {
    7273                /*
    73                  * Reallocate a PATH_MAX buffer before we return.
     74                 * Reallocate the buffer before we return?
    7475                 */
    75                 if (!cchBuffer)
     76                size_t cbReturned = strlen(pszBuffer) + 1;
     77                if (   cbReturned + 64 <= cbAlloced
     78                    && cbBuffer        <  (ssize_t)cbAlloced)
    7679                {
    7780                    char *pvOld = pszBuffer;
    78                     cch = strlen(pszBuffer) + 1;
    79                     pszBuffer = realloc(pszBuffer, cch > cchBuffer ? cch : cchBuffer);
    80                     if (!pszBuffer)
     81                    if (cbReturned <= cbBuffer)
     82                        cbReturned = cbBuffer + 1;
     83                    pszBuffer = realloc(pszBuffer, cbReturned);
     84                    if (pszBuffer)
     85                        cbAlloced = cbReturned;
     86                    else
    8187                        pszBuffer = pvOld;
    8288                }
    83                 LIBCLOG_RETURN_MSG(pszBuffer, "ret %p:{%s}\n", (void *)pszBuffer, pszBuffer);
     89                LIBCLOG_RETURN_MSG(pszBuffer, "ret %p:{%s} (%zu bytes)\n", (void *)pszBuffer, pszBuffer, cbAlloced);
    8490            }
     91
    8592            free(pszBuffer);
    8693        }
     
    9097    else
    9198    {
    92         rc = __libc_Back_fsDirCurrentGet(pszBuffer, cchBuffer, chDrive, 0);
     99        rc = __libc_Back_fsDirCurrentGet(pszBuffer, cbBuffer, chDrive, 0);
    93100        if (!rc)
    94101            LIBCLOG_RETURN_MSG(pszBuffer, "ret %p:{%s}\n", (void *)pszBuffer, pszBuffer);
Note: See TracChangeset for help on using the changeset viewer.