Changeset 2867 for trunk/kLdr/kLdrHlp.c


Ignore:
Timestamp:
Nov 11, 2006, 10:33:17 AM (19 years ago)
Author:
bird
Message:

top half of the filesearching is done.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kLdr/kLdrHlp.c

    r2861 r2867  
    385385 * Get an environment variable.
    386386 *
    387  * @returns 0 on success, on failure an OS specific status code is returned.
     387 * @returns 0 on success.
     388 * @returns KLDR_ERR_BUFFER_OVERFLOW on if the buffer is too small (it'll be partially filled).
     389 * @returns KLDR_ERR_SYMBOL_NOT_FOUND if not found. (Yeah, abusing the status code, but it's only internally...)
     390 * @returns OS specfic status code on other error.
    388391 * @param   pszVar      The variable name.
    389  * @param   pszVal      Where to store the value. NULL is allowed if *pcchVal is 0.
    390  * @param   pcchVal     On input the size of the buffer pointed to by pszVal.
    391  *                      On output this contains the string length on success, while on
    392  *                      failure (including buffer overflow) the require buffer size.
    393  *                      If the variable wasn't found, it's set to 0.
    394  */
    395 int     kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t *pcchVal)
     392 * @param   pszVal      Where to store the value.
     393 * @param   cchVal      The size of the buffer pointed to by pszVal.
     394 */
     395int     kldrHlpGetEnv(const char *pszVar, char *pszVal, size_t cchVal)
    396396{
    397397#ifdef __OS2__
    398398    PSZ pszValue = NULL;
    399     int rc = DosScanEnv((PCSZ)pszVar, &pszValue);
     399    int rc;
     400
     401    *pszVal = '\0';
     402    rc = DosScanEnv((PCSZ)pszVar, &pszValue);
    400403    if (!rc)
    401404    {
    402405        size_t cch = kLdrHlpStrLen(pszValue);
    403         if (pszVal)
    404         {
    405             if (*pcchVal > cch)
    406             {
    407                 kLdrHlpMemCopy(pszVal, pszValue, cch + 1);
    408                 *pcchVal = cch;
    409             }
    410             else if (*pcchVal)
     406        if (cchVal > cch)
     407            kLdrHlpMemCopy(pszVal, pszValue, cch + 1);
     408        else
     409        {
     410            rc = KLDR_ERR_BUFFER_OVERFLOW;
     411            if (cchVal > 1)
    411412            {
    412413                kLdrHlpMemCopy(pszVal, pszValue, *pcchVal);
    413414                pszVal[*pcchVal - 1] = '\0';
    414                 rc = ERROR_BUFFER_OVERFLOW;
    415                 *pcchVal = cch + 1;
    416415            }
    417416        }
    418         else
    419         {
    420             rc = ERROR_BUFFER_OVERFLOW;
    421             *pcchVal = cch + 1;
    422         }
    423417    }
    424418    else
    425     {
    426         if (pszVal)
    427             *pszVal = '\0';
    428         *pcchVal = 0;
    429     }
    430 
     419        rc = KLDR_ERR_SYMBOL_NOT_FOUND;
    431420    return rc;
    432421
    433422#elif defined(__WIN__)
    434423    DWORD cch;
     424
    435425    SetLastError(0);
    436     cch = GetEnvironmentVariable(pszVar, pszVal, *pcchVal);
    437     if (cch)
    438     {
    439         *pcchVal = cch;
     426    cch = GetEnvironmentVariable(pszVar, pszVal, cchVal);
     427    if (cch < cchVal)
    440428        return 0;
    441     }
    442     if (!GetLastError() == ERROR_ENVVAR_NOT_FOUND)
    443     {
    444         *pcchVal = 0;
    445         return ERROR_ENVVAR_NOT_FOUND;
    446     }
    447     *pcchVal = cch;
    448     return ERROR_BUFFER_OVERFLOW;
     429
     430    *pszVal = '\0';
     431    if (cch >= cchVal)
     432        return KLDR_ERR_BUFFER_OVERFLOW;
     433    if (GetLastError() == ERROR_ENVVAR_NOT_FOUND)
     434        return KLDR_ERR_SYMBOL_NOT_FOUND;
     435    return GetLastError();
    449436
    450437#else
     
    458445 *
    459446 * @returns 0 and *pcb on success.
    460  *          Some non-zero OS or kLdr status code on failure.
     447 * @returns On failure see kldrHlpGetEnv.
    461448 * @param   pszVar  The name of the variable.
    462449 * @param   pcb     Where to put the value.
     
    472459
    473460    *pcb = 0;
    474     rc = kldrHlpGetEnv(pszVar, szVal,  &cchVal);
     461    rc = kldrHlpGetEnv(pszVar, szVal, cchVal);
    475462    if (rc)
    476463        return rc;
     
    602589
    603590
     591/**
     592 * Checks if this is only a filename or if it contains any kind
     593 * of drive, directory, or server specs.
     594 *
     595 * @returns 1 if this is a filename only.
     596 * @returns 0 of it's isn't only a filename.
     597 * @param   pszFilename     The filename to parse.
     598 */
     599int kldrHlpIsFilenameOnly(const char *pszFilename)
     600{
     601    const char *pszLast = NULL;
     602    for (;;)
     603    {
     604        const char ch = *pszFilename++;
     605#if defined(__OS2__) || defined(__WIN__)
     606        if (ch == '/' || ch == '\\' || ch == ':')
     607#else
     608        if (ch == '/')
     609#endif
     610            return 0;
     611        if (!ch)
     612            return 1;
     613    }
     614}
     615
    604616
    605617/**
Note: See TracChangeset for help on using the changeset viewer.