Make more libc improvements

- Make memmem() faster
- Make readdir() thread safe
- Remove 64kb limit from mkdeps.com
- Add old crypt() function from Musl
- Improve new fix-third-party.py tool
- Improve libc/isystem/ headers and fix bugs
This commit is contained in:
Justine Tunney 2022-08-06 17:18:40 -07:00
parent a8cf0f7e89
commit 6a5717a48f
50 changed files with 3783 additions and 280 deletions

View file

@ -1,6 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_ASSERT_H_
#define COSMOPOLITAN_LIBC_ASSERT_H_
#include "libc/bits/likely.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
@ -11,7 +10,7 @@ void __assert_fail(const char *, const char *, int) hidden relegated;
#define assert(EXPR) ((void)0)
#else
#define assert(EXPR) \
((void)(LIKELY(EXPR) || (__assert_fail(#EXPR, __FILE__, __LINE__), 0)))
((void)((EXPR) || (__assert_fail(#EXPR, __FILE__, __LINE__), 0)))
#endif
#ifndef __cplusplus

View file

@ -20,6 +20,7 @@ int dirfd(DIR *);
long telldir(DIR *);
struct dirent *readdir(DIR *);
void rewinddir(DIR *);
void seekdir(DIR *, long);
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_CALLS_STRUCT_DIRENT_H_ */

View file

@ -27,9 +27,10 @@ COSMOPOLITAN_C_START_
#define isset(x, i) __bitop(x, i, &)
#define isclr(x, i) !isset(x, i)
#define howmany(n, d) (((n) + ((d)-1)) / (d))
#undef roundup
#define roundup(n, d) (howmany(n, d) * (d))
#define powerof2(n) !(((n)-1) & (n))
#define howmany(n, d) (((n) + ((d)-1)) / (d))
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -30,6 +30,7 @@ int __fmt(void *, void *, const char *, va_list) hidden;
char *itoa(int, char *, int) compatfn;
char *fcvt(double, int, int *, int *);
char *ecvt(double, int, int *, int *);
char *gcvt(double, int, char *);
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § string formatting » optimizations

4
libc/isystem/crypt.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_CRYPT_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_CRYPT_H_
#include "third_party/musl/crypt.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_CRYPT_H_ */

View file

@ -1,4 +1,4 @@
#ifndef LIBC_ISYSTEM_SYS_DLFCN_H_
#define LIBC_ISYSTEM_SYS_DLFCN_H_
#include "libc/calls/calls.h"
#include "libc/runtime/dlfcn.h"
#endif

View file

@ -1,4 +1,4 @@
#ifndef LIBC_ISYSTEM_ERR_H_
#define LIBC_ISYSTEM_ERR_H_
#include "libc/log/log.h"
#include "libc/log/bsd.h"
#endif

View file

@ -1,4 +1,5 @@
#ifndef LIBC_ISYSTEM_FLOAT_H_
#define LIBC_ISYSTEM_FLOAT_H_
#include "libc/math.h"
#include "libc/runtime/fenv.h"
#endif

View file

@ -1,4 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_LANGINFO_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_LANGINFO_H_
#include "libc/unicode/langinfo.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_LANGINFO_H_ */

4
libc/isystem/nl_types.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_NL_TYPES_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_NL_TYPES_H_
#include "libc/unicode/nltypes.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_NL_TYPES_H_ */

View file

@ -2,7 +2,6 @@
#define LIBC_ISYSTEM_STDIO_H_
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/log/log.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/temp.h"
#endif

View file

@ -1,10 +1,8 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYS_FILE_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYS_FILE_H_
#include "libc/calls/calls.h"
#include "libc/calls/struct/flock.h"
#include "libc/sysv/consts/l.h"
#include "libc/sysv/consts/lock.h"
#define L_SET SEEK_SET
#define L_INCR SEEK_CUR
#define L_XTND SEEK_END
#include "libc/sysv/consts/ok.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYS_FILE_H_ */

View file

@ -2,5 +2,4 @@
#define COSMOPOLITAN_LIBC_ISYSTEM_SYS_IPC_H_
#include "libc/calls/ipc.h"
#include "libc/calls/weirdtypes.h"
#include "libc/sysv/consts/ipc.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYS_IPC_H_ */

5
libc/isystem/sys/prctl.h Normal file
View file

@ -0,0 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYS_PRCTL_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYS_PRCTL_H_
#include "libc/calls/calls.h"
#include "libc/sysv/consts/pr.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYS_PRCTL_H_ */

View file

@ -120,18 +120,16 @@ static int PrintBacktraceUsingAddr2line(int fd, const struct StackFrame *bp) {
j += uint64toarray_radix16(addr - 1, buf + j) + 1;
}
argv[i++] = NULL;
if (sys_pipe(pipefds) == -1) {
if (sys_pipe2(pipefds, O_CLOEXEC) == -1) {
return -1;
}
if ((pid = vfork()) == -1) {
if ((pid = fork()) == -1) {
sys_close(pipefds[0]);
sys_close(pipefds[1]);
return -1;
}
if (!pid) {
sys_dup2(pipefds[1], 1);
if (pipefds[0] != 1) sys_close(pipefds[0]);
if (pipefds[1] != 1) sys_close(pipefds[1]);
sys_execve(addr2line, argv, environ);
_Exit(127);
}

View file

@ -41,7 +41,6 @@ COSMOPOLITAN_C_START_
extern FILE *__log_file;
void perror(const char *) relegated; /* print the last system error */
void __die(void) relegated wontreturn; /* print backtrace and abort() */
void meminfo(int); /* shows malloc statistics &c. */
void memsummary(int); /* light version of same thing */

64
libc/nexgen32e/ksha512.S Normal file
View file

@ -0,0 +1,64 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 fenc=utf-8 :vi
Copyright 2022 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/macros.internal.h"
.rodata
.align 64
kSha512:
.quad 0x428a2f98d728ae22,0x7137449123ef65cd
.quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
.quad 0x3956c25bf348b538,0x59f111f1b605d019
.quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118
.quad 0xd807aa98a3030242,0x12835b0145706fbe
.quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
.quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1
.quad 0x9bdc06a725c71235,0xc19bf174cf692694
.quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3
.quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
.quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483
.quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
.quad 0x983e5152ee66dfab,0xa831c66d2db43210
.quad 0xb00327c898fb213f,0xbf597fc7beef0ee4
.quad 0xc6e00bf33da88fc2,0xd5a79147930aa725
.quad 0x06ca6351e003826f,0x142929670a0e6e70
.quad 0x27b70a8546d22ffc,0x2e1b21385c26c926
.quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
.quad 0x650a73548baf63de,0x766a0abb3c77b2a8
.quad 0x81c2c92e47edaee6,0x92722c851482353b
.quad 0xa2bfe8a14cf10364,0xa81a664bbc423001
.quad 0xc24b8b70d0f89791,0xc76c51a30654be30
.quad 0xd192e819d6ef5218,0xd69906245565a910
.quad 0xf40e35855771202a,0x106aa07032bbd1b8
.quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53
.quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
.quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
.quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
.quad 0x748f82ee5defb2fc,0x78a5636f43172f60
.quad 0x84c87814a1f0ab72,0x8cc702081a6439ec
.quad 0x90befffa23631e28,0xa4506cebde82bde9
.quad 0xbef9a3f7b2c67915,0xc67178f2e372532b
.quad 0xca273eceea26619c,0xd186b8c721c0c207
.quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
.quad 0x06f067aa72176fba,0x0a637dc5a2c898a6
.quad 0x113f9804bef90dae,0x1b710b35131c471b
.quad 0x28db77f523047d84,0x32caab7b40c72493
.quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
.endobj kSha512,globl

View file

@ -5,6 +5,8 @@ COSMOPOLITAN_C_START_
extern long kHalfCache3;
extern const uint64_t kTens[20];
extern const uint32_t kSha256[64];
extern const uint64_t kSha512[80];
extern const unsigned char kTensIndex[64];
void imapxlatab(void *);

View file

@ -609,7 +609,7 @@ sha512_transform_rorx:
vmovdqa PSHUFFLE_BYTE_FLIP_MASK(%rip), BYTE_FLIP_MASK
.Loop0:
lea K512(%rip), TBL
lea kSha512(%rip), TBL
## byte swap first 16 dwords
COPY_YMM_AND_BSWAP Y_0, (INP), BYTE_FLIP_MASK
@ -693,55 +693,6 @@ sha512_transform_rorx:
########################################################################
### Binary Data
# Mergeable 640-byte rodata section. This allows linker to merge the table
# with other, exactly the same 640-byte fragment of another rodata section
# (if such section exists).
.section .rodata.cst640.K512, "aM", @progbits, 640
.align 64
# K[t] used in SHA512 hashing
K512:
.quad 0x428a2f98d728ae22,0x7137449123ef65cd
.quad 0xb5c0fbcfec4d3b2f,0xe9b5dba58189dbbc
.quad 0x3956c25bf348b538,0x59f111f1b605d019
.quad 0x923f82a4af194f9b,0xab1c5ed5da6d8118
.quad 0xd807aa98a3030242,0x12835b0145706fbe
.quad 0x243185be4ee4b28c,0x550c7dc3d5ffb4e2
.quad 0x72be5d74f27b896f,0x80deb1fe3b1696b1
.quad 0x9bdc06a725c71235,0xc19bf174cf692694
.quad 0xe49b69c19ef14ad2,0xefbe4786384f25e3
.quad 0x0fc19dc68b8cd5b5,0x240ca1cc77ac9c65
.quad 0x2de92c6f592b0275,0x4a7484aa6ea6e483
.quad 0x5cb0a9dcbd41fbd4,0x76f988da831153b5
.quad 0x983e5152ee66dfab,0xa831c66d2db43210
.quad 0xb00327c898fb213f,0xbf597fc7beef0ee4
.quad 0xc6e00bf33da88fc2,0xd5a79147930aa725
.quad 0x06ca6351e003826f,0x142929670a0e6e70
.quad 0x27b70a8546d22ffc,0x2e1b21385c26c926
.quad 0x4d2c6dfc5ac42aed,0x53380d139d95b3df
.quad 0x650a73548baf63de,0x766a0abb3c77b2a8
.quad 0x81c2c92e47edaee6,0x92722c851482353b
.quad 0xa2bfe8a14cf10364,0xa81a664bbc423001
.quad 0xc24b8b70d0f89791,0xc76c51a30654be30
.quad 0xd192e819d6ef5218,0xd69906245565a910
.quad 0xf40e35855771202a,0x106aa07032bbd1b8
.quad 0x19a4c116b8d2d0c8,0x1e376c085141ab53
.quad 0x2748774cdf8eeb99,0x34b0bcb5e19b48a8
.quad 0x391c0cb3c5c95a63,0x4ed8aa4ae3418acb
.quad 0x5b9cca4f7763e373,0x682e6ff3d6b2b8a3
.quad 0x748f82ee5defb2fc,0x78a5636f43172f60
.quad 0x84c87814a1f0ab72,0x8cc702081a6439ec
.quad 0x90befffa23631e28,0xa4506cebde82bde9
.quad 0xbef9a3f7b2c67915,0xc67178f2e372532b
.quad 0xca273eceea26619c,0xd186b8c721c0c207
.quad 0xeada7dd6cde0eb1e,0xf57d4f7fee6ed178
.quad 0x06f067aa72176fba,0x0a637dc5a2c898a6
.quad 0x113f9804bef90dae,0x1b710b35131c471b
.quad 0x28db77f523047d84,0x32caab7b40c72493
.quad 0x3c9ebe0a15c9bebc,0x431d67c49c100d4c
.quad 0x4cc5d4becb3e42b6,0x597f299cfc657e2a
.quad 0x5fcb6fab3ad6faec,0x6c44198c4a475817
.rodata.cst32
# Mask for byte-swapping a couple of qwords in an XMM register using (v)pshufb.
PSHUFFLE_BYTE_FLIP_MASK:

View file

@ -18,11 +18,14 @@
*/
#include "libc/assert.h"
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/strace.internal.h"
#include "libc/calls/struct/dirent.h"
#include "libc/calls/syscall_support-nt.internal.h"
#include "libc/intrin/asan.internal.h"
#include "libc/intrin/nopl.h"
#include "libc/intrin/pthread.h"
#include "libc/mem/mem.h"
#include "libc/nt/enum/fileflagandattributes.h"
#include "libc/nt/enum/filetype.h"
@ -53,6 +56,7 @@ struct dirstream {
bool iszip;
int64_t fd;
int64_t tell;
pthread_mutex_t lock;
struct {
uint64_t offset;
uint64_t records;
@ -109,6 +113,22 @@ struct dirent_netbsd {
char d_name[512];
};
void _lockdir(DIR *dir) {
pthread_mutex_lock(&dir->lock);
}
void _unlockdir(DIR *dir) {
pthread_mutex_unlock(&dir->lock);
}
#ifdef _NOPL1
#define _lockdir(d) _NOPL1("__threadcalls", _lockdir, d)
#define _unlockdir(d) _NOPL1("__threadcalls", _unlockdir, d)
#else
#define _lockdir(d) (__threaded ? _lockdir(d) : 0)
#define _unlockdir(d) (__threaded ? _unlockdir(d) : 0)
#endif
static textwindows DIR *opendir_nt_impl(char16_t *name, size_t len) {
DIR *res;
if (len + 2 + 1 <= PATH_MAX) {
@ -299,16 +319,7 @@ DIR *fdopendir(int fd) {
return dir;
}
/**
* Reads next entry from directory stream.
*
* This API doesn't define any particular ordering.
*
* @param dir is the object opendir() or fdopendir() returned
* @return next entry or NULL on end or error, which can be
* differentiated by setting errno to 0 beforehand
*/
struct dirent *readdir(DIR *dir) {
static struct dirent *readdir_impl(DIR *dir) {
size_t n;
long basep;
int rc, mode;
@ -393,6 +404,23 @@ struct dirent *readdir(DIR *dir) {
}
}
/**
* Reads next entry from directory stream.
*
* This API doesn't define any particular ordering.
*
* @param dir is the object opendir() or fdopendir() returned
* @return next entry or NULL on end or error, which can be
* differentiated by setting errno to 0 beforehand
*/
struct dirent *readdir(DIR *dir) {
struct dirent *e;
_lockdir(dir);
e = readdir_impl(dir);
_unlockdir(dir);
return e;
}
/**
* Closes directory object returned by opendir().
* @return 0 on success or -1 w/ errno
@ -421,8 +449,12 @@ int closedir(DIR *dir) {
* Returns offset into directory data.
*/
long telldir(DIR *dir) {
STRACE("telldir(%p) → %d", dir, dir->tell);
return dir->tell;
long rc;
_lockdir(dir);
rc = dir->tell;
STRACE("telldir(%p) → %ld", dir, rc);
_unlockdir(dir);
return rc;
}
/**
@ -430,6 +462,7 @@ long telldir(DIR *dir) {
*/
int dirfd(DIR *dir) {
int rc;
_lockdir(dir);
if (dir->iszip) {
rc = eopnotsupp();
} else if (IsWindows()) {
@ -438,6 +471,7 @@ int dirfd(DIR *dir) {
rc = dir->fd;
}
STRACE("dirfd(%p) → %d% m", dir, rc);
_unlockdir(dir);
return rc;
}
@ -445,6 +479,7 @@ int dirfd(DIR *dir) {
* Seeks to beginning of directory stream.
*/
void rewinddir(DIR *dir) {
_lockdir(dir);
if (dir->iszip) {
dir->tell = 0;
dir->zip.offset = GetZipCdirOffset(weaken(__zipos_get)()->cdir);
@ -463,4 +498,27 @@ void rewinddir(DIR *dir) {
}
}
STRACE("rewinddir(%p)", dir);
_unlockdir(dir);
}
/**
* Seeks in directory stream.
*/
void seekdir(DIR *dir, long off) {
long i;
struct Zipos *zip;
_lockdir(dir);
zip = weaken(__zipos_get)();
if (dir->iszip) {
dir->zip.offset = GetZipCdirOffset(weaken(__zipos_get)()->cdir);
for (i = 0; i < off && i < dir->zip.records; ++i) {
dir->zip.offset += ZIP_CFILE_HDRSIZE(zip->map + dir->zip.offset);
}
} else {
i = lseek(dir->fd, off, SEEK_SET);
dir->buf_pos = dir->buf_end = 0;
}
dir->tell = i;
STRACE("seekdir(%p, %ld) → %ld", dir, off, i);
_unlockdir(dir);
}

25
libc/stdio/gcvt.c Normal file
View file

@ -0,0 +1,25 @@
/*-*- 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 2022 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/fmt/fmt.h"
#include "libc/stdio/stdio.h"
char *gcvt(double x, int n, char *b) {
sprintf(b, "%.*g", n, x);
return b;
}

View file

@ -6,6 +6,7 @@
#include "libc/nexgen32e/threaded.h"
#include "libc/runtime/symbolic.h"
#define _STDIO_H
#define L_ctermid 20
#define FILENAME_MAX PATH_MAX
@ -78,12 +79,14 @@ int fseeko(FILE *, int64_t, int) paramsnonnull();
int64_t ftello(FILE *) paramsnonnull();
void rewind(FILE *) paramsnonnull();
int fopenflags(const char *) paramsnonnull();
void setlinebuf(FILE *);
void setbuf(FILE *, char *);
void setbuffer(FILE *, char *, size_t);
int setvbuf(FILE *, char *, int, size_t);
FILE *popen(const char *, const char *);
int pclose(FILE *);
char *ctermid(char *);
void perror(const char *) relegated;
typedef uint64_t fpos_t;
compatfn char *gets(char *) paramsnonnull();

View file

@ -16,8 +16,13 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/likely.h"
#include "libc/dce.h"
#include "libc/intrin/asan.internal.h"
#include "libc/str/str.h"
typedef char xmm_t __attribute__((__vector_size__(16), __aligned__(16)));
/**
* Searches for fixed-length substring in memory region.
*
@ -27,26 +32,41 @@
* @param needlelen is its character count
* @return pointer to first result or NULL if not found
*/
void *memmem(const void *haystack, size_t haystacklen, const void *needle,
size_t needlelen) {
size_t i, j;
const char *p;
noasan void *memmem(const void *haystack, size_t haystacklen,
const void *needle, size_t needlelen) {
char c;
xmm_t n, *v;
unsigned i, k, m;
const char *p, *q, *e;
if (IsAsan()) __asan_verify(needle, needlelen);
if (IsAsan()) __asan_verify(haystack, haystacklen);
if (!needlelen) return haystack;
if (needlelen <= haystacklen) {
p = memchr(haystack, *(const char *)needle, haystacklen);
if (needlelen == 1) return p;
if (p) {
haystacklen -= p - (const char *)haystack;
haystack = p;
if (UNLIKELY(needlelen > haystacklen)) return 0;
q = needle;
c = *q;
n = (xmm_t){c, c, c, c, c, c, c, c, c, c, c, c, c, c, c, c};
p = haystack;
e = p + haystacklen;
k = (uintptr_t)p & 15;
v = (const xmm_t *)((uintptr_t)p & -16);
m = __builtin_ia32_pmovmskb128(*v == n);
m >>= k;
m <<= k;
for (;;) {
while (!m) {
++v;
if ((const char *)v >= e) return 0;
m = __builtin_ia32_pmovmskb128(*v == n);
}
/* TODO: make not quadratic */
for (i = 0; i < haystacklen; ++i) {
for (j = 0;; ++j) {
if (j == needlelen) return (/*unconst*/ char *)haystack + i;
if (i + j == haystacklen) break;
if (((char *)needle)[j] != ((char *)haystack)[i + j]) break;
do {
k = __builtin_ctzl(m);
p = (const char *)v + k;
if (UNLIKELY(p + needlelen > e)) return 0;
for (i = 1;; ++i) {
if (i == needlelen) return (/*unconst*/ char *)p;
if (p[i] != q[i]) break;
}
}
m &= ~(1 << k);
} while (m);
}
return NULL;
}

9
libc/sysv/consts/l.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_L_H_
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_L_H_
#include "libc/calls/calls.h"
#define L_SET SEEK_SET
#define L_INCR SEEK_CUR
#define L_XTND SEEK_END
#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_L_H_ */

View file

@ -1,89 +1,105 @@
/*-*- 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
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=8 tw=8 fenc=utf-8 :vi
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.
Musl Libc
Copyright © 2005-2014 Rich Felker, et al.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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/nexgen32e/gettls.h"
#include "libc/thread/thread.h"
#include "libc/unicode/langinfo.h"
#include "libc/unicode/locale.h"
#include "libc/unicode/nltypes.h"
static const char c_time[] = "\
Sun\0\n\
Mon\0\n\
Tue\0\n\
Wed\0\n\
Thu\0\n\
Fri\0\n\
Sat\0\n\
Sunday\0\n\
Monday\0\n\
Tuesday\0\n\
Wednesday\0\n\
Thursday\0\n\
Friday\0\n\
Saturday\0\n\
Jan\0\n\
Feb\0\n\
Mar\0\n\
Apr\0\n\
May\0\n\
Jun\0\n\
Jul\0\n\
Aug\0\n\
Sep\0\n\
Oct\0\n\
Nov\0\n\
Dec\0\n\
January\0\n\
February\0\n\
March\0\n\
April\0\n\
May\0\n\
June\0\n\
July\0\n\
August\0\n\
September\0\n\
October\0\n\
November\0\n\
December\0\n\
AM\0\n\
PM\0\n\
a %b %e %T %Y\0\n\
m/%d/%y\0\n\
H:%M:%S\0\n\
I:%M:%S %p\0\n\
0\n\
0\n\
m/%d/%y\0\n\
0123456789\0\n\
a %b %e %T %Y\0\n\
H:%M:%S";
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
static const char c_messages[] = "\
^[yY]\0\
^[nN]\0\
yes\0\
no";
static const char c_time[] =
"Sun\0" "Mon\0" "Tue\0" "Wed\0" "Thu\0" "Fri\0" "Sat\0"
"Sunday\0" "Monday\0" "Tuesday\0" "Wednesday\0"
"Thursday\0" "Friday\0" "Saturday\0"
"Jan\0" "Feb\0" "Mar\0" "Apr\0" "May\0" "Jun\0"
"Jul\0" "Aug\0" "Sep\0" "Oct\0" "Nov\0" "Dec\0"
"January\0" "February\0" "March\0" "April\0"
"May\0" "June\0" "July\0" "August\0"
"September\0" "October\0" "November\0" "December\0"
"AM\0" "PM\0"
"%a %b %e %T %Y\0"
"%m/%d/%y\0"
"%H:%M:%S\0"
"%I:%M:%S %p\0"
"\0"
"\0"
"%m/%d/%y\0"
"0123456789\0"
"%a %b %e %T %Y\0"
"%H:%M:%S";
static const char c_numeric[] = "\
.\0\
";
static const char c_messages[] = "^[yY]\0" "^[nN]\0" "yes\0" "no";
static const char c_numeric[] = ".\0" "";
char *nl_langinfo(int item) {
if (item == CODESET) {
return "UTF-8";
} else {
return 0;
}
char *nl_langinfo_l(nl_item item, locale_t loc)
{
int cat = item >> 16;
int idx = item & 65535;
const char *str;
if (item == CODESET) return loc->cat[LC_CTYPE] ? "UTF-8" : "ASCII";
/* _NL_LOCALE_NAME extension */
if (idx == 65535 && cat < LC_ALL)
return loc->cat[cat] ? (char *)loc->cat[cat]->name : "C";
switch (cat) {
case LC_NUMERIC:
if (idx > 1) return "";
str = c_numeric;
break;
case LC_TIME:
if (idx > 0x31) return "";
str = c_time;
break;
case LC_MONETARY:
if (idx > 0) return "";
str = "";
break;
case LC_MESSAGES:
if (idx > 3) return "";
str = c_messages;
break;
default:
return "";
}
for (; idx; idx--, str++) for (; *str; str++);
// if (cat != LC_NUMERIC && *str) str = LCTRANS(str, cat, loc);
return (char *)str;
}
char *nl_langinfo(nl_item item)
{
return nl_langinfo_l(item, ((cthread_t)__get_tls())->locale);
}

View file

@ -17,13 +17,27 @@
#define LC_MONETARY_MASK 16
#define LC_MESSAGES_MASK 32
#define LC_ALL_MASK 0x1fbf
#define LOCALE_NAME_MAX 23
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct __locale_struct;
#define LC_GLOBAL_LOCALE ((locale_t)-1)
struct __locale_map {
const void *map;
size_t map_size;
char name[LOCALE_NAME_MAX + 1];
const struct __locale_map *next;
};
struct __locale_struct {
const struct __locale_map *cat[6];
};
typedef struct __locale_struct *locale_t;
char *nl_langinfo_l(int, locale_t);
char *setlocale(int, const char *);
double strtod_l(const char *, char **, locale_t);
double wcstod_l(const wchar_t *, wchar_t **, locale_t);

19
libc/unicode/nltypes.h Normal file
View file

@ -0,0 +1,19 @@
#ifndef COSMOPOLITAN_LIBC_UNICODE_NLTYPES_H_
#define COSMOPOLITAN_LIBC_UNICODE_NLTYPES_H_
#define NL_SETD 1
#define NL_CAT_LOCALE 1
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
typedef int nl_item;
typedef void *nl_catd;
nl_catd catopen(const char *, int);
char *catgets(nl_catd, int, int, const char *);
int catclose(nl_catd);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_UNICODE_NLTYPES_H_ */