[2] | 1 | /* $Id: kHlpGetEnvUZ.c 29 2009-07-01 20:30:29Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kHlpEnv - kHlpGetEnvUZ.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[29] | 7 | * Copyright (c) 2006-2007 Knut St. Osmundsen <bird-kStuff-spamix@anduin.net>
|
---|
[2] | 8 | *
|
---|
[29] | 9 | * Permission is hereby granted, free of charge, to any person
|
---|
| 10 | * obtaining a copy of this software and associated documentation
|
---|
| 11 | * files (the "Software"), to deal in the Software without
|
---|
| 12 | * restriction, including without limitation the rights to use,
|
---|
| 13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
| 14 | * copies of the Software, and to permit persons to whom the
|
---|
| 15 | * Software is furnished to do so, subject to the following
|
---|
| 16 | * conditions:
|
---|
[2] | 17 | *
|
---|
[29] | 18 | * The above copyright notice and this permission notice shall be
|
---|
| 19 | * included in all copies or substantial portions of the Software.
|
---|
[2] | 20 | *
|
---|
[29] | 21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
| 22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
| 23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
| 24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
| 25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
| 26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
| 27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
| 28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
[2] | 29 | */
|
---|
| 30 |
|
---|
| 31 | /*******************************************************************************
|
---|
| 32 | * Header Files *
|
---|
| 33 | *******************************************************************************/
|
---|
| 34 | #include <k/kHlpEnv.h>
|
---|
| 35 | #include <k/kHlpString.h>
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | /**
|
---|
| 39 | * Gets an environment variable and converts it to a KSIZE.
|
---|
| 40 | *
|
---|
| 41 | * @returns 0 and *pcb on success.
|
---|
| 42 | * @returns On failure see kHlpGetEnv.
|
---|
| 43 | * @param pszVar The name of the variable.
|
---|
| 44 | * @param pcb Where to put the value.
|
---|
| 45 | */
|
---|
| 46 | KHLP_DECL(int) kHlpGetEnvUZ(const char *pszVar, KSIZE *pcb)
|
---|
| 47 | {
|
---|
| 48 | KSIZE cb;
|
---|
| 49 | unsigned uBase;
|
---|
| 50 | char szVal[64];
|
---|
| 51 | KSIZE cchVal = sizeof(szVal);
|
---|
| 52 | const char *psz;
|
---|
| 53 | int rc;
|
---|
| 54 |
|
---|
| 55 | *pcb = 0;
|
---|
| 56 | rc = kHlpGetEnv(pszVar, szVal, cchVal);
|
---|
| 57 | if (rc)
|
---|
| 58 | return rc;
|
---|
| 59 |
|
---|
| 60 | /* figure out the base. */
|
---|
| 61 | uBase = 10;
|
---|
| 62 | psz = szVal;
|
---|
| 63 | if ( *psz == '0'
|
---|
| 64 | && (psz[1] == 'x' || psz[1] == 'X'))
|
---|
| 65 | {
|
---|
| 66 | uBase = 16;
|
---|
| 67 | psz += 2;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /* convert it up to the first unknown char. */
|
---|
| 71 | cb = 0;
|
---|
| 72 | for(;;)
|
---|
| 73 | {
|
---|
| 74 | const char ch = *psz;
|
---|
| 75 | unsigned uDigit;
|
---|
| 76 | if (!ch)
|
---|
| 77 | break;
|
---|
| 78 | else if (ch >= '0' && ch <= '9')
|
---|
| 79 | uDigit = ch - '0';
|
---|
| 80 | else if (ch >= 'a' && ch <= 'z')
|
---|
| 81 | uDigit = ch - 'a' + 10;
|
---|
| 82 | else if (ch >= 'A' && ch <= 'Z')
|
---|
| 83 | uDigit = ch - 'A' + 10;
|
---|
| 84 | else
|
---|
| 85 | break;
|
---|
| 86 | if (uDigit >= uBase)
|
---|
| 87 | break;
|
---|
| 88 |
|
---|
| 89 | /* add the digit */
|
---|
| 90 | cb *= uBase;
|
---|
| 91 | cb += uDigit;
|
---|
| 92 |
|
---|
| 93 | psz++;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | /* check for unit */
|
---|
| 97 | if (*psz == 'm' || *psz == 'M')
|
---|
| 98 | cb *= 1024*1024;
|
---|
| 99 | else if (*psz == 'k' ||*psz == 'K')
|
---|
| 100 | cb *= 1024;
|
---|
| 101 | else if (*psz == 'g' || *psz == 'G')
|
---|
| 102 | cb *= 1024*1024*1024;
|
---|
| 103 |
|
---|
| 104 | *pcb = cb;
|
---|
| 105 | return 0;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 |
|
---|