OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [uclinux/] [uC-libc/] [misc/] [strtol.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * strtol.c - This file is part of the libc-8086 package for ELKS,
3
 * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
4
 *
5
 *  This library is free software; you can redistribute it and/or
6
 *  modify it under the terms of the GNU Library General Public
7
 *  License as published by the Free Software Foundation; either
8
 *  version 2 of the License, or (at your option) any later version.
9
 *
10
 *  This library is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 *  Library General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Library General Public
16
 *  License along with this library; if not, write to the Free
17
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 *
19
 */
20
 
21
#include <ctype.h>
22
#include <stdlib.h>
23
 
24
long int
25
strtol(const char *nptr, char **endptr, int base)
26
{
27
  const char * ptr;
28
  unsigned short negative;
29
  long int number;
30
 
31
  ptr=nptr;
32
 
33
  while (isspace(*ptr))
34
    ptr++;
35
 
36
  negative=0;
37
  if (*ptr=='-')
38
    negative=1;
39
 
40
  number=(long int)strtoul(nptr, endptr, base);
41
 
42
  return (negative ? -number:number);
43
}
44
 
45
unsigned long int
46
strtoul(const char *nptr, char **endptr, int base)
47
{
48
  unsigned long int number;
49
 
50
  /* Sanity check the arguments */
51
  if (base==1 || base>36 || base<0)
52
    base=0;
53
 
54
  /* advance beyond any leading whitespace */
55
  while (isspace(*nptr))
56
    nptr++;
57
 
58
  /* check for optional '+' or '-' */
59
  if (*nptr=='-')
60
      nptr++;
61
  else
62
    if (*nptr=='+')
63
      nptr++;
64
 
65
  /* If base==0 and the string begins with "0x" then we're supposed
66
     to assume that it's hexadecimal (base 16). */
67
  else
68
    if (base==0 && *nptr=='0')
69
      if (toupper(*(nptr+1))=='X')
70
        {
71
          base=16;
72
          nptr+=2;
73
        }
74
  /* If base==0 and the string begins with "0" but not "0x",
75
     then we're supposed to assume that it's octal (base 8). */
76
      else
77
        {
78
          base=8;
79
          nptr++;
80
        }
81
 
82
 
83
  /* If base is still 0 (it was 0 to begin with and the string didn't begin
84
     with "0"), then we are supposed to assume that it's base 10 */
85
  if (base==0)
86
    base=10;
87
 
88
  number=0;
89
  while (isalnum(*nptr))
90
    {
91
      number= (number*base)+((isalpha(*nptr) ? toupper(*nptr) : *nptr)
92
                             - (isdigit(*nptr) ? '0' : 'A' - 10));
93
      nptr++;
94
 
95
      /*if (*nptr>'0'+base)
96
        break;*/
97
    }
98
 
99
  /* Some code is simply _impossible_ to write with -Wcast-qual .. :-\ */
100
  if (endptr!=NULL)
101
    *endptr=(char *)nptr;
102
 
103
  /* All done */
104
  return number;
105
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.