From 3e19b96ab85a3ed31c39d44eacced5940f69ebf9 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Tue, 2 Mar 2021 03:27:55 -0800 Subject: [PATCH] Add more POSIX function stubs Cosmopolitan currently doesn't support threads and it doesn't do anything fancy in longjmp/setjmp so this change was simple to do - localeconv - _setjmp (same as setjmp) - _longjmp (same as longjmp) - strcoll (same as strcmp) - flockfile (does nothing) - funlockfile (does nothing) - ftrylockfile (does nothing) See #61 --- libc/nexgen32e/longjmp.S | 2 +- libc/nexgen32e/setjmp.S | 2 +- libc/runtime/runtime.h | 2 ++ libc/stdio/flockfile.c | 38 ++++++++++++++++++++++++ libc/stdio/unlocked.h | 3 ++ libc/str/strcoll.c | 26 +++++++++++++++++ libc/unicode/localeconv.c | 51 +++++++++++++++++++++++++++++++++ libc/unicode/unicode.h | 28 ++++++++++++++++++ tool/emacs/cosmo-c-constants.el | 1 + 9 files changed, 151 insertions(+), 2 deletions(-) create mode 100644 libc/stdio/flockfile.c create mode 100644 libc/str/strcoll.c create mode 100644 libc/unicode/localeconv.c diff --git a/libc/nexgen32e/longjmp.S b/libc/nexgen32e/longjmp.S index 45aa3f486..b46dadfab 100644 --- a/libc/nexgen32e/longjmp.S +++ b/libc/nexgen32e/longjmp.S @@ -40,4 +40,4 @@ longjmp:mov %esi,%eax mov 48(%rdi),%r15 jmp *56(%rdi) .endfn longjmp,globl - .source __FILE__ + .alias longjmp,_longjmp diff --git a/libc/nexgen32e/setjmp.S b/libc/nexgen32e/setjmp.S index 5b3d00eb8..cc5cb975a 100644 --- a/libc/nexgen32e/setjmp.S +++ b/libc/nexgen32e/setjmp.S @@ -39,4 +39,4 @@ setjmp: lea 8(%rsp),%rax xor %eax,%eax ret .endfn setjmp,globl - .source __FILE__ + .alias setjmp,_setjmp diff --git a/libc/runtime/runtime.h b/libc/runtime/runtime.h index 7e76a8aef..d81f58a88 100644 --- a/libc/runtime/runtime.h +++ b/libc/runtime/runtime.h @@ -39,6 +39,8 @@ unsigned long getauxval(unsigned long); void *mapanon(size_t) vallocesque attributeallocsize((1)); int setjmp(jmp_buf) libcesque returnstwice paramsnonnull(); void longjmp(jmp_buf, int) libcesque wontreturn paramsnonnull(); +int _setjmp(jmp_buf) libcesque returnstwice paramsnonnull(); +void _longjmp(jmp_buf, int) libcesque wontreturn paramsnonnull(); void exit(int) wontreturn; void _exit(int) libcesque wontreturn; void _Exit(int) libcesque wontreturn; diff --git a/libc/stdio/flockfile.c b/libc/stdio/flockfile.c new file mode 100644 index 000000000..fd6e8e6ad --- /dev/null +++ b/libc/stdio/flockfile.c @@ -0,0 +1,38 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/stdio/stdio.h" + +/** + * Does nothing since Cosmopolitan currently doesn't support threads. + */ +void flockfile(FILE *f) { +} + +/** + * Does nothing since Cosmopolitan currently doesn't support threads. + */ +void funlockfile(FILE *f) { +} + +/** + * Does nothing since Cosmopolitan currently doesn't support threads. + */ +int ftrylockfile(FILE *f) { + return 0; +} diff --git a/libc/stdio/unlocked.h b/libc/stdio/unlocked.h index d73ba55c5..868646671 100644 --- a/libc/stdio/unlocked.h +++ b/libc/stdio/unlocked.h @@ -4,6 +4,9 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ +void flockfile(FILE *); +void funlockfile(FILE *); +int ftrylockfile(FILE *); int getc_unlocked(FILE *) paramsnonnull(); int getchar_unlocked(void); int putc_unlocked(int, FILE *) paramsnonnull(); diff --git a/libc/str/strcoll.c b/libc/str/strcoll.c new file mode 100644 index 000000000..aaeb9e164 --- /dev/null +++ b/libc/str/strcoll.c @@ -0,0 +1,26 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/str/str.h" + +/** + * Compares strings in the C locale. + */ +int strcoll(const char *p, const char *q) { + return strcmp(p, q); +} diff --git a/libc/unicode/localeconv.c b/libc/unicode/localeconv.c new file mode 100644 index 000000000..f262cc824 --- /dev/null +++ b/libc/unicode/localeconv.c @@ -0,0 +1,51 @@ +/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│ +│vi: set net ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi│ +╞══════════════════════════════════════════════════════════════════════════════╡ +│ Copyright 2021 Justine Alexandra Roberts Tunney │ +│ │ +│ Permission to use, copy, modify, and/or distribute this software for │ +│ any purpose with or without fee is hereby granted, provided that the │ +│ above copyright notice and this permission notice appear in all copies. │ +│ │ +│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │ +│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │ +│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │ +│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │ +│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │ +│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │ +│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ +│ PERFORMANCE OF THIS SOFTWARE. │ +╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/limits.h" +#include "libc/unicode/unicode.h" + +static const struct lconv kLocaleConv = { + .decimal_point = ".", + .thousands_sep = "", + .grouping = "", + .int_curr_symbol = "", + .currency_symbol = "", + .mon_decimal_point = "", + .mon_thousands_sep = "", + .mon_grouping = "", + .positive_sign = "", + .negative_sign = "", + .int_frac_digits = CHAR_MAX, + .frac_digits = CHAR_MAX, + .p_cs_precedes = CHAR_MAX, + .p_sep_by_space = CHAR_MAX, + .n_cs_precedes = CHAR_MAX, + .n_sep_by_space = CHAR_MAX, + .p_sign_posn = CHAR_MAX, + .n_sign_posn = CHAR_MAX, + .int_p_cs_precedes = CHAR_MAX, + .int_p_sep_by_space = CHAR_MAX, + .int_n_cs_precedes = CHAR_MAX, + .int_n_sep_by_space = CHAR_MAX, + .int_p_sign_posn = CHAR_MAX, + .int_n_sign_posn = CHAR_MAX, +}; + +struct lconv *localeconv(void) { + return (/* unconst */ struct lconv *)&kLocaleConv; +} diff --git a/libc/unicode/unicode.h b/libc/unicode/unicode.h index c60cfa418..dcda307a4 100644 --- a/libc/unicode/unicode.h +++ b/libc/unicode/unicode.h @@ -3,6 +3,33 @@ #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ +struct lconv { + char *decimal_point; + char *thousands_sep; + char *grouping; + char *int_curr_symbol; + char *currency_symbol; + char *mon_decimal_point; + char *mon_thousands_sep; + char *mon_grouping; + char *positive_sign; + char *negative_sign; + char int_frac_digits; + char frac_digits; + char p_cs_precedes; + char p_sep_by_space; + char n_cs_precedes; + char n_sep_by_space; + char p_sign_posn; + char n_sign_posn; + char int_p_cs_precedes; + char int_n_cs_precedes; + char int_p_sep_by_space; + char int_n_sep_by_space; + char int_p_sign_posn; + char int_n_sign_posn; +}; + int wcwidth(wchar_t) pureconst; int wcswidth(const wchar_t *, size_t) strlenesque; int wcsnwidth(const wchar_t *, size_t, size_t) strlenesque; @@ -10,6 +37,7 @@ int strwidth(const char *, size_t) strlenesque; int strnwidth(const char *, size_t, size_t) strlenesque; int strwidth16(const char16_t *, size_t) strlenesque; int strnwidth16(const char16_t *, size_t, size_t) strlenesque; +struct lconv *localeconv(void); COSMOPOLITAN_C_END_ #endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ diff --git a/tool/emacs/cosmo-c-constants.el b/tool/emacs/cosmo-c-constants.el index 76993514a..f5c1a8cd5 100644 --- a/tool/emacs/cosmo-c-constants.el +++ b/tool/emacs/cosmo-c-constants.el @@ -37,6 +37,7 @@ "SIZEOF_FLOAT80" "SIZEOF_LONG_DOUBLE" "SIZEOF_INTMAX" + "CHAR_MAX" "SCHAR_MAX" "SHRT_MAX" "INT_MAX"