Initial import

This commit is contained in:
Justine Tunney 2020-06-15 07:18:57 -07:00
commit c91b3c5006
14915 changed files with 590219 additions and 0 deletions

250
third_party/musl/grp.c vendored Normal file
View file

@ -0,0 +1,250 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 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/weirdtypes.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "third_party/musl/passwd.h"
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
#define PTHREAD_CANCEL_DISABLE 0
#define pthread_setcancelstate(x, y) (void)y
static unsigned atou(char **s) {
unsigned x;
for (x = 0; **s - '0' < 10U; ++*s) x = 10 * x + (**s - '0');
return x;
}
static int __getgrent_a(FILE *f, struct group *gr, char **line, size_t *size,
char ***mem, size_t *nmem, struct group **res) {
ssize_t l;
char *s, *mems;
size_t i;
int rv = 0;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
for (;;) {
if ((l = getline(line, size, f)) < 0) {
rv = ferror(f) ? errno : 0;
free(*line);
*line = 0;
gr = 0;
goto end;
}
line[0][l - 1] = 0;
s = line[0];
gr->gr_name = s++;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
gr->gr_passwd = s;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
gr->gr_gid = atou(&s);
if (*s != ':') continue;
*s++ = 0;
mems = s;
break;
}
for (*nmem = !!*s; *s; s++)
if (*s == ',') ++*nmem;
free(*mem);
*mem = calloc(sizeof(char *), *nmem + 1);
if (!*mem) {
rv = errno;
free(*line);
*line = 0;
gr = 0;
goto end;
}
if (*mems) {
mem[0][0] = mems;
for (s = mems, i = 0; *s; s++)
if (*s == ',') *s++ = 0, mem[0][++i] = s;
mem[0][++i] = 0;
} else {
mem[0][0] = 0;
}
gr->gr_mem = *mem;
end:
pthread_setcancelstate(cs, 0);
*res = gr;
if (rv) errno = rv;
return rv;
}
static int __getgr_a(const char *name, gid_t gid, struct group *gr, char **buf,
size_t *size, char ***mem, size_t *nmem,
struct group **res) {
FILE *f;
int rv = 0;
int cs;
*res = 0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
f = fopen("/etc/group", "rbe");
if (!f) {
rv = errno;
goto done;
}
while (!(rv = __getgrent_a(f, gr, buf, size, mem, nmem, res)) && *res) {
if ((name && !strcmp(name, (*res)->gr_name)) ||
(!name && (*res)->gr_gid == gid)) {
break;
}
}
done:
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
}
static int getgr_r(const char *name, gid_t gid, struct group *gr, char *buf,
size_t size, struct group **res) {
#define FIX(x) (gr->gr_##x = gr->gr_##x - line + buf)
char *line = 0;
size_t len = 0;
char **mem = 0;
size_t nmem = 0;
int rv = 0;
size_t i;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
rv = __getgr_a(name, gid, gr, &line, &len, &mem, &nmem, res);
if (*res && size < len + (nmem + 1) * sizeof(char *) + 32) {
*res = 0;
rv = ERANGE;
}
if (*res) {
buf += (16 - (uintptr_t)buf) % 16;
gr->gr_mem = (void *)buf;
buf += (nmem + 1) * sizeof(char *);
memcpy(buf, line, len);
FIX(name);
FIX(passwd);
for (i = 0; mem[i]; i++) gr->gr_mem[i] = mem[i] - line + buf;
gr->gr_mem[i] = 0;
}
free(mem);
free(line);
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
#undef FIX
}
int getgrouplist(const char *user, gid_t gid, gid_t *groups, int *ngroups) {
int rv, nlim, ret = -1;
ssize_t i, n = 1;
struct group gr;
struct group *res;
FILE *f;
char *buf = 0;
char **mem = 0;
size_t nmem = 0;
size_t size;
nlim = *ngroups;
if (nlim >= 1) *groups++ = gid;
f = fopen("/etc/group", "rbe");
if (!f && errno != ENOENT && errno != ENOTDIR) goto cleanup;
if (f) {
while (!(rv = __getgrent_a(f, &gr, &buf, &size, &mem, &nmem, &res)) &&
res) {
for (i = 0; gr.gr_mem[i] && strcmp(user, gr.gr_mem[i]); i++)
;
if (!gr.gr_mem[i]) continue;
if (++n <= nlim) *groups++ = gr.gr_gid;
}
if (rv) {
errno = rv;
goto cleanup;
}
}
ret = n > nlim ? -1 : n;
*ngroups = n;
cleanup:
if (f) fclose(f);
free(buf);
free(mem);
return ret;
}
int getgrnam_r(const char *name, struct group *gr, char *buf, size_t size,
struct group **res) {
return getgr_r(name, 0, gr, buf, size, res);
}
int getgrgid_r(gid_t gid, struct group *gr, char *buf, size_t size,
struct group **res) {
return getgr_r(0, gid, gr, buf, size, res);
}
static struct GetgrentState {
FILE *f;
char *line;
char **mem;
struct group gr;
} g_getgrent[1];
void endgrent() {
setgrent();
}
void setgrent() {
if (g_getgrent->f) fclose(g_getgrent->f);
g_getgrent->f = 0;
}
struct group *getgrent() {
struct group *res;
size_t size = 0, nmem = 0;
if (!g_getgrent->f) g_getgrent->f = fopen("/etc/group", "rbe");
if (!g_getgrent->f) return 0;
__getgrent_a(g_getgrent->f, &g_getgrent->gr, &g_getgrent->line, &size,
&g_getgrent->mem, &nmem, &res);
return res;
}
struct group *getgrgid(gid_t gid) {
struct group *res;
size_t size = 0, nmem = 0;
__getgr_a(0, gid, &g_getgrent->gr, &g_getgrent->line, &size, &g_getgrent->mem,
&nmem, &res);
return res;
}
struct group *getgrnam(const char *name) {
struct group *res;
size_t size = 0, nmem = 0;
__getgr_a(name, 0, &g_getgrent->gr, &g_getgrent->line, &size,
&g_getgrent->mem, &nmem, &res);
return res;
}

52
third_party/musl/musl.mk vendored Normal file
View file

@ -0,0 +1,52 @@
#-*-mode:makefile-gmake;indent-tabs-mode:t;tab-width:8;coding:utf-8-*-┐
#───vi: set et ft=make ts=8 tw=8 fenc=utf-8 :vi───────────────────────┘
PKGS += THIRD_PARTY_MUSL
THIRD_PARTY_MUSL_SRCS = $(THIRD_PARTY_MUSL_A_SRCS)
THIRD_PARTY_MUSL_HDRS = $(THIRD_PARTY_MUSL_A_HDRS)
THIRD_PARTY_MUSL_ARTIFACTS += THIRD_PARTY_MUSL_A
THIRD_PARTY_MUSL = $(THIRD_PARTY_MUSL_A_DEPS) $(THIRD_PARTY_MUSL_A)
THIRD_PARTY_MUSL_A = o/$(MODE)/third_party/musl/musl.a
THIRD_PARTY_MUSL_A_FILES := $(wildcard third_party/musl/*)
THIRD_PARTY_MUSL_A_HDRS = $(filter %.h,$(THIRD_PARTY_MUSL_A_FILES))
THIRD_PARTY_MUSL_A_SRCS = $(filter %.c,$(THIRD_PARTY_MUSL_A_FILES))
THIRD_PARTY_MUSL_A_OBJS = \
$(THIRD_PARTY_MUSL_A_SRCS:%=o/$(MODE)/%.zip.o) \
$(THIRD_PARTY_MUSL_A_SRCS:%.c=o/$(MODE)/%.o)
THIRD_PARTY_MUSL_A_DIRECTDEPS = \
LIBC_CALLS \
LIBC_MEM \
LIBC_NEXGEN32E \
LIBC_STDIO \
LIBC_STR \
LIBC_STUBS \
LIBC_SYSV
THIRD_PARTY_MUSL_A_DEPS := \
$(call uniq,$(foreach x,$(THIRD_PARTY_MUSL_A_DIRECTDEPS),$($(x))))
THIRD_PARTY_MUSL_A_CHECKS = \
$(THIRD_PARTY_MUSL_A).pkg \
$(THIRD_PARTY_MUSL_A_HDRS:%=o/$(MODE)/%.ok)
$(THIRD_PARTY_MUSL_A): \
third_party/musl/ \
$(THIRD_PARTY_MUSL_A).pkg \
$(THIRD_PARTY_MUSL_A_OBJS)
$(THIRD_PARTY_MUSL_A).pkg: \
$(THIRD_PARTY_MUSL_A_OBJS) \
$(foreach x,$(THIRD_PARTY_MUSL_A_DIRECTDEPS),$($(x)_A).pkg)
THIRD_PARTY_MUSL_LIBS = $(foreach x,$(THIRD_PARTY_MUSL_ARTIFACTS),$($(x)))
THIRD_PARTY_MUSL_SRCS = $(foreach x,$(THIRD_PARTY_MUSL_ARTIFACTS),$($(x)_SRCS))
THIRD_PARTY_MUSL_CHECKS = $(foreach x,$(THIRD_PARTY_MUSL_ARTIFACTS),$($(x)_CHECKS))
THIRD_PARTY_MUSL_OBJS = $(foreach x,$(THIRD_PARTY_MUSL_ARTIFACTS),$($(x)_OBJS))
$(THIRD_PARTY_MUSL_OBJS): $(BUILD_FILES) third_party/musl/musl.mk
.PHONY: o/$(MODE)/third_party/musl
o/$(MODE)/third_party/musl: $(THIRD_PARTY_MUSL_CHECKS)

51
third_party/musl/passwd.h vendored Normal file
View file

@ -0,0 +1,51 @@
#ifndef COSMOPOLITAN_LIBC_PASSWD_H_
#define COSMOPOLITAN_LIBC_PASSWD_H_
#include "libc/calls/weirdtypes.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
struct FILE;
struct passwd {
char *pw_name;
char *pw_passwd;
uint32_t pw_uid;
uint32_t pw_gid;
char *pw_gecos;
char *pw_dir;
char *pw_shell;
};
void setpwent(void);
void endpwent(void);
struct passwd *getpwent(void);
struct passwd *getpwuid(uint32_t);
struct passwd *getpwnam(const char *);
int getpwuid_r(uint32_t, struct passwd *, char *, size_t, struct passwd **);
int getpwnam_r(const char *, struct passwd *, char *, size_t, struct passwd **);
struct passwd *fgetpwent(struct FILE *);
int putpwent(const struct passwd *, struct FILE *);
struct group {
char *gr_name;
char *gr_passwd;
int32_t gr_gid;
char **gr_mem;
};
struct group *getgrgid(gid_t);
struct group *getgrnam(const char *);
int getgrgid_r(gid_t, struct group *, char *, size_t, struct group **);
int getgrnam_r(const char *, struct group *, char *, size_t, struct group **);
struct group *getgrent(void);
void endgrent(void);
void setgrent(void);
struct group *fgetgrent(struct FILE *);
int putgrent(const struct group *, struct FILE *);
int getgrouplist(const char *, gid_t, gid_t *, int *);
int setgroups(size_t, const int32_t *);
int initgroups(const char *, int32_t);
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
#endif /* COSMOPOLITAN_LIBC_PASSWD_H_ */

188
third_party/musl/pwd.c vendored Normal file
View file

@ -0,0 +1,188 @@
/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
vi: set et ft=c ts=2 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/weirdtypes.h"
#include "libc/errno.h"
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "third_party/musl/passwd.h"
asm(".ident\t\"\\n\\n\
Musl libc (MIT License)\\n\
Copyright 2005-2014 Rich Felker, et. al.\"");
asm(".include \"libc/disclaimer.inc\"");
#define PTHREAD_CANCEL_DISABLE 0
#define pthread_setcancelstate(x, y) (void)y
static unsigned atou(char **s) {
unsigned x;
for (x = 0; **s - '0' < 10U; ++*s) x = 10 * x + (**s - '0');
return x;
}
static int __getpwent_a(FILE *f, struct passwd *pw, char **line, size_t *size,
struct passwd **res) {
ssize_t l;
char *s;
int rv = 0;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
for (;;) {
if ((l = getline(line, size, f)) < 0) {
rv = ferror(f) ? errno : 0;
free(*line);
*line = 0;
pw = 0;
break;
}
line[0][l - 1] = 0;
s = line[0];
pw->pw_name = s++;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
pw->pw_passwd = s;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
pw->pw_uid = atou(&s);
if (*s != ':') continue;
*s++ = 0;
pw->pw_gid = atou(&s);
if (*s != ':') continue;
*s++ = 0;
pw->pw_gecos = s;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
pw->pw_dir = s;
if (!(s = strchr(s, ':'))) continue;
*s++ = 0;
pw->pw_shell = s;
break;
}
pthread_setcancelstate(cs, 0);
*res = pw;
if (rv) errno = rv;
return rv;
}
static int __getpw_a(const char *name, uid_t uid, struct passwd *pw, char **buf,
size_t *size, struct passwd **res) {
FILE *f;
int cs;
int rv = 0;
*res = 0;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
if ((f = fopen("/etc/passwd", "rbe"))) {
while (!(rv = __getpwent_a(f, pw, buf, size, res)) && *res) {
if ((name && !strcmp(name, (*res)->pw_name)) ||
(!name && (*res)->pw_uid == uid)) {
break;
}
}
fclose(f);
}
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
}
static int getpw_r(const char *name, uid_t uid, struct passwd *pw, char *buf,
size_t size, struct passwd **res) {
#define FIX(x) (pw->pw_##x = pw->pw_##x - line + buf)
char *line = 0;
size_t len = 0;
int rv = 0;
int cs;
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
rv = __getpw_a(name, uid, pw, &line, &len, res);
if (*res && size < len) {
*res = 0;
rv = ERANGE;
}
if (*res) {
memcpy(buf, line, len);
FIX(name);
FIX(passwd);
FIX(gecos);
FIX(dir);
FIX(shell);
}
free(line);
pthread_setcancelstate(cs, 0);
if (rv) errno = rv;
return rv;
#undef FIX
}
int getpwnam_r(const char *name, struct passwd *pw, char *buf, size_t size,
struct passwd **res) {
return getpw_r(name, 0, pw, buf, size, res);
}
int getpwuid_r(uid_t uid, struct passwd *pw, char *buf, size_t size,
struct passwd **res) {
return getpw_r(0, uid, pw, buf, size, res);
}
static struct GetpwentState {
FILE *f;
char *line;
struct passwd pw;
size_t size;
} g_getpwent[1];
void endpwent() {
setpwent();
}
void setpwent() {
if (g_getpwent->f) fclose(g_getpwent->f);
g_getpwent->f = 0;
}
struct passwd *getpwent() {
struct passwd *res;
if (!g_getpwent->f) g_getpwent->f = fopen("/etc/passwd", "rbe");
if (!g_getpwent->f) return 0;
__getpwent_a(g_getpwent->f, &g_getpwent->pw, &g_getpwent->line,
&g_getpwent->size, &res);
return res;
}
struct passwd *getpwuid(uid_t uid) {
struct passwd *res;
__getpw_a(0, uid, &g_getpwent->pw, &g_getpwent->line, &g_getpwent->size,
&res);
return res;
}
struct passwd *getpwnam(const char *name) {
struct passwd *res;
__getpw_a(name, 0, &g_getpwent->pw, &g_getpwent->line, &g_getpwent->size,
&res);
return res;
}