akaros/kern/src/strstr.c
<<
>>
Prefs
   1/* Copyright © 1994-1999 Lucent Technologies Inc.  All rights reserved.
   2 * Portions Copyright © 1997-1999 Vita Nuova Limited
   3 * Portions Copyright © 2000-2007 Vita Nuova Holdings Limited
   4 *                                (www.vitanuova.com)
   5 * Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
   6 *
   7 * Modified for the Akaros operating system:
   8 * Copyright (c) 2013-2014 The Regents of the University of California
   9 * Copyright (c) 2013-2015 Google Inc.
  10 *
  11 * Permission is hereby granted, free of charge, to any person obtaining a copy
  12 * of this software and associated documentation files (the "Software"), to deal
  13 * in the Software without restriction, including without limitation the rights
  14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15 * copies of the Software, and to permit persons to whom the Software is
  16 * furnished to do so, subject to the following conditions:
  17 *
  18 * The above copyright notice and this permission notice shall be included in
  19 * all copies or substantial portions of the Software.
  20 *
  21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  27 * SOFTWARE. */
  28
  29#include <string.h>
  30
  31/*
  32 * Return pointer to first occurrence of s2 in s1,
  33 * 0 if none
  34 */
  35char *strstr(const char *s1, const char *s2)
  36{
  37        const char *p;
  38        int f, n;
  39
  40        f = s2[0];
  41        if (f == 0)
  42                return (char *) s1;
  43        n = strlen(s2);
  44        for (p = strchr(s1, f); p; p = strchr(p + 1, f))
  45                if (strncmp(p, s2, n) == 0)
  46                        return (char *) p;
  47        return NULL;
  48}
  49
  50/* Case insensitive strcmp */
  51int cistrcmp(char *s1, char *s2)
  52{
  53        int c1, c2;
  54
  55        while (*s1) {
  56                c1 = *(uint8_t *) s1++;
  57                c2 = *(uint8_t *) s2++;
  58
  59                if (c1 == c2)
  60                        continue;
  61
  62                if (c1 >= 'A' && c1 <= 'Z')
  63                        c1 -= 'A' - 'a';
  64
  65                if (c2 >= 'A' && c2 <= 'Z')
  66                        c2 -= 'A' - 'a';
  67
  68                if (c1 != c2)
  69                        return c1 - c2;
  70        }
  71        return -*s2;
  72}
  73