Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / openbios / libc / misc.c
1 /*
2  *   Creation Date: <2002/10/19 21:05:07 samuel>
3  *   Time-stamp: <2002/10/22 22:29:18 samuel>
4  *
5  *      <misc.c>
6  *
7  *      Miscellaneous
8  *
9  *   Copyright (C) 2002, 2003 Samuel Rydh (samuel@ibrium.se)
10  *
11  *   This program is free software; you can redistribute it and/or
12  *   modify it under the terms of the GNU General Public License
13  *   as published by the Free Software Foundation
14  *
15  */
16
17 #include "config.h"
18 #include "libc/string.h"
19
20 int errno_int;
21
22 void
23 qsort( void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void*) )
24 {
25         unsigned int worked, i, j;
26
27         /* even more inefficient than the glibc variant :-) */
28         do {
29                 char *p = base;
30                 worked = 0;
31                 for( i=0; i<nmemb-1; i++, p+= size ) {
32                         if( compar( p, p + size ) > 0 ) {
33                                 worked = 1;
34                                 for( j=0; j<size; j++ ) {
35                                         char ch = p[j];
36                                         p[j] = p[j+size];
37                                         p[j+size] = ch;
38                                 }
39                         }
40                 }
41         } while( worked );
42 }
43
44
45 long int
46 strtol( const char *nptr, char **endptr, int base )
47 {
48         int sum, n, sign=1;
49         while( isspace(*nptr) )
50                 nptr++;
51
52         if( *nptr == '-' || *nptr == '+' )
53                 sign = (*nptr++ == '-') ? -1 : 1;
54
55         if( base == 16 || base == 0) {
56                 if( !base )
57                         base = (nptr[0] == '0')? 8 : 10;
58                 if( nptr[0] == '0' && nptr[1] == 'x' ) {
59                         nptr += 2;
60                         base = 16;
61                 }
62         }
63         for( sum=0 ;; nptr++ ) {
64                 char ch = *nptr;
65                 if( !isalnum(ch) )
66                         break;
67                 n = isdigit(ch) ? ch - '0' : toupper(ch) - 'A' + 10;
68                 if( n >= base || n < 0 )
69                         break;
70                 sum *= base;
71                 sum += n;
72         }
73         if( endptr )
74                 *endptr = (char*)nptr;
75
76         return sum * sign;
77 }
78
79 long long int
80 strtoll( const char *nptr, char **endptr, int base )
81 {
82         long long int sum;
83         int n, sign=1;
84         while( isspace(*nptr) )
85                 nptr++;
86
87         if( *nptr == '-' || *nptr == '+' )
88                 sign = (*nptr++ == '-') ? -1 : 1;
89
90         if( base == 16 || base == 0) {
91                 if( !base )
92                         base = (nptr[0] == '0')? 8 : 10;
93                 if( nptr[0] == '0' && nptr[1] == 'x' ) {
94                         nptr += 2;
95                         base = 16;
96                 }
97         }
98         for( sum=0 ;; nptr++ ) {
99                 char ch = *nptr;
100                 if( !isalnum(ch) )
101                         break;
102                 n = isdigit(ch) ? ch - '0' : toupper(ch) - 'A' + 10;
103                 if( n >= base || n < 0 )
104                         break;
105                 sum *= base;
106                 sum += n;
107         }
108         if( endptr )
109                 *endptr = (char*)nptr;
110
111         return sum * sign;
112 }
113
114 // Propolice support
115 long __guard[8] = {
116 #ifdef CONFIG_BIG_ENDIAN
117     (0 << 24) | (0 << 16) | ('\n' << 8) | 255,
118 #else
119     (255 << 24) | ('\n' << 16) | (0 << 8) | 0,
120 #endif
121     0, 0, 0, 0, 0, 0, 0
122 };
123
124 static void freeze(void)
125 {
126     // Freeze
127     // XXX: Disable interrupts?
128     for(;;)
129         ;
130 }
131
132 void __stack_smash_handler(const char *func, int damaged)
133 {
134     printk("Propolice detected a stack smashing attack %x at function %s,"
135            " freezing\n", damaged, func);
136     freeze();
137 }
138
139 void __stack_chk_fail(void)
140 {
141     printk("Propolice detected a stack smashing attack, freezing\n");
142
143     freeze();
144 }