- Implemented first time functions.
[planlOS.git] / programs / libc / stdlib.c
blob695bfd8fcc886fd75d76d99ecf83953b16d0b790
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include <stdlib.h>
23 #include <planlos/syscalls.h>
25 unsigned int errno;
27 void abort(void)
29 asm volatile("int $0x80" : : "a"(SYS_EXIT), "b"(-1));
32 void *sbrk(int increment)
34 return 0;
37 void exit(int value)
39 // TODO: Close files
40 asm volatile("int $0x80" : : "a"(SYS_EXIT), "b"(value));
43 int atoi(const char *str)
45 int number = 0;
46 int negative = 0;
47 if (*str == '-')
49 str++;
50 negative = 1;
52 while (*str)
54 if ((*str >= '0') && (*str <= '9'))
56 number = number * 10 + (*str - '0');
57 str++;
59 else
61 break;
64 return number;
67 int abs(int n)
69 if (n < 0) return -n;
70 return n;
73 char *getenv(const char *name)
75 if (!strcmp(name, "TERM"))
77 return "/dev/tty0";
79 else if (!strcmp(name, "TERMINFO"))
81 return "/usr/lib/terminfo";
83 // TODO
84 return "";
87 static int strtol_is_digit(const char c, int base)
89 if ((c >= '0') && (c <= '9'))
91 if (c - '0' < base)
93 return 1;
95 return 0;
97 else if ((c >= 'a') && (c <= 'z'))
99 if (c - 'a' < base - 10)
101 return 1;
104 else if ((c >= 'A') && (c <= 'Z'))
106 if (c - 'A' < base - 10)
108 return 1;
111 return 0;
113 static int strtol_get_digit(const char c, int base)
115 if ((c >= '0') && (c <= '9'))
117 return c - '0';
119 else if ((c >= 'a') && (c <= 'z'))
121 return c - 'a';
123 else if ((c >= 'A') && (c <= 'Z'))
125 return c - 'A';
127 else return 0;
130 long int strtol(const char *nptr, char **endptr, int base)
132 int negative = 0;
133 if (*nptr == '-')
135 negative = 1;
136 nptr++;
139 long int number = 0;
141 while (strtol_is_digit(*nptr, base))
143 number = number * base + strtol_get_digit(*nptr, base);
144 nptr++;
146 if (endptr) *endptr = (char*)nptr;
147 if (negative) number = -number;
148 return number;