File: strfunc.c

package info (click to toggle)
units 2.24-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,488 kB
  • sloc: ansic: 6,512; sh: 899; python: 796; yacc: 457; makefile: 442; perl: 435
file content (109 lines) | stat: -rw-r--r-- 2,201 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 *  Copyright (C) 1996 Free Software Foundation, Inc
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *     
 */

#define NULL 0

#define size_t int

#ifdef NO_STRTOK

/* Find the first ocurrence in S of any character in ACCEPT.  */
char *
strpbrk(char *s, char *accept)
{
  while (*s != '\0')
    if (strchr(accept, *s) == NULL)
      ++s;
    else
      return (char *) s;

  return NULL;
}


static char *olds = NULL;

char *
strtok(char *s, char *delim)
{
  char *token;

  if (s == NULL)
    {
      if (olds == NULL)
	{
	  /*errno = EINVAL;  Wonder where errno is defined....*/
	  return NULL;
	}
      else
	s = olds;
    }

  /* Scan leading delimiters.  */
  s += strspn(s, delim);
  if (*s == '\0')
    {
      olds = NULL;
      return NULL;
    }

  /* Find the end of the token.  */
  token = s;
  s = strpbrk(token, delim);
  if (s == NULL)
    /* This token finishes the string.  */
    olds = NULL;
  else
    {
      /* Terminate the token and make OLDS point past it.  */
      *s = '\0';
      olds = s + 1;
    }
  return token;
}


#endif /* NO_STRTOK */

#ifdef NO_STRSPN

/* Return the length of the maximum initial segment
   of S which contains only characters in ACCEPT.  */
size_t
strspn(char *s, char *accept)
{
  register char *p;
  register char *a;
  register size_t count = 0;

  for (p = s; *p != '\0'; ++p)
    {
      for (a = accept; *a != '\0'; ++a)
	if (*p == *a)
	  break;
      if (*a == '\0')
	return count;
      else
	++count;
    }

  return count;
}

#endif NO_STRSPN