Clean up some code

This commit is contained in:
Justine Tunney 2023-10-11 11:45:31 -07:00
parent ec3275179f
commit 285c565051
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
33 changed files with 122 additions and 382 deletions

View file

@ -1,26 +0,0 @@
#ifndef COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_
#define COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/* TODO: DELETE */
forceinline int32_t bisectcarleft(const int32_t (*cons)[2], size_t count,
const int32_t key) {
size_t left = 0;
size_t right = count;
while (left < right) {
size_t m = (left + right) >> 1;
if (cons[m][0] < key) {
left = m + 1;
} else {
right = m;
}
}
if (left && (left == count || cons[left][0] > key)) left--;
return left;
}
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_ALG_BISECTCARLEFT_H_ */

View file

@ -21,7 +21,7 @@
/**
* Searches sorted array for exact item in logarithmic time.
* @see bsearch_r(), bisectcarleft()
* @see bsearch_r()
*/
void *bsearch(const void *key, const void *base, size_t nmemb, size_t size,
int cmp(const void *a, const void *b)) {

View file

@ -21,7 +21,7 @@
/**
* Searches sorted array for exact item in logarithmic time.
* @see bsearch(), bisectcarleft()
* @see bsearch()
*/
void *bsearch_r(const void *key, const void *base, size_t nmemb, size_t size,
int cmp(const void *a, const void *b, void *arg), void *arg) {

View file

@ -44,15 +44,10 @@ Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
// clang-format off
static inline bool IsSlash(char c)
{
return c == '/' || c == '\\';
}
static size_t GetSlashLen(const char *s)
{
const char *s0 = s;
while (IsSlash(*s)) s++;
while (*s == '/') s++;
return s-s0;
}
@ -123,7 +118,7 @@ restart:
for (; ; p+=GetSlashLen(stack+p)) {
/* If stack starts with /, the whole component is / or //
* and the output state must be reset. */
if (IsSlash(stack[p])) {
if (stack[p] == '/') {
check_dir=0;
nup=0;
q=0;
@ -147,7 +142,7 @@ restart:
/* Copy next component onto output at least temporarily, to
* call readlink, but wait to advance output position until
* determining it's not a link. */
if (q && !IsSlash(output[q-1])) {
if (q && output[q-1] != '/') {
if (!p) goto toolong;
stack[--p] = '/';
l++;
@ -180,8 +175,8 @@ restart:
skip_readlink:
check_dir = 0;
if (up) {
while(q && !IsSlash(output[q-1])) q--;
if (q>1 && (q>2 || !IsSlash(output[0]))) q--;
while(q && output[q-1] != '/') q--;
if (q>1 && (q>2 || output[0] != '/')) q--;
continue;
}
if (l0) q += l;
@ -203,8 +198,8 @@ skip_readlink:
/* If link contents end in /, strip any slashes already on
* stack to avoid /->// or //->/// or spurious toolong. */
if (IsSlash(stack[k-1])) {
while (IsSlash(stack[p]))
if (stack[k-1] == '/') {
while (stack[p] == '/')
p++;
}
p -= k;
@ -217,18 +212,18 @@ skip_readlink:
output[q] = 0;
if (!IsSlash(output[0])) {
if (output[0] != '/') {
if (!getcwd(stack, sizeof(stack))) return 0;
l = strlen(stack);
/* Cancel any initial .. components. */
p = 0;
while (nup--) {
while(l>1 && !IsSlash(stack[l-1])) l--;
while(l>1 && stack[l-1] != '/') l--;
if (l>1) l--;
p += 2;
if (p<q) p++;
}
if (q-p && !IsSlash(stack[l-1])) stack[l++] = '/';
if (q-p && stack[l-1] != '/') stack[l++] = '/';
if (l + (q-p) + 1 >= PATH_MAX) goto toolong;
memmove(output + l, output + p, q - p + 1);
if (l) memcpy(output, stack, l);