Add pthread attributes and other libc functions

This commit is contained in:
Justine Tunney 2022-09-07 05:23:44 -07:00
parent d5c9308a43
commit 4339d9f15e
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
81 changed files with 1111 additions and 428 deletions

110
libc/stdio/ecvt.c Normal file
View file

@ -0,0 +1,110 @@
/*-*- 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
$OpenBSD: ecvt.c,v 1.11 2019/01/25 00:19:25 millert Exp $
Copyright (c) 2002, 2006 Todd C. Miller <millert@openbsd.org>
Permission to use, copy, modify, and 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.
Sponsored in part by the Defense Advanced Research Projects
Agency (DARPA) and Air Force Research Laboratory, Air Force
Materiel Command, USAF, under agreement number F39502-99-1-0512.
SUCH DAMAGE.
*/
#include "libc/fmt/fmt.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "third_party/gdtoa/gdtoa.h"
asm(".ident\t\"\\n\\n\
OpenBSD ecvt/gcvt (MIT)\\n\
Copyright (c) 2002, 2006, 2010 Todd C. Miller <millert@openbsd.org>\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
static char *
__cvt(double value, int ndigit, int *decpt, int *sign, int fmode, int pad)
{
static char *s;
char *p, *rve, c;
size_t siz;
if (ndigit == 0) {
*sign = value < 0.0;
*decpt = 0;
return ("");
}
free(s);
s = NULL;
if (ndigit < 0)
siz = -ndigit + 1;
else
siz = ndigit + 1;
/* __dtoa() doesn't allocate space for 0 so we do it by hand */
if (value == 0.0) {
*decpt = 1 - fmode; /* 1 for 'e', 0 for 'f' */
*sign = 0;
if ((rve = s = malloc(siz)) == NULL)
return(NULL);
*rve++ = '0';
*rve = '\0';
} else {
p = dtoa(value, fmode + 2, ndigit, decpt, sign, &rve);
if (p == NULL)
return (NULL);
if (*decpt == 9999) {
/* Infinity or Nan, convert to inf or nan like printf */
*decpt = 0;
c = *p;
freedtoa(p);
return(c == 'I' ? "inf" : "nan");
}
/* Make a local copy and adjust rve to be in terms of s */
if (pad && fmode)
siz += *decpt;
if ((s = malloc(siz)) == NULL) {
freedtoa(p);
return(NULL);
}
(void) strlcpy(s, p, siz);
rve = s + (rve - p);
freedtoa(p);
}
/* Add trailing zeros */
if (pad) {
siz -= rve - s;
while (--siz)
*rve++ = '0';
*rve = '\0';
}
return(s);
}
char *
ecvt(double value, int ndigit, int *decpt, int *sign)
{
return(__cvt(value, ndigit, decpt, sign, 0, 1));
}
char *
fcvt(double value, int ndigit, int *decpt, int *sign)
{
return(__cvt(value, ndigit, decpt, sign, 1, 1));
}

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/intrin/pthread.h"
#include "libc/mem/mem.h"
#include "libc/stdio/internal.h"
#include "libc/stdio/stdio.h"
@ -37,6 +38,7 @@ FILE *fdopen(int fd, const char *mode) {
f->fd = fd;
f->bufmode = ischardev(fd) ? _IOLBF : _IOFBF;
f->iomode = fopenflags(mode);
f->lock.type = PTHREAD_MUTEX_RECURSIVE;
f->size = BUFSIZ;
if ((f->buf = malloc(f->size))) {
if ((f->iomode & O_ACCMODE) != O_RDONLY) {

View file

@ -16,14 +16,14 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/arraylist.internal.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/pushpop.h"
#include "libc/calls/calls.h"
#include "libc/errno.h"
#include "libc/intrin/bits.h"
#include "libc/intrin/pthread.h"
#include "libc/intrin/pushpop.h"
#include "libc/intrin/spinlock.h"
#include "libc/macros.internal.h"
#include "libc/mem/arraylist.internal.h"
#include "libc/mem/mem.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/fflush.internal.h"
@ -34,12 +34,10 @@
static pthread_mutex_t __fflush_lock_obj;
void(__fflush_lock)(void) {
__fflush_lock_obj.attr = PTHREAD_MUTEX_RECURSIVE;
pthread_mutex_lock(&__fflush_lock_obj);
}
void(__fflush_unlock)(void) {
__fflush_lock_obj.attr = PTHREAD_MUTEX_RECURSIVE;
pthread_mutex_unlock(&__fflush_lock_obj);
}

View file

@ -23,6 +23,5 @@
* Acquires reentrant lock on stdio object, blocking if needed.
*/
void(flockfile)(FILE *f) {
f->lock.attr = PTHREAD_MUTEX_RECURSIVE;
pthread_mutex_lock(&f->lock);
}

View file

@ -54,6 +54,7 @@ FILE *fmemopen(void *buf, size_t size, const char *mode) {
f->end = size;
f->size = size;
f->iomode = fopenflags(mode);
f->lock.type = PTHREAD_MUTEX_RECURSIVE;
if (f->iomode & O_APPEND) {
if ((p = memchr(buf, '\0', size))) {
f->beg = p - (char *)buf;

View file

@ -22,6 +22,5 @@
* Releases lock on stdio object.
*/
void(funlockfile)(FILE *f) {
f->lock.attr = PTHREAD_MUTEX_RECURSIVE;
pthread_mutex_unlock(&f->lock);
}

View file

@ -1,25 +1,130 @@
/*-*- 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
/*-*- 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
Copyright 2022 Justine Alexandra Roberts Tunney
$OpenBSD: ecvt.c,v 1.11 2019/01/25 00:19:25 millert Exp $
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.
Copyright (c) 2002, 2006, 2010 Todd C. Miller <millert@openbsd.org>
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.
Permission to use, copy, modify, and 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.
Sponsored in part by the Defense Advanced Research Projects
Agency (DARPA) and Air Force Research Laboratory, Air Force
Materiel Command, USAF, under agreement number F39502-99-1-0512.
SUCH DAMAGE.
*/
#include "libc/fmt/fmt.h"
#include "libc/stdio/stdio.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/str/unicode.h"
#include "third_party/gdtoa/gdtoa.h"
char *gcvt(double x, int n, char *b) {
sprintf(b, "%.*g", n, x);
return b;
asm(".ident\t\"\\n\\n\
OpenBSD ecvt/gcvt (MIT)\\n\
Copyright (c) 2002, 2006, 2010 Todd C. Miller <millert@openbsd.org>\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
#define DEFPREC 6
char *
gcvt(double value, int ndigit, char *buf)
{
char *digits, *dst, *src;
int i, decpt, sign;
struct lconv *lconv;
lconv = localeconv();
if (ndigit <= 0) {
/* Match printf(3) behavior. */
ndigit = ndigit ? DEFPREC : 1;
}
digits = dtoa(value, 2, ndigit, &decpt, &sign, NULL);
if (digits == NULL)
return (NULL);
if (decpt == 9999) {
/*
* Infinity or NaN, convert to inf or nan with sign.
* We can't infer buffer size based on ndigit.
* We have to assume it is at least 5 chars.
*/
snprintf(buf, 5, "%s%s", sign ? "-" : "",
*digits == 'I' ? "inf" : "nan");
freedtoa(digits);
return (buf);
}
dst = buf;
if (sign)
*dst++ = '-';
/* Match printf(3) behavior for exponential vs. regular fomatting. */
if (decpt <= -4 || decpt > ndigit) {
/* exponential format (e.g. 1.2345e+13) */
if (--decpt < 0) {
sign = 1;
decpt = -decpt;
} else
sign = 0;
src = digits;
*dst++ = *src++;
if (*src != '\0') {
*dst++ = *lconv->decimal_point;
do {
*dst++ = *src++;
} while (*src != '\0');
}
*dst++ = 'e';
if (sign)
*dst++ = '-';
else
*dst++ = '+';
if (decpt < 10) {
*dst++ = '0';
*dst++ = '0' + decpt;
*dst = '\0';
} else {
/* XXX - optimize */
for (sign = decpt, i = 0; (sign /= 10) != 0; i++)
continue;
dst[i + 1] = '\0';
while (decpt != 0) {
dst[i--] = '0' + decpt % 10;
decpt /= 10;
}
}
} else {
/* standard format */
for (i = 0, src = digits; i < decpt; i++) {
if (*src != '\0')
*dst++ = *src++;
else
*dst++ = '0';
}
if (*src != '\0') {
if (src == digits)
*dst++ = '0'; /* zero before decimal point */
*dst++ = *lconv->decimal_point;
while (decpt < 0) {
*dst++ = '0';
decpt++;
}
for (i = decpt; digits[i] != '\0'; i++) {
*dst++ = digits[i];
}
}
*dst = '\0';
}
freedtoa(digits);
return (buf);
}

View file

@ -20,6 +20,7 @@
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
.init.start 400,_init_stderr
@ -33,5 +34,6 @@
ezlea __stderr_buf,cx
mov %rcx,0x18(%rax) # f.buf
movl $BUFSIZ,0x20(%rax) # f.size
movb $PTHREAD_MUTEX_RECURSIVE,0x38(%rax) # f.lock.attr
mov %rax,stderr(%rip)
.init.end 400,_init_stderr,globl,hidden

View file

@ -20,6 +20,7 @@
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
.init.start 400,_init_stdin
@ -29,5 +30,6 @@
ezlea __stdin_buf,cx
mov %rcx,0x18(%rax) # f.buf
movl $BUFSIZ,0x20(%rax) # f.size
movb $PTHREAD_MUTEX_RECURSIVE,0x38(%rax) # f.lock.attr
mov %rax,stdin(%rip)
.init.end 400,_init_stdin,globl,hidden

View file

@ -20,6 +20,7 @@
#include "libc/dce.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/intrin/pthread.h"
#include "libc/macros.internal.h"
.init.start 400,_init_stdout
@ -31,5 +32,6 @@
ezlea __stdout_buf,cx
mov %rcx,0x18(%rax) # f.buf
movl $BUFSIZ,0x20(%rax) # f.size
movb $PTHREAD_MUTEX_RECURSIVE,0x38(%rax) # f.lock.attr
mov %rax,stdout(%rip)
.init.end 400,_init_stdout,globl,hidden