13

Is there a version of strstr that works over a fixed length of memory that may include null characters?

I could phrase my question like this: strncpy is to memcpy as strstr is to ?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
brian
  • 3,344
  • 2
  • 26
  • 35
  • 1
    Better you can write a new c program to have such functionality. i dont think it would be a complex thing. – Vijay Jan 02 '10 at 18:10

3 Answers3

17

memmem, unfortunately it's GNU-specific rather than standard C. However, it's open-source so you can copy the code (if the license is amenable to you).

8

Not in the standard library (which is not that large, so take a look). However writing your own is trivial, either directly byte by byte or using memchr() followed by memcmp() iteratively.

Clifford
  • 88,407
  • 13
  • 85
  • 165
  • +1, I like the idea to use memchr and memcpy. You can take a strstr implementation verbatim and replace all the str-functions with their mem-counterparts. – quinmars Jan 02 '10 at 21:55
  • 1
    Good implementations of strstr are not trivial at all -- they use sophisticated algorithms to work much, much faster than a trivial algorithm can. For example here's the core of the implementation in GNU libc: http://bazaar.launchpad.net/~vcs-imports/glibc/master/view/head:/string/str-two-way.h – Greg Price Sep 09 '15 at 21:22
  • @GregPrice : I am sot suggesting that the standard library `strstr()` is necessarily trivial; simply that the functionality that the OP requires can be trivially implemented, even if a generally optimal solution is less so. Standard library implementations must be highly optimised because their application is not known in advance. When writing code specific to an application, it only need be fast (or small) enough to meet he application needs. In this specific case an implementation similar to your example may be unwarranted. – Clifford Sep 10 '15 at 09:25
  • While the implementation of `strstr()` is surprisingly complex, the [GNU implementation](https://github.com/gcc-mirror/gcc/blob/master/libiberty/memmem.c) of `memmem()` is short and simple. If this function is not available on your platform, it's only a few lines long (and even shorter if you can do without all the pointer casting). – Zilk Jul 29 '23 at 23:46
0

In the standard library, no. However, a quick google search for "safe c string library" turns up several potentially useful results. Without knowing more about the task you are trying to perform, I cannot recommend any particular third-party implementation.

If this is the only "safe" function that you need beyond the standard functions, then it may be best to roll your own rather than expend the effort of integrating a third-party library, provided you are confident that you can do so without introducing additional bugs.

Adam
  • 406
  • 3
  • 9
  • 2
    I don't see the word "safe" in the question. –  Jan 02 '10 at 18:11
  • You are correct that I took a slightly different interpretation of the question. A broad reading of "may include nulls" could lead on to believe that it also may not include nulls. As lack of null termination is a major issue that leads people to consider `strncpy()` (and, indeed, `strlcpy()`), it seemed appropriate to address my answer from that direction, since other answers had already covered other approaches. I attempted to note this by stating "Without knowing more about the task ... I cannot recommend any particular third-party implementation." Perhaps I could have been more clear. – Adam Jan 02 '10 at 18:36