2 meinOS - A unix-like x86 microkernel operating system
3 Copyright (C) 2008 Janosch Gräf <janosch.graef@gmx.net>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include <sys/types.h>
29 // some variables/functions needed
30 void stdio_init(); ///< @see stdio.h
31 void env_init(); ///< @see env.c
32 llist_t atexit_list
; ///< @see stdlib.c
33 void _close_all_filehandles(); ///< @see files.c
34 void _signal_init(); ///< @see signal.c
35 void _fs_init(char *_stdin
,char *_stdout
,char *_stderr
); ///< @see apps/lib/stdlibc/files.c
37 void _stdlib_init_pre() {
38 rand_seed
= rand_seed
%RAND_MAX
;
40 atexit_list
= llist_create();
44 void _stdlib_init_post(char *_stdin
,char *_stdout
,char *_stderr
) {
46 _fs_init(_stdin
,_stdout
,_stderr
);
52 * @param result Program result value
53 * @see also used by crt0.asm
55 void exit(int result
) {
57 while ((func
= llist_pop(atexit_list
))!=NULL
) func();
62 * Closes all filehandles and exits
63 * @param result Program result
65 void _Exit(int result
) {
66 _close_all_filehandles();
67 syscall_call(SYSCALL_PROC_EXIT
,1,result
);
71 * Registers a function to run at process termination
72 * @param func Function
73 * @return 0=successful; -1=failed
75 int atexit(void (*func
)(void)) {
76 return llist_push(atexit_list
,func
)!=NULL
?0:-1;
80 * Converts a string to a number
81 * @param str String representing a number
82 * @return Represented number from string
84 int atoi(const char *str
) {
91 if (str
[i
]=='+') sign
= 0;
92 else if (str
[i
]=='-') sign
= 1;
93 else num
= num
*10+str
[i
]-'0';
100 * Returns absolute number
101 * @param num Number to get absolute number of
102 * @return Absolute number
105 if (num
<0) num
= -num
;
113 * @return Devision result
115 div_t div(int numer
, int denom
) {
117 res
.quot
= numer
/denom
;
118 res
.rem
= numer
%denom
;
123 * Binary search a sorted table
126 * @param nel Num elements
127 * @param width Size of an element
128 * @param compar Function called when match found
130 void *bsearch(const void *vdkey
,const void *vdbase
,size_t nel
,size_t width
,int (*compar
)(const void *, const void *)) {
131 char *key
= (char*)vdkey
;
132 char *cpkey
= memcpy(malloc(width
),key
,width
);
133 char *base
= (char*)vdbase
;
138 for (i
=0;i
<nel
;i
++) {
139 if (*(base
+nel
*width
)==*key
) found
++;
141 char *cpbase
= memcpy(malloc(width
),base
,width
);
142 if (compar
!=NULL
) compar(key
,base
);
147 last
= (void*)(vdbase
+nel
*width
);
156 * Gets a new page from kernel
157 * @param size How many bytes (should be devidable by PAGESIZE)
158 * @return Pointer to new page
160 void *sbrk(intptr_t size
) {
161 return (void*)syscall_call(SYSCALL_MEM_MALLOC
,1,size
);
169 const unsigned A
[2] = { 0, 0x9908b0df };
170 const unsigned HI
= 0x80000000;
171 const unsigned LO
= 0x7fffffff;
183 // initialisiere y mit Pseudozufallszahlen:
184 y
= calloc(N
,sizeof(unsigned int));
185 unsigned r
= 9, s
= 3402;
187 for (i
=0 ; i
<N
; ++i
) {
188 r
= 509845221 * r
+ 3;
190 y
[i
] = s
+ (r
>> 10);
195 for (k
=0 ; k
<N
-M
; ++k
) {
196 h
= (y
[k
] & HI
) | (y
[k
+1] & LO
);
197 y
[k
] = y
[k
+M
] ^ (h
>> 1) ^ A
[h
& 1];
199 for (k
=N
-M
; k
<N
-1 ; ++k
) {
200 h
= (y
[k
] & HI
) | (y
[k
+1] & LO
);
201 y
[k
] = y
[k
+(M
-N
)] ^ (h
>> 1) ^ A
[h
& 1];
203 h
= (y
[N
-1] & HI
) | (y
[0] & LO
);
204 y
[N
-1] = y
[M
-1] ^ (h
>> 1) ^ A
[h
& 1];
208 unsigned e
= y
[index
++];
211 e
^= (e
<< 7) & 0x9d2c5680;
212 e
^= (e
<< 15) & 0xefc60000;