LCOV - code coverage report
Current view: top level - lib/libc/minimal/source/stdlib - strtoll.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 39 0.0 %
Date: 2022-08-18 11:36:24 Functions: 0 0 -
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 48 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: BSD-4-Clause-UC */
       2                 :            : /*-
       3                 :            :  * Copyright (c) 1990, 1993
       4                 :            :  *      The Regents of the University of California.  All rights reserved.
       5                 :            :  *
       6                 :            :  * Redistribution and use in source and binary forms, with or without
       7                 :            :  * modification, are permitted provided that the following conditions
       8                 :            :  * are met:
       9                 :            :  * 1. Redistributions of source code must retain the above copyright
      10                 :            :  *    notice, this list of conditions and the following disclaimer.
      11                 :            :  * 2. Redistributions in binary form must reproduce the above copyright
      12                 :            :  *    notice, this list of conditions and the following disclaimer in the
      13                 :            :  *    documentation and/or other materials provided with the distribution.
      14                 :            :  * 3. All advertising materials mentioning features or use of this software
      15                 :            :  *    must display the following acknowledgment:
      16                 :            :  *      This product includes software developed by the University of
      17                 :            :  *      California, Berkeley and its contributors.
      18                 :            :  * 4. Neither the name of the University nor the names of its contributors
      19                 :            :  *    may be used to endorse or promote products derived from this software
      20                 :            :  *    without specific prior written permission.
      21                 :            :  *
      22                 :            :  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
      23                 :            :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      24                 :            :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      25                 :            :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
      26                 :            :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      27                 :            :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      28                 :            :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      29                 :            :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      30                 :            :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      31                 :            :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      32                 :            :  * SUCH DAMAGE.
      33                 :            :  */
      34                 :            : #include <limits.h>
      35                 :            : #include <ctype.h>
      36                 :            : #include <errno.h>
      37                 :            : #include <stdlib.h>
      38                 :            : 
      39                 :            : /*
      40                 :            :  * Convert a string to a long long integer.
      41                 :            :  *
      42                 :            :  * Ignores `locale' stuff.  Assumes that the upper and lower case
      43                 :            :  * alphabets and digits are each contiguous.
      44                 :            :  */
      45                 :            : long long strtoll(const char *nptr, char **endptr, register int base)
      46                 :            : {
      47                 :          0 :         register const char *s = nptr;
      48                 :            :         register unsigned long long acc;
      49                 :            :         register int c;
      50                 :            :         register unsigned long long cutoff;
      51                 :          0 :         register int neg = 0, any, cutlim;
      52                 :            : 
      53                 :            :         /*
      54                 :            :          * See strtol for comments as to the logic used.
      55                 :            :          */
      56                 :            :         do {
      57                 :          0 :                 c = *s++;
      58         [ #  # ]:          0 :         } while (isspace(c));
      59         [ #  # ]:          0 :         if (c == '-') {
      60                 :          0 :                 neg = 1;
      61                 :          0 :                 c = *s++;
      62         [ #  # ]:          0 :         } else if (c == '+') {
      63                 :          0 :                 c = *s++;
      64                 :            :         }
      65                 :            : 
      66   [ #  #  #  #  :          0 :         if ((base == 0 || base == 16) && c == '0' && (*s == 'x' || *s == 'X')) {
          #  #  #  #  #  
                      # ]
      67                 :          0 :                 c = s[1];
      68                 :          0 :                 s += 2;
      69                 :          0 :                 base = 16;
      70                 :            :         }
      71                 :            : 
      72         [ #  # ]:          0 :         if (base == 0) {
      73         [ #  # ]:          0 :                 base = c == '0' ? 8 : 10;
      74                 :            :         }
      75                 :            : 
      76         [ #  # ]:          0 :         cutoff = neg ? -(unsigned long long)LLONG_MIN : LLONG_MAX;
      77                 :          0 :         cutlim = cutoff % (unsigned long long)base;
      78                 :          0 :         cutoff /= (unsigned long long)base;
      79                 :          0 :         for (acc = 0, any = 0;; c = *s++) {
      80         [ #  # ]:          0 :                 if (isdigit(c)) {
      81                 :          0 :                         c -= '0';
      82         [ #  # ]:          0 :                 } else if (isalpha(c)) {
      83         [ #  # ]:          0 :                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
      84                 :            :                 } else {
      85                 :          0 :                         break;
      86                 :            :                 }
      87         [ #  # ]:          0 :                 if (c >= base) {
      88                 :          0 :                         break;
      89                 :            :                 }
      90   [ #  #  #  #  :          0 :                 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) {
             #  #  #  # ]
      91                 :          0 :                         any = -1;
      92                 :            :                 } else {
      93                 :          0 :                         any = 1;
      94                 :          0 :                         acc *= base;
      95                 :          0 :                         acc += c;
      96                 :            :                 }
      97                 :            :         }
      98                 :            : 
      99         [ #  # ]:          0 :         if (any < 0) {
     100         [ #  # ]:          0 :                 acc = neg ? LLONG_MIN : LLONG_MAX;
     101                 :          0 :                 errno = ERANGE;
     102         [ #  # ]:          0 :         } else if (neg) {
     103                 :          0 :                 acc = -acc;
     104                 :            :         }
     105                 :            : 
     106         [ #  # ]:          0 :         if (endptr != NULL) {
     107         [ #  # ]:          0 :                 *endptr = (char *)(any ? s - 1 : nptr);
     108                 :            :         }
     109                 :          0 :         return acc;
     110                 :            : }

Generated by: LCOV version 1.14