[16da5f8e] | 1 | /*
|
---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[2f57690] | 29 | /** @addtogroup generic
|
---|
[16da5f8e] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /**
|
---|
| 34 | * @file
|
---|
[2f57690] | 35 | * @brief Miscellaneous functions.
|
---|
[16da5f8e] | 36 | */
|
---|
| 37 |
|
---|
| 38 | #include <string.h>
|
---|
| 39 | #include <print.h>
|
---|
| 40 | #include <cpu.h>
|
---|
| 41 | #include <arch/asm.h>
|
---|
| 42 | #include <arch.h>
|
---|
| 43 | #include <console/kconsole.h>
|
---|
| 44 |
|
---|
| 45 | /** Return number of characters in a string.
|
---|
| 46 | *
|
---|
| 47 | * @param str NULL terminated string.
|
---|
| 48 | *
|
---|
| 49 | * @return Number of characters in str.
|
---|
[2f57690] | 50 | *
|
---|
[16da5f8e] | 51 | */
|
---|
| 52 | size_t strlen(const char *str)
|
---|
| 53 | {
|
---|
| 54 | int i;
|
---|
| 55 |
|
---|
[2f57690] | 56 | for (i = 0; str[i]; i++);
|
---|
[16da5f8e] | 57 |
|
---|
| 58 | return i;
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | /** Compare two NULL terminated strings
|
---|
| 62 | *
|
---|
| 63 | * Do a char-by-char comparison of two NULL terminated strings.
|
---|
| 64 | * The strings are considered equal iff they consist of the same
|
---|
| 65 | * characters on the minimum of their lengths.
|
---|
| 66 | *
|
---|
| 67 | * @param src First string to compare.
|
---|
| 68 | * @param dst Second string to compare.
|
---|
| 69 | *
|
---|
| 70 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
|
---|
| 71 | *
|
---|
| 72 | */
|
---|
| 73 | int strcmp(const char *src, const char *dst)
|
---|
| 74 | {
|
---|
| 75 | for (; *src && *dst; src++, dst++) {
|
---|
| 76 | if (*src < *dst)
|
---|
| 77 | return -1;
|
---|
| 78 | if (*src > *dst)
|
---|
| 79 | return 1;
|
---|
| 80 | }
|
---|
| 81 | if (*src == *dst)
|
---|
| 82 | return 0;
|
---|
[2f57690] | 83 |
|
---|
[16da5f8e] | 84 | if (!*src)
|
---|
| 85 | return -1;
|
---|
[2f57690] | 86 |
|
---|
[16da5f8e] | 87 | return 1;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | /** Compare two NULL terminated strings
|
---|
| 92 | *
|
---|
| 93 | * Do a char-by-char comparison of two NULL terminated strings.
|
---|
| 94 | * The strings are considered equal iff they consist of the same
|
---|
| 95 | * characters on the minimum of their lengths and specified maximal
|
---|
| 96 | * length.
|
---|
| 97 | *
|
---|
| 98 | * @param src First string to compare.
|
---|
| 99 | * @param dst Second string to compare.
|
---|
| 100 | * @param len Maximal length for comparison.
|
---|
| 101 | *
|
---|
| 102 | * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
|
---|
| 103 | *
|
---|
| 104 | */
|
---|
| 105 | int strncmp(const char *src, const char *dst, size_t len)
|
---|
| 106 | {
|
---|
| 107 | unsigned int i;
|
---|
| 108 |
|
---|
| 109 | for (i = 0; (*src) && (*dst) && (i < len); src++, dst++, i++) {
|
---|
| 110 | if (*src < *dst)
|
---|
| 111 | return -1;
|
---|
[2f57690] | 112 |
|
---|
[16da5f8e] | 113 | if (*src > *dst)
|
---|
| 114 | return 1;
|
---|
| 115 | }
|
---|
[2f57690] | 116 |
|
---|
[16da5f8e] | 117 | if (i == len || *src == *dst)
|
---|
| 118 | return 0;
|
---|
[2f57690] | 119 |
|
---|
[16da5f8e] | 120 | if (!*src)
|
---|
| 121 | return -1;
|
---|
[2f57690] | 122 |
|
---|
[16da5f8e] | 123 | return 1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 |
|
---|
| 127 |
|
---|
| 128 | /** Copy NULL terminated string.
|
---|
| 129 | *
|
---|
| 130 | * Copy at most 'len' characters from string 'src' to 'dest'.
|
---|
| 131 | * If 'src' is shorter than 'len', '\0' is inserted behind the
|
---|
| 132 | * last copied character.
|
---|
| 133 | *
|
---|
[2f57690] | 134 | * @param src Source string.
|
---|
[16da5f8e] | 135 | * @param dest Destination buffer.
|
---|
[2f57690] | 136 | * @param len Size of destination buffer.
|
---|
| 137 | *
|
---|
[16da5f8e] | 138 | */
|
---|
| 139 | void strncpy(char *dest, const char *src, size_t len)
|
---|
| 140 | {
|
---|
| 141 | unsigned int i;
|
---|
[2f57690] | 142 |
|
---|
[16da5f8e] | 143 | for (i = 0; i < len; i++) {
|
---|
| 144 | if (!(dest[i] = src[i]))
|
---|
| 145 | return;
|
---|
| 146 | }
|
---|
[2f57690] | 147 |
|
---|
[16da5f8e] | 148 | dest[i - 1] = '\0';
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[20f1597] | 151 | /** Find first occurence of character in string.
|
---|
| 152 | *
|
---|
[2f57690] | 153 | * @param s String to search.
|
---|
| 154 | * @param i Character to look for.
|
---|
[20f1597] | 155 | *
|
---|
[2f57690] | 156 | * @return Pointer to character in @a s or NULL if not found.
|
---|
[20f1597] | 157 | */
|
---|
| 158 | extern char *strchr(const char *s, int i)
|
---|
| 159 | {
|
---|
| 160 | while (*s != '\0') {
|
---|
[2f57690] | 161 | if (*s == i)
|
---|
| 162 | return (char *) s;
|
---|
[20f1597] | 163 | ++s;
|
---|
| 164 | }
|
---|
[2f57690] | 165 |
|
---|
[20f1597] | 166 | return NULL;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
[16da5f8e] | 169 | /** @}
|
---|
| 170 | */
|
---|