source: mainline/generic/src/lib/func.c@ 9d3e185

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 9d3e185 was 2cf5634, checked in by Jakub Jermar <jakub@…>, 20 years ago

Make the kernel compile with -Wall.

  • Property mode set to 100644
File size: 4.5 KB
RevLine 
[f761f1eb]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
29#include <func.h>
30#include <print.h>
31#include <cpu.h>
32#include <arch/asm.h>
33#include <arch.h>
[ff3b3197]34#include <typedefs.h>
[93b84b3]35#include <console/kconsole.h>
[f761f1eb]36
[36e7ee98]37atomic_t haltstate = {0}; /**< Halt flag */
[f761f1eb]38
[d34657e]39
40/** Halt wrapper
41 *
42 * Set halt flag and halt the cpu.
43 *
44 */
[36e7ee98]45void halt()
[f761f1eb]46{
[36e7ee98]47#ifdef CONFIG_DEBUG
[2cf5634]48 bool rundebugger = false;
[36e7ee98]49
50// TODO test_and_set not defined on all arches
51// if (!test_and_set(&haltstate))
52 if (!atomic_get(&haltstate)) {
53 atomic_set(&haltstate, 1);
54 rundebugger = true;
55 }
56#else
57 atomic_set(haltstate, 1);
58#endif
59
[22f7769]60 interrupts_disable();
[93b84b3]61#ifdef CONFIG_DEBUG
[36e7ee98]62 if (rundebugger) {
63 printf("\n");
64 kconsole("panic"); /* Run kconsole as a last resort to user */
65 }
[93b84b3]66#endif
[623ba26c]67 if (CPU)
68 printf("cpu%d: halted\n", CPU->id);
69 else
70 printf("cpu: halted\n");
[f761f1eb]71 cpu_halt();
72}
73
[ff3b3197]74/** Return number of characters in a string.
75 *
76 * @param str NULL terminated string.
77 *
78 * @return Number of characters in str.
79 */
80size_t strlen(const char *str)
81{
82 int i;
83
84 for (i = 0; str[i]; i++)
85 ;
86
87 return i;
88}
[f761f1eb]89
[d34657e]90/** Compare two NULL terminated strings
91 *
[ff3b3197]92 * Do a char-by-char comparison of two NULL terminated strings.
93 * The strings are considered equal iff they consist of the same
94 * characters on the minimum of their lengths and specified maximal
95 * length.
[d34657e]96 *
97 * @param src First string to compare.
98 * @param dst Second string to compare.
[ff3b3197]99 * @param len Maximal length for comparison.
[d34657e]100 *
[0c8e692]101 * @return 0 if the strings are equal, -1 if first is smaller, 1 if second smaller.
[d34657e]102 *
[f761f1eb]103 */
[f4338d2]104int strncmp(const char *src, const char *dst, size_t len)
[f761f1eb]105{
106 int i;
107
108 i = 0;
[0c8e692]109 for (;*src && *dst && i < len;src++,dst++,i++) {
110 if (*src < *dst)
111 return -1;
112 if (*src > *dst)
113 return 1;
[f761f1eb]114 }
[0c8e692]115 if (i == len || *src == *dst)
116 return 0;
[07bd114e]117 if (!*src)
[0c8e692]118 return -1;
[f761f1eb]119 return 1;
120}
[8491c48]121
[f4338d2]122/** Copy NULL terminated string.
123 *
124 * Copy at most 'len' characters from string 'src' to 'dest'.
125 * If 'src' is shorter than 'len', '\0' is inserted behind the
126 * last copied character.
127 *
128 * @param src Source string.
129 * @param dst Destination buffer.
130 * @param len Size of destination buffer.
131 */
132void strncpy(char *dest, const char *src, size_t len)
133{
134 int i;
135 for (i = 0; i < len; i++) {
136 if (!(dest[i] = src[i]))
137 return;
138 }
[91c78c9]139 dest[i-1] = '\0';
[f4338d2]140}
[6e716a59]141
142/** Convert ascii representation to __native
143 *
144 * Supports 0x for hexa & 0 for octal notation.
145 * Does not check for overflows, does not support negative numbers
146 *
147 * @param text Textual representation of number
148 * @return Converted number or 0 if no valid number ofund
149 */
150__native atoi(const char *text)
151{
152 int base = 10;
153 __native result = 0;
154
155 if (text[0] == '0' && text[1] == 'x') {
156 base = 16;
157 text += 2;
158 } else if (text[0] == '0')
159 base = 8;
160
161 while (*text) {
[ba276f7]162 if (base != 16 && \
163 ((*text >= 'A' && *text <= 'F' )
164 || (*text >='a' && *text <='f')))
[6e716a59]165 break;
166 if (base == 8 && *text >='8')
167 break;
168
[2312685]169 if (*text >= '0' && *text <= '9') {
170 result *= base;
[6e716a59]171 result += *text - '0';
[2312685]172 } else if (*text >= 'A' && *text <= 'F') {
173 result *= base;
[6e716a59]174 result += *text - 'A' + 10;
[ba276f7]175 } else if (*text >= 'a' && *text <= 'f') {
176 result *= base;
177 result += *text - 'a' + 10;
[2312685]178 } else
[6e716a59]179 break;
180 text++;
181 }
182
183 return result;
184}
Note: See TracBrowser for help on using the repository browser.