[net.unix] strtol

rlk@mit-trillian.MIT.EDU (Robert L Krawitz) (11/01/86)

What does this do?  It doesn't seem to exist on my 4.3 system.  I
suspect it's a SysV library fn, so it seems like I'll have to write it
myself.  (The reason I'm cross-posting it to net.bicycle is that
that's where the particlar program came from.)

Example:

long strtol();

getnum()
{
    return (int)strtol(lineptr, &lineptr, 10);
}
-- 
Robert^Z

dml@rabbit1.UUCP (David Langdon) (11/03/86)

> Xref: rabbit1 net.unix:1814 net.bicycle:619
> rlk@mit-trillian.MIT.EDU (Robert L Krawitz): writes
>
> What does this do?  It doesn't seem to exist on my 4.3 system.  I
> suspect it's a SysV library fn, so it seems like I'll have to write it
> myself.  (The reason I'm cross-posting it to net.bicycle is that
> that's where the particlar program came from.)
> 
> Example:
> 
> long strtol();
> 
> getnum()
> {
>     return (int)strtol(lineptr, &lineptr, 10);
> }
> -- 
> Robert^Z

If I'm not mistaken, strtol() is "string-to-long" conversion routine. It
takes an ASCII string and converts it to a long value type

-- 
David Langdon    Rabbit Software Corp.
(215) 647-0440   7 Great Valley Parkway East  Malvern PA 19355

...!ihnp4!{cbmvax,cuuxb}!hutch!dml        ...!psuvax1!burdvax!hutch!dml

guy@sun.uucp (Guy Harris) (11/03/86)

> What does this do?  It doesn't seem to exist on my 4.3 system.  I
> suspect it's a SysV library fn, so it seems like I'll have to write it
> myself.

Yes, it's a generalization of "atol"; it first appeared in System V.  It is
passed a pointer to a "char *"; if this pointer is non-null, it will return
a pointer to the first character after the value it converted.  It is also
passed a radix, so it can convert hex or octal numbers; if the radix is 0,
it converts the number as if it were a C constant (if it begins with "0x",
it's hex; otherwise, if it begins with "0", it's octal; otherwise, it's
decimal).

> getnum()
> {
>     return (int)strtol(lineptr, &lineptr, 10);
> }

As you can probably infer, the "pointer to pointer" is the second argument
and the radix is the third argument.
-- 
	Guy Harris
	{ihnp4, decvax, seismo, decwrl, ...}!sun!guy
	guy@sun.com (or guy@sun.arpa)

carroll@snail.CS.UIUC.EDU (11/04/86)

From the SysV "Programmers Reference Manual" :

long strtol(str,ptr,base)
char *str,**ptr;
int base;

Strtol returns as a long integer the value represented by the character string
pointed to by str. The string is scanned up to the first character inconsistent
with the base. Leading whitespace is ignored.

If ptr is not NULL, a pointer to the character terminating the scan is
returned.

If base is positive (and <= 36), it is used as the base for conversion. After
an optional leading minus sign, leading zero's are ignored, and a leading
0X or 0x is ignored if base == 16. If base is zero, then a leading zero
indicates base 8, leading 0x or 0X indicates base 16, otherwise it is base
10.

naftoli@aecom.UUCP (Robert N. Berlinger) (11/05/86)

> What does strtol() do?

This is from the AT&T manual.

     SYNOPSIS
          long strtol (str, ptr, base)
          char *str, **ptr;
          int base;
     DESCRIPTION
          Strtol returns as a long integer the value represented by
          the character string pointed to by str.  The string is
          scanned up to the first character inconsistent with the
          base.  Leading ``white-space'' characters (as defined by
          isspace in ctype(3C)) are ignored.

          If the value of ptr is not (char **)NULL, a pointer to the
          character terminating the scan is returned in the location
          pointed to by ptr.  If no integer can be formed, that
          location is set to str, and zero is returned.

          If base is positive (and not greater than 36), it is used as
          the base for conversion.  After an optional leading sign,
          leading zeros are ignored, and ``0x'' or ``0X'' is ignored
          if base is 16.

          If base is zero, the string itself determines the base
          thusly: After an optional leading sign a leading zero
          indicates octal conversion, and a leading ``0x'' or ``0X''
          hexadecimal conversion.  Otherwise, decimal conversion is
          used.

          Truncation from long to int can, of course, take place upon
          assignment or by an explicit cast.

Hope that helps.
-- 
Robert N. Berlinger
Systems Analyst
Scientific Computing Center
Albert Einstein College of Medicine

UUCP:       ...{philabs,cucard,pegasus,ima,rocky2}!aecom!naftoli
Compuserve: 73047,741
Easylink:   62956067
GEnie:	    R.Berlinger

greg@xios.UUCP (Greg Franks) (11/06/86)

In article <1366@mit-trillian.MIT.EDU> rlk@athena.MIT.EDU writes:
>What does this do?  It doesn't seem to exist on my 4.3 system.  I
>suspect it's a SysV library fn, so it seems like I'll have to write it
>myself.  (The reason I'm cross-posting it to net.bicycle is that
>that's where the particlar program came from.)

Our XENIX systems lack strtol too.  My solution:

getnum()
{
#ifdef V7
    int i;
    if ( sscanf( lineptr, "%d", &i ) > 0 ) {
    	while( isspace( *lineptr ) )
    	    lineptr++;
    	while( isdigit( *lineptr ) )
    	    lineptr++;
    } else
        i = 0;
    return( i );
#else
    return (int)strtol(lineptr, &lineptr, 10);
#endif
}

Strtol(3) adjusts lineptr by the number of characters eaten.  So, after
successfully grabbing the number using sscanf (which skips white space)
adjust the pointer the hard way.

There are public domain versions of strtok.  Henry Spencer (utzoo) 
wrote one - I used his...
  Good luck!

  ..greg