summaryrefslogtreecommitdiff
path: root/LibC
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-02-26 15:57:59 +0100
committerAndreas Kling <awesomekling@gmail.com>2019-02-26 15:57:59 +0100
commita356746d04b50968f4b5d4eb43c587efc61dffba (patch)
treee55e826ddbdfc2e184c325933a910228ec310019 /LibC
parent2e5b9d318f51f73b0ac155f00345a70bf287f5e6 (diff)
downloadserenity-a356746d04b50968f4b5d4eb43c587efc61dffba.zip
Compat work towards porting vim.
Diffstat (limited to 'LibC')
-rw-r--r--LibC/crt0.cpp2
-rw-r--r--LibC/ctype.cpp13
-rw-r--r--LibC/ctype.h15
-rw-r--r--LibC/signal.cpp36
-rw-r--r--LibC/signal.h1
-rw-r--r--LibC/signal_numbers.h75
-rw-r--r--LibC/stdlib.cpp55
-rw-r--r--LibC/string.cpp2
-rw-r--r--LibC/strings.cpp28
-rw-r--r--LibC/termios.cpp9
-rw-r--r--LibC/termios.h2
11 files changed, 166 insertions, 72 deletions
diff --git a/LibC/crt0.cpp b/LibC/crt0.cpp
index ab6a3ebfe2..a607390e14 100644
--- a/LibC/crt0.cpp
+++ b/LibC/crt0.cpp
@@ -8,6 +8,7 @@ int main(int, char**);
int errno;
char** environ;
+//bool __environ_is_malloced;
void __malloc_init();
void __stdio_init();
@@ -16,6 +17,7 @@ int _start(int argc, char** argv, char** env)
{
errno = 0;
environ = env;
+ //__environ_is_malloced = false;
__stdio_init();
__malloc_init();
diff --git a/LibC/ctype.cpp b/LibC/ctype.cpp
index ba4eb48a2b..af37f95036 100644
--- a/LibC/ctype.cpp
+++ b/LibC/ctype.cpp
@@ -1,3 +1,16 @@
#include <ctype.h>
#include <string.h>
+int __tolower(int c)
+{
+ if (c >= 'A' && c <= 'Z')
+ return c | 0x20;
+ return c;
+}
+
+int __toupper(int c)
+{
+ if (c >= 'a' && c <= 'z')
+ return c & ~0x20;
+ return c;
+}
diff --git a/LibC/ctype.h b/LibC/ctype.h
index c1282644bd..03ee173e05 100644
--- a/LibC/ctype.h
+++ b/LibC/ctype.h
@@ -25,19 +25,8 @@ ALWAYS_INLINE int __isupper(int c)
return c >= 'A' && c <= 'Z';
}
-ALWAYS_INLINE int __tolower(int c)
-{
- if (__isupper(c))
- return c | 0x20;
- return c;
-}
-
-ALWAYS_INLINE int __toupper(int c)
-{
- if (__islower(c))
- return c & ~0x20;
- return c;
-}
+int __tolower(int);
+int __toupper(int);
ALWAYS_INLINE int __isdigit(int c)
{
diff --git a/LibC/signal.cpp b/LibC/signal.cpp
index 248ac08556..ec2d8c2767 100644
--- a/LibC/signal.cpp
+++ b/LibC/signal.cpp
@@ -102,10 +102,38 @@ int sigpending(sigset_t* set)
}
const char* sys_siglist[NSIG] = {
-#undef __SIGNAL
-#define __SIGNAL(a, b) b,
- __ENUMERATE_ALL_SIGNALS
-#undef __SIGNAL
+ "Invalid signal number",
+ "Hangup",
+ "Interrupt",
+ "Quit",
+ "Illegal instruction",
+ "Trap",
+ "Aborted",
+ "Bus error",
+ "FP exception",
+ "Killed",
+ "User signal 1",
+ "Segmentation violation",
+ "User signal 2",
+ "Broken pipe",
+ "Alarm clock",
+ "Terminated",
+ "Stack fault",
+ "Child exited",
+ "Continued",
+ "Stopped (signal)",
+ "Stopped",
+ "Stopped (tty input)",
+ "Stopped (tty output)",
+ "Urgent I/O condition)",
+ "CPU limit exceeded",
+ "File size limit exceeded",
+ "Virtual timer expired",
+ "Profiling timer expired",
+ "Window changed",
+ "I/O possible",
+ "Power failure",
+ "Bad system call",
};
int sigsetjmp(jmp_buf env, int savesigs)
diff --git a/LibC/signal.h b/LibC/signal.h
index 2977c7e802..e854b7ed0b 100644
--- a/LibC/signal.h
+++ b/LibC/signal.h
@@ -35,7 +35,6 @@ int sigprocmask(int how, const sigset_t* set, sigset_t* old_set);
int sigpending(sigset_t*);
int raise(int sig);
-#define NSIG 32
extern const char* sys_siglist[NSIG];
#define SIG_DFL ((__sighandler_t)0)
diff --git a/LibC/signal_numbers.h b/LibC/signal_numbers.h
index a26beeaa15..5c974cbf1c 100644
--- a/LibC/signal_numbers.h
+++ b/LibC/signal_numbers.h
@@ -1,44 +1,35 @@
#pragma once
-#define __ENUMERATE_ALL_SIGNALS \
- __SIGNAL(SIGINVAL, "Invalid signal number") \
- __SIGNAL(SIGHUP, "Hangup") \
- __SIGNAL(SIGINT, "Interrupt") \
- __SIGNAL(SIGQUIT, "Quit") \
- __SIGNAL(SIGILL, "Illegal instruction") \
- __SIGNAL(SIGTRAP, "Trap") \
- __SIGNAL(SIGABRT, "Aborted") \
- __SIGNAL(SIGBUS, "Bus error") \
- __SIGNAL(SIGFPE, "FP exception") \
- __SIGNAL(SIGKILL, "Killed") \
- __SIGNAL(SIGUSR1, "User signal 1") \
- __SIGNAL(SIGSEGV, "Segmentation violation") \
- __SIGNAL(SIGUSR2, "User signal 2") \
- __SIGNAL(SIGPIPE, "Broken pipe") \
- __SIGNAL(SIGALRM, "Alarm clock") \
- __SIGNAL(SIGTERM, "Terminated") \
- __SIGNAL(SIGSTKFLT, "Stack fault") \
- __SIGNAL(SIGCHLD, "Child exited") \
- __SIGNAL(SIGCONT, "Continued") \
- __SIGNAL(SIGSTOP, "Stopped (signal)") \
- __SIGNAL(SIGTSTP, "Stopped") \
- __SIGNAL(SIGTTIN, "Stopped (tty input)") \
- __SIGNAL(SIGTTOU, "Stopped (tty output)") \
- __SIGNAL(SIGURG, "Urgent I/O condition)") \
- __SIGNAL(SIGXCPU, "CPU limit exceeded") \
- __SIGNAL(SIGXFSZ, "File size limit exceeded") \
- __SIGNAL(SIGVTALRM, "Virtual timer expired") \
- __SIGNAL(SIGPROF, "Profiling timer expired") \
- __SIGNAL(SIGWINCH, "Window changed") \
- __SIGNAL(SIGIO, "I/O possible") \
- __SIGNAL(SIGPWR, "Power failure") \
- __SIGNAL(SIGSYS, "Bad system call") \
-
-
-enum __signal_numbers {
-#undef __SIGNAL
-#define __SIGNAL(a, b) a,
- __ENUMERATE_ALL_SIGNALS
-#undef __SIGNAL
- __signal_count
-};
+#define SIGINVAL 0
+#define SIGHUP 1
+#define SIGINT 2
+#define SIGQUIT 3
+#define SIGILL 4
+#define SIGTRAP 5
+#define SIGABRT 6
+#define SIGBUS 7
+#define SIGFPE 8
+#define SIGKILL 9
+#define SIGUSR1 10
+#define SIGSEGV 11
+#define SIGUSR2 12
+#define SIGPIPE 13
+#define SIGALRM 14
+#define SIGTERM 15
+#define SIGSTKFLT 16
+#define SIGCHLD 17
+#define SIGCONT 18
+#define SIGSTOP 19
+#define SIGTSTP 20
+#define SIGTTIN 21
+#define SIGTTOU 22
+#define SIGURG 23
+#define SIGXCPU 24
+#define SIGXFSZ 25
+#define SIGVTALRM 26
+#define SIGPROF 27
+#define SIGWINCH 28
+#define SIGIO 29
+#define SIGPWR 30
+#define SIGSYS 31
+#define NSIG 32
diff --git a/LibC/stdlib.cpp b/LibC/stdlib.cpp
index d7e12e9276..37078f4986 100644
--- a/LibC/stdlib.cpp
+++ b/LibC/stdlib.cpp
@@ -10,6 +10,8 @@
#include <AK/Types.h>
#include <Kernel/Syscall.h>
#include <AK/StdLibExtras.h>
+#include <AK/HashMap.h>
+#include <AK/AKString.h>
extern "C" {
@@ -31,8 +33,8 @@ struct MallocFooter {
uint32_t xorcheck;
};
-#define CHUNK_SIZE 8
-#define POOL_SIZE 128 * 1024
+#define CHUNK_SIZE 16
+#define POOL_SIZE 4 * 1048576
static const size_t malloc_budget = POOL_SIZE;
static byte s_malloc_map[POOL_SIZE / CHUNK_SIZE / 8];
@@ -201,13 +203,56 @@ char* getenv(const char* name)
return nullptr;
}
-int putenv(char*)
+int putenv(char* new_var)
{
- assert(false);
+ HashMap<String, String> environment;
+
+ auto handle_environment_entry = [&environment] (const char* decl) {
+ char* eq = strchr(decl, '=');
+ if (!eq)
+ return;
+ size_t var_length = eq - decl;
+ char* var = (char*)alloca(var_length + 1);
+ memcpy(var, decl, var_length);
+ var[var_length] = '\0';
+ const char* value = eq + 1;
+ environment.set(var, value);
+ };
+ for (size_t i = 0; environ[i]; ++i)
+ handle_environment_entry(environ[i]);
+ handle_environment_entry(new_var);
+
+ //extern bool __environ_is_malloced;
+ //if (__environ_is_malloced)
+ // free(environ);
+ //__environ_is_malloced = true;
+
+ int environment_size = sizeof(char*); // For the null sentinel.
+ for (auto& it : environment)
+ environment_size += (int)sizeof(char*) + it.key.length() + 1 + it.value.length() + 1;
+
+ char* buffer = (char*)malloc(environment_size);
+ environ = (char**)buffer;
+ char* bufptr = buffer + sizeof(char*) * (environment.size() + 1);
+
+ int i = 0;
+ for (auto& it : environment) {
+ environ[i] = bufptr;
+ memcpy(bufptr, it.key.characters(), it.key.length());
+ bufptr += it.key.length();
+ *(bufptr++) = '=';
+ memcpy(bufptr, it.value.characters(), it.value.length());
+ bufptr += it.value.length();
+ *(bufptr++) = '\0';
+ ++i;
+ }
+ environ[environment.size()] = nullptr;
+ return 0;
}
-double atof(const char*)
+double atof(const char* str)
{
+ dbgprintf("LibC: atof: '%s'\n", str);
assert(false);
}
diff --git a/LibC/string.cpp b/LibC/string.cpp
index 01661e3174..e252377cc5 100644
--- a/LibC/string.cpp
+++ b/LibC/string.cpp
@@ -260,7 +260,7 @@ char* strerror(int errnum)
char* strsignal(int signum)
{
- if (signum >= __signal_count) {
+ if (signum >= NSIG) {
printf("strsignal() missing string for signum=%d\n", signum);
return const_cast<char*>("Unknown signal");
}
diff --git a/LibC/strings.cpp b/LibC/strings.cpp
index 27d4b8135c..abbfeca214 100644
--- a/LibC/strings.cpp
+++ b/LibC/strings.cpp
@@ -1,16 +1,36 @@
#include <strings.h>
#include <assert.h>
+#include <ctype.h>
extern "C" {
-int strcasecmp(const char*, const char*)
+static char foldcase(char ch)
{
- assert(false);
+ if (isalpha(ch))
+ return tolower(ch);
+ return ch;
}
-int strncasecmp(const char*, const char*, size_t)
+int strcasecmp(const char* s1, const char* s2)
{
- assert(false);
+ for (; foldcase(*s1) == foldcase(*s2); ++s1, ++s2) {
+ if (*s1 == 0)
+ return 0;
+ }
+ return foldcase(*(const unsigned char*)s1) < foldcase(*(const unsigned char*)s2) ? -1 : 1;
+}
+
+int strncasecmp(const char* s1, const char* s2, size_t n)
+{
+ if (!n)
+ return 0;
+ do {
+ if (foldcase(*s1) != foldcase(*s2++))
+ return foldcase(*(const unsigned char*)s1) - foldcase(*(const unsigned char*)--s2);
+ if (*s1++ == 0)
+ break;
+ } while (--n);
+ return 0;
}
}
diff --git a/LibC/termios.cpp b/LibC/termios.cpp
index c3c0ea6b57..8af27ffa06 100644
--- a/LibC/termios.cpp
+++ b/LibC/termios.cpp
@@ -39,9 +39,14 @@ int tcflush(int fd, int queue_selector)
assert(false);
}
-speed_t cfgetospeed(const struct termios*)
+speed_t cfgetispeed(const struct termios* tp)
{
- assert(false);
+ return tp->c_ispeed;
+}
+
+speed_t cfgetospeed(const struct termios* tp)
+{
+ return tp->c_ospeed;
}
}
diff --git a/LibC/termios.h b/LibC/termios.h
index a055f8e255..01529f3df9 100644
--- a/LibC/termios.h
+++ b/LibC/termios.h
@@ -17,6 +17,8 @@ struct termios {
tcflag_t c_cflag;
tcflag_t c_lflag;
cc_t c_cc[NCCS];
+ speed_t c_ispeed;
+ speed_t c_ospeed;
};
int tcgetattr(int fd, struct termios*);