[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] strnstr
From: |
Simon Josefsson |
Subject: |
[Bug-gnulib] strnstr |
Date: |
Wed, 29 Sep 2004 19:35:17 +0200 |
User-agent: |
Gnus/5.110003 (No Gnus v0.3) Emacs/21.3.50 (gnu/linux) |
Darwin and FreeBSD has this, and GnuTLS is using it. Only lightly
tested.
2004-09-29 Simon Josefsson <address@hidden>
* tests/test-strnstr.c: New file.
* MODULES.html.sh (Extra functions based on ANSI C 89): Add
strnstr.
* modules/strnstr: New file.
2004-09-29 Simon Josefsson <address@hidden>
* strnstr.m4: New file.
2004-09-29 Simon Josefsson <address@hidden>
* strnstr.h, strnstr.c: New file.
Index: MODULES.html.sh
===================================================================
RCS file: /cvsroot/gnulib/gnulib/MODULES.html.sh,v
retrieving revision 1.60
diff -u -p -r1.60 MODULES.html.sh
--- MODULES.html.sh 8 Sep 2004 12:43:11 -0000 1.60
+++ MODULES.html.sh 29 Sep 2004 17:34:40 -0000
@@ -1518,6 +1518,7 @@ func_all_modules ()
func_module strdup
func_module strnlen
func_module strndup
+ func_module strnstr
#func_module fstrcmp
func_module xstrndup
func_end_table
Index: modules/strnstr
===================================================================
RCS file: modules/strnstr
diff -N modules/strnstr
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ modules/strnstr 29 Sep 2004 17:34:40 -0000
@@ -0,0 +1,25 @@
+Description:
+strnstr() function: locate substring in the prefix of a string.
+
+Files:
+lib/strnstr.h
+lib/strnstr.c
+m4/strnstr.m4
+
+Depends-on:
+minmax
+
+configure.ac:
+gl_FUNC_STRNSTR
+
+Makefile.am:
+lib_SOURCES += strnstr.h
+
+Include:
+"strnstr.h"
+
+License:
+LGPL
+
+Maintainer:
+Simon Josefsson
Index: m4/strnstr.m4
===================================================================
RCS file: m4/strnstr.m4
diff -N m4/strnstr.m4
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ m4/strnstr.m4 29 Sep 2004 17:34:40 -0000
@@ -0,0 +1,17 @@
+# strnstr.m4 serial 1
+dnl Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
+dnl This file is free software, distributed under the terms of the GNU
+dnl General Public License. As a special exception to the GNU General
+dnl Public License, this file may be distributed as part of a program
+dnl that contains a configuration script generated by Autoconf, under
+dnl the same distribution terms as the rest of that program.
+
+AC_DEFUN([gl_FUNC_STRNSTR],
+[
+ AC_REPLACE_FUNCS(strnstr)
+ AC_CHECK_DECLS_ONCE(strnstr)
+ gl_PREREQ_STRNSTR
+])
+
+# Prerequisites of lib/strnstr.c.
+AC_DEFUN([gl_PREREQ_STRNSTR], [:])
Index: lib/strnstr.h
===================================================================
RCS file: lib/strnstr.h
diff -N lib/strnstr.h
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/strnstr.h 29 Sep 2004 17:34:40 -0000
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * 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 2, 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., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA. */
+
+#ifndef STRNSTR_H
+# define STRNSTR_H
+
+/* Get strnstr, if available. */
+# include <string.h>
+
+# if defined HAVE_DECL_STRNSTR && !HAVE_DECL_STRNSTR
+char *strnstr (const char *big, const char *little, size_t len);
+# endif
+
+#endif /* STRNSTR_H */
Index: lib/strnstr.c
===================================================================
RCS file: lib/strnstr.c
diff -N lib/strnstr.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ lib/strnstr.c 29 Sep 2004 17:34:40 -0000
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * 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 2, 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., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+/* Get MIN. */
+#include <minmax.h>
+
+/* Get strlen, strncmp. */
+#include <string.h>
+
+/* Locate first occurance of zero terminated string LITLE in the first
+ MIN(LEN, strlen(BIG)) characters of the string BIG. Returns
+ pointer to match within BIG, or NULL if no match is found. If
+ LITTLE is the empty string, BIG is returned. */
+char *
+strnstr (const char *big, const char *little, size_t len)
+{
+ size_t searchlen = MIN (len, strlen (big));
+ size_t littlelen = strlen (little);
+ char *p = (char*) big;
+ size_t i;
+
+ if (*little == '\0')
+ return p;
+
+ if (searchlen < littlelen)
+ return NULL;
+
+ for (i = 0; i <= searchlen - littlelen; i++)
+ if (strncmp (&p[i], little, littlelen) == 0)
+ return &p[i];
+
+ return NULL;
+}
Index: tests/test-strnstr.c
===================================================================
RCS file: tests/test-strnstr.c
diff -N tests/test-strnstr.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ tests/test-strnstr.c 29 Sep 2004 17:34:40 -0000
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2004 Free Software Foundation
+ * Written by Simon Josefsson
+ *
+ * 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 2, 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., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA. */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <strnstr.h>
+
+int
+main ()
+{
+ const char *big = "foo-bar-baz";
+ char *p;
+
+ p = strnstr (big, "foo", strlen (big));
+ if (p != big)
+ fprintf (stderr, "strnstr FAILURE 1\n");
+
+ p = strnstr (big, "baz", strlen (big));
+ if (p != big + strlen (big) - 3)
+ fprintf (stderr, "strnstr FAILURE 2\n");
+
+ p = strnstr (big, "-", strlen (big));
+ if (p != big + 3)
+ fprintf (stderr, "strnstr FAILURE 3\n");
+
+ p = strnstr (big, "baz", strlen (big) - 1);
+ if (p != NULL)
+ fprintf (stderr, "strnstr FAILURE 4\n");
+}
- [Bug-gnulib] strnstr,
Simon Josefsson <=
- Re: [Bug-gnulib] strnstr, Bruno Haible, 2004/09/29
- Re: [Bug-gnulib] strnstr, Paul Eggert, 2004/09/29
- [Bug-gnulib] Re: strnstr, Simon Josefsson, 2004/09/29
- [Bug-gnulib] memstr (was: Re: strnstr), Simon Josefsson, 2004/09/30
- Re: [Bug-gnulib] memstr, Paul Eggert, 2004/09/30
- [Bug-gnulib] Re: memstr, Simon Josefsson, 2004/09/30
- Re: [Bug-gnulib] Re: memstr, Paul Eggert, 2004/09/30