mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 16:02:45 +00:00 
			
		
		
		
	LibC, LibM: Add functions needed to compile python3
This commit is contained in:
		
							parent
							
								
									3042c942d8
								
							
						
					
					
						commit
						e7affa24dc
					
				
					 7 changed files with 136 additions and 1 deletions
				
			
		|  | @ -3,6 +3,7 @@ | ||||||
| #include <AK/StdLibExtras.h> | #include <AK/StdLibExtras.h> | ||||||
| #include <AK/String.h> | #include <AK/String.h> | ||||||
| #include <AK/Types.h> | #include <AK/Types.h> | ||||||
|  | #include <AK/Utf8View.h> | ||||||
| #include <Kernel/Syscall.h> | #include <Kernel/Syscall.h> | ||||||
| #include <alloca.h> | #include <alloca.h> | ||||||
| #include <assert.h> | #include <assert.h> | ||||||
|  | @ -469,6 +470,28 @@ int wctomb(char*, wchar_t) | ||||||
|     ASSERT_NOT_REACHED(); |     ASSERT_NOT_REACHED(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | size_t wcstombs(char* dest, const wchar_t* src, size_t max) | ||||||
|  | { | ||||||
|  |     char* originalDest = dest; | ||||||
|  |     while ((size_t)(dest - originalDest) < max) { | ||||||
|  |         StringView v { (const char*)src, sizeof(wchar_t) }; | ||||||
|  | 
 | ||||||
|  |         // FIXME: dependent on locale, for now utf-8 is supported.
 | ||||||
|  |         Utf8View utf8 { v }; | ||||||
|  |         if (*utf8.begin() == '\0') { | ||||||
|  |             *dest = '\0'; | ||||||
|  |             return (size_t)(dest - originalDest); // Exclude null character in returned size
 | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         for (auto byte : utf8) { | ||||||
|  |             if (byte != '\0') | ||||||
|  |                 *dest++ = byte; | ||||||
|  |         } | ||||||
|  |         ++src; | ||||||
|  |     } | ||||||
|  |     return max; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| template<typename T, T min_value, T max_value> | template<typename T, T min_value, T max_value> | ||||||
| static T strtol_impl(const char* nptr, char** endptr, int base) | static T strtol_impl(const char* nptr, char** endptr, int base) | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -46,6 +46,7 @@ void* bsearch(const void* key, const void* base, size_t nmemb, size_t size, int | ||||||
| size_t mbstowcs(wchar_t*, const char*, size_t); | size_t mbstowcs(wchar_t*, const char*, size_t); | ||||||
| size_t mbtowc(wchar_t*, const char*, size_t); | size_t mbtowc(wchar_t*, const char*, size_t); | ||||||
| int wctomb(char*, wchar_t); | int wctomb(char*, wchar_t); | ||||||
|  | size_t wcstombs(char*, const wchar_t*, size_t); | ||||||
| 
 | 
 | ||||||
| #define RAND_MAX 32767 | #define RAND_MAX 32767 | ||||||
| int rand(); | int rand(); | ||||||
|  |  | ||||||
|  | @ -134,4 +134,18 @@ int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec* reques | ||||||
|     __RETURN_WITH_ERRNO(rc, rc, -1); |     __RETURN_WITH_ERRNO(rc, rc, -1); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | int clock_getres(clockid_t, struct timespec*) | ||||||
|  | { | ||||||
|  |     ASSERT_NOT_REACHED(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | struct tm* gmtime_r(const time_t*, struct tm*) | ||||||
|  | { | ||||||
|  |     ASSERT_NOT_REACHED(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | struct tm* localtime_r(const time_t*, struct tm*) | ||||||
|  | { | ||||||
|  |     ASSERT_NOT_REACHED(); | ||||||
|  | } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -48,11 +48,13 @@ typedef int clockid_t; | ||||||
| 
 | 
 | ||||||
| int clock_gettime(clockid_t, struct timespec*); | int clock_gettime(clockid_t, struct timespec*); | ||||||
| int clock_nanosleep(clockid_t, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep); | int clock_nanosleep(clockid_t, int flags, const struct timespec* requested_sleep, struct timespec* remaining_sleep); | ||||||
|  | int clock_getres(clockid_t, struct timespec* result); | ||||||
|  | struct tm* gmtime_r(const time_t* timep, struct tm* result); | ||||||
|  | struct tm* localtime_r(const time_t* timep, struct tm* result); | ||||||
| 
 | 
 | ||||||
| double difftime(time_t, time_t); | double difftime(time_t, time_t); | ||||||
| size_t strftime(char* s, size_t max, const char* format, const struct tm*); | size_t strftime(char* s, size_t max, const char* format, const struct tm*); | ||||||
| 
 | 
 | ||||||
| #define difftime(t1, t0) (double)(t1 - t0) | #define difftime(t1, t0) (double)(t1 - t0) | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| __END_DECLS | __END_DECLS | ||||||
|  |  | ||||||
|  | @ -1,3 +1,4 @@ | ||||||
|  | #include <AK/Assertions.h> | ||||||
| #include <wchar.h> | #include <wchar.h> | ||||||
| 
 | 
 | ||||||
| extern "C" { | extern "C" { | ||||||
|  | @ -18,6 +19,14 @@ wchar_t* wcscpy(wchar_t* dest, const wchar_t* src) | ||||||
|     return originalDest; |     return originalDest; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | wchar_t* wcsncpy(wchar_t* dest, const wchar_t* src, size_t num) | ||||||
|  | { | ||||||
|  |     wchar_t* originalDest = dest; | ||||||
|  |     while (((*dest++ = *src++) != '\0') && ((size_t)(dest - originalDest) < num)) | ||||||
|  |         ; | ||||||
|  |     return originalDest; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| int wcscmp(const wchar_t* s1, const wchar_t* s2) | int wcscmp(const wchar_t* s1, const wchar_t* s2) | ||||||
| { | { | ||||||
|     while (*s1 == *s2++) |     while (*s1 == *s2++) | ||||||
|  | @ -37,4 +46,79 @@ wchar_t* wcschr(const wchar_t* str, int c) | ||||||
|     } |     } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | const wchar_t* wcsrchr(const wchar_t* str, wchar_t wc) | ||||||
|  | { | ||||||
|  |     wchar_t* last = nullptr; | ||||||
|  |     wchar_t c; | ||||||
|  |     for (; (c = *str); ++str) { | ||||||
|  |         if (c == wc) | ||||||
|  |             last = const_cast<wchar_t*>(str); | ||||||
|  |     } | ||||||
|  |     return last; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | wchar_t* wcscat(wchar_t* dest, const wchar_t* src) | ||||||
|  | { | ||||||
|  |     size_t dest_length = wcslen(dest); | ||||||
|  |     size_t i; | ||||||
|  |     for (i = 0; src[i] != '\0'; i++) | ||||||
|  |         dest[dest_length + i] = src[i]; | ||||||
|  |     dest[dest_length + i] = '\0'; | ||||||
|  |     return dest; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | wchar_t* wcstok(wchar_t* str, const wchar_t* delim, wchar_t** ptr) | ||||||
|  | { | ||||||
|  |     wchar_t* used_str = str; | ||||||
|  |     if (!used_str) { | ||||||
|  |         used_str = *ptr; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     size_t token_start = 0; | ||||||
|  |     size_t token_end = 0; | ||||||
|  |     size_t str_len = wcslen(used_str); | ||||||
|  |     size_t delim_len = wcslen(delim); | ||||||
|  | 
 | ||||||
|  |     for (size_t i = 0; i < str_len; ++i) { | ||||||
|  |         bool is_proper_delim = false; | ||||||
|  | 
 | ||||||
|  |         for (size_t j = 0; j < delim_len; ++j) { | ||||||
|  |             if (used_str[i] == delim[j]) { | ||||||
|  |                 // Skip beginning delimiters
 | ||||||
|  |                 if (token_end - token_start == 0) { | ||||||
|  |                     ++token_start; | ||||||
|  |                     break; | ||||||
|  |                 } | ||||||
|  | 
 | ||||||
|  |                 is_proper_delim = true; | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  | 
 | ||||||
|  |         ++token_end; | ||||||
|  |         if (is_proper_delim && token_end > 0) { | ||||||
|  |             --token_end; | ||||||
|  |             break; | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     if (used_str[token_start] == '\0') | ||||||
|  |         return nullptr; | ||||||
|  | 
 | ||||||
|  |     if (token_end == 0) { | ||||||
|  |         return &used_str[token_start]; | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     used_str[token_end] = '\0'; | ||||||
|  |     return &used_str[token_start]; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | wchar_t* wcsncat(wchar_t* dest, const wchar_t* src, size_t n) | ||||||
|  | { | ||||||
|  |     size_t dest_length = wcslen(dest); | ||||||
|  |     size_t i; | ||||||
|  |     for (i = 0; i < n && src[i] != '\0'; i++) | ||||||
|  |         dest[dest_length + i] = src[i]; | ||||||
|  |     dest[dest_length + i] = '\0'; | ||||||
|  |     return dest; | ||||||
|  | } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -11,7 +11,12 @@ __BEGIN_DECLS | ||||||
| 
 | 
 | ||||||
| size_t wcslen(const wchar_t*); | size_t wcslen(const wchar_t*); | ||||||
| wchar_t* wcscpy(wchar_t*, const wchar_t*); | wchar_t* wcscpy(wchar_t*, const wchar_t*); | ||||||
|  | wchar_t* wcsncpy(wchar_t*, const wchar_t*, size_t); | ||||||
| int wcscmp(const wchar_t*, const wchar_t*); | int wcscmp(const wchar_t*, const wchar_t*); | ||||||
| wchar_t* wcschr(const wchar_t*, int); | wchar_t* wcschr(const wchar_t*, int); | ||||||
|  | const wchar_t* wcsrchr(const wchar_t*, wchar_t); | ||||||
|  | wchar_t* wcscat(wchar_t*, const wchar_t*); | ||||||
|  | wchar_t* wcstok(wchar_t*, const wchar_t*, wchar_t**); | ||||||
|  | wchar_t* wcsncat(wchar_t*, const wchar_t*, size_t); | ||||||
| 
 | 
 | ||||||
| __END_DECLS | __END_DECLS | ||||||
|  |  | ||||||
|  | @ -301,4 +301,10 @@ float ceilf(float value) | ||||||
|     } |     } | ||||||
|     return as_int + 1; |     return as_int + 1; | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | double modf(double x, double* intpart) | ||||||
|  | { | ||||||
|  |     *intpart = (double)((int)(x)); | ||||||
|  |     return x - (int)x; | ||||||
|  | } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Emanuel Sprung
						Emanuel Sprung