LCOV - code coverage report
Current view: top level - libc/stdlib - strtol.c (source / functions) Hit Total Coverage
Test: skiboot.info Lines: 37 38 97.4 %
Date: 2024-01-02 21:04:04 Functions: 1 1 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /******************************************************************************
       2                 :            :  * Copyright (c) 2004, 2008 IBM Corporation
       3                 :            :  * All rights reserved.
       4                 :            :  * This program and the accompanying materials
       5                 :            :  * are made available under the terms of the BSD License
       6                 :            :  * which accompanies this distribution, and is available at
       7                 :            :  * http://www.opensource.org/licenses/bsd-license.php
       8                 :            :  *
       9                 :            :  * Contributors:
      10                 :            :  *     IBM Corporation - initial implementation
      11                 :            :  *****************************************************************************/
      12                 :            : 
      13                 :            : #include <stdlib.h>
      14                 :            : 
      15                 :        105 : long int strtol(const char *S, char **PTR,int BASE)
      16                 :            : {
      17                 :        105 :         long rval = 0;
      18                 :        105 :         short int negative = 0;
      19                 :            :         short int digit;
      20                 :            :         // *PTR is S, unless PTR is NULL, in which case i override it with my own ptr
      21                 :            :         char* ptr;
      22                 :        105 :         if (PTR == NULL)
      23                 :            :         {
      24                 :            :                 //override
      25                 :        100 :                 PTR = &ptr;
      26                 :            :         }
      27                 :            :         // i use PTR to advance through the string
      28                 :        105 :         *PTR = (char *) S;
      29                 :            :         //check if BASE is ok
      30                 :        105 :         if ((BASE < 0) || BASE > 36)
      31                 :            :         {
      32                 :         10 :                 return 0;
      33                 :            :         }
      34                 :            :         // ignore white space at beginning of S
      35                 :         95 :         while ((**PTR == ' ')
      36                 :         95 :                         || (**PTR == '\t')
      37                 :         95 :                         || (**PTR == '\n')
      38                 :        205 :                         || (**PTR == '\r')
      39                 :            :                         )
      40                 :            :         {
      41                 :         15 :                 (*PTR)++;
      42                 :            :         }
      43                 :            :         // check if S starts with "-" in which case the return value is negative
      44                 :         95 :         if (**PTR == '-')
      45                 :            :         {
      46                 :         20 :                 negative = 1;
      47                 :         20 :                 (*PTR)++;
      48                 :            :         }
      49                 :            :         // if BASE is 0... determine the base from the first chars...
      50                 :         95 :         if (BASE == 0)
      51                 :            :         {
      52                 :            :                 // if S starts with "0x", BASE = 16, else 10
      53                 :         10 :                 if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
      54                 :            :                 {
      55                 :         10 :                         BASE = 16;
      56                 :            :                 }
      57                 :            :                 else
      58                 :            :                 {
      59                 :          0 :                         BASE = 10;
      60                 :            :                 }
      61                 :            :         }
      62                 :         95 :         if (BASE == 16)
      63                 :            :         {
      64                 :            :                 // S may start with "0x"
      65                 :         10 :                 if ((**PTR == '0') && (*((*PTR)+1) == 'x'))
      66                 :            :                 {
      67                 :         10 :                         (*PTR)++;
      68                 :         10 :                         (*PTR)++;
      69                 :            :                 }
      70                 :            :         }
      71                 :            :         //until end of string
      72                 :        375 :         while (**PTR)
      73                 :            :         {
      74                 :        315 :                 if (((**PTR) >= '0') && ((**PTR) <= '9'))
      75                 :            :                 {
      76                 :            :                         //digit (0..9)
      77                 :        275 :                         digit = **PTR - '0';
      78                 :            :                 }
      79                 :         40 :                 else if (((**PTR) >= 'a') && ((**PTR) <='z'))
      80                 :            :                 {
      81                 :            :                         //alphanumeric digit lowercase(a (10) .. z (35) )
      82                 :         25 :                         digit = (**PTR - 'a') + 10;
      83                 :            :                 }
      84                 :         15 :                 else if (((**PTR) >= 'A') && ((**PTR) <='Z'))
      85                 :            :                 {
      86                 :            :                         //alphanumeric digit uppercase(a (10) .. z (35) )
      87                 :          5 :                         digit = (**PTR - 'A') + 10;
      88                 :            :                 }
      89                 :            :                 else
      90                 :            :                 {
      91                 :            :                         //end of parseable number reached...
      92                 :            :                         break;
      93                 :            :                 }
      94                 :        305 :                 if (digit < BASE)
      95                 :            :                 {
      96                 :        280 :                         rval = (rval * BASE) + digit;
      97                 :            :                 }
      98                 :            :                 else
      99                 :            :                 {
     100                 :            :                         //digit found, but its too big for current base
     101                 :            :                         //end of parseable number reached...
     102                 :         25 :                         break;
     103                 :            :                 }
     104                 :            :                 //next...
     105                 :        280 :                 (*PTR)++;
     106                 :            :         }
     107                 :         95 :         if (negative)
     108                 :            :         {
     109                 :         20 :                 return rval * -1;
     110                 :            :         }
     111                 :            :         //else
     112                 :         75 :         return rval;
     113                 :            : }

Generated by: LCOV version 1.14