From 9f4477a969dad59a883d145baf20e1fc1a48ffc7 Mon Sep 17 00:00:00 2001
From: whh8b <whh8b@git.zephyr-software.com>
Date: Thu, 3 Sep 2015 17:48:00 +0000
Subject: [PATCH] Add some new features to libc.

---
 .gitattributes         |  5 +++++
 libc/include/close.h   | 29 +++++++++++++++++++++++++++++
 libc/include/open.h    | 29 +++++++++++++++++++++++++++++
 libc/include/stdint.h  |  5 +++++
 libc/include/stdlib.h  |  2 ++
 libc/include/syscall.h |  4 ++++
 libc/src/Makefile.in   |  2 +-
 libc/src/close.c       | 28 ++++++++++++++++++++++++++++
 libc/src/open.c        | 28 ++++++++++++++++++++++++++++
 libc/src/stdlib.c      | 35 +++++++++++++++++++++++++++++++++++
 10 files changed, 166 insertions(+), 1 deletion(-)
 create mode 100644 libc/include/close.h
 create mode 100644 libc/include/open.h
 create mode 100644 libc/src/close.c
 create mode 100644 libc/src/open.c
 create mode 100644 libc/src/stdlib.c

diff --git a/.gitattributes b/.gitattributes
index 65ac26a..7794d16 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -24,10 +24,12 @@ lib/tiny_linker_script -text
 libc/Makefile.in -text
 libc/include/assert.h -text
 libc/include/cgc.h -text
+libc/include/close.h -text
 libc/include/itoa.h -text
 libc/include/itox.h -text
 libc/include/malloc.h -text
 libc/include/null.h -text
+libc/include/open.h -text
 libc/include/read.h -text
 libc/include/stdint.h -text
 libc/include/stdlib.h -text
@@ -36,9 +38,12 @@ libc/include/syscall.h -text
 libc/include/write.h -text
 libc/src/Makefile.in -text
 libc/src/cgc.s -text
+libc/src/close.c -text
 libc/src/itoa.c -text
 libc/src/malloc.c -text
+libc/src/open.c -text
 libc/src/read.c -text
+libc/src/stdlib.c -text
 libc/src/strlen.c -text
 libc/src/syscall.s -text
 libc/src/write.c -text
diff --git a/libc/include/close.h b/libc/include/close.h
new file mode 100644
index 0000000..45a7003
--- /dev/null
+++ b/libc/include/close.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#ifndef close_h
+#define close_h
+
+#include <stdint.h>
+
+int close(int fd);
+
+#endif
+
diff --git a/libc/include/open.h b/libc/include/open.h
new file mode 100644
index 0000000..459dfb4
--- /dev/null
+++ b/libc/include/open.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#ifndef open_h
+#define open_h
+
+#include <stdint.h>
+
+int open(char *file, int mode);
+
+#endif
+
diff --git a/libc/include/stdint.h b/libc/include/stdint.h
index d7f0f9d..f9cf7b8 100644
--- a/libc/include/stdint.h
+++ b/libc/include/stdint.h
@@ -21,10 +21,15 @@
 #ifndef stdint_h
 #define stdint_h
 
+#define NULL 0
+
 #ifdef LINUX64
 	typedef signed long long int ssize_t;
 	typedef unsigned long long int size_t;
 	typedef unsigned long long int uintptr_t;
+	typedef unsigned long long int uint64_t;
+	typedef unsigned short int uint16_t;
+	typedef unsigned int uint32_t;
 #else
 	typedef signed long int ssize_t;
 	typedef unsigned long int size_t;
diff --git a/libc/include/stdlib.h b/libc/include/stdlib.h
index 5d0e8b1..8b39fa1 100644
--- a/libc/include/stdlib.h
+++ b/libc/include/stdlib.h
@@ -25,6 +25,8 @@
 #define FALSE 0
 #define NULL 0
 
+void __stack_chk_fail(void);
+
 #include <malloc.h>
 
 #endif
diff --git a/libc/include/syscall.h b/libc/include/syscall.h
index 1fdd0a7..115c35d 100644
--- a/libc/include/syscall.h
+++ b/libc/include/syscall.h
@@ -21,7 +21,11 @@
 #ifdef LINUX64
 #define SYS_read 0
 #define SYS_write 1
+#define SYS_open 2
+#define SYS_close 3
 #else
 #define SYS_read 3
 #define SYS_write 4
+#define SYS_open 5
+#define SYS_close 6
 #endif
diff --git a/libc/src/Makefile.in b/libc/src/Makefile.in
index 02d6a60..1560c20 100644
--- a/libc/src/Makefile.in
+++ b/libc/src/Makefile.in
@@ -7,7 +7,7 @@ AR=@AR@
 AS=@AS@
 ASFLAGS=@ASFLAGS@
 
-OBJS=malloc.o read.o write.o itoa.o strlen.o cgc.o
+OBJS=malloc.o read.o write.o itoa.o strlen.o cgc.o open.o close.o stdlib.o
 
 # note:  cgc.s is empty when -DCGC is not in ASFLAGS, so it's an empty object file that's created..
 
diff --git a/libc/src/close.c b/libc/src/close.c
new file mode 100644
index 0000000..0512a6f
--- /dev/null
+++ b/libc/src/close.c
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#include <close.h>
+#include <syscall.h>
+
+int close(int fd)
+{
+	syscall(SYS_close,fd);
+}
+
diff --git a/libc/src/open.c b/libc/src/open.c
new file mode 100644
index 0000000..27ec96e
--- /dev/null
+++ b/libc/src/open.c
@@ -0,0 +1,28 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#include <open.h>
+#include <syscall.h>
+
+int open(char *file, int mode)
+{
+	syscall(SYS_open,file,mode);
+}
+
diff --git a/libc/src/stdlib.c b/libc/src/stdlib.c
new file mode 100644
index 0000000..2823575
--- /dev/null
+++ b/libc/src/stdlib.c
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2014 - Zephyr Software LLC
+ *
+ * This file may be used and modified for non-commercial purposes as long as
+ * all copyright, permission, and nonwarranty notices are preserved.
+ * Redistribution is prohibited without prior written consent from Zephyr
+ * Software.
+ *
+ * Please contact the authors for restrictions applying to commercial use.
+ *
+ * THIS SOURCE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ *
+ * Author: Zephyr Software
+ * e-mail: jwd@zephyr-software.com
+ * URL   : http://www.zephyr-software.com/
+ *
+ */
+
+#include <stdlib.h>
+#include <write.h>
+
+#define STOP_IMM() \
+do { \
+	asm volatile ("mov $1, %%eax\n" \
+	"int $0x80" \
+	::); \
+} while (0);
+
+void __stack_chk_fail(void) {
+	char *error = "Error: Stack check failed.\n";
+	write(2, error, strlen(error));
+	STOP_IMM();
+}
-- 
GitLab