| 1 |
/*
|
| 2 |
* Copyright (C) 1999 Ross Combs (rocombs@cs.nmsu.edu)
|
| 3 |
*
|
| 4 |
* This program is free software; you can redistribute it and/or
|
| 5 |
* modify it under the terms of the GNU General Public License
|
| 6 |
* as published by the Free Software Foundation; either version 2
|
| 7 |
* of the License, or (at your option) any later version.
|
| 8 |
*
|
| 9 |
* This program is distributed in the hope that it will be useful,
|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
* GNU General Public License for more details.
|
| 13 |
*
|
| 14 |
* You should have received a copy of the GNU General Public License
|
| 15 |
* along with this program; if not, write to the Free Software
|
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
*/
|
| 18 |
#include "common/setup_before.h"
|
| 19 |
#ifndef HAVE_STRTOUL
|
| 20 |
|
| 21 |
#include "strtoul.h"
|
| 22 |
#include "common/setup_after.h"
|
| 23 |
|
| 24 |
|
| 25 |
extern unsigned long strtoul(char const * str, char * * endptr, int base)
|
| 26 |
{
|
| 27 |
unsigned int pos;
|
| 28 |
unsigned int i;
|
| 29 |
unsigned long val;
|
| 30 |
|
| 31 |
if (!str)
|
| 32 |
return 0; /* EINVAL */
|
| 33 |
|
| 34 |
for (pos=0; str[pos]==' ' || str[pos]=='\t'; pos++);
|
| 35 |
if (str[pos]=='-' || str[pos]=='+')
|
| 36 |
pos++;
|
| 37 |
if ((base==0 || base==16) && str[pos]=='0' && (str[pos+1]=='x' || str[pos+1]=='X'))
|
| 38 |
{
|
| 39 |
base = 16;
|
| 40 |
pos += 2; /* skip 0x prefix */
|
| 41 |
}
|
| 42 |
else if ((base==0 || base==8) && str[pos]=='0')
|
| 43 |
{
|
| 44 |
base = 8;
|
| 45 |
pos += 1;
|
| 46 |
}
|
| 47 |
else if (base==0)
|
| 48 |
{
|
| 49 |
base = 10;
|
| 50 |
}
|
| 51 |
|
| 52 |
if (base<2 || base>16) /* sorry, not complete emulation (should do up to 36) */
|
| 53 |
return 0; /* EINVAL */
|
| 54 |
|
| 55 |
val = 0;
|
| 56 |
for (i=pos; str[i]!='\0'; i++)
|
| 57 |
{
|
| 58 |
val *= base;
|
| 59 |
|
| 60 |
/* we could assume ASCII and subtraction but this isn't too hard */
|
| 61 |
if (str[i]=='0')
|
| 62 |
val += 0;
|
| 63 |
else if (str[i]=='1')
|
| 64 |
val += 1;
|
| 65 |
else if (base>2 && str[i]=='2')
|
| 66 |
val += 2;
|
| 67 |
else if (base>3 && str[i]=='3')
|
| 68 |
val += 3;
|
| 69 |
else if (base>4 && str[i]=='4')
|
| 70 |
val += 4;
|
| 71 |
else if (base>5 && str[i]=='5')
|
| 72 |
val += 5;
|
| 73 |
else if (base>6 && str[i]=='6')
|
| 74 |
val += 6;
|
| 75 |
else if (base>7 && str[i]=='7')
|
| 76 |
val += 7;
|
| 77 |
else if (base>8 && str[i]=='8')
|
| 78 |
val += 8;
|
| 79 |
else if (base>9 && str[i]=='9')
|
| 80 |
val += 9;
|
| 81 |
else if (base>10 && (str[i]=='a' || str[i]=='A'))
|
| 82 |
val += 10;
|
| 83 |
else if (base>11 && (str[i]=='b' || str[i]=='B'))
|
| 84 |
val += 11;
|
| 85 |
else if (base>12 && (str[i]=='c' || str[i]=='C'))
|
| 86 |
val += 12;
|
| 87 |
else if (base>13 && (str[i]=='d' || str[i]=='D'))
|
| 88 |
val += 13;
|
| 89 |
else if (base>14 && (str[i]=='e' || str[i]=='E'))
|
| 90 |
val += 14;
|
| 91 |
else if (base>15 && (str[i]=='f' || str[i]=='F'))
|
| 92 |
val += 15;
|
| 93 |
else
|
| 94 |
break;
|
| 95 |
}
|
| 96 |
|
| 97 |
if (endptr)
|
| 98 |
*endptr = (void *)&str[i]; /* avoid warning */
|
| 99 |
|
| 100 |
return val;
|
| 101 |
}
|
| 102 |
|
| 103 |
#else
|
| 104 |
typedef int filenotempty; /* make ISO standard happy */
|
| 105 |
#endif
|