home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 0 / 0987 / strtol.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  2.2 KB  |  90 lines

  1. /* 
  2.  * strtol.c --
  3.  *
  4.  *    Source code for the "strtol" library procedure.
  5.  *
  6.  * Copyright 1988 Regents of the University of California
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  */
  15.  
  16. #ifndef lint
  17. static char rcsid[] = "$Header: /sprite/src/lib/c/stdlib/RCS/strtol.c,v 1.4 89/03/22 00:47:30 rab Exp $ SPRITE (Berkeley)";
  18. #endif /* not lint */
  19.  
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22.  
  23.  
  24. /*
  25.  *----------------------------------------------------------------------
  26.  *
  27.  * strtol --
  28.  *
  29.  *    Convert an ASCII string into an integer.
  30.  *
  31.  * Results:
  32.  *    The return value is the integer equivalent of string.  If endPtr
  33.  *    is non-NULL, then *endPtr is filled in with the character
  34.  *    after the last one that was part of the integer.  If string
  35.  *    doesn't contain a valid integer value, then zero is returned
  36.  *    and *endPtr is set to string.
  37.  *
  38.  * Side effects:
  39.  *    None.
  40.  *
  41.  *----------------------------------------------------------------------
  42.  */
  43.  
  44. long int
  45. strtol(string, endPtr, base)
  46.     char *string;        /* String of ASCII digits, possibly
  47.                  * preceded by white space.  For bases
  48.                  * greater than 10, either lower- or
  49.                  * upper-case digits may be used.
  50.                  */
  51.     char **endPtr;        /* Where to store address of terminating
  52.                  * character, or NULL. */
  53.     int base;            /* Base for conversion.  Must be less
  54.                  * than 37.  If 0, then the base is chosen
  55.                  * from the leading characters of string:
  56.                  * "0x" means hex, "0" means octal, anything
  57.                  * else means decimal.
  58.                  */
  59. {
  60.     register char *p;
  61.     int result;
  62.  
  63.     /*
  64.      * Skip any leading blanks.
  65.      */
  66.  
  67.     p = string;
  68.     while (isspace(*p)) {
  69.     p += 1;
  70.     }
  71.  
  72.     /*
  73.      * Check for a sign.
  74.      */
  75.  
  76.     if (*p == '-') {
  77.     p += 1;
  78.     result = -(strtoul(p, endPtr, base));
  79.     } else {
  80.     if (*p == '+') {
  81.         p += 1;
  82.     }
  83.     result = strtoul(p, endPtr, base);
  84.     }
  85.     if ((result == 0) && (endPtr != 0) && (*endPtr == p)) {
  86.     *endPtr = string;
  87.     }
  88.     return result;
  89. }
  90.