~ubuntu-branches/ubuntu/gutsy/findutils/gutsy-proposed

« back to all changes in this revision

Viewing changes to gnulib/lib/xstrtol.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2005-07-04 11:37:37 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050704113737-ll89ui8be35r0pir
Tags: 4.2.22-2
* Remove locatedb on purge. (Closes: #315343)
* revert regex-syntax back to emacs-re. (Closes: #315136) Future versions
  will allow to select this by commandline parameter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* A more useful interface to strtol.
2
 
   Copyright (C) 1995, 1996, 1998-2001 Free Software Foundation, Inc.
 
2
 
 
3
   Copyright (C) 1995, 1996, 1998, 1999, 2000, 2001, 2003, 2004, 2005
 
4
   Free Software Foundation, Inc.
3
5
 
4
6
   This program is free software; you can redistribute it and/or modify
5
7
   it under the terms of the GNU General Public License as published by
13
15
 
14
16
   You should have received a copy of the GNU General Public License
15
17
   along with this program; if not, write to the Free Software Foundation,
16
 
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
18
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
19
 
18
20
/* Written by Jim Meyering. */
19
21
 
21
23
# include <config.h>
22
24
#endif
23
25
 
 
26
#include "xstrtol.h"
 
27
 
24
28
#ifndef __strtol
25
29
# define __strtol strtol
26
30
# define __strtol_t long int
27
31
# define __xstrtol xstrtol
 
32
# define STRTOL_T_MINIMUM LONG_MIN
 
33
# define STRTOL_T_MAXIMUM LONG_MAX
28
34
#endif
29
35
 
30
36
/* Some pre-ANSI implementations (e.g. SunOS 4)
31
37
   need stderr defined if assertion checking is enabled.  */
32
38
#include <stdio.h>
33
39
 
34
 
#if STDC_HEADERS
35
 
# include <stdlib.h>
36
 
#endif
37
 
 
38
 
#if HAVE_STRING_H
39
 
# include <string.h>
40
 
#else
41
 
# include <strings.h>
42
 
# ifndef strchr
43
 
#  define strchr index
44
 
# endif
45
 
#endif
46
 
 
47
40
#include <assert.h>
48
41
#include <ctype.h>
49
 
 
50
42
#include <errno.h>
51
 
#ifndef errno
52
 
extern int errno;
53
 
#endif
54
 
 
55
 
#if HAVE_LIMITS_H
56
 
# include <limits.h>
57
 
#endif
58
 
 
59
 
#ifndef CHAR_BIT
60
 
# define CHAR_BIT 8
61
 
#endif
62
 
 
63
 
/* The extra casts work around common compiler bugs.  */
64
 
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
65
 
/* The outer cast is needed to work around a bug in Cray C 5.0.3.0.
66
 
   It is necessary at least when t == time_t.  */
67
 
#define TYPE_MINIMUM(t) ((t) (TYPE_SIGNED (t) \
68
 
                              ? ~ (t) 0 << (sizeof (t) * CHAR_BIT - 1) : (t) 0))
69
 
#define TYPE_MAXIMUM(t) (~ (t) 0 - TYPE_MINIMUM (t))
 
43
#include <limits.h>
 
44
#include <stdlib.h>
 
45
#include <string.h>
 
46
 
 
47
#include "intprops.h"
 
48
 
 
49
#ifndef STRTOL_T_MINIMUM
 
50
# define STRTOL_T_MINIMUM TYPE_MINIMUM (__strtol_t)
 
51
# define STRTOL_T_MAXIMUM TYPE_MAXIMUM (__strtol_t)
 
52
#endif
70
53
 
71
54
#if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
72
55
# define IN_CTYPE_DOMAIN(c) 1
76
59
 
77
60
#define ISSPACE(c) (IN_CTYPE_DOMAIN (c) && isspace (c))
78
61
 
79
 
#include "xstrtol.h"
80
 
 
81
 
#if !HAVE_DECL_STRTOL && !defined strtol
82
 
long int strtol ();
83
 
#endif
84
 
 
85
 
#if !HAVE_DECL_STRTOUL && !defined strtoul
86
 
unsigned long int strtoul ();
87
 
#endif
88
 
 
89
62
#if !HAVE_DECL_STRTOIMAX && !defined strtoimax
90
63
intmax_t strtoimax ();
91
64
#endif
94
67
uintmax_t strtoumax ();
95
68
#endif
96
69
 
97
 
static int
 
70
static strtol_error
98
71
bkm_scale (__strtol_t *x, int scale_factor)
99
72
{
100
 
  __strtol_t product = *x * scale_factor;
101
 
  if (*x != product / scale_factor)
102
 
    return 1;
103
 
  *x = product;
104
 
  return 0;
 
73
  if (TYPE_SIGNED (__strtol_t) && *x < STRTOL_T_MINIMUM / scale_factor)
 
74
    {
 
75
      *x = STRTOL_T_MINIMUM;
 
76
      return LONGINT_OVERFLOW;
 
77
    }
 
78
  if (STRTOL_T_MAXIMUM / scale_factor < *x)
 
79
    {
 
80
      *x = STRTOL_T_MAXIMUM;
 
81
      return LONGINT_OVERFLOW;
 
82
    }
 
83
  *x *= scale_factor;
 
84
  return LONGINT_OK;
105
85
}
106
86
 
107
 
static int
 
87
static strtol_error
108
88
bkm_scale_by_power (__strtol_t *x, int base, int power)
109
89
{
 
90
  strtol_error err = LONGINT_OK;
110
91
  while (power--)
111
 
    if (bkm_scale (x, base))
112
 
      return 1;
113
 
 
114
 
  return 0;
 
92
    err |= bkm_scale (x, base);
 
93
  return err;
115
94
}
116
95
 
117
96
/* FIXME: comment.  */
123
102
  char *t_ptr;
124
103
  char **p;
125
104
  __strtol_t tmp;
 
105
  strtol_error err = LONGINT_OK;
126
106
 
127
107
  assert (0 <= strtol_base && strtol_base <= 36);
128
108
 
131
111
  if (! TYPE_SIGNED (__strtol_t))
132
112
    {
133
113
      const char *q = s;
134
 
      while (ISSPACE ((unsigned char) *q))
135
 
        ++q;
136
 
      if (*q == '-')
 
114
      unsigned char ch = *q;
 
115
      while (ISSPACE (ch))
 
116
        ch = *++q;
 
117
      if (ch == '-')
137
118
        return LONGINT_INVALID;
138
119
    }
139
120
 
140
121
  errno = 0;
141
122
  tmp = __strtol (s, p, strtol_base);
142
 
  if (errno != 0)
143
 
    return LONGINT_OVERFLOW;
144
123
 
145
124
  if (*p == s)
146
125
    {
151
130
      else
152
131
        return LONGINT_INVALID;
153
132
    }
 
133
  else if (errno != 0)
 
134
    {
 
135
      if (errno != ERANGE)
 
136
        return LONGINT_INVALID;
 
137
      err = LONGINT_OVERFLOW;
 
138
    }
154
139
 
155
140
  /* Let valid_suffixes == NULL mean `allow any suffix'.  */
156
141
  /* FIXME: update all callers except the ones that allow suffixes
158
143
  if (!valid_suffixes)
159
144
    {
160
145
      *val = tmp;
161
 
      return LONGINT_OK;
 
146
      return err;
162
147
    }
163
148
 
164
149
  if (**p != '\0')
165
150
    {
166
151
      int base = 1024;
167
152
      int suffixes = 1;
168
 
      int overflow;
 
153
      strtol_error overflow;
169
154
 
170
155
      if (!strchr (valid_suffixes, **p))
171
156
        {
172
157
          *val = tmp;
173
 
          return LONGINT_INVALID_SUFFIX_CHAR;
 
158
          return err | LONGINT_INVALID_SUFFIX_CHAR;
174
159
        }
175
160
 
176
161
      if (strchr (valid_suffixes, '0'))
253
238
 
254
239
        default:
255
240
          *val = tmp;
256
 
          return LONGINT_INVALID_SUFFIX_CHAR;
257
 
          break;
 
241
          return err | LONGINT_INVALID_SUFFIX_CHAR;
258
242
        }
259
243
 
260
 
      if (overflow)
261
 
        return LONGINT_OVERFLOW;
262
 
 
263
 
      (*p) += suffixes;
 
244
      err |= overflow;
 
245
      *p += suffixes;
 
246
      if (**p)
 
247
        err |= LONGINT_INVALID_SUFFIX_CHAR;
264
248
    }
265
249
 
266
250
  *val = tmp;
267
 
  return LONGINT_OK;
 
251
  return err;
268
252
}
269
253
 
270
254
#ifdef TESTING_XSTRTO
275
259
char *program_name;
276
260
 
277
261
int
278
 
main (int argc, char** argv)
 
262
main (int argc, char **argv)
279
263
{
280
264
  strtol_error s_err;
281
265
  int i;