Make improvements

- Document redbean's argon2 module
- Fix regressions in cthreads library
- Make testlib work better with threads
- Give the cthreads library lots of love
- Remove some of the stdio assembly code
- Implement getloadavg() across platforms
- Code size optimizations for errnos, etc.
- Only check for signals in main thread on Windows
- Make errnos for dup2 / dup3 consistent with posix

This change also fixes a bug in the argon2 module, where the NUL
terminator was being included in the hash encoded ascii string. This
shouldn't require any database migrations to folks who found this module
and productionized it, since the argon2 library treats it as a c string.
This commit is contained in:
Justine Tunney 2022-05-27 13:25:46 -07:00
parent cb67223051
commit de5de19004
234 changed files with 1728 additions and 1993 deletions

View file

@ -18,6 +18,15 @@
*/
#include "libc/stdio/stdio.h"
void clearerr_unlocked(FILE *f) {
f->state = 0;
/**
* Clears error state on stream.
*
* @param f is file object stream pointer
* @see clearerr_unlocked()
* @threadsafe
*/
void clearerr(FILE *f) {
flockfile(f);
clearerr_unlocked(f);
funlockfile(f);
}

View file

@ -1,5 +1,5 @@
/*-*- 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
/*-*- 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 2020 Justine Alexandra Roberts Tunney
@ -16,15 +16,8 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/stdio/stdio.h"
// Clears error state on stream.
//
// @param rdi has stream pointer
// @see clearerr_unlocked()
// @threadsafe
clearerr:
mov %rdi,%r11
ezlea clearerr_unlocked,ax
jmp stdio_unlock
.endfn clearerr,globl
void clearerr_unlocked(FILE *f) {
f->state = 0;
}

View file

@ -20,7 +20,15 @@
/**
* Returns true if stream is in end-of-file state.
*
* @param f is file object stream pointer
* @see feof_unlocked()
* @threadsafe
*/
int feof_unlocked(FILE *f) {
return f->state == -1;
int feof(FILE *f) {
int rc;
flockfile(f);
rc = feof_unlocked(f);
funlockfile(f);
return rc;
}

View file

@ -1,5 +1,5 @@
/*-*- 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
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
@ -16,15 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/stdio/stdio.h"
// Reads byte from stream.
//
// @param rdi has stream object pointer
// @return byte in range 0..255, or -1 w/ errno
// @see fgetc_unlocked()
// @threadsafe
fgetc: mov %rdi,%r11
ezlea fgetc_unlocked,ax
jmp stdio_unlock
.endfn fgetc,globl
/**
* Returns true if stream is in end-of-file state.
*
* @param f is file object stream pointer
* @see feof()
*/
int feof_unlocked(FILE *f) {
return f->state == -1;
}

View file

@ -21,9 +21,16 @@
/**
* Returns nonzero if stream is in error state.
*
* @param f is file stream pointer
* @return non-zero if and only if it's an error state
* @see ferror_unlocked(), feof()
* @note EOF doesn't count
* @see feof()
* @threadsafe
*/
errno_t ferror_unlocked(FILE *f) {
return f->state > 0 ? f->state : 0;
errno_t ferror(FILE *f) {
int rc;
flockfile(f);
rc = ferror_unlocked(f);
funlockfile(f);
return rc;
}

View file

@ -1,5 +1,5 @@
/*-*- 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
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
@ -16,15 +16,16 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/stdio/stdio.h"
// Returns true if stream is in end-of-file state.
//
// @param rdi has file stream object pointer
// @note EOF doesn't count
// @see feof_unlocked()
// @threadsafe
feof: mov %rdi,%r11
ezlea feof_unlocked,ax
jmp stdio_unlock
.endfn feof,globl
/**
* Returns nonzero if stream is in error state.
*
* @param f is file stream pointer
* @return non-zero if and only if it's an error state
* @note EOF doesn't count
* @see ferror(), feof()
*/
errno_t ferror_unlocked(FILE *f) {
return f->state > 0 ? f->state : 0;
}

View file

@ -20,14 +20,16 @@
/**
* Reads byte from stream.
*
* @param f is non-null file object stream pointer
* @return byte in range 0..255, or -1 w/ errno
* @see fgetc_unlocked()
* @threadsafe
*/
int fgetc_unlocked(FILE *f) {
unsigned char b[1];
if (f->beg < f->end) {
return f->buf[f->beg++] & 0xff;
} else {
if (!fread_unlocked(b, 1, 1, f)) return -1;
return b[0];
}
int fgetc(FILE *f) {
int rc;
flockfile(f);
rc = fgetc_unlocked(f);
funlockfile(f);
return rc;
}

View file

@ -1,5 +1,5 @@
/*-*- 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
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set net ft=c ts=8 sts=2 sw=2 fenc=utf-8 :vi
Copyright 2020 Justine Alexandra Roberts Tunney
@ -16,15 +16,21 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/macros.internal.h"
#include "libc/stdio/stdio.h"
// Returns nonzero if stream is in error state.
//
// @param rdi has file stream object pointer
// @note EOF doesn't count
// @see ferror_unlocked()
// @threadsafe
ferror: mov %rdi,%r11
ezlea ferror_unlocked,ax
jmp stdio_unlock
.endfn ferror,globl
/**
* Reads byte from stream.
*
* @param f is file object stream pointer
* @return byte in range 0..255, or -1 w/ errno
* @see fgetc()
*/
int fgetc_unlocked(FILE *f) {
unsigned char b[1];
if (f->beg < f->end) {
return f->buf[f->beg++] & 0xff;
} else {
if (!fread_unlocked(b, 1, 1, f)) return -1;
return b[0];
}
}

View file

@ -16,7 +16,6 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/errno.h"
#include "libc/stdio/stdio.h"
/**
@ -31,24 +30,13 @@
* @param f is non-null file oject stream pointer
* @return s on success, NULL on error, or NULL if EOF happens when
* zero characters have been read
* @see fgets_unlocked()
* @threadsafe
*/
char *fgets_unlocked(char *s, int size, FILE *f) {
int c;
char *p;
p = s;
if (size > 0) {
while (--size > 0) {
if ((c = fgetc_unlocked(f)) == -1) {
if (ferror_unlocked(f) == EINTR) {
continue;
} else {
break;
}
}
*p++ = c & 255;
if (c == '\n') break;
}
*p = '\0';
}
return p > s ? s : NULL;
char *fgets(char *s, int size, FILE *f) {
char *res;
flockfile(f);
res = fgets_unlocked(s, size, f);
funlockfile(f);
return res;
}

View file

@ -0,0 +1,54 @@
/*-*- 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 2020 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/errno.h"
#include "libc/stdio/stdio.h"
/**
* Reads line from stream.
*
* This function is similar to getline() except it'll truncate lines
* exceeding size. The line ending marker is included and may be removed
* using _chomp().
*
* @param s is output buffer
* @param size is capacity of s
* @param f is non-null file oject stream pointer
* @return s on success, NULL on error, or NULL if EOF happens when
* zero characters have been read
*/
char *fgets_unlocked(char *s, int size, FILE *f) {
int c;
char *p;
p = s;
if (size > 0) {
while (--size > 0) {
if ((c = fgetc_unlocked(f)) == -1) {
if (ferror_unlocked(f) == EINTR) {
continue;
} else {
break;
}
}
*p++ = c & 255;
if (c == '\n') break;
}
*p = '\0';
}
return p > s ? s : NULL;
}

View file

@ -17,35 +17,19 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/stdio/stdio.h"
#include "libc/str/thompike.h"
#include "libc/str/tpdecodecb.internal.h"
/**
* Reads UTF-8 character from stream.
*
* @param f is non-null file object stream pointer
* @return wide character or -1 on EOF or error
* @see fgetwc_unlocked()
* @threadsafe
*/
wint_t fgetwc_unlocked(FILE *f) {
int c, n;
wint_t b, x, y;
if (f->beg < f->end) {
b = f->buf[f->beg++] & 0xff;
} else if ((c = fgetc_unlocked(f)) != -1) {
b = c;
} else {
return -1;
}
if (b < 0300) return b;
n = ThomPikeLen(b);
x = ThomPikeByte(b);
while (--n) {
if ((c = fgetc_unlocked(f)) == -1) return -1;
y = c;
if (ThomPikeCont(y)) {
x = ThomPikeMerge(x, y);
} else {
ungetc_unlocked(y, f);
return b;
}
}
return x;
wint_t fgetwc(FILE *f) {
wint_t wc;
flockfile(f);
wc = fgetwc_unlocked(f);
funlockfile(f);
return wc;
}

View file

@ -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 2020 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"
#include "libc/str/thompike.h"
#include "libc/str/tpdecodecb.internal.h"
/**
* Reads UTF-8 character from stream.
* @return wide character or -1 on EOF or error
*/
wint_t fgetwc_unlocked(FILE *f) {
int c, n;
wint_t b, x, y;
if (f->beg < f->end) {
b = f->buf[f->beg++] & 0xff;
} else if ((c = fgetc_unlocked(f)) != -1) {
b = c;
} else {
return -1;
}
if (b < 0300) return b;
n = ThomPikeLen(b);
x = ThomPikeByte(b);
while (--n) {
if ((c = fgetc_unlocked(f)) == -1) return -1;
y = c;
if (ThomPikeCont(y)) {
x = ThomPikeMerge(x, y);
} else {
ungetc_unlocked(y, f);
return b;
}
}
return x;
}

View file

@ -1,37 +0,0 @@
/*-*- 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 2020 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"
// Reads line from stream.
//
// This function is similar to getline() except it'll truncate
// lines exceeding size. The line ending marker is included
// and may be removed using _chomp().
//
// @param rdi is output buffer
// @param rsi is size of rdi buffer
// @param rdx is file stream object pointer
// @return rax has rdi on success, NULL on error or
// NULL if EOF happens with zero chars read
// @see fgets_unlocked()
// @threadsafe
fgets: mov %rdx,%r11
ezlea fgets_unlocked,ax
jmp stdio_unlock
.endfn fgets,globl

View file

@ -1,30 +0,0 @@
/*-*- 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 2020 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"
// Reads UTF-8 wide character from stream.
//
// @param rdi has stream object pointer
// @return wide character or -1 on EOF or error
// @see fgetwc_unlocked()
// @threadsafe
fgetwc: mov %rdi,%r11
ezlea fgetwc_unlocked,ax
jmp stdio_unlock
.endfn fgetwc,globl