Improve cosmo's conformance to libc-test

This change addresses various open source compatibility issues, so that
we pass 313/411 of the tests in https://github.com/jart/libc-test where
earlier today we were passing about 30/411 of them, due to header toil.
Please note that Glibc only passes 341/411 so 313 today is pretty good!

- Make the conformance of libc/isystem/ headers nearly perfect
- Import more of the remaining math library routines from Musl
- Fix inconsistencies with type signatures of calls like umask
- Write tests for getpriority/setpriority which work great now
- conform to `struct sockaddr *` on remaining socket functions
- Import a bunch of uninteresting stdlib functions e.g. rand48
- Introduce readdir_r, scandir, pthread_kill, sigsetjmp, etc..

Follow the instructions in our `tool/scripts/cosmocc` toolchain to run
these tests yourself. You use `make CC=cosmocc` on the test repository
This commit is contained in:
Justine Tunney 2022-10-10 17:52:41 -07:00
parent 467a332e38
commit e557058ac8
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
189 changed files with 5091 additions and 884 deletions

24
libc/stdio/alphasort.c Normal file
View file

@ -0,0 +1,24 @@
/*-*- 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/calls/struct/dirent.h"
#include "libc/str/str.h"
int alphasort(const struct dirent **a, const struct dirent **b) {
return strcoll((*a)->d_name, (*b)->d_name);
}

View file

@ -439,6 +439,39 @@ struct dirent *readdir(DIR *dir) {
return e;
}
/**
* Reads directory entry reentrantly.
*
* @param dir is the object opendir() or fdopendir() returned
* @param output is where directory entry is copied if not eof
* @param result will receive `output` pointer, or null on eof
* @return 0 on success, or errno on error
* @returnserrno
* @threadsafe
*/
errno_t readdir_r(DIR *dir, struct dirent *output, struct dirent **result) {
int err, olderr;
struct dirent *entry;
_lockdir(dir);
olderr = errno;
errno = 0;
entry = readdir_impl(dir);
err = errno;
errno = olderr;
if (err) {
_unlockdir(dir);
return err;
}
if (entry) {
memcpy(output, entry, entry->d_reclen);
} else {
output = 0;
}
_unlockdir(dir);
*result = output;
return 0;
}
/**
* Closes directory object returned by opendir().
* @return 0 on success or -1 w/ errno
@ -464,6 +497,7 @@ int closedir(DIR *dir) {
/**
* Returns offset into directory data.
* @threadsafe
*/
long telldir(DIR *dir) {
long rc;
@ -475,6 +509,7 @@ long telldir(DIR *dir) {
/**
* Returns file descriptor associated with DIR object.
* @threadsafe
*/
int dirfd(DIR *dir) {
int rc;
@ -492,6 +527,7 @@ int dirfd(DIR *dir) {
/**
* Seeks to beginning of directory stream.
* @threadsafe
*/
void rewinddir(DIR *dir) {
_lockdir(dir);
@ -517,6 +553,7 @@ void rewinddir(DIR *dir) {
/**
* Seeks in directory stream.
* @threadsafe
*/
void seekdir(DIR *dir, long off) {
long i;

View file

@ -64,7 +64,9 @@ char *fgets_unlocked(char *s, int size, FILE *f) {
if (c == '\n') break;
}
}
*p = '\0';
if (p > s || f->state != -1) {
*p = '\0';
}
}
return p > s ? s : NULL;
}

View file

@ -8,7 +8,7 @@ COSMOPOLITAN_C_START_
*/
int rand(void);
void srand(uint64_t);
void srand(unsigned);
double poz(double);
double pochisq(double, int);
void rt_init(int);

View file

@ -28,8 +28,7 @@
*/
void rewind(FILE *f) {
flockfile(f);
if (!fseeko_unlocked(f, 0, SEEK_SET)) {
f->state = 0;
}
fseeko_unlocked(f, 0, SEEK_SET);
f->state = 0;
funlockfile(f);
}

78
libc/stdio/scandir.c Normal file
View file

@ -0,0 +1,78 @@
/*-*- 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
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.
*/
#include "libc/calls/struct/dirent.h"
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/mem/alg.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
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
int scandir(const char *path, struct dirent ***res,
int (*sel)(const struct dirent *),
int (*cmp)(const struct dirent **, const struct dirent **))
{
DIR *d = opendir(path);
struct dirent *de, **names=0, **tmp;
size_t cnt=0, len=0;
int old_errno = errno;
if (!d) return -1;
while ((errno=0), (de = readdir(d))) {
if (sel && !sel(de)) continue;
if (cnt >= len) {
len = 2*len+1;
if (len > SIZE_MAX/sizeof *names) break;
tmp = realloc(names, len * sizeof *names);
if (!tmp) break;
names = tmp;
}
names[cnt] = malloc(de->d_reclen);
if (!names[cnt]) break;
memcpy(names[cnt++], de, de->d_reclen);
}
closedir(d);
if (errno) {
if (names) while (cnt-->0) free(names[cnt]);
free(names);
return -1;
}
errno = old_errno;
if (cmp) qsort(names, cnt, sizeof *names, (int (*)(const void *, const void *))cmp);
*res = names;
return cnt;
}

View file

@ -42,7 +42,7 @@ int posix_spawnattr_setschedparam(posix_spawnattr_t *,
const struct sched_param *);
int posix_spawnattr_getsigmask(const posix_spawnattr_t *, sigset_t *);
int posix_spawnattr_setsigmask(posix_spawnattr_t *, const sigset_t *);
int posix_spawnattr_getdefault(const posix_spawnattr_t *, sigset_t *);
int posix_spawnattr_getsigdefault(const posix_spawnattr_t *, sigset_t *);
int posix_spawnattr_setsigdefault(posix_spawnattr_t *, const sigset_t *);
COSMOPOLITAN_C_END_

View file

@ -23,4 +23,6 @@ extern uint64_t g_rando;
/**
* Seeds random number generator that's used by rand().
*/
void(srand)(uint64_t seed) { g_rando = seed; }
void srand(unsigned seed) {
g_rando = seed;
}

24
libc/stdio/versionsort.c Normal file
View file

@ -0,0 +1,24 @@
/*-*- 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/calls/struct/dirent.h"
#include "libc/str/str.h"
int versionsort(const struct dirent **a, const struct dirent **b) {
return strverscmp((*a)->d_name, (*b)->d_name);
}