~ubuntu-branches/ubuntu/trusty/bash/trusty-security

« back to all changes in this revision

Viewing changes to lib/sh/strtoimax.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2014-03-03 22:52:05 UTC
  • mfrom: (1.3.5) (2.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20140303225205-87ltrt5kspeq0g1b
Tags: 4.3-1ubuntu1
* Merge with Debian; remaining changes:
  - skel.bashrc:
    - Run lesspipe.
    - Enable ls aliases.
    - Set options in ll alias to -alF.
    - Define an alert alias.
    - Enabled colored grep aliases.
  - etc.bash.bashrc:
    - Add sudo hint.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* strtoimax - convert string representation of a number into an intmax_t value. */
 
2
 
 
3
/* Copyright 1999-2009 Free Software Foundation, Inc.
 
4
 
 
5
   This file is part of GNU Bash, the Bourne Again SHell.
 
6
   
 
7
   Bash is free software: you can redistribute it and/or modify
 
8
   it under the terms of the GNU General Public License as published by
 
9
   the Free Software Foundation, either version 3 of the License, or
 
10
   (at your option) any later version.
 
11
 
 
12
   Bash is distributed in the hope that it will be useful,
 
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
   GNU General Public License for more details.
 
16
 
 
17
   You should have received a copy of the GNU General Public License
 
18
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
19
*/
 
20
 
 
21
/* Written by Paul Eggert.  Modified by Chet Ramey for Bash. */
 
22
 
 
23
#if HAVE_CONFIG_H
 
24
#  include <config.h>
 
25
#endif
 
26
 
 
27
#if HAVE_INTTYPES_H
 
28
#  include <inttypes.h>
 
29
#endif
 
30
 
 
31
#if HAVE_STDINT_H
 
32
#  include <stdint.h>
 
33
#endif
 
34
 
 
35
#if HAVE_STDLIB_H
 
36
#  include <stdlib.h>
 
37
#endif
 
38
 
 
39
#include <stdc.h>
 
40
 
 
41
/* Verify a requirement at compile-time (unlike assert, which is runtime).  */
 
42
#define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
 
43
 
 
44
#ifndef HAVE_DECL_STRTOL
 
45
"this configure-time declaration test was not run"
 
46
#endif
 
47
#if !HAVE_DECL_STRTOL
 
48
extern long strtol __P((const char *, char **, int));
 
49
#endif
 
50
 
 
51
#ifndef HAVE_DECL_STRTOLL
 
52
"this configure-time declaration test was not run"
 
53
#endif
 
54
#if !HAVE_DECL_STRTOLL && HAVE_LONG_LONG
 
55
extern long long strtoll __P((const char *, char **, int));
 
56
#endif
 
57
 
 
58
#ifdef strtoimax
 
59
#undef strtoimax
 
60
#endif
 
61
 
 
62
intmax_t
 
63
strtoimax (ptr, endptr, base)
 
64
     const char *ptr;
 
65
     char **endptr;
 
66
     int base;
 
67
{
 
68
#if HAVE_LONG_LONG
 
69
  verify(size_is_that_of_long_or_long_long,
 
70
         (sizeof (intmax_t) == sizeof (long) ||
 
71
          sizeof (intmax_t) == sizeof (long long)));
 
72
 
 
73
  if (sizeof (intmax_t) != sizeof (long))
 
74
    return (strtoll (ptr, endptr, base));
 
75
#else
 
76
  verify (size_is_that_of_long, sizeof (intmax_t) == sizeof (long));
 
77
#endif
 
78
 
 
79
  return (strtol (ptr, endptr, base));
 
80
}
 
81
 
 
82
#ifdef TESTING
 
83
# include <stdio.h>
 
84
int
 
85
main ()
 
86
{
 
87
  char *p, *endptr;
 
88
  intmax_t x;
 
89
#if HAVE_LONG_LONG
 
90
  long long y;
 
91
#endif
 
92
  long z;
 
93
  
 
94
  printf ("sizeof intmax_t: %d\n", sizeof (intmax_t));
 
95
 
 
96
#if HAVE_LONG_LONG
 
97
  printf ("sizeof long long: %d\n", sizeof (long long));
 
98
#endif
 
99
  printf ("sizeof long: %d\n", sizeof (long));
 
100
 
 
101
  x = strtoimax("42", &endptr, 10);
 
102
#if HAVE_LONG_LONG
 
103
  y = strtoll("42", &endptr, 10);
 
104
#else
 
105
  y = -1;
 
106
#endif
 
107
  z = strtol("42", &endptr, 10);
 
108
 
 
109
  printf ("%lld %lld %ld\n", x, y, z);
 
110
 
 
111
  exit (0);
 
112
}
 
113
#endif