home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / compat / strtol.c < prev    next >
C/C++ Source or Header  |  1993-03-19  |  3KB  |  100 lines

  1. /* 
  2.  * strtol.c --
  3.  *
  4.  *    Source code for the "strtol" library procedure.
  5.  *
  6.  * Copyright (c) 1988 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Permission is hereby granted, without written agreement and without
  10.  * license or royalty fees, to use, copy, modify, and distribute this
  11.  * software and its documentation for any purpose, provided that the
  12.  * above copyright notice and the following two paragraphs appear in
  13.  * all copies of this software.
  14.  * 
  15.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  16.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  17.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  18.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  19.  *
  20.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  21.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  22.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  23.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  24.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  25.  */
  26.  
  27. #ifndef lint
  28. static char rcsid[] = "$Header: /user6/ouster/tcl/compat/RCS/strtol.c,v 1.2 93/03/19 15:25:43 ouster Exp $ SPRITE (Berkeley)";
  29. #endif /* not lint */
  30.  
  31. #include <ctype.h>
  32.  
  33.  
  34. /*
  35.  *----------------------------------------------------------------------
  36.  *
  37.  * strtol --
  38.  *
  39.  *    Convert an ASCII string into an integer.
  40.  *
  41.  * Results:
  42.  *    The return value is the integer equivalent of string.  If endPtr
  43.  *    is non-NULL, then *endPtr is filled in with the character
  44.  *    after the last one that was part of the integer.  If string
  45.  *    doesn't contain a valid integer value, then zero is returned
  46.  *    and *endPtr is set to string.
  47.  *
  48.  * Side effects:
  49.  *    None.
  50.  *
  51.  *----------------------------------------------------------------------
  52.  */
  53.  
  54. long int
  55. strtol(string, endPtr, base)
  56.     char *string;        /* String of ASCII digits, possibly
  57.                  * preceded by white space.  For bases
  58.                  * greater than 10, either lower- or
  59.                  * upper-case digits may be used.
  60.                  */
  61.     char **endPtr;        /* Where to store address of terminating
  62.                  * character, or NULL. */
  63.     int base;            /* Base for conversion.  Must be less
  64.                  * than 37.  If 0, then the base is chosen
  65.                  * from the leading characters of string:
  66.                  * "0x" means hex, "0" means octal, anything
  67.                  * else means decimal.
  68.                  */
  69. {
  70.     register char *p;
  71.     int result;
  72.  
  73.     /*
  74.      * Skip any leading blanks.
  75.      */
  76.  
  77.     p = string;
  78.     while (isspace(*p)) {
  79.     p += 1;
  80.     }
  81.  
  82.     /*
  83.      * Check for a sign.
  84.      */
  85.  
  86.     if (*p == '-') {
  87.     p += 1;
  88.     result = -(strtoul(p, endPtr, base));
  89.     } else {
  90.     if (*p == '+') {
  91.         p += 1;
  92.     }
  93.     result = strtoul(p, endPtr, base);
  94.     }
  95.     if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
  96.     *endPtr = string;
  97.     }
  98.     return result;
  99. }
  100.