[comp.sources.amiga] v89i005: edlib - manx library of useful functions

page@swan.ulowell.edu (Bob Page) (02/01/89)

Submitted-by: hcr!hcrvax!edwin (Edwin Hoogerbeets)
Posting-number: Volume 89, Issue 5
Archive-name: libraries/edlib.1

#	This is a shell archive.
#	Remove everything above and including the cut line.
#	Then run the rest of the file through sh.
#----cut here-----cut here-----cut here-----cut here----#
#!/bin/sh
# shar:    Shell Archiver
#	Run the following text with /bin/sh to create:
#	README
#	bintoint.c
#	dectoint.c
#	edlib.h
#	getopt.c
#	hextoint.c
#	isbdigit.c
#	iscsym.c
#	iscsymf.c
#	isodigit.c
#	makefile
#	man
#	stolower.c
#	stoupper.c
#	strcspn.c
#	stricmp.c
#	strnicmp.c
#	strpbrk.c
#	strpos.c
#	strrpbrk.c
#	strrpos.c
#	strspn.c
#	strtok.c
#	test.c
#	toint.c
# This archive created: Mon Jan 30 16:57:00 1989
cat << \SHAR_EOF > README
Edlib           copyright 1988  Edwin Hoogerbeets 
Version 1.0     04/08/88

Some time ago, I noticed a lack of certain useful functions in the Manx
C libraries that were mentioned in the Harbison and Steele C reference
manual. So, I wrote them. Since then, I have added a few of my own as
well as some from other people. Now, I think it's big enough to be
useful to others, so here it is!

This is the source and executable to the edlib runtime libraries. They
are for Manx 3.6a. (I have tried some of these functions with 3.4b when
I used to use that and they worked fine) A makefile is included with the
source. Two version of the library are provided: ed.lib for your normal
programming needs and the ed32.lib for those extra special 32 bit
programs.

This software collection is freely redistributable as long as no charge,
other than reasonable copy fees, is levied for them.

Thanks to Daniel J. Barrett for some of these functions, and to Henry
Spencer for getopt.

If you find any bugs, or have any other nifty-neato functions that you
find useful, please mail them to me at:

{backbone}!utai!{utzoo,utcsri}!hcr!edwin   until September '88
{backbone}!watmath!trillium!ehoogerbeets   Sept '88 to Dec '88


------ --------- = -------------------------------------------
Edwin (Deepthot)                      Waterloo co-op student, HCR Corporation
Hoogerbeets		                   2A computer science and psychology
utai!{utzoo,utcsri}!hcr!edwin                                Me Tarzan, Unix.
                    edwin@hcr       //           Freudian slips? This message
or:                                //               contains no Freudian sex.
...!hcr!MsgPort!edwin          \\ //   Amiga        Glider pilots are experts
A B2000 running UUPC            \X/  Enthusiast             at keeping it up!
SHAR_EOF
cat << \SHAR_EOF > bintoint.c
/* edlib  version 1.0 of 04/08/88 */
/* this function takes a string of binary digits and returns its value */
int bintoint(number)
char *number;
{
    int value = 0;

    while ( *number )
        if ( isbdigit(*number) ) {
            value = (value << 1) + toint(*number++);
        } else {
            return(value);
        }

    return(value);
}
SHAR_EOF
cat << \SHAR_EOF > dectoint.c
/* edlib  version 1.0 of 04/08/88 */
/* this function takes a string of decimal digits and returns its value */
#include <ctype.h>

int dectoint(number)
char *number;
{
    int value = 0;

    while ( *number )
        if ( isdigit(*number) ) {
            value = value*10  + toint(*number++);
        } else {
            return(value);
        }

    return(value);
}
SHAR_EOF
cat << \SHAR_EOF > edlib.h
/*
    edlib.h Copyright 1988 Edwin Hoogerbeets

    This code may be freely redistributed as long as no cost is levied
    for it and that this copyright notice remains intact.

    edlib contains a bunch of routines that are listed in H&S that are
    not in the Manx libraries. Also contains other public domain as well
    as freely redistributable routines.

    The library was compiled with Manx 3.6a.
*/
#include <exec/types.h>

/* character processing functions */
extern int   isbdigit();  /* is the character a `1' or a `0'? */
extern int   iscsym();    /* character part of a valid C symbol? */
extern int   iscsymf();   /* character valid a first char in a C symbol? */
extern int   toint();     /* converts a character 0..f to its hexadecimal value */
extern int   isodigit();  /* is this an octal digit? */

/* string processing functions */
extern int  bintoint();   /* these three take character strings and return the */
extern int  dectoint();   /* int value of the number in the string */
extern int  hextoint();
extern char *stoupper();  /* converts a string to entirely upper case chars */
extern char *stolower();  /* converts a string to entirely lower case chars */
extern int  strpos();     /* gives position of first occurance of char in string */
extern int  strrpos();    /* gives position of last occurance of char in string */
extern char *strrpbrk();
extern int  stricmp();    /* case insensitive string compare */
extern int  strnicmp();   /* " with a certain length */
extern int  strcspn();
extern char *strpbrk();   /* these four courtesy Daniel J. Barrett */
extern char *strtok();
extern int  strspn();

/* definitions to use getopt() */
extern int getopt();
extern char *optarg;
extern int optind;




SHAR_EOF
cat << \SHAR_EOF > getopt.c
/* edlib  version 1.0 of 04/08/88 */
#define NULL    0
#define EOF     (-1)
#define ERR(s, c)       if(opterr){\
        extern int strlen(), write();\
        char errbuf[2];\
        errbuf[0] = c; errbuf[1] = '\n';\
        (void) write(2, argv[0], (unsigned)strlen(argv[0]));\
        (void) write(2, s, (unsigned)strlen(s));\
        (void) write(2, errbuf, 2);}

extern int strcmp();
extern char *strchr();

int     opterr = 1;
int     optind = 1;
int     optopt;
char    *optarg;

int
getopt(argc, argv, opts)
int     argc;
char    **argv, *opts;
{
        static int sp = 1;
        register int c;
        register char *cp;

        if(sp == 1)
                if(optind >= argc)
                        return(EOF);
                else {
                    if(argv[optind][0] != '-') {
                        optarg = argv[optind++];
                        return(NULL);
                    }
                    if(strcmp(argv[optind], "--") == NULL) {
                        optind++;
                        return(EOF);
                    }
                }
        optopt = c = argv[optind][sp];
        if(c == ':' || (cp=strchr(opts, c)) == NULL) {
                ERR(": illegal option -- ", c);
                if(argv[optind][++sp] == '\0') {
                        optind++;
                        sp = 1;
                }
                return('?');
        }
        if(*++cp == ':') {
                if(argv[optind][sp+1] != '\0')
                        optarg = &argv[optind++][sp+1];
                else if(++optind >= argc) {
                        ERR(": option requires an argument -- ", c);
                        sp = 1;
                        return('?');
                } else
                        optarg = argv[optind++];
                sp = 1;
        } else {
                if(argv[optind][++sp] == '\0') {
                        sp = 1;
                        optind++;
                }
                optarg = NULL;
        }
        return(c);
}
SHAR_EOF
cat << \SHAR_EOF > hextoint.c
/* edlib  version 1.0 of 04/08/88 */
/* this function takes a string of hex digits and returns its value */
#include <ctype.h>

int hextoint(number)
char *number;
{
    int value = 0;

    while ( *number )
        if ( isxdigit(*number) ) {
            value = ( value << 4 )  + toint(*number++);
        } else {
            return(value);
        }

    return(value);
}
SHAR_EOF
cat << \SHAR_EOF > isbdigit.c
/* edlib  version 1.0 of 04/08/88 */
/*
    tests whether the given character could be a binary digit
*/
#include <stdio.h>

int isbdigit(c)
char c;
{
    if ( c == '1' || c == '0' ) {
        return(1);
    } else {
        return(NULL);
    }
}
SHAR_EOF
cat << \SHAR_EOF > iscsym.c
/* edlib  version 1.0 of 04/08/88 */
/*
    this function returns a non-zero number if the character given
    is suitable for the a character of an identifier
*/
#include <ctype.h>
#define NULL 0

int iscsym(c)
char c;
{
    if ( iscsymf(c) || isdigit(c) )
        return(c);

    return(NULL);
}

SHAR_EOF
cat << \SHAR_EOF > iscsymf.c
/* edlib  version 1.0 of 04/08/88 */
/*
    iscsymf is included here because Manx for some reason does not have
    it in ctype.h. tells whether or not the given character may be the
    first character of a valid C symbol
*/
#include <ctype.h>
int iscsymf(c)
char c;
{
    return( isalpha(c) || c == '_' );
}
SHAR_EOF
cat << \SHAR_EOF > isodigit.c
/* edlib  version 1.0 of 04/08/88 */
/*
    returns a nonzero value if c is the code for an octal digit, otherwise
    return zero
*/
#include <ctype.h>

int isodigit(c)
char c;
{
    if (c == '9' || c == '8') {
        return(0);
    } else {
        return(isdigit(c));
    }
}

SHAR_EOF
cat << \SHAR_EOF > makefile
# makefile for the edlib library source directory
# copyright 1988 Edwin Hoogerbeets
#
# make sure that lb is in your path or make will give you an exec failure

.c.o:
        cc +L -o $*.o $*.c

OBJS= bintoint.o dectoint.o getopt.o hextoint.o isbdigit.o \
      iscsym.o iscsymf.o isodigit.o stolower.o stoupper.o  \
      strcspn.o strpbrk.o strpos.o strrpos.o strspn.o      \
      strtok.o toint.o strrpbrk.o stricmp.o strnicmp.o

ed32.lib: $(OBJS)
        lb ed32.lib $(OBJS)

test: test.o
        ln test.o -led -lc -o test

SHAR_EOF
cat << \SHAR_EOF > man
Here are the man pages for the library functions:
This is for edlib version 1.0 04/08/88

BINTOINT(3)                Library Functions               BINTOINT(3)



NAME
     bintoint - give the binary value of a string of binary digits

SYNOPSIS
     #include <edlib.h>

     int bintoint(number)
     char *number;

DESCRIPTION
     Bintoint takes a string that may have been read in with scanf(3)
     or from the console, and treats it as if it were a binary number.
     The integer value of the binary number is returned. Bintoint
     stops at the first character that is not a '1' or a '0'. If the
     first character in the string is not a binary digit, then a value
     of 0 is returned.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     dectoint(3), hextoint(3), toint(3)



GETOPT(3)                  Library Functions                 GETOPT(3)



NAME
     getopt - get option letter from argv

SYNOPSIS
     #include <edlib.h>

     int getopt(argc, argv, optstring)
     inta argc;

     char **optstring;

     extern char *optarg;
     extern int optind;

DESCRIPTION
     Getopt returns the next option letter in argv that matches a
     letter in optstring.  Optstring is a string of recognized
     option letters; if a letter is followed by a colon, the
     option is expected to have an argument that may or may not
     be separated from it by white space.  Optarg is set to point
     to the start of the option argument on return from getopt.

     Getopt places in optind the argv index of the next argument
     to be processed.  Because optind is external, it is normally
     initialized to zero automatically before the first call to
     getopt.

     When an option that is not in the list occurs, a NULL is
     returned and the optarg pointer is set to point to the
     first character of the null terminated string. This is done
     so that options may be specified with other parameters
     interspersed between them.

DIAGNOSTICS
     Getopt prints an error message on stderr and returns a ques-
     tion mark (?) when it encounters an option letter not
     included in optstring.

EXAMPLE
     The following code fragment shows how one might process the
     arguments for a command that can take the mutually exclusive
     options a and b, and the options f and o, both of which
     require arguments:

          main(argc, argv)
          int argc;
          char **argv;
          {
               int c;
               extern int optind;
               extern char *optarg;
               .
               .
               .
                while ((c = getopt(argc, argv, "abf:o:")) != EOF)
                    switch (c) {
                    case `a':
                         if (bflg)
                              errflg++;
                         else
                              aflg++;
                         break;
                    case `b':
                         if (aflg)
                              errflg++;
                         else
                              bproc();
                         break;
                    case `f':
                         ifile = optarg;
                         break;
                    case `o':
                         ofile = optarg;
                         break;
                    case `?':
                    default:
                         errflg++;
                         break;
                    }
               if (errflg) {
                    fprintf(stderr, "Usage: ...");
                    exit(2);
               }
               for (; optind < argc; optind++) {
                    .
                    .
                    .
               }
               .
               .
               .
          }

HISTORY
     Written by Henry Spencer, working from a Bell Labs manual
     page.  Modified by Keith Bostic to behave more like the Sys-
     tem V version. Ported to the Amiga and modified to take
     options anywhere by Edwin (Deepthot) Hoogerbeets.

BUGS
     It is not obvious how `-' standing alone should be treated;
     this version treats it as a non-option argument, which is
     not always right.

     Option arguments are allowed to begin with `-'; this is rea-
     sonable but reduces the amount of error checking possible.
     Getopt is quite flexible but the obvious price must be paid:
     there is much it could do that it doesn't, like checking
     mutually exclusive options, checking type of option argu-
     ments, etc.




DECTOINT(3)                Library Functions               DECTOINT(3)





NAME
     dectoint - give the decimal value of a string of decimal digits

SYNOPSIS
     #include <edlib.h>

     int dectoint(number)
     char *number;

DESCRIPTION
     Dectoint takes a string that may have been read in with scanf(3)
     or from the console, and treats it as if it were a decimal number.
     The integer value of the decimal number is returned. Dectoint
     stops at the first character that is not a decimal digit. If the
     first character in the string is not a decimal digit, then a value
     of 0 is returned.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     bintoint(3), hextoint(3), toint(3)



HEXTOINT(3)                Library Functions               HEXTOINT(3)





NAME
     hextoint - give the decimal value of a string of decimal digits

SYNOPSIS
     #include <edlib.h>

     int hextoint(number)
     char *number;

DESCRIPTION
     Hextoint takes a string that may have been read in with scanf(3)
     or from the console, and treats it as if it were a hexadecimal
     number. The integer value of the hexadecimal number is returned.
     Hextoint stops at the first character that is not a hexadecimal
     digit. If the first character in the string is not a hexadecimal
     digit, then a value of 0 is returned.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     bintoint(3), dectoint(3), toint(3)



ISBDIGIT(3)                Library Functions               ISBDIGIT(3)



NAME
     isbdigit - tell whether given character is a binary digit

SYNOPSIS
     #include <edlib.h>

     int isbdigit(c)
     char c;

DESCRIPTION
     Isbdigit returns a 1 if the given characters is either a '1'
     or a '0'. Isbdigit returns a 0 for all other characters.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     isdigit(3), isodigit(3), isxdigit(3)



ISCSYM(3)                  Library Functions                 ISCSYM(3)



NAME
     iscsym - tell whether given character could be found in a C
     symbol

SYNOPSIS
     #include <edlib.h>

     int iscsym(c)
     char c;

DESCRIPTION
     Iscsym tells whether it is possible that the given character
     could appear in a C symbol. A C symbol might be the name of a
     variable, structure, reserved word or a function. Not all
     characters that are valid in a C symbol may be the first
     character of an identifier in most implementations of C. In
     most versions, the characters allowed after the first character
     are the upper and lower case alphabetic characters, the digits
     '0' to '9' and the underscore (_).

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     iscsymf(3)

BUGS
     This function depends on the C compiler used, and thus the
     characters that are valid in a C symbol may be different, even
     on the same machine.



ISCSYMF(3)                 Library Functions                ISCSYMF(3)



NAME
     iscsymf - tell whether given character could be found as the
     first character of a C symbol

SYNOPSIS
     #include <edlib.h>

     int iscsymf(c)
     char c;

DESCRIPTION
     Iscsymf tells whether it is possible that the given character
     could appear as the first character of a C symbol. A C symbol
     might be the the name of a variable, structure, reserved word
     or a functions. Not all characters that are valid in a C symbol
     may be the first character of an identifier in most implementations
     of C. In most versions, the characters allowed as the first
     character are the upper and lower case alphbetic characters and
     the underscore (_).

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     iscsym(3)

BUGS
     This function depends on the C compiler used, and thus the
     characters that are valid as the intial character in a C symbol
     may be different, even on the same machine.




ISODIGIT(3)                Library Functions               ISODIGIT(3)



NAME
     isodigit - tell whether given character is an octal digit

SYNOPSIS
     #include <edlib.h>

     int isodigit(c)
     char c;

DESCRIPTION
     Isodigit returns a 1 if the given characters is an octal digit.
     Isodigit returns a 0 for all other characters.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     isdigit(3), isbdigit(3), isxdigit(3)



STOUPPER(3)                Library Functions               STOUPPER(3)



NAME
     stoupper - convert a string to only upper case characters

SYNOPSIS
     #include <edlib.h>

     char *stoupper(str)
     char *str;

DESCRIPTION
     Stoupper takes a pointer to a null terminated string and
     converts each lower case character into its upper case
     equivalent. All other characters are left untouched.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     stolower(3)



STOLOWER(3)                Library Functions               STOLOWER(3)



NAME
     stolower - convert a string to only lower case characters

SYNOPSIS


     char *stolower(str)
     char *str;

DESCRIPTION
     Stolower takes a pointer to a null terminated string and
     converts each upper case character into its lower case
     equivalent. All other characters are left untouched.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     stoupper(3)




STRCSPN(3)                 Library Functions                STRCSPN(3)



NAME
     strcspn - find the length of the longest intial segment of a
     string that consists of characters not from a certain set.

SYNOPSIS
     #include <edlib.h>

     int strcspn(str, charset)
     char *str, *charset;

DESCRIPTION
     Strcspn searches the null terminated string 'str' for characters
     in the set 'charset'. The length of the longest intial string
     that consists of characters not from 'charset' is returned. If no
     characters of 'str' are also members of 'charset', then the
     length of 'str' is returned. If 'charset' is null then this
     will also cause the full length of 'str' to be returned.

     This function is also known as instr.

AUTHOR
     Daniel J. Barrett.
     barrett@cs.jhu.edu or ins_adjb@jhunix.UUCP

SEE ALSO
     strspn(3)



STRICMP(3)                 Library Functions                STRICMP(3)



NAME
     stricmp - compare two strings with case insensitivity

SYNOPSIS
     #include <edlib.h>

     int stricmp(str1,str2)
     char *str1,*str2;

DESCRIPTION
     Stricmp lexographically compares the two null terminated strings.
     It returns a number less than zero if the first differing
     character of str1 is less than that of str2, zero if the two
     strings are equal, and a number greater than zero if the
     first differing character in str1 is greater than the
     corresponding character of str2. Stricmp works like strcmp(3)
     except that all alphabetic characters are treated as lower case
     for the purposes of the comparison.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     strcmp(3), strncmp(3), strnicmp(3)



STRNICMP(3)                Library Functions               STRNICMP(3)



NAME
     strnicmp - compare two strings with case insensitivity up to
     a certain length

SYNOPSIS
     #include <edlib.h>

     int strnicmp(str1,str2,len)
     char *str1,*str2;
     int len;

DESCRIPTION
     Strnicmp lexographically compares the two null terminated
     strings up to the length 'len'. It returns a number less than
     zero if the first differing character of str1 is less than that
     of str2, zero if the two strings are equal, and a number greater
     than zero if the first differing character in str1 is greater
     than the corresponding character of str2. Strnicmp works like
     strncmp(3) except that all alphabetic characters are treated as
     lower case for the purposes of the comparison.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     strcmp(3), strncmp(3), stricmp(3)



STRPBRK(3)                 Library Functions                STRPBRK(3)



NAME
     strpbrk - find the first occurance of a character of a set in
     a string

SYNOPSIS
     #include <edlib.h>

     char *strpbrk(str, charset)
     char *str, *charset;

DESCRIPTION
     Strpbrk searches forwards through the null terminated string
     'str' for occurances of a character included in the character
     set 'charset'. The 'charset' variable is null terminated string
     that is treated as a character set. Therefore, repetition and
     order are ignored. Strpbrk returns a pointer to the first
     character of 'charset' that is found in 'str'.

DIAGNOSTICS
     If no character in 'charset' is found in 'str', then a null
     pointer (NULL) is returned.

AUTHOR
     Daniel J. Barrett.
     barrett@cs.jhu.edu or ins_adjb@jhunix.UUCP

SEE ALSO
     strrpbrk(3), strcspn(3), strspn(3)



STRPOS(3)                  Library Functions                 STRPOS(3)



NAME
     strpos - give the first position of a character withing a string

SYNOPSIS
     #include <edlib.h>

     int strpos(string,key)
     char *string;
     char key;

DESCRIPTION
     Strpos searches the null terminated string 'string' for the
     first occurance of the character 'key'. The position of this
     character is returned. The terminating null character is
     considered to be part of the string for the purposes of this
     search. Thus, using strpos to find the null will give the
     same result as a strlen(3).

     Some implementations of C use a variant called scnstr.

DIAGNOSTICS
     Strpos returns a -1 if the character is not found in the string.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     strrpos(3)



STRRPBRK(3)                Library Functions               STRRPBRK(3)



NAME
     strrpbrk - find the last occurance of a character of a set in
     a string

SYNOPSIS
     #include <edlib.h>

     char *strrpbrk(str, charset)
     char *str, *charset;

DESCRIPTION
     Strrpbrk searches backwards through the null terminated string
     'str' for occurances of a character included in the character
     set 'charset'. The 'charset' variable is null terminated string
     that is treated as a character set. Therefore, repetition and
     order are ignored. Strrpbrk returns a pointer to the last
     character of 'charset' that is found in 'str'.

DIAGNOSTICS
     If no character in 'charset' is found in 'str', then a null
     pointer (NULL) is returned.

AUTHOR
     Edwin Hoogerbeets 01/08/88 modified from strpbrk(3) by:
     Daniel J. Barrett.
     barrett@cs.jhu.edu or ins_adjb@jhunix.UUCP

SEE ALSO
     strpbrk(3), strcspn(3), strspn(3)



STRRPOS(3)                 Library Functions                STRRPOS(3)



NAME
     strrpos - give the last position of a character withing a string

SYNOPSIS
     #include <edlib.h>

     int strrpos(string,key)
     char *string;
     char key;

DESCRIPTION
     Strrpos searches the null terminated string 'string' for the
     last occurance of the character 'key'. The position of this
     character is returned. The terminating null character is
     considered to be part of the string for the purposes of this
     search. Thus, using strrpos to find the null will give the
     same result as a strlen(3).

DIAGNOSTICS
     Strrpos returns a -1 if the character is not found in the
     string.

AUTHOR
     Edwin Hoogerbeets 01/08/88

SEE ALSO
     strpos(3)



STRSPN(3)                  Library Functions                 STRSPN(3)



NAME
     strspn - find the length of the longest intial segment
     of a string that consists of characters from a certain set.

SYNOPSIS
     #include <edlib.h>

     int strspn(str, charset)
     char *str, *charset;

DESCRIPTION
     Strspn searches the null terminated string 'str' for characters
     in the set 'charset'. The length of the longest intial string
     that consists of characters from 'charset' is returned. If all
     characters of 'str' are also members of 'charset', then the
     length of 'str' is returned. If 'charset' is null then this
     function will return a zero as none of the characters in 'str'
     could possibly be in the set.

     This function is also known as notstr.

AUTHOR
     Daniel J. Barrett.
     barrett@cs.jhu.edu or ins_adjb@jhunix.UUCP

SEE ALSO
     strcspn(3)



STRTOK(3)                  Library Functions                 STRTOK(3)



NAME
     strtok - search a string for tokens delimited by characters
     from a set

SYNOPSIS
     #include <edlib.h>

     char *strtok(buf, separators)
     char *buf, *separators;

DESCRIPTION
     Strtok searches the null terminated string 'buf' for tokens
     delimited by characters from the character set 'separators'.
     The null terminated string 'separators' is treated as a set.
     Thus, repetition and order are ignored. Strtok replaces the
     separator character with a null byte and returns a pointer to
     the beginning of the token, effectively singling out the first
     token. Subsequent calls to strtok with the parameter 'buf' set
     to NULL returns the next token after the previous one using the
     same string as previous invocations. The character set
     'separators' may be different at each invocation.

DIAGNOSTICS
     If no token is found, a NULL pointer is returned.

EXAMPLE
     Here is an example program demonstrating strtok(3).

     #include <stdio.h>

     extern char *strtok();
     char tokesep[] = " \n\t\rx";

     main()
     {
             char buf[BUFSIZ], *tokep;

             while (fgets(buf, sizeof(buf), stdin)) {
                     tokep = strtok(buf, tokesep);
                     do {
                             printf("Token is %s\n", tokep);
                             tokep = strtok((char *)NULL, tokesep);
                     }while (tokep);
             }
     }

AUTHOR
     Daniel J. Barrett.
     barrett@cs.jhu.edu or ins_adjb@jhunix.UUCP



TOINT(3)                   Library Functions                  TOINT(3)



NAME
     toint - return the hexadecimal value of a character

SYNOPSIS
     #include <edlib.h>

     int toint(c)
     char c;

DESCRIPTION
     Toint treats the character 'c' as a hexadecimal character and
     returns its integer equivalent.

DIAGNOSTICS
     
     returned.

AUTHOR
     Edwin Hoogerbeets 01/08/88
SHAR_EOF
cat << \SHAR_EOF > stolower.c
/* edlib  version 1.0 of 04/08/88 */
/*
    string to lower changes all upper case letters in a string to lower
    case.
*/
#include <ctype.h>

char *stolower(str)
char *str;
{
    char *temp = str;

    for ( ; *temp ; temp++ )
        *temp = (char) tolower(*temp);

    return(str);
}
SHAR_EOF
cat << \SHAR_EOF > stoupper.c
/* edlib  version 1.0 of 04/08/88 */
/*
    string to upper changes all lower case letters in a string to upper
    case.
*/
#include <ctype.h>

char *stoupper(str)
char *str;
{
    char *temp = str;

    for ( ; *temp ; temp++ )
        *temp = (char) toupper(*temp);

    return(str);
}
SHAR_EOF
cat << \SHAR_EOF > strcspn.c
/* edlib  version 1.0 of 04/08/88 */
/* Return the number of characters NOT from "charset" that are at the
 * BEGINNING of string "string".
*/

int strcspn(str, charset)
char *str, *charset;
{
        char *temp = str;

        while (!strchr(charset, *temp))
                ++temp;

        return(temp - str);
}

SHAR_EOF
cat << \SHAR_EOF > stricmp.c
/* edlib  version 1.0 of 04/08/88 */
#include <ctype.h>

int stricmp(str1,str2)
char *str1,*str2;
{
    int index = 0;

    while ( str1[index] && str2[index] &&
            tolower(str1[index]) == tolower(str2[index]) )
        ++index;

    return( (tolower(str1[index]) < tolower(str2[index])) ? -1 :
          ( (tolower(str1[index]) > tolower(str2[index])) ?  1 : 0) );
}

SHAR_EOF
cat << \SHAR_EOF > strnicmp.c
/* edlib  version 1.0 of 04/08/88 */
#include <ctype.h>

int strnicmp(str1,str2,len)
char *str1,*str2;
int len;
{
    int index = 0;

    while ( str1[index] && str2[index] && index < len &&
            tolower(str1[index]) == tolower(str2[index]) )
        ++index;

    return( (tolower(str1[index]) < tolower(str2[index])) ? -1 :
          ( (tolower(str1[index]) > tolower(str2[index])) ?  1 : 0) );
}

SHAR_EOF
cat << \SHAR_EOF > strpbrk.c
/* edlib  version 1.0 of 04/08/88 */
#define NULL    0L

char *strpbrk(str, charset)
char *str, *charset;
{
        char *temp;
        extern char *index();

        temp = str;

        while ( *temp && !index(charset, *temp) )
                temp++;

        return( *temp ? temp : NULL);
}
SHAR_EOF
cat << \SHAR_EOF > strpos.c
/* edlib  version 1.0 of 04/08/88 */
/*
    strpos searches the null-terminated string string for the first
    occurance of the character "key". It returns either the position
    or EOF if it is not found.
*/

int strpos(string,key)
char *string;
char key;
{
    int counter = 0;

    if ( !key )
        return(strlen(string));

    while (string[counter]) {
        if (string[counter] == key)
            return(counter);
        counter++;
    }
    return(-1);
}
SHAR_EOF
cat << \SHAR_EOF > strrpbrk.c
/* edlib  version 1.0 of 04/08/88 */
#define NULL    0L

char *strrpbrk(str, charset)
char *str, *charset;
{
        char *temp;
        extern char *index();

        temp = str + strlen(str) - 1;

        while ( temp != (str - 1)  && !index(charset, *temp) )
                --temp;

        return( (temp != (str - 1)) ? temp : NULL);
}
SHAR_EOF
cat << \SHAR_EOF > strrpos.c
/* edlib  version 1.0 of 04/08/88 */
/*
    strrpos searches the null-terminated string string for the last
    occurance of the character "key". It returns either the position
    or -1 if it is not found.
*/

int strrpos(string,key)
char *string;
char key;
{
    char *temp;

    if ( !key )
        return(strlen(string));

    for (temp = string + strlen(string) - 1; temp >= string ; temp-- )
        if ( *temp == key)
            return(temp - string);

    return(-1);
}
SHAR_EOF
cat << \SHAR_EOF > strspn.c
/* edlib  version 1.0 of 04/08/88 */
/* Return the number of characters from "charset" that are at the BEGINNING
 * of string "str".
*/

int strspn(str, charset)
char *str, *charset;
{
        char *temp = str;

        while ( index(charset, *temp) )
                ++temp;

        return(temp - str);
}
SHAR_EOF
cat << \SHAR_EOF > strtok.c
/* edlib  version 1.0 of 04/08/88 */
#define STRING_END	'\0'
#ifndef NULL
#define NULL	0L
#endif

char *strtok(buf, separators)
char *buf, *separators;
{
	register char *token, *end;	/* Start and end of token. */
	extern char *strpbrk();
	static char *fromLastTime;

	if (token = buf ? buf : fromLastTime) {
		token += strspn(token, separators);	/* Find token! */
		if (*token == STRING_END)
			return(NULL);
		fromLastTime = ((end = strpbrk(token,separators))
				? &end[1]
				: NULL);
		*end = STRING_END;			/* Cut it short! */
	}
	return(token);
}
SHAR_EOF
cat << \SHAR_EOF > test.c
/* edlib  version 1.0 of 04/08/88 */
#include <stdio.h>
#include "edlib.h"
#include <ctype.h>
#include <exec/types.h>

char b[] = "10101";
char d[] = "12345";
char h[] = "2a";

char s1[] = "hey man this was in lower case.";
char s2[] = "THIS WAS ENTIRELY UPPER CASE IN THE FASHION OF IBM.";

char s3[] = "hey man this was in lower case.";
char s4[] = "THIS WAS ENTIRELY UPPER CASE IN THE FASHION OF IBM.";

main()
{
    printf("Bin: %d\n",bintoint(b));
    printf("Dec: %d\n",dectoint(d));
    printf("Hex: %d\n",hextoint(h));

    printf("Is 1 a bdigit? %d Is 'a'? %d\n",isbdigit('1'),isbdigit('a'));

    printf("iscsym: 'a' %d '_' %d '4' %d '!' %d '/' %d\n",iscsym('a'),
            iscsym('_'), iscsym('4'), iscsym('!'), iscsym('/'));

    printf("iscsymf: 'a' %d '_' %d '4' %d '!' %d '/' %d\n",iscsymf('a'),
            iscsymf('_'), iscsymf('4'), iscsymf('!'), iscsymf('/'));

    printf("isodigit: '2' %d '8' %d 'a' %d\n", isodigit('2'), isodigit('8'),
            isodigit('a'));

    printf("stoupper: %s\n",stoupper(s1));
    printf("stolower: %s\n",stolower(s2));

    printf("strcspn: '%s' '%s' gives %d\n",s3,"an",strcspn(s3,"an"));

    printf("stricmp: '%s' '%s' gives %d\n",s1,s3,stricmp(s1,s3));
    printf("strnicmp: '%s' '%s' 10 gives %d\n",s1,s3,strnicmp(s1,s3,10));

    printf("strpbrk: '%s' '%s' gives '%s'\n",s3,"a",strpbrk(s3,"a"));

    printf("strpos: '%s' '%c' gives %d\n",s3,'a',strpos(s3,'a'));

    printf("strrpbrk: '%s' '%s' gives '%s'\n",s3,"a",strrpbrk(s3,"a"));

    printf("strrpos: '%s' '%c' gives %d\n",s3,'a',strrpos(s3,'a'));

    printf("strspn: '%s' '%s' gives %d\n",s3,"ma yeh",strspn(s3,"ma yeh"));

    printf("strtok: '%s'\n",s3);
    printf("  tok1: '%s'\n",strtok(s3," "));
    printf("  tok2: '%s'\n",strtok(NULL," "));
    printf("  tok3: '%s'\n",strtok(NULL," "));

    printf("toint: '1' %d 'b' %d 'k' %d\n",toint('1'),toint('b'),toint('k'));

    exit(0);
}

/* Testing the following functions:

 bintoint.c                rwed     317    1  01-Aug-88 21:17:27
 dectoint.c                rwed     334    1  01-Aug-88 21:19:17
 hextoint.c                rwed     337    1  01-Aug-88 21:21:57
 isbdigit.c                rwed     236    1  23-Jun-88 22:18:41
 iscsym.c                  rwed     262    1  31-Jul-88 10:16:17
 iscsymf.c                 rwed     273    1  07-Jul-88 23:17:39
 isodigit.c                rwed     270    1  01-Aug-88 21:46:43
 stolower.c                rwed     213    1  02-Aug-88 23:08:39
 stoupper.c                rwed     213    1  01-Aug-88 21:50:24
 strcspn.c                 rwed     279    1  02-Aug-88 23:10:52
 stricmp.c                 rwed     321    1  02-Aug-88 23:30:25
 strnicmp.c                rwed     350    1  02-Aug-88 23:32:01
 strpbrk.c                 rwed     258    1  02-Aug-88 23:12:28
 strpos.c                  rwed     433    1  03-Aug-88 00:04:26
 strrpbrk.c                rwed     304    1  02-Aug-88 23:19:03
 strrpos.c                 rwed     442    1  03-Aug-88 00:09:53
 strspn.c                  rwed     270    1  02-Aug-88 23:14:16
 strtok.c                  rwed     515    2  23-Jun-88 20:38:17
 toint.c                   rwed     488    1  01-Aug-88 21:21:45

*/

SHAR_EOF
cat << \SHAR_EOF > toint.c
/* edlib  version 1.0 of 04/08/88 */
/*
    toint is also not included in Manx. converts an ascii character
    into its corresponding hexadecimal value. Can be used for
    binary and decimal digits since these are subsets of the
    hexadecimal character set.
*/
int toint(c)
char c;
{
    if ( c >= '0' && c <= '9') {
        return( c - '0' );
    } else if ( c >= 'a' && c <= 'f' ) {
        return( c - 'a' +10 );
    } else if ( c >= 'A' && c <= 'F' ) {
        return( c - 'A' +10 );
    } else
        return(-1);
}
SHAR_EOF
#	End of shell archive
exit 0
-- 
Bob Page, U of Lowell CS Dept.  page@swan.ulowell.edu  ulowell!page
Have five nice days.