source: libcaca/trunk/kernel/kernel.c @ 568

Last change on this file since 568 was 568, checked in by Sam Hocevar, 15 years ago
  • Added glue code to compile libcaca without a libc and build applications as multiboot kernels.
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1/*
2 *  libcucul      Unicode canvas library
3 *  libcaca       ASCII-Art library
4 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
5 *                All Rights Reserved
6 *
7 *  This library is free software; you can redistribute it and/or
8 *  modify it under the terms of the Do What The Fuck You Want To
9 *  Public License, Version 2, as published by Sam Hocevar. See
10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
11 */
12
13/** \file kernel.c
14 *  \version \$Id: kernel.c 568 2006-03-09 12:47:37Z sam $
15 *  \author Sam Hocevar <sam@zoy.org>
16 *  \brief Kernel features
17 *
18 *  This file contains replacement functions for the standard C library
19 *  that must be used when building libcucul and libcaca into a kernel.
20 */
21
22#include "config.h"
23
24#ifdef __KERNEL__
25
26/* Our memory mapping */
27static uint32_t *freemem = (uint32_t*) 0x00200000;
28
29/* Multiboot kernel entry point */
30void cmain(unsigned long int magic, unsigned long int addr)
31{
32    char *argv[] = { NULL };
33    int argc = 0;
34    main(argc, argv);
35}
36
37/* stdlib.h functions */
38void *malloc(size_t size)
39{
40    uint32_t *p = freemem;
41    size = (size + 0x7) / 4;
42    *p = size;
43    freemem += size + 1;
44    return p + 1;
45}
46
47void free(void *ptr)
48{
49    return;
50}
51
52void *realloc(void *ptr, size_t size)
53{
54    uint32_t oldsize = ((uint32_t *)ptr)[-1];
55    void *p;
56    if(oldsize >= size)
57        return ptr;
58
59    p = malloc(size);
60    memcpy(p, ptr, oldsize);
61    return p;
62}
63
64char *getenv(const char *name)
65{
66    return NULL;
67}
68
69int rand(void)
70{
71    static int seed = 0x68743284;
72    seed = (seed * 0x7f32ba17) ^ 0xf893a735;
73    return seed % RAND_MAX;
74}
75
76int abs(int j)
77{
78    if(j < 0)
79        return -j;
80    return j;
81}
82
83void exit(int status)
84{
85    /* FIXME: reboot? */
86    while(1);
87}
88
89/* string.h functions */
90void *memset(void *s, int c, size_t n)
91{
92    uint8_t *ptr = s;
93
94    while(n--)
95        *ptr++ = c;
96
97    return s;
98}
99
100void *memcpy(void *dest, const void *src, size_t n)
101{
102    uint8_t *destptr = dest;
103    uint8_t const *srcptr = src;
104
105    while(n--)
106        *destptr++ = *srcptr++;
107
108    return dest;
109}
110
111size_t strlen(const char *s)
112{
113    int len = 0;
114
115    while(*s++)
116        len++;
117
118    return len;
119}
120
121int strcasecmp(const char *s1, const char *s2)
122{
123    /* FIXME */
124    return -1;
125}
126
127/* stdarg.h functions */
128int vsnprintf(char *str, size_t size, const char *format, va_list ap)
129{
130    /* FIXME */
131    return 0;
132}
133
134/* stdio.h functions */
135FILE *fopen(const char *path, const char *mode)
136{
137    /* FIXME */
138    return NULL;
139}
140
141int feof(FILE *stream)
142{
143    /* FIXME */
144    return 0;
145}
146
147char *fgets(char *s, int size, FILE *stream)
148{
149    /* FIXME */
150    return NULL;
151}
152
153int fclose(FILE *fp)
154{
155    /* FIXME */
156    return 0;
157}
158
159int printf(const char *format, ...)
160{
161    /* FIXME */
162    return 0;
163}
164
165int sprintf(char *str, const char *format, ...)
166{
167    /* FIXME */
168    return 0;
169}
170
171int sscanf(const char *str, const char *format, ...)
172{
173    /* FIXME */
174    return 0;
175}
176
177/* unistd.h functions */
178void usleep(unsigned long usec)
179{
180    /* FIXME */
181    return;
182}
183
184/* time.h functions */
185int gettimeofday(struct timeval *tv, struct timezone *tz)
186{
187    static int usec = 0;
188    static int sec = 0;
189
190    /* FIXME */
191    usec += 10000;
192    if(usec > 1000000)
193    {
194        sec++;
195        usec -= 1000000;
196    }
197
198    tv->tv_sec = sec;
199    tv->tv_usec = usec;
200
201    return 0;
202}
203
204#endif /* __KERNEL__ */
Note: See TracBrowser for help on using the repository browser.