Add glob and some finer tuning of documentation

This commit is contained in:
Justine Tunney 2020-06-21 00:10:11 -07:00
parent 799e24a87b
commit d51409ccd9
77 changed files with 1321 additions and 736 deletions

View file

@ -7,11 +7,10 @@ struct avir1 {
void *p;
};
void avir1init(struct avir1 *self);
void avir1free(struct avir1 *self);
void avir1(struct avir1 *resizer, size_t dyn, size_t dxn, void *dst,
size_t dstsize, size_t syn, size_t sxn, size_t ssw, const void *src,
size_t srcsize);
void avir1init(struct avir1 *);
void avir1free(struct avir1 *);
void avir1(struct avir1 *, size_t, size_t, void *, size_t, size_t, size_t,
size_t, const void *, size_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -20,6 +20,7 @@
#include "libc/bits/mmintrin.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/xmmintrin.h"
#include "libc/bits/emmintrin.h"
namespace avir {

368
third_party/musl/fnmatch.c vendored Normal file
View file

@ -0,0 +1,368 @@
/*-*- 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
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/limits.h"
#include "libc/str/str.h"
#include "third_party/musl/fnmatch.h"
/*
* An implementation of what I call the "Sea of Stars" algorithm for
* POSIX fnmatch(). The basic idea is that we factor the pattern into
* a head component (which we match first and can reject without ever
* measuring the length of the string), an optional tail component
* (which only exists if the pattern contains at least one star), and
* an optional "sea of stars", a set of star-separated components
* between the head and tail. After the head and tail matches have
* been removed from the input string, the components in the "sea of
* stars" are matched sequentially by searching for their first
* occurrence past the end of the previous match.
*
* - Rich Felker, April 2012
*/
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
#define END 0
#define UNMATCHABLE -2
#define BRACKET -3
#define QUESTION -4
#define STAR -5
static int FnmatchNextString(const char *str, size_t n, size_t *step) {
if (!n) {
*step = 0;
return 0;
}
if (str[0] >= 128U) {
wchar_t wc;
int k = mbtowc(&wc, str, n);
if (k < 0) {
*step = 1;
return -1;
}
*step = k;
return wc;
}
*step = 1;
return str[0];
}
static int FnmatchNextPattern(const char *pat, size_t m, size_t *step,
int flags) {
int esc = 0;
if (!m || !*pat) {
*step = 0;
return END;
}
*step = 1;
if (pat[0] == '\\' && pat[1] && !(flags & FNM_NOESCAPE)) {
*step = 2;
pat++;
esc = 1;
goto escaped;
}
if (pat[0] == '[') {
size_t k = 1;
if (k < m)
if (pat[k] == '^' || pat[k] == '!') k++;
if (k < m)
if (pat[k] == ']') k++;
for (; k < m && pat[k] && pat[k] != ']'; k++) {
if (k + 1 < m && pat[k + 1] && pat[k] == '[' &&
(pat[k + 1] == ':' || pat[k + 1] == '.' || pat[k + 1] == '=')) {
int z = pat[k + 1];
k += 2;
if (k < m && pat[k]) k++;
while (k < m && pat[k] && (pat[k - 1] != z || pat[k] != ']')) k++;
if (k == m || !pat[k]) break;
}
}
if (k == m || !pat[k]) {
*step = 1;
return '[';
}
*step = k + 1;
return BRACKET;
}
if (pat[0] == '*') return STAR;
if (pat[0] == '?') return QUESTION;
escaped:
if (pat[0] >= 128U) {
wchar_t wc;
int k = mbtowc(&wc, pat, m);
if (k < 0) {
*step = 0;
return UNMATCHABLE;
}
*step = k + esc;
return wc;
}
return pat[0];
}
static int FnmatchCaseFold(int k) {
int c = towupper(k);
return c == k ? towlower(k) : c;
}
static int FnmatchBracket(const char *p, int k, int kfold) {
wchar_t wc;
int inv = 0;
p++;
if (*p == '^' || *p == '!') {
inv = 1;
p++;
}
if (*p == ']') {
if (k == ']') return !inv;
p++;
} else if (*p == '-') {
if (k == '-') return !inv;
p++;
}
wc = p[-1];
for (; *p != ']'; p++) {
if (p[0] == '-' && p[1] != ']') {
wchar_t wc2;
int l = mbtowc(&wc2, p + 1, 4);
if (l < 0) return 0;
if (wc <= wc2)
if ((unsigned)k - wc <= wc2 - wc || (unsigned)kfold - wc <= wc2 - wc)
return !inv;
p += l - 1;
continue;
}
if (p[0] == '[' && (p[1] == ':' || p[1] == '.' || p[1] == '=')) {
const char *p0 = p + 2;
int z = p[1];
p += 3;
while (p[-1] != z || p[0] != ']') p++;
if (z == ':' && p - 1 - p0 < 16) {
char buf[16];
memcpy(buf, p0, p - 1 - p0);
buf[p - 1 - p0] = 0;
if (iswctype(k, wctype(buf)) || iswctype(kfold, wctype(buf)))
return !inv;
}
continue;
}
if (*p < 128U) {
wc = (unsigned char)*p;
} else {
int l = mbtowc(&wc, p, 4);
if (l < 0) return 0;
p += l - 1;
}
if (wc == k || wc == kfold) return !inv;
}
return inv;
}
static int FnmatchPerform(const char *pat, size_t m, const char *str, size_t n,
int flags) {
const char *p, *ptail, *endpat;
const char *s, *stail, *endstr;
size_t pinc, sinc, tailcnt = 0;
int c, k, kfold;
if (flags & FNM_PERIOD) {
if (*str == '.' && *pat != '.') {
return FNM_NOMATCH;
}
}
for (;;) {
switch ((c = FnmatchNextPattern(pat, m, &pinc, flags))) {
case UNMATCHABLE:
return FNM_NOMATCH;
case STAR:
pat++;
m--;
break;
default:
k = FnmatchNextString(str, n, &sinc);
if (k <= 0) return (c == END) ? 0 : FNM_NOMATCH;
str += sinc;
n -= sinc;
kfold = flags & FNM_CASEFOLD ? FnmatchCaseFold(k) : k;
if (c == BRACKET) {
if (!FnmatchBracket(pat, k, kfold)) return FNM_NOMATCH;
} else if (c != QUESTION && k != c && kfold != c) {
return FNM_NOMATCH;
}
pat += pinc;
m -= pinc;
continue;
}
break;
}
/* Compute real pat length if it was initially unknown/-1 */
m = strnlen(pat, m);
endpat = pat + m;
/* Find the last * in pat and count chars needed after it */
for (p = ptail = pat; p < endpat; p += pinc) {
switch (FnmatchNextPattern(p, endpat - p, &pinc, flags)) {
case UNMATCHABLE:
return FNM_NOMATCH;
case STAR:
tailcnt = 0;
ptail = p + 1;
break;
default:
tailcnt++;
break;
}
}
/* Past this point we need not check for UNMATCHABLE in pat,
* because all of pat has already been parsed once. */
/* Compute real str length if it was initially unknown/-1 */
n = strnlen(str, n);
endstr = str + n;
if (n < tailcnt) {
return FNM_NOMATCH;
}
/* Find the final tailcnt chars of str, accounting for UTF-8.
* On illegal sequences we may get it wrong, but in that case
* we necessarily have a matching failure anyway. */
for (s = endstr; s > str && tailcnt; tailcnt--) {
if (s[-1] < 128U || MB_CUR_MAX == 1) {
s--;
} else {
while ((unsigned char)*--s - 0x80U < 0x40 && s > str)
;
}
}
if (tailcnt) return FNM_NOMATCH;
stail = s;
/* Check that the pat and str tails match */
p = ptail;
for (;;) {
c = FnmatchNextPattern(p, endpat - p, &pinc, flags);
p += pinc;
if ((k = FnmatchNextString(s, endstr - s, &sinc)) <= 0) {
if (c != END) return FNM_NOMATCH;
break;
}
s += sinc;
kfold = flags & FNM_CASEFOLD ? FnmatchCaseFold(k) : k;
if (c == BRACKET) {
if (!FnmatchBracket(p - pinc, k, kfold)) return FNM_NOMATCH;
} else if (c != QUESTION && k != c && kfold != c) {
return FNM_NOMATCH;
}
}
/* We're all done with the tails now, so throw them out */
endstr = stail;
endpat = ptail;
/* Match pattern components until there are none left */
while (pat < endpat) {
p = pat;
s = str;
for (;;) {
c = FnmatchNextPattern(p, endpat - p, &pinc, flags);
p += pinc;
/* Encountering * completes/commits a component */
if (c == STAR) {
pat = p;
str = s;
break;
}
k = FnmatchNextString(s, endstr - s, &sinc);
if (!k) return FNM_NOMATCH;
kfold = flags & FNM_CASEFOLD ? FnmatchCaseFold(k) : k;
if (c == BRACKET) {
if (!FnmatchBracket(p - pinc, k, kfold)) break;
} else if (c != QUESTION && k != c && kfold != c) {
break;
}
s += sinc;
}
if (c == STAR) continue;
/* If we failed, advance str, by 1 char if it's a valid
* char, or past all invalid bytes otherwise. */
k = FnmatchNextString(str, endstr - str, &sinc);
if (k > 0) {
str += sinc;
} else {
str++;
while (FnmatchNextString(str, endstr - str, &sinc) < 0) {
str++;
}
}
}
return 0;
}
/**
* Matches filename.
*
* - `*` for wildcard
* - `?` for single character
* - `[abc]` to match character within set
* - `[!abc]` to match character not within set
* - `\*\?\[\]` for escaping above special syntax
*
* @see glob()
*/
int fnmatch(const char *pat, const char *str, int flags) {
const char *s, *p;
size_t inc;
int c;
if (flags & FNM_PATHNAME) {
for (;;) {
for (s = str; *s && *s != '/'; s++)
;
for (p = pat;
(c = FnmatchNextPattern(p, -1, &inc, flags)) != END && c != '/';
p += inc)
;
if (c != *s && (!*s || !(flags & FNM_LEADING_DIR))) return FNM_NOMATCH;
if (FnmatchPerform(pat, p - pat, str, s - str, flags)) return FNM_NOMATCH;
if (!c) return 0;
str = s + 1;
pat = p + inc;
}
} else if (flags & FNM_LEADING_DIR) {
for (s = str; *s; s++) {
if (*s != '/') continue;
if (!FnmatchPerform(pat, -1, str, s - str, flags)) return 0;
}
}
return FnmatchPerform(pat, -1, str, -1, flags);
}

20
third_party/musl/fnmatch.h vendored Normal file
View file

@ -0,0 +1,20 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_REGEX_FNMATCH_H_
#define COSMOPOLITAN_THIRD_PARTY_REGEX_FNMATCH_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define FNM_PATHNAME 0x1
#define FNM_NOESCAPE 0x2
#define FNM_PERIOD 0x4
#define FNM_LEADING_DIR 0x8
#define FNM_CASEFOLD 0x10
#define FNM_FILE_NAME FNM_PATHNAME
#define FNM_NOMATCH 1
#define FNM_NOSYS (-1)
int fnmatch(const char *, const char *, int);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_REGEX_FNMATCH_H_ */

320
third_party/musl/glob.c vendored Normal file
View file

@ -0,0 +1,320 @@
/*-*- 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
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/alg/alg.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/dirent.h"
#include "libc/calls/struct/stat.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/dt.h"
#include "third_party/musl/fnmatch.h"
#include "third_party/musl/glob.h"
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
struct GlobList {
struct GlobList *next;
char name[];
};
static int AppendGlob(struct GlobList **tail, const char *name, size_t len,
int mark) {
struct GlobList *new;
if ((new = malloc(sizeof(struct GlobList) + len + 2))) {
(*tail)->next = new;
new->next = NULL;
memcpy(new->name, name, len + 1);
if (mark && len && name[len - 1] != '/') {
new->name[len] = '/';
new->name[len + 1] = 0;
}
*tail = new;
return 0;
} else {
return -1;
}
}
static int PerformGlob(char *buf, size_t pos, int type, char *pat, int flags,
int (*errfunc)(const char *path, int err),
struct GlobList **tail) {
DIR *dir;
size_t l;
char *p, *p2;
char saved_sep;
ptrdiff_t i, j;
struct stat st;
struct dirent *de;
int r, readerr, in_bracket, overflow, old_errno, fnm_flags;
/* If GLOB_MARK is unused, we don't care about type. */
if (!type && !(flags & GLOB_MARK)) type = DT_REG;
/* Special-case the remaining pattern being all slashes, in
* which case we can use caller-passed type if it's a dir. */
if (*pat && type != DT_DIR) type = 0;
while (pos + 1 < PATH_MAX && *pat == '/') {
buf[pos++] = *pat++;
}
/* Consume maximal [escaped-]literal prefix of pattern, copying
* and un-escaping it to the running buffer as we go. */
i = 0;
j = 0;
overflow = 0;
in_bracket = 0;
for (; pat[i] != '*' && pat[i] != '?' && (!in_bracket || pat[i] != ']');
i++) {
if (!pat[i]) {
if (overflow) return 0;
pat += i;
pos += j;
i = j = 0;
break;
} else if (pat[i] == '[') {
in_bracket = 1;
} else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) {
/* Backslashes inside a bracket are (at least by
* our interpretation) non-special, so if next
* char is ']' we have a complete expression. */
if (in_bracket && pat[i + 1] == ']') break;
/* Unpaired final backslash never matches. */
if (!pat[i + 1]) return 0;
i++;
}
if (pat[i] == '/') {
if (overflow) return 0;
in_bracket = 0;
pat += i + 1;
i = -1;
pos += j + 1;
j = -1;
}
/* Only store a character if it fits in the buffer, but if
* a potential bracket expression is open, the overflow
* must be remembered and handled later only if the bracket
* is unterminated (and thereby a literal), so as not to
* disallow long bracket expressions with short matches. */
if (pos + (j + 1) < PATH_MAX) {
buf[pos + j++] = pat[i];
} else if (in_bracket) {
overflow = 1;
} else {
return 0;
}
/* If we consume any new components, the caller-passed type
* or dummy type from above is no longer valid. */
type = 0;
}
buf[pos] = 0;
if (!*pat) {
/* If we consumed any components above, or if GLOB_MARK is
* requested and we don't yet know if the match is a dir,
* we must call stat to confirm the file exists and/or
* determine its type. */
if ((flags & GLOB_MARK) && type == DT_LNK) type = 0;
if (!type && stat(buf, &st)) {
if (errno != ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR))) {
return GLOB_ABORTED;
}
return 0;
}
if (!type && S_ISDIR(st.st_mode)) type = DT_DIR;
if (AppendGlob(tail, buf, pos, (flags & GLOB_MARK) && type == DT_DIR)) {
return GLOB_NOSPACE;
}
return 0;
}
p2 = strchr(pat, '/');
saved_sep = '/';
/* Check if the '/' was escaped and, if so, remove the escape char
* so that it will not be unpaired when passed to fnmatch. */
if (p2 && !(flags & GLOB_NOESCAPE)) {
for (p = p2; p > pat && p[-1] == '\\'; p--)
;
if ((p2 - p) % 2) {
p2--;
saved_sep = '\\';
}
}
dir = opendir(pos ? buf : ".");
if (!dir) {
if (errfunc(buf, errno) || (flags & GLOB_ERR)) return GLOB_ABORTED;
return 0;
}
old_errno = errno;
while (errno = 0, de = readdir(dir)) {
/* Quickly skip non-directories when there's pattern left. */
if (p2 && de->d_type && de->d_type != DT_DIR && de->d_type != DT_LNK) {
continue;
}
l = strlen(de->d_name);
if (l >= PATH_MAX - pos) continue;
if (p2) *p2 = 0;
fnm_flags = ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0) |
((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
if (fnmatch(pat, de->d_name, fnm_flags)) continue;
/* With GLOB_PERIOD don't allow matching . or .. unless fnmatch()
* would match them with FNM_PERIOD rules in effect. */
if (p2 && (flags & GLOB_PERIOD) && de->d_name[0] == '.' &&
(!de->d_name[1] || de->d_name[1] == '.' && !de->d_name[2]) &&
fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD)) {
continue;
}
memcpy(buf + pos, de->d_name, l + 1);
if (p2) *p2 = saved_sep;
r = PerformGlob(buf, pos + l, de->d_type, p2 ? p2 : "", flags, errfunc,
tail);
if (r) {
closedir(dir);
return r;
}
}
readerr = errno;
if (p2) *p2 = saved_sep;
closedir(dir);
if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR))) {
return GLOB_ABORTED;
}
errno = old_errno;
return 0;
}
static int IgnoreGlobError(const char *path, int err) {
return 0;
}
static void FreeGlobList(struct GlobList *head) {
struct GlobList *match, *next;
for (match = head->next; match; match = next) {
next = match->next;
free(match);
}
}
static int GlobPredicate(const void *a, const void *b) {
return strcmp(*(const char **)a, *(const char **)b);
}
/**
* Finds pathnames matching pattern.
*
* For example:
*
* glob_t g = {.gl_offs = 2};
* glob("*.*", GLOB_DOOFFS, NULL, &g);
* glob("../.*", GLOB_DOOFFS | GLOB_APPEND, NULL, &g);
* g.gl_pathv[0] = "ls";
* g.gl_pathv[1] = "-l";
* execvp("ls", &g.gl_pathv[0]);
* globfree(g);
*
* @param pat can have star wildcard see fnmatch()
* @param g will receive matching entries and needs globfree()
* @return 0 on success or GLOB_NOMATCH, GLOB_NOSPACE on OOM, or
* GLOB_ABORTED on read error
*/
int glob(const char *pat, int flags, int errfunc(const char *path, int err),
glob_t *g) {
int error = 0;
size_t cnt, i;
char *p, **pathv, buf[PATH_MAX];
struct GlobList head = {.next = NULL}, *tail = &head;
size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
if (!errfunc) errfunc = IgnoreGlobError;
if (!(flags & GLOB_APPEND)) {
g->gl_offs = offs;
g->gl_pathc = 0;
g->gl_pathv = NULL;
}
if (*pat) {
char *p = strdup(pat);
if (!p) return GLOB_NOSPACE;
buf[0] = 0;
error = PerformGlob(buf, 0, 0, p, flags, errfunc, &tail);
free(p);
}
if (error == GLOB_NOSPACE) {
FreeGlobList(&head);
return error;
}
for (cnt = 0, tail = head.next; tail; tail = tail->next, cnt++)
;
if (!cnt) {
if (flags & GLOB_NOCHECK) {
tail = &head;
if (AppendGlob(&tail, pat, strlen(pat), 0)) {
return GLOB_NOSPACE;
}
cnt++;
} else
return GLOB_NOMATCH;
}
if (flags & GLOB_APPEND) {
pathv =
realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
if (!pathv) {
FreeGlobList(&head);
return GLOB_NOSPACE;
}
g->gl_pathv = pathv;
offs += g->gl_pathc;
} else {
g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
if (!g->gl_pathv) {
FreeGlobList(&head);
return GLOB_NOSPACE;
}
for (i = 0; i < offs; i++) {
g->gl_pathv[i] = NULL;
}
}
for (i = 0, tail = head.next; i < cnt; tail = tail->next, i++) {
g->gl_pathv[offs + i] = tail->name;
}
g->gl_pathv[offs + i] = NULL;
g->gl_pathc += cnt;
if (!(flags & GLOB_NOSORT)) {
qsort(g->gl_pathv + offs, cnt, sizeof(char *), GlobPredicate);
}
return error;
}
/**
* Frees entries allocated by glob().
*/
void globfree(glob_t *g) {
size_t i;
for (i = 0; i < g->gl_pathc; i++) {
free(g->gl_pathv[g->gl_offs + i] - offsetof(struct GlobList, name));
}
free(g->gl_pathv);
g->gl_pathc = 0;
g->gl_pathv = NULL;
}

36
third_party/musl/glob.h vendored Normal file
View file

@ -0,0 +1,36 @@
#ifndef COSMOPOLITAN_THIRD_PARTY_MUSL_GLOB_H_
#define COSMOPOLITAN_THIRD_PARTY_MUSL_GLOB_H_
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
#define GLOB_ERR 0x01
#define GLOB_MARK 0x02
#define GLOB_NOSORT 0x04
#define GLOB_DOOFFS 0x08 /* reserves null slots at start of gl_pathv */
#define GLOB_NOCHECK 0x10 /* just yield pattern if GLOB_NOMATCH happens */
#define GLOB_APPEND 0x20 /* enables us to call glob() multiple times */
#define GLOB_NOESCAPE 0x40 /* don't allow things like \*\?\[\] escaping */
#define GLOB_PERIOD 0x80
#define GLOB_TILDE 0x1000
#define GLOB_TILDE_CHECK 0x4000
#define GLOB_NOSPACE 1
#define GLOB_ABORTED 2
#define GLOB_NOMATCH 3
#define GLOB_NOSYS 4
typedef struct {
size_t gl_pathc;
char **gl_pathv;
size_t gl_offs;
int __dummy1;
void *__dummy2[5];
} glob_t;
int glob(const char *, int, int (*)(const char *, int), glob_t *);
void globfree(glob_t *);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_THIRD_PARTY_MUSL_GLOB_H_ */

View file

@ -18,7 +18,9 @@ THIRD_PARTY_MUSL_A_OBJS = \
$(THIRD_PARTY_MUSL_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_MUSL_A_DIRECTDEPS = \
LIBC_ALG \
LIBC_CALLS \
LIBC_CALLS_HEFTY \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_STDIO \

View file

@ -1,294 +0,0 @@
/*-*- 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│
╚─────────────────────────────────────────────────────────────────────────────*/
/* clang-format off */
/*
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.
*/
#define _BSD_SOURCE
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
#include <glob.h>
#include <limits.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
asm(".ident\t\"\\n\\n\
Musl Libc » glob (MIT License)\\n\
Copyright 2005-2014 Rich Felker\"");
struct match
{
struct match *next;
char name[];
};
static int append(struct match **tail, const char *name, size_t len, int mark)
{
struct match *new = malloc(sizeof(struct match) + len + 2);
if (!new) return -1;
(*tail)->next = new;
new->next = NULL;
memcpy(new->name, name, len+1);
if (mark && len && name[len-1]!='/') {
new->name[len] = '/';
new->name[len+1] = 0;
}
*tail = new;
return 0;
}
static int do_glob(char *buf, size_t pos, int type, char *pat, int flags, int (*errfunc)(const char *path, int err), struct match **tail)
{
/* If GLOB_MARK is unused, we don't care about type. */
if (!type && !(flags & GLOB_MARK)) type = DT_REG;
/* Special-case the remaining pattern being all slashes, in
* which case we can use caller-passed type if it's a dir. */
if (*pat && type!=DT_DIR) type = 0;
while (pos+1 < PATH_MAX && *pat=='/') buf[pos++] = *pat++;
/* Consume maximal [escaped-]literal prefix of pattern, copying
* and un-escaping it to the running buffer as we go. */
ptrdiff_t i=0, j=0;
int in_bracket = 0, overflow = 0;
for (; pat[i]!='*' && pat[i]!='?' && (!in_bracket || pat[i]!=']'); i++) {
if (!pat[i]) {
if (overflow) return 0;
pat += i;
pos += j;
i = j = 0;
break;
} else if (pat[i] == '[') {
in_bracket = 1;
} else if (pat[i] == '\\' && !(flags & GLOB_NOESCAPE)) {
/* Backslashes inside a bracket are (at least by
* our interpretation) non-special, so if next
* char is ']' we have a complete expression. */
if (in_bracket && pat[i+1]==']') break;
/* Unpaired final backslash never matches. */
if (!pat[i+1]) return 0;
i++;
}
if (pat[i] == '/') {
if (overflow) return 0;
in_bracket = 0;
pat += i+1;
i = -1;
pos += j+1;
j = -1;
}
/* Only store a character if it fits in the buffer, but if
* a potential bracket expression is open, the overflow
* must be remembered and handled later only if the bracket
* is unterminated (and thereby a literal), so as not to
* disallow long bracket expressions with short matches. */
if (pos+(j+1) < PATH_MAX) {
buf[pos+j++] = pat[i];
} else if (in_bracket) {
overflow = 1;
} else {
return 0;
}
/* If we consume any new components, the caller-passed type
* or dummy type from above is no longer valid. */
type = 0;
}
buf[pos] = 0;
if (!*pat) {
/* If we consumed any components above, or if GLOB_MARK is
* requested and we don't yet know if the match is a dir,
* we must call stat to confirm the file exists and/or
* determine its type. */
struct stat st;
if ((flags & GLOB_MARK) && type==DT_LNK) type = 0;
if (!type && stat(buf, &st)) {
if (errno!=ENOENT && (errfunc(buf, errno) || (flags & GLOB_ERR)))
return GLOB_ABORTED;
return 0;
}
if (!type && S_ISDIR(st.st_mode)) type = DT_DIR;
if (append(tail, buf, pos, (flags & GLOB_MARK) && type==DT_DIR))
return GLOB_NOSPACE;
return 0;
}
char *p2 = strchr(pat, '/'), saved_sep = '/';
/* Check if the '/' was escaped and, if so, remove the escape char
* so that it will not be unpaired when passed to fnmatch. */
if (p2 && !(flags & GLOB_NOESCAPE)) {
char *p;
for (p=p2; p>pat && p[-1]=='\\'; p--);
if ((p2-p)%2) {
p2--;
saved_sep = '\\';
}
}
DIR *dir = opendir(pos ? buf : ".");
if (!dir) {
if (errfunc(buf, errno) || (flags & GLOB_ERR))
return GLOB_ABORTED;
return 0;
}
int old_errno = errno;
struct dirent *de;
while (errno=0, de=readdir(dir)) {
/* Quickly skip non-directories when there's pattern left. */
if (p2 && de->d_type && de->d_type!=DT_DIR && de->d_type!=DT_LNK)
continue;
size_t l = strlen(de->d_name);
if (l >= PATH_MAX-pos) continue;
if (p2) *p2 = 0;
int fnm_flags= ((flags & GLOB_NOESCAPE) ? FNM_NOESCAPE : 0)
| ((!(flags & GLOB_PERIOD)) ? FNM_PERIOD : 0);
if (fnmatch(pat, de->d_name, fnm_flags))
continue;
/* With GLOB_PERIOD, don't allow matching . or .. unless
* fnmatch would match them with FNM_PERIOD rules in effect. */
if (p2 && (flags & GLOB_PERIOD) && de->d_name[0]=='.'
&& (!de->d_name[1] || de->d_name[1]=='.' && !de->d_name[2])
&& fnmatch(pat, de->d_name, fnm_flags | FNM_PERIOD))
continue;
memcpy(buf+pos, de->d_name, l+1);
if (p2) *p2 = saved_sep;
int r = do_glob(buf, pos+l, de->d_type, p2 ? p2 : "", flags, errfunc, tail);
if (r) {
closedir(dir);
return r;
}
}
int readerr = errno;
if (p2) *p2 = saved_sep;
closedir(dir);
if (readerr && (errfunc(buf, errno) || (flags & GLOB_ERR)))
return GLOB_ABORTED;
errno = old_errno;
return 0;
}
static int ignore_err(const char *path, int err)
{
return 0;
}
static void freelist(struct match *head)
{
struct match *match, *next;
for (match=head->next; match; match=next) {
next = match->next;
free(match);
}
}
static int sort(const void *a, const void *b)
{
return strcmp(*(const char **)a, *(const char **)b);
}
int glob(const char *restrict pat, int flags, int (*errfunc)(const char *path, int err), glob_t *restrict g)
{
struct match head = { .next = NULL }, *tail = &head;
size_t cnt, i;
size_t offs = (flags & GLOB_DOOFFS) ? g->gl_offs : 0;
int error = 0;
char buf[PATH_MAX];
if (!errfunc) errfunc = ignore_err;
if (!(flags & GLOB_APPEND)) {
g->gl_offs = offs;
g->gl_pathc = 0;
g->gl_pathv = NULL;
}
if (*pat) {
char *p = strdup(pat);
if (!p) return GLOB_NOSPACE;
buf[0] = 0;
error = do_glob(buf, 0, 0, p, flags, errfunc, &tail);
free(p);
}
if (error == GLOB_NOSPACE) {
freelist(&head);
return error;
}
for (cnt=0, tail=head.next; tail; tail=tail->next, cnt++);
if (!cnt) {
if (flags & GLOB_NOCHECK) {
tail = &head;
if (append(&tail, pat, strlen(pat), 0))
return GLOB_NOSPACE;
cnt++;
} else
return GLOB_NOMATCH;
}
if (flags & GLOB_APPEND) {
char **pathv = realloc(g->gl_pathv, (offs + g->gl_pathc + cnt + 1) * sizeof(char *));
if (!pathv) {
freelist(&head);
return GLOB_NOSPACE;
}
g->gl_pathv = pathv;
offs += g->gl_pathc;
} else {
g->gl_pathv = malloc((offs + cnt + 1) * sizeof(char *));
if (!g->gl_pathv) {
freelist(&head);
return GLOB_NOSPACE;
}
for (i=0; i<offs; i++)
g->gl_pathv[i] = NULL;
}
for (i=0, tail=head.next; i<cnt; tail=tail->next, i++)
g->gl_pathv[offs + i] = tail->name;
g->gl_pathv[offs + i] = NULL;
g->gl_pathc += cnt;
if (!(flags & GLOB_NOSORT))
qsort(g->gl_pathv+offs, cnt, sizeof(char *), sort);
return error;
}
void globfree(glob_t *g)
{
size_t i;
for (i=0; i<g->gl_pathc; i++)
free(g->gl_pathv[g->gl_offs + i] - offsetof(struct match, name));
free(g->gl_pathv);
g->gl_pathc = 0;
g->gl_pathv = NULL;
}

View file

@ -1229,7 +1229,7 @@ privileged static void xed_decode_instruction_length(
*
* @return d->decoded_length is byte length, or 1 w/ error code
* @note binary footprint increases ~4kb if this is used
* @see cf. biggest thing in tensorflow, gdb, clang, etc. binaries
* @see biggest code in gdb/clang/tensorflow binaries
*/
privileged enum XedError xed_instruction_length_decode(
struct XedDecodedInst *xedd, const uint8_t *itext, const size_t bytes) {

View file

@ -1,25 +1,25 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 sw=8 fenc=utf-8 :vi
Copyright 2018 Intel Corporation
Copyright 2020 Justine Alexandra Roberts Tunney
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License. │
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of │
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
http://www.apache.org/licenses/LICENSE-2.0
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software │
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "libc/macros.h"
/ Phash tables for instruction length decoding.
/ @see build/rle.py for more context here
.initbss 300,_init_x86tab
xed_prefix_table_bit:

View file

@ -7,7 +7,7 @@
#
# DESCRIPTION
#
# See test/libc/x86ild_test.c for more information.
# See test/libc/xed/x86ild_test.c for more information.
PKGS += THIRD_PARTY_XED