From 441bf9f00b67a5c82ddb2e33d77a8961a2279a69 Mon Sep 17 00:00:00 2001
From: jD91mZM2 <me@krake.one>
Date: Mon, 25 Jun 2018 11:43:44 +0200
Subject: [PATCH] Add the few last things to get gcc_complete working

---
 include/assert.h             | 14 ++++++++++++++
 include/stddef.h             |  4 ++--
 include/stdint.h             | 24 ++++++++++++------------
 src/stdlib/src/lib.rs        | 10 +++++++---
 tests/.gitignore             |  1 +
 tests/Makefile               |  1 +
 tests/assert.c               | 14 ++++++++++++++
 tests/expected/assert.stderr |  0
 tests/expected/assert.stdout |  1 +
 9 files changed, 52 insertions(+), 17 deletions(-)
 create mode 100644 tests/assert.c
 create mode 100644 tests/expected/assert.stderr
 create mode 100644 tests/expected/assert.stdout

diff --git a/include/assert.h b/include/assert.h
index e69de29b..1a402062 100644
--- a/include/assert.h
+++ b/include/assert.h
@@ -0,0 +1,14 @@
+#ifndef _ASSERT_H
+#define _ASSERT_H
+
+#ifdef NDEBUG
+# define assert(cond)
+#else
+# include <stdio.h>
+# define assert(cond) if (!(cond)) { \
+    fprintf(stderr, "%s: %s:%d: Assertion `%s` failed.\n", __func__, __FILE__, __LINE__, #cond); \
+    abort(); \
+    }
+#endif
+
+#endif
diff --git a/include/stddef.h b/include/stddef.h
index fa0bc7da..099ca157 100644
--- a/include/stddef.h
+++ b/include/stddef.h
@@ -3,10 +3,10 @@
 
 #define NULL 0
 
-typedef signed long long ptrdiff_t;
+typedef signed long ptrdiff_t;
 
 typedef unsigned char wchar_t;
 
-typedef unsigned long long size_t;
+typedef unsigned long size_t;
 
 #endif /* _STDDEF_H */
diff --git a/include/stdint.h b/include/stdint.h
index 8b787427..cf25e00f 100644
--- a/include/stdint.h
+++ b/include/stdint.h
@@ -56,54 +56,54 @@ typedef unsigned short uint_fast16_t;
 #define INT32_C(value) value ## L
 #define INT32_MIN -0x80000000
 #define INT32_MAX 0x7FFFFFFF
-typedef signed long int32_t;
+typedef signed int int32_t;
 
 #define INT_LEAST32_MIN -0x80000000
 #define INT_LEAST32_MAX 0x7FFFFFFF
-typedef signed long int_least32_t;
+typedef signed int int_least32_t;
 
 #define INT_FAST32_MIN -0x80000000
 #define INT_FAST32_MAX 0x7FFFFFFF
-typedef signed long int_fast32_t;
+typedef signed int int_fast32_t;
 
 #define UINT32_C(value) value ## UL
 #define UINT32_MIN 0x00000000
 #define UINT32_MAX 0xFFFFFFFF
-typedef unsigned long uint32_t;
+typedef unsigned int uint32_t;
 
 #define UINT_LEAST32_MIN 0x00000000
 #define UINT_LEAST32_MAX 0xFFFFFFFF
-typedef unsigned long uint_least32_t;
+typedef unsigned int uint_least32_t;
 
 #define UINT_FAST32_MIN 0x00000000
 #define UINT_FAST32_MAX 0xFFFFFFFF
-typedef unsigned long uint_fast32_t;
+typedef unsigned int uint_fast32_t;
 
 #define INT64_C(value) value ## LL
 #define INT64_MIN -0x8000000000000000
 #define INT64_MAX 0x7FFFFFFFFFFFFFFF
-typedef signed long long int64_t;
+typedef signed long int64_t;
 
 #define INT_LEAST64_MIN -0x8000000000000000
 #define INT_LEAST64_MAX 0x7FFFFFFFFFFFFFFF
-typedef signed long long int_least64_t;
+typedef signed long int_least64_t;
 
 #define INT_FAST64_MIN -0x8000000000000000
 #define INT_FAST64_MAX 0x7FFFFFFFFFFFFFFF
-typedef signed long long int_fast64_t;
+typedef signed long int_fast64_t;
 
 #define UINT64_C(value) value ## ULL
 #define UINT64_MIN 0x0000000000000000
 #define UINT64_MAX 0xFFFFFFFFFFFFFFFF
-typedef unsigned long long uint64_t;
+typedef unsigned long uint64_t;
 
 #define UINT_LEAST64_MIN 0x0000000000000000
 #define UINT_LEAST64_MAX 0xFFFFFFFFFFFFFFFF
-typedef unsigned long long uint_least64_t;
+typedef unsigned long uint_least64_t;
 
 #define UINT_FAST64_MIN 0x0000000000000000
 #define UINT_FAST64_MAX 0xFFFFFFFFFFFFFFFF
-typedef unsigned long long uint_fast64_t;
+typedef unsigned long uint_fast64_t;
 
 #define INTMAX_C(value) value ## LL
 #define INTMAX_MIN INT64_MIN
diff --git a/src/stdlib/src/lib.rs b/src/stdlib/src/lib.rs
index 8707ef54..de4b44ba 100644
--- a/src/stdlib/src/lib.rs
+++ b/src/stdlib/src/lib.rs
@@ -620,7 +620,11 @@ macro_rules! strto_impl {
 
         let set_endptr = |idx: isize| {
             if !$endptr.is_null() {
-                *$endptr = $s.offset(idx);
+                // This is stupid, but apparently strto* functions want
+                // const input but mut output, yet the man page says
+                // "stores the address of the first invalid character in *endptr"
+                // so obviously it doesn't want us to clone it.
+                *$endptr = $s.offset(idx) as *mut _;
             }
         };
 
@@ -712,7 +716,7 @@ macro_rules! strto_impl {
 
 #[no_mangle]
 pub unsafe extern "C" fn strtoul(s: *const c_char,
-                                 endptr: *mut *const c_char,
+                                 endptr: *mut *mut c_char,
                                  base: c_int)
                                  -> c_ulong {
     strto_impl!(
@@ -728,7 +732,7 @@ pub unsafe extern "C" fn strtoul(s: *const c_char,
 
 #[no_mangle]
 pub unsafe extern "C" fn strtol(s: *const c_char,
-                                endptr: *mut *const c_char,
+                                endptr: *mut *mut c_char,
                                 base: c_int)
                                 -> c_long {
     strto_impl!(
diff --git a/tests/.gitignore b/tests/.gitignore
index 37a4bde5..156ddf26 100644
--- a/tests/.gitignore
+++ b/tests/.gitignore
@@ -1,6 +1,7 @@
 /*.out
 /gen/
 /alloc
+/assert
 /args
 /atof
 /atoi
diff --git a/tests/Makefile b/tests/Makefile
index c15213a2..c08209e3 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -1,5 +1,6 @@
 # Binaries that should generate the same output every time
 EXPECT_BINS=\
+	assert \
 	atof \
 	atoi \
 	brk \
diff --git a/tests/assert.c b/tests/assert.c
new file mode 100644
index 00000000..320737f2
--- /dev/null
+++ b/tests/assert.c
@@ -0,0 +1,14 @@
+#include <assert.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+int main() {
+    assert(1 == 1);
+    assert(1 + 1 == 2);
+
+    puts("yay!");
+
+    //This fails, but I can't test it because that'd
+    //make the test fail lol
+    //assert(42 == 1337);
+}
diff --git a/tests/expected/assert.stderr b/tests/expected/assert.stderr
new file mode 100644
index 00000000..e69de29b
diff --git a/tests/expected/assert.stdout b/tests/expected/assert.stdout
new file mode 100644
index 00000000..3650d378
--- /dev/null
+++ b/tests/expected/assert.stdout
@@ -0,0 +1 @@
+yay!
-- 
GitLab