[3575] | 1 | /* $Id: kHlpGetEnvUZ.c 3608 2007-10-29 00:49:05Z bird $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * kHlpEnv - kHlpGetEnvUZ.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[3608] | 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
|
---|
[3575] | 8 | *
|
---|
| 9 | * This file is part of kStuff.
|
---|
| 10 | *
|
---|
| 11 | * kStuff is free software; you can redistribute it and/or
|
---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
---|
| 13 | * License as published by the Free Software Foundation; either
|
---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
---|
| 15 | *
|
---|
[3608] | 16 | * In addition to the permissions in the GNU Lesser General Public
|
---|
| 17 | * License, you are granted unlimited permission to link the compiled
|
---|
| 18 | * version of this file into combinations with other programs, and to
|
---|
| 19 | * distribute those combinations without any restriction coming from
|
---|
| 20 | * the use of this file.
|
---|
| 21 | *
|
---|
[3575] | 22 | * kStuff is distributed in the hope that it will be useful,
|
---|
| 23 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | * Lesser General Public License for more details.
|
---|
| 26 | *
|
---|
| 27 | * You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | * License along with kStuff; if not, write to the Free Software
|
---|
[3608] | 29 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
---|
| 30 | * 02110-1301, USA
|
---|
[3575] | 31 | */
|
---|
| 32 |
|
---|
| 33 | /*******************************************************************************
|
---|
| 34 | * Header Files *
|
---|
| 35 | *******************************************************************************/
|
---|
| 36 | #include <k/kHlpEnv.h>
|
---|
| 37 | #include <k/kHlpString.h>
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Gets an environment variable and converts it to a KSIZE.
|
---|
| 42 | *
|
---|
| 43 | * @returns 0 and *pcb on success.
|
---|
| 44 | * @returns On failure see kHlpGetEnv.
|
---|
| 45 | * @param pszVar The name of the variable.
|
---|
| 46 | * @param pcb Where to put the value.
|
---|
| 47 | */
|
---|
| 48 | KHLP_DECL(int) kHlpGetEnvUZ(const char *pszVar, KSIZE *pcb)
|
---|
| 49 | {
|
---|
| 50 | KSIZE cb;
|
---|
| 51 | unsigned uBase;
|
---|
| 52 | char szVal[64];
|
---|
| 53 | KSIZE cchVal = sizeof(szVal);
|
---|
| 54 | const char *psz;
|
---|
| 55 | int rc;
|
---|
| 56 |
|
---|
| 57 | *pcb = 0;
|
---|
| 58 | rc = kHlpGetEnv(pszVar, szVal, cchVal);
|
---|
| 59 | if (rc)
|
---|
| 60 | return rc;
|
---|
| 61 |
|
---|
| 62 | /* figure out the base. */
|
---|
| 63 | uBase = 10;
|
---|
| 64 | psz = szVal;
|
---|
| 65 | if ( *psz == '0'
|
---|
| 66 | && (psz[1] == 'x' || psz[1] == 'X'))
|
---|
| 67 | {
|
---|
| 68 | uBase = 16;
|
---|
| 69 | psz += 2;
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | /* convert it up to the first unknown char. */
|
---|
| 73 | cb = 0;
|
---|
| 74 | for(;;)
|
---|
| 75 | {
|
---|
| 76 | const char ch = *psz;
|
---|
| 77 | unsigned uDigit;
|
---|
| 78 | if (!ch)
|
---|
| 79 | break;
|
---|
| 80 | else if (ch >= '0' && ch <= '9')
|
---|
| 81 | uDigit = ch - '0';
|
---|
| 82 | else if (ch >= 'a' && ch <= 'z')
|
---|
| 83 | uDigit = ch - 'a' + 10;
|
---|
| 84 | else if (ch >= 'A' && ch <= 'Z')
|
---|
| 85 | uDigit = ch - 'A' + 10;
|
---|
| 86 | else
|
---|
| 87 | break;
|
---|
| 88 | if (uDigit >= uBase)
|
---|
| 89 | break;
|
---|
| 90 |
|
---|
| 91 | /* add the digit */
|
---|
| 92 | cb *= uBase;
|
---|
| 93 | cb += uDigit;
|
---|
| 94 |
|
---|
| 95 | psz++;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | /* check for unit */
|
---|
| 99 | if (*psz == 'm' || *psz == 'M')
|
---|
| 100 | cb *= 1024*1024;
|
---|
| 101 | else if (*psz == 'k' ||*psz == 'K')
|
---|
| 102 | cb *= 1024;
|
---|
| 103 | else if (*psz == 'g' || *psz == 'G')
|
---|
| 104 | cb *= 1024*1024*1024;
|
---|
| 105 |
|
---|
| 106 | *pcb = cb;
|
---|
| 107 | return 0;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|