common.c 845 B

123456789101112131415161718192021222324252627282930
  1. #import <stdio.h>
  2. #import <sys/types.h>
  3. #import <string.h>
  4. /*
  5. * find the last occurrance of find in string
  6. *
  7. * Copyright 1998-2002 University of Illinois Board of Trustees
  8. * Copyright 1998-2002 Mark D. Roth
  9. * All rights reserved.
  10. *
  11. * strrstr.c - strrstr() function for compatibility library
  12. *
  13. * Mark D. Roth <roth@uiuc.edu>
  14. * Campus Information Technologies and Educational Services
  15. * University of Illinois at Urbana-Champaign
  16. */
  17. const char *common_strrstr(const char *string, const char *find) {
  18. size_t stringlen, findlen;
  19. char *cp;
  20. findlen = strlen(find);
  21. stringlen = strlen(string);
  22. if (findlen > stringlen)
  23. return NULL;
  24. for (cp = (char*)string + stringlen - findlen; cp >= string; cp--)
  25. if (strncmp(cp, find, findlen) == 0)
  26. return cp;
  27. return NULL;
  28. }
  29. // TODO: check if the system have strrstr and use that instead