[comp.os.minix] string

tholm@uvicctr.UUCP (Terrence W. Holm) (08/26/88)

EFTH MINIX report #29  - August 1988 -  string(3)


There follows an implementation of strchr(3), strrchr(3),
strspn(3), strcspn(3), strpbrk(3), strstr(3) and strtok(3)
for Minix. Please consider this public domain software.

An #include file and "man" pages are included.


Note: Henry Spencer has also written a very good set of public
domain string(3) routines. You may choose to use his instead.

----------------------------------------------------------
echo x - strcat.3
gres '^X' '' > strcat.3 << '/'
XSUBROUTINES
X    strcat(3)		- append a string onto another string
X
XINVOCATION
X    #include <string.h>
X
X    char *strcat( to, from )
X      char *to;
X      char *from;
X
X    char *strncat( to, from, count )
X      char *to;
X      char *from;
X      int   count;
X
XEXPLANATION
X    The string pointed to by <from> is appended onto the end of the
X    string pointed to by <to>. Strncat(3) will copy no more than
X    <count> characters.
X
XRESULTS
X    <to>.
/
echo x - strchr.3
gres '^X' '' > strchr.3 << '/'
XSUBROUTINES
X    strchr(3)		- find a character in a string
X
XINVOCATION
X    #include <string.h>
X
X    char *strchr( string, chr )
X      char *string;
X      int   chr;
X
X    char *strrchr( string, chr )
X      char *string;
X      int   chr;
X
X    char *index( string, chr )
X      char *string;
X      char  chr;
X
X    char *rindex( string, chr )
X      char *string;
X      char  chr;
X
XEXPLANATION
X    These functions search for a character <chr> in the string
X    pointed to by <string>. Strchr(3) starts at the beginning of
X    the string and strrchr(3) searches backwards from the end of
X    the string.
X
X    Index(3) is the same as strchr(3). Rindex(3) is the same
X    as strrchr(3)
X
XRESULTS
X    A pointer to the character within the string, if found,
X    otherwise returns NULL.
/
echo x - strchr.c
gres '^X' '' > strchr.c << '/'
X/*  strchr(3) 
X *
X *  Derived from MINIX index(3)
X */
X
X
X#define  NULL  (char *) 0
X
X
Xchar *strchr( string, chr )
X  register char *string;
X  register char  chr;
X
X  {
X  do {
X     if ( *string == chr )
X	return( string );
X     } while ( *string++ != '\0' );
X
X  return( NULL );
X  }
/
echo x - strcmp.3
gres '^X' '' > strcmp.3 << '/'
XSUBROUTINES
X    strcmp(3)		- compare two strings
X
XINVOCATION
X    #include <string.h>
X
X    int strcmp( string1, string2 )
X      char *string1;
X      char *string2;
X
X    int strncmp( string1, string2, size )
X      char *string1;
X      char *string2;
X      int   size;
X
XEXPLANATION
X    The string pointed to by <string1> is compared to the string
X    pointed to by <string2>. The comparison is based on the
X    ordering of ASCII characters. Strncmp(3) compares up to <count>
X    characters.
X
XRESULTS
X    <0 : string1 is less than string2
X    =0 : string1 is equal to string2
X    >0 : string1 is greater than string2
/
echo x - strcpy.3
gres '^X' '' > strcpy.3 << '/'
XSUBROUTINES
X    strcpy(3)		- copy a string
X
XINVOCATION
X    #include <string.h>
X
X    char *strcpy( to, from )
X      char *to;
X      char *from;
X
X    char *strncpy( to, from, count )
X      char *to;
X      char *from;
X      int   count;
X
XEXPLANATION
X    The string pointed to by <from> is copied to the storage pointed
X    to by <to>. Strncpy(3) will copy <count> characters, if <from>
X    was too short then the result will be padded with '\0's, if
X    <from> is too long then the result will not have a final '\0'.
X
XRESULTS
X    <to>.
/
echo x - strcspn.c
gres '^X' '' > strcspn.c << '/'
X/*  strcspn(3)
X *
X *  Author:  Terrence W. Holm          July 1988
X *
X *
X *  This function determines the length of a span from the
X *  beginning of <string> which contains none of the
X *  characters specified in <char_set>. The length of the
X *  span is returned.
X */
X
X#define NULL (char *) 0
X
X
Xint strcspn( string, char_set )
X  char *string;
X  char *char_set;
X
X  {
X  register char *str;
X  register char *chr;
X
X  if ( string == NULL )
X      return( 0 );
X
X  if ( char_set == NULL )
X      return( strlen(string) );
X
X  for ( str = string;  *str != '\0';  ++str )
X      for ( chr = char_set;  *chr != '\0';  ++chr )
X	  if ( *str == *chr )
X	      return( str - string );
X
X  return( str - string );
X  }
/
echo x - string.h
gres '^X' '' > string.h << '/'
X/*  string.h  (a.k.a. strings.h)  */
X
Xint strlen();
Xint strcmp(), strncmp();
Xint strspn(), strcspn();
X
Xchar *strcpy(), *strncpy();
Xchar *strcat(), *strncat();
Xchar *index(),  *rindex();
Xchar *strchr(), *strrchr();
Xchar *strpbrk();
Xchar *strtok();
Xchar *strstr();
/
echo x - strlen.3
gres '^X' '' > strlen.3 << '/'
XSUBROUTINES
X    strlen(3)		- find the length of a string
X
XINVOCATION
X    #include <string.h>
X
X    int strlen( string )
X      char *strlen;
X
XRESULTS
X    The size of the string pointed to by <string>. Note that the
X    final '\0' is not counted.
/
echo x - strpbrk.3
gres '^X' '' > strpbrk.3 << '/'
XSUBROUTINES
X    strpbrk(3)		- find one of a set of characters a string
X
XINVOCATION
X    #include <string.h>
X
X    char *strpbrk( string, char_set )
X      char *string;
X      char *char_set;
X
XEXPLANATION
X    Strpbrk(3) scans <string> for the first occurrence of a character
X    from the string <char_set>.
X
XRESULTS
X    If a character from the <char_set> was found then a pointer to it
X    within <string> is returned, otherwise NULL is returned.
X
XREFERENCES
X    strspn(3)
/
echo x - strpbrk.c
gres '^X' '' > strpbrk.c << '/'
X/*  strpbrk(3)
X *
X *  Author:  Terrence W. Holm          July 1988
X *
X *
X *  Strpbrk(3) scans <string> for the first occurrence of a
X *  character from the string <char_set>. If a character from
X *  the <char_set> was found then a pointer to it within
X *  <string> is returned, otherwise NULL is returned.
X */
X
X#define NULL (char *) 0
X
X
Xchar *strpbrk( string, char_set )
X  char *string;
X  char *char_set;
X
X  {
X  register char  c;
X  register char *p;
X
X  if ( string == NULL  ||  char_set == NULL )
X      return( NULL );
X
X  while ( (c = *string++) != '\0' )
X      for ( p = char_set;  *p != '\0';  ++p )
X	  if ( c == *p )
X	    return( string - 1 );
X
X  return( NULL );
X  }
/
echo x - strrchr.c
gres '^X' '' > strrchr.c << '/'
X/*  strrchr(3) 
X *
X *  Derived from MINIX rindex(3)
X */
X
X
X#define  NULL  (char *) 0
X
X
Xchar *strrchr( string, chr )
X  register char *string;
X  register char  chr;
X
X  {
X  char *index = NULL;
X
X  do {
X     if ( *string == chr )
X	index = string;
X     } while ( *string++ != '\0' );
X
X  return( index );
X  }
/
echo x - strspn.3
gres '^X' '' > strspn.3 << '/'
XSUBROUTINES
X    strspn(3)		- look for a span of a set of characters in a string
X
XINVOCATION
X    #include <string.h>
X
X    int strspn( string, char_set )
X      char *string;
X      char *char_set;
X
X    int strcspn( string, char_set )
X      char *string;
X      char *char_set;
X
XEXPLANATION
X    These functions are used to determine the length of a span of
X    characters at the beginning of <string>. Strspn(3) finds a span
X    containing only characters specified in <char_set>. Strcspn(3)
X    finds the span containing no characters from <char_set>.
X
XRESULTS
X    The length of the span.
X
XREFERENCES
X    strpbrk(3)
/
echo x - strspn.c
gres '^X' '' > strspn.c << '/'
X/*  strspn(3)
X *
X *  Author:  Terrence W. Holm          July 1988
X *
X *
X *  This function determines the length of a span from the
X *  beginning of <string> which contains only characters
X *  specified in <char_set>. The length of the span is
X *  returned.
X */
X
X#define NULL (char *) 0
X
X
Xint strspn( string, char_set )
X  char *string;
X  char *char_set;
X
X  {
X  register char *str;
X  register char *chr;
X
X  if ( string == NULL  ||  char_set == NULL )
X      return( 0 );
X
X  for ( str = string;  *str != '\0';  ++str )
X      {
X      for ( chr = char_set;  *chr != '\0';  ++chr )
X	  if ( *str == *chr )
X	      break;
X
X      if ( *chr == '\0' )
X	  return( str - string );
X      }
X
X  return( str - string );
X  }
/
echo x - strstr.3
gres '^X' '' > strstr.3 << '/'
XSUBROUTINES
X    strstr(3)		- find a substring in a string
X
XINVOCATION
X    #include <string.h>
X
X    char *strstr( string, substr )
X      char *string;
X      char *substr;
X
XEXPLANATION
X    Finds the first occurrence of a substring, pointed to by
X    <substr>, within a string pointed to by <string>.
X
XRESULTS
X    If the substring is found then a pointer to it within
X    <string> is returned, otherwise NULL is returned.
/
echo x - strstr.c
gres '^X' '' > strstr.c << '/'
X/*  strstr(3)
X *
X *  Author: Terrence W. Holm          July 1988
X *
X *
X *  Finds the first occurrence of a substring, pointed to by
X *  <substr>, within a string pointed to by <string>.
X *  If the substring is found then a pointer to it within
X *  <string> is returned, otherwise NULL is returned.
X */
X
X#define NULL (char *) 0
X
X
Xchar *strstr( string, substr )
X  char *string;
X  char *substr;
X
X  {
X  register char head_string;
X  register char head_substr;
X
X  if ( string == NULL  ||  substr == NULL )
X      return( NULL );
X
X  head_substr = *substr++;
X
X  while ( (head_string = *string++) != '\0' )
X    if ( head_string == head_substr )
X	{
X	register char *tail_string = string;
X	register char *tail_substr = substr;
X
X	do  {
X	    if ( *tail_substr == '\0' )
X		return( string - 1 );
X	    } while ( *tail_string++ == *tail_substr++ );
X	}
X
X  return( NULL );
X  }
/
echo x - strtok.3
gres '^X' '' > strtok.3 << '/'
XSUBROUTINES
X    strtok(3)		- scan for a token in a string
X
XINVOCATION
X    #include <string.h>
X
X    char *strtok( string, char_set )
X      char *string;
X      char *char_set;
X
X    char *strtok( NULL, char_set )
X      char *char_set;
X
XEXPLANATION
X    This function is used to divide up a string into tokens.
X    Strtok(3) is called with <string> pointing to the string
X    to be scanned and <char_set> pointing to a string which
X    consists of the set of separator characters. Tokens are
X    substrings bordered by separator characters. A pointer to
X    the first token encountered is returned.
X
X    Subsequent scans of the same <string> use the second call
X    format.
X
XRESULTS
X    A pointer to the next token. Each token is terminated by
X    a '\0'. If there are no tokens remaining in the string
X    then NULL is returned.
/
echo x - strtok.c
gres '^X' '' > strtok.c << '/'
X/*  strtok(3)
X *
X *  Author: Terrence W. Holm          July 1988
X *
X *
X *  This function is used to divide up a string into tokens.
X *  Strtok(3) is called with <string> pointing to the string
X *  to be scanned and <char_set> pointing to a string which
X *  consists of the set of separator characters. Tokens are
X *  substrings bordered by separator characters. A pointer to
X *  the first token encountered is returned. If <string> is
X *  NULL then the scan is continued from the last token
X *  returned. Each token is terminated by a '\0'. If there are
X *  no tokens remaining in the string then NULL is returned.
X */
X
X#define NULL (char *) 0
X
X
Xchar *strtok( string, char_set )
X  char *string;
X  char *char_set;
X
X  {
X  static char *last_string = "";
X  register char *chr;
X  char *next_token;
X
X  if ( string == NULL )
X      string = last_string;
X
X  if ( char_set == NULL )
X      return( NULL );
X
X
X  /*  First skip over any separator characters  */
X
X  while ( *string != '\0' )
X      {
X      for ( chr = char_set;  *chr != '\0';  ++chr )
X	  if ( *string == *chr )
X	      break;
X
X      if ( *chr == '\0' )
X	  break;
X
X      ++string;
X      }
X
X
X  /*  Check if we have reached the end of the string  */
X
X  if ( *string == '\0' )
X      return( NULL );
X
X
X  /*  If not, then we have found the next token  */
X
X  next_token = string;
X
X
X  /*  Scan for the end of this token  */
X
X  while ( *string != '\0' )
X      {
X      for ( chr = char_set;  *chr != '\0';  ++chr )
X	  if ( *string == *chr )
X	      {
X	      *string = '\0';
X	      last_string = string + 1;
X	      return( next_token );
X	      }
X
X      ++string;
X      }
X
X  last_string = string;
X  return( next_token );
X  }
/
----------------------------------------------------------

		Edwin L. Froese
		  uw-beaver!ubc-cs!mprg!handel!froese

		Terrence W. Holm
		  uw-beaver!uvicctr!tholm