summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGreg McGary <greg@mcgary.org>1999-04-12 07:56:55 +0000
committerGreg McGary <greg@mcgary.org>1999-04-12 07:56:55 +0000
commita05ba261e9c40c9ef95b286d7a8abff519551295 (patch)
treea6ba2a8343cfcc9838e52b5ef4a75c238c8598b7
parent258a961c23341294b12a317ad6534b30ad02d987 (diff)
downloadidutils-a05ba261e9c40c9ef95b286d7a8abff519551295.tar.gz
idutils-a05ba261e9c40c9ef95b286d7a8abff519551295.tar.bz2
idutils-a05ba261e9c40c9ef95b286d7a8abff519551295.zip
* lib/strsep.c: Add file.
* lib/strtok.c: Remove file. * configure.in: Check for strsep instead of strtok. * libidu/idfile.c (locate_id_file_name): Use strsep instead of strtok. * libidu/scanners.c (language_save_arg, language_save_arg, tokenize_args_string): Ditto. * libidu/walker.c (append_strings_to_vector, vectorize_string): Ditto.
-rw-r--r--configure.in4
-rw-r--r--lib/strsep.c62
-rw-r--r--lib/strtok.c73
-rw-r--r--libidu/idfile.c4
-rw-r--r--libidu/scanners.c8
-rw-r--r--libidu/walker.c23
6 files changed, 86 insertions, 88 deletions
diff --git a/configure.in b/configure.in
index c4709e2..8be4710 100644
--- a/configure.in
+++ b/configure.in
@@ -100,9 +100,11 @@ AC_FUNC_ALLOCA
AC_FUNC_VPRINTF
AM_FUNC_OBSTACK
AC_CHECK_FUNCS(getwd getcwd link strerror isascii bcopy bzero memcpy)
-AC_REPLACE_FUNCS(strdup strndup strspn strcspn strpbrk strstr strtok)
+AC_REPLACE_FUNCS(strdup strndup strspn strcspn strpbrk strstr strsep)
AC_REPLACE_FUNCS(basename dirname fnmatch error memcpy memset)
AC_CHECK_FUNC(regcomp, , [LIBOBJS="$LIBOBJS regex.o"])
+jm_FUNC_MALLOC
+jm_FUNC_REALLOC
##############################################################################
################ Internationalization ########################################
diff --git a/lib/strsep.c b/lib/strsep.c
new file mode 100644
index 0000000..a118b1b
--- /dev/null
+++ b/lib/strsep.c
@@ -0,0 +1,62 @@
+/* Copyright (C) 1992, 1993, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <string.h>
+
+char *
+strsep (char **stringp, const char *delim)
+{
+ char *begin, *end;
+
+ begin = *stringp;
+ if (begin == NULL)
+ return NULL;
+
+ /* A frequent case is when the delimiter string contains only one
+ character. Here we don't need to call the expensive `strpbrk'
+ function and instead work using `strchr'. */
+ if (delim[0] == '\0' || delim[1] == '\0')
+ {
+ char ch = delim[0];
+
+ if (ch == '\0')
+ end = NULL;
+ else
+ {
+ if (*begin == ch)
+ end = begin;
+ else
+ end = strchr (begin + 1, ch);
+ }
+ }
+ else
+ /* Find the end of the token. */
+ end = strpbrk (begin, delim);
+
+ if (end)
+ {
+ /* Terminate the token and set *STRINGP past NUL character. */
+ *end++ = '\0';
+ *stringp = end;
+ }
+ else
+ /* No more delimiters; this is the last token. */
+ *stringp = NULL;
+
+ return begin;
+}
diff --git a/lib/strtok.c b/lib/strtok.c
deleted file mode 100644
index 0b95084..0000000
--- a/lib/strtok.c
+++ /dev/null
@@ -1,73 +0,0 @@
-/* Copyright (C) 1991 Free Software Foundation, Inc.
-This file is part of the GNU C Library.
-
-The GNU C Library is free software; you can redistribute it and/or
-modify it under the terms of the GNU Library General Public License as
-published by the Free Software Foundation; either version 2 of the
-License, or (at your option) any later version.
-
-The GNU C Library 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
-Library General Public License for more details.
-
-You should have received a copy of the GNU Library General Public
-License along with the GNU C Library; see the file COPYING.LIB. If
-not, write to the Free Software Foundation, Inc., 675 Mass Ave,
-Cambridge, MA 02139, USA. */
-
-#include <ansidecl.h>
-#include <errno.h>
-#include <string.h>
-
-
-static char *olds = NULL;
-
-/* Parse S into tokens separated by characters in DELIM.
- If S is NULL, the last string strtok() was called with is
- used. For example:
- char s[] = "-abc=-def";
- x = strtok(s, "-"); // x = "abc"
- x = strtok(NULL, "=-"); // x = "def"
- x = strtok(NULL, "="); // x = NULL
- // s = "abc\0-def\0"
-*/
-char *
-DEFUN(strtok, (s, delim),
- register char *s AND register CONST char *delim)
-{
- char *token;
-
- if (s == NULL)
- {
- if (olds == NULL)
- {
- errno = EINVAL;
- 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;
-}
diff --git a/libidu/idfile.c b/libidu/idfile.c
index d27c4b3..1b46ab7 100644
--- a/libidu/idfile.c
+++ b/libidu/idfile.c
@@ -1,5 +1,5 @@
/* idfile.c -- read & write mkid database file header
- Copyright (C) 1986, 1995, 1996 Free Software Foundation, Inc.
+ Copyright (C) 1986, 1995, 1996, 1999 Free Software Foundation, Inc.
Written by Greg McGary <gkm@gnu.ai.mit.edu>
This program is free software; you can redistribute it and/or modify
@@ -54,7 +54,7 @@ locate_id_file_name (char const *arg)
if (id_path)
{
id_path = strdup (id_path);
- arg = strtok (id_path, ":");
+ arg = strsep (&id_path, ":");
/* FIXME: handle multiple ID file names */
}
}
diff --git a/libidu/scanners.c b/libidu/scanners.c
index 0db6bcd..261837a 100644
--- a/libidu/scanners.c
+++ b/libidu/scanners.c
@@ -87,7 +87,7 @@ void
language_save_arg (char *arg)
{
static char horizontal_space[] = " \t";
- char *lang_name = strtok (arg, ":");
+ char *lang_name = strsep (&arg, ":");
struct language *lang = get_language (lang_name);
if (lang == 0)
@@ -97,7 +97,7 @@ language_save_arg (char *arg)
}
if (lang->lg_argc == 0)
lang->lg_argv[lang->lg_argc++] = program_name;
- lang->lg_argv[lang->lg_argc++] = strtok (0, horizontal_space);
+ lang->lg_argv[lang->lg_argc++] = strsep (&arg, horizontal_space);
}
void
@@ -287,11 +287,11 @@ tokenize_args_string (char *args_string, int *argcp, char ***argvp)
char *arg;
*argv++ = program_name;
- arg = strtok (args_string, horizontal_space);
+ arg = strsep (&args_string, horizontal_space);
while (arg)
{
*argv++ = arg;
- arg = strtok (0, horizontal_space);
+ arg = strsep (&args_string, horizontal_space);
}
*argcp = argv - argv_0;
*argvp = REALLOC (argv_0, char *, *argcp);
diff --git a/libidu/walker.c b/libidu/walker.c
index 9a7372e..6584179 100644
--- a/libidu/walker.c
+++ b/libidu/walker.c
@@ -498,10 +498,10 @@ append_strings_to_vector (char **vector_0, char *string, char const *delimiter_c
else
vector = vector_0 = MALLOC (char *, 2 + strlen (string) / 2);
- *vector = strtok (string, delimiter_class);
- while (*vector)
- *++vector = strtok (0, delimiter_class);
- return REALLOC (vector_0, char *, ++vector - vector_0);
+ do
+ *vector = strsep (&string, delimiter_class);
+ while (*vector++);
+ return REALLOC (vector_0, char *, vector - vector_0);
}
int
@@ -673,10 +673,17 @@ vectorize_string (char *string, char const *delimiter_class)
char **vector_0 = MALLOC (char *, 2 + strlen (string) / 2);
char **vector = vector_0;
- *vector = strtok (string, delimiter_class);
- while (*vector)
- *++vector = strtok (0, delimiter_class);
- return REALLOC (vector_0, char *, ++vector - vector_0);
+ *vector++ = strsep (&string, delimiter_class);
+ if (vector[-1])
+ {
+ /* Toss first field if empty */
+ if (vector[-1][0] == '\0')
+ vector--;
+ do
+ *vector = strsep (&string, delimiter_class);
+ while (*vector++);
+ }
+ return REALLOC (vector_0, char *, vector - vector_0);
}
void