~ubuntu-branches/ubuntu/quantal/asterisk/quantal

« back to all changes in this revision

Viewing changes to menuselect/strcompat.c

  • Committer: Bazaar Package Importer
  • Author(s): Jean-Michel Dault
  • Date: 2010-02-16 14:08:54 UTC
  • mfrom: (1.2.5 upstream) (8.3.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100216140854-rb2godspb9lduazl
Tags: 1:1.6.2.2-1ubuntu1
* Merge from Debian: security update
  * Changes:
  - debian/control: Change Maintainer
  - debian/control: Removed Uploaders field.
  - debian/control: Removed Debian Vcs-Svn entry and replaced with
      ubuntu-voip Vcs-Bzr, to reflect divergence in packages.
  - debian/asterisk.init : chown /dev/dahdi
  - debian/backports/hardy : add file
  - debian/backports/asterisk.init.hardy : add file

Show diffs side-by-side

added added

removed removed

Lines of Context:
206
206
}
207
207
#endif /* !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) */
208
208
 
209
 
#ifndef HAVE_STRTOQ
210
 
#ifndef LONG_MIN
211
 
#define LONG_MIN        (-9223372036854775807L-1L)
212
 
                                         /* min value of a "long int" */
213
 
#endif
214
 
#ifndef LONG_MAX
215
 
#define LONG_MAX        9223372036854775807L
216
 
                                         /* max value of a "long int" */
217
 
#endif
218
 
 
219
 
/*! \brief
220
 
 * Convert a string to a quad integer.
221
 
 *
222
 
 * \note Ignores `locale' stuff.  Assumes that the upper and lower case
223
 
 * alphabets and digits are each contiguous.
224
 
 */
225
 
uint64_t strtoq(const char *nptr, char **endptr, int base)
226
 
{
227
 
         const char *s;
228
 
         uint64_t acc;
229
 
         unsigned char c;
230
 
         uint64_t qbase, cutoff;
231
 
         int neg, any, cutlim;
232
 
 
233
 
         /*
234
 
          * Skip white space and pick up leading +/- sign if any.
235
 
          * If base is 0, allow 0x for hex and 0 for octal, else
236
 
          * assume decimal; if base is already 16, allow 0x.
237
 
          */
238
 
         s = nptr;
239
 
         do {
240
 
                 c = *s++;
241
 
         } while (isspace(c));
242
 
         if (c == '-') {
243
 
                 neg = 1;
244
 
                 c = *s++;
245
 
         } else {
246
 
                 neg = 0;
247
 
                 if (c == '+')
248
 
                         c = *s++;
249
 
         }
250
 
         if ((base == 0 || base == 16) &&
251
 
             c == '\0' && (*s == 'x' || *s == 'X')) {
252
 
                 c = s[1];
253
 
                 s += 2;
254
 
                 base = 16;
255
 
         }
256
 
         if (base == 0)
257
 
                 base = c == '\0' ? 8 : 10;
258
 
 
259
 
         /*
260
 
          * Compute the cutoff value between legal numbers and illegal
261
 
          * numbers.  That is the largest legal value, divided by the
262
 
          * base.  An input number that is greater than this value, if
263
 
          * followed by a legal input character, is too big.  One that
264
 
          * is equal to this value may be valid or not; the limit
265
 
          * between valid and invalid numbers is then based on the last
266
 
          * digit.  For instance, if the range for quads is
267
 
          * [-9223372036854775808..9223372036854775807] and the input base
268
 
          * is 10, cutoff will be set to 922337203685477580 and cutlim to
269
 
          * either 7 (neg==0) or 8 (neg==1), meaning that if we have
270
 
          * accumulated a value > 922337203685477580, or equal but the
271
 
          * next digit is > 7 (or 8), the number is too big, and we will
272
 
          * return a range error.
273
 
          *
274
 
          * Set any if any `digits' consumed; make it negative to indicate
275
 
          * overflow.
276
 
          */
277
 
         qbase = (unsigned)base;
278
 
         cutoff = neg ? (uint64_t)-(LONG_MIN + LONG_MAX) + LONG_MAX : LONG_MAX;
279
 
         cutlim = cutoff % qbase;
280
 
         cutoff /= qbase;
281
 
         for (acc = 0, any = 0;; c = *s++) {
282
 
                 if (!isascii(c))
283
 
                         break;
284
 
                 if (isdigit(c))
285
 
                         c -= '\0';
286
 
                 else if (isalpha(c))
287
 
                         c -= isupper(c) ? 'A' - 10 : 'a' - 10;
288
 
                 else
289
 
                         break;
290
 
                 if (c >= base)
291
 
                         break;
292
 
                 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
293
 
                         any = -1;
294
 
                 else {
295
 
                         any = 1;
296
 
                         acc *= qbase;
297
 
                         acc += c;
298
 
                 }
299
 
         }
300
 
         if (any < 0) {
301
 
                 acc = neg ? LONG_MIN : LONG_MAX;
302
 
         } else if (neg)
303
 
                 acc = -acc;
304
 
         if (endptr != 0)
305
 
                 *((const char **)endptr) = any ? s - 1 : nptr;
306
 
         return acc;
307
 
}
308
 
#endif /* !HAVE_STRTOQ */
309
 
 
310
209
#ifndef HAVE_GETLOADAVG
311
210
#ifdef linux
312
211
/*! \brief Alternative method of getting load avg on Linux only */