Add script.com and whois.com courtesy of FreeBSD

This commit is contained in:
Justine Tunney 2022-09-13 20:11:09 -07:00
parent 654ceaba7d
commit 1ad2f530f9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
33 changed files with 1735 additions and 265 deletions

View file

@ -16,6 +16,27 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/termios.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/termios.h"
int grantpt(int fd) { return 0; }
/**
* Grants access to subordinate pseudoteletypewriter.
*
* @return 0 on success, or -1 w/ errno
* @raise EBADF if fd isn't open
* @raise EINVAL if fd is valid but not associated with pty
* @raise EACCES if pseudoterminal couldn't be accessed
*/
int grantpt(int fd) {
int rc;
if (IsXnu()) {
rc = sys_ioctl(fd, TIOCPTYGRANT);
} else {
rc = _isptmaster(fd);
}
STRACE("grantpt(%d) → %d% m", fd, rc);
return rc;
}

View file

@ -12,13 +12,6 @@ COSMOPOLITAN_C_START_
#define kIoMotion ((const int8_t[3]){1, 0, 0})
struct IoctlPtmGet {
int theduxfd;
int workerfd;
char theduxname[16];
char workername[16];
};
hidden extern struct Fds g_fds;
hidden extern const struct Fd kEmptyFd;

View file

@ -1,7 +1,7 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
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
@ -16,11 +16,23 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/log/bsd.h"
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/sysv/consts/termios.h"
wontreturn void(errx)(int eval, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
(verrx)(eval, fmt, va);
va_end(va);
int _isptmaster(int fd) {
if (IsFreebsd()) {
if (!sys_ioctl(fd, TIOCPTMASTER)) {
return 0;
} else {
if (errno != EBADF) {
errno = EINVAL;
}
return -1;
}
} else {
return 0;
}
}

View file

@ -18,12 +18,23 @@
*/
#include "libc/calls/calls.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/termios.h"
#include "libc/calls/struct/winsize.h"
#include "libc/calls/termios.h"
#include "libc/fmt/itoa.h"
#include "libc/dce.h"
#include "libc/intrin/kprintf.h"
#include "libc/log/rop.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/pty.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
struct IoctlPtmGet {
int m;
int s;
char mname[16];
char sname[16];
};
/**
* Opens new pseudo teletypewriter.
@ -37,24 +48,29 @@
*/
int openpty(int *mfd, int *sfd, char *name, const struct termios *tio,
const struct winsize *wsz) {
int m, s, n;
char buf[20];
if ((m = open("/dev/ptmx", O_RDWR | O_NOCTTY)) != -1) {
n = 0;
if (!ioctl(m, TIOCSPTLCK, &n) && !ioctl(m, TIOCGPTN, &n)) {
if (!name) name = buf;
name[0] = '/', name[1] = 'd', name[2] = 'e', name[3] = 'v';
name[4] = '/', name[5] = 'p', name[6] = 't', name[7] = 's';
name[8] = '/', FormatInt32(name + 9, n);
if ((s = open(name, O_RDWR | O_NOCTTY)) != -1) {
if (tio) ioctl(s, TCSETS, tio);
if (wsz) ioctl(s, TIOCSWINSZ, wsz);
*mfd = m;
*sfd = s;
return 0;
}
}
int m, s, p;
const char *t;
struct IoctlPtmGet ptm;
RETURN_ON_ERROR((m = posix_openpt(O_RDWR | O_NOCTTY)));
if (!IsOpenbsd()) {
RETURN_ON_ERROR(grantpt(m));
RETURN_ON_ERROR(unlockpt(m));
if (!(t = ptsname(m))) goto OnError;
RETURN_ON_ERROR((s = open(t, O_RDWR)));
} else {
RETURN_ON_ERROR(ioctl(m, PTMGET, &ptm));
close(m);
m = ptm.m;
s = ptm.s;
t = ptm.sname;
}
*mfd = m;
*sfd = s;
if (name) strcpy(name, t);
if (tio) ioctl(s, TCSETSF, tio);
if (wsz) ioctl(s, TIOCSWINSZ, wsz);
return 0;
OnError:
if (m != -1) close(m);
return -1;
}

View file

@ -16,43 +16,35 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/internal.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/termios.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/pty.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
/**
* Opens new pseudo teletypewriter.
*
* @return fd of master pty, or -1 w/ errno
* @params flags is usually O_RDWR|O_NOCTTY
* @return file descriptor, or -1 w/ errno
*/
int posix_openpt(int flags) {
int fd, ilduce;
struct IoctlPtmGet ptm;
if ((flags & O_ACCMODE) != O_RDWR) return einval();
if (SupportsFreebsd() &&
((fd = sys_posix_openpt(flags)) != -1 || errno != ENOSYS)) {
return fd;
} else if ((fd = open("/dev/ptmx", flags)) != -1 || errno != ENOENT) {
return fd;
} else if (SupportsOpenbsd() &&
((fd = open("/dev/ptm", flags)) != -1 || errno != ENOENT)) {
if (ioctl(fd, PTMGET, &ptm) != -1) {
close(ptm.workerfd);
ilduce = ptm.theduxfd;
} else {
ilduce = -1;
}
close(fd);
return ilduce;
int rc;
if ((flags & O_ACCMODE) != O_RDWR) {
rc = einval();
} else if (IsLinux() || IsXnu()) {
rc = sys_open("/dev/ptmx", flags, 0);
} else if (IsOpenbsd()) {
rc = sys_open("/dev/ptm", flags, 0);
} else if (IsFreebsd()) {
rc = sys_posix_openpt(flags);
if (rc == -1 && errno == ENOSPC) errno = EAGAIN;
} else {
return enosys();
rc = enosys();
}
STRACE("posix_openpt(%#o) → %d% m", flags, rc);
return rc;
}

View file

@ -17,9 +17,25 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/termios.h"
#include "libc/errno.h"
#include "libc/intrin/strace.internal.h"
static char g_ptsname[32];
static char g_ptsname[256];
/**
* Gets name subordinate pseudoteletypewriter.
*
* @return static string path on success, or NULL w/ errno
*/
char *ptsname(int fd) {
return ptsname_r(fd, g_ptsname, sizeof(g_ptsname)) ? g_ptsname : NULL;
char *res;
errno_t e;
if (!(e = ptsname_r(fd, g_ptsname, sizeof(g_ptsname)))) {
res = g_ptsname;
} else {
errno = e;
res = 0;
}
STRACE("ptsname(%d) → %s% m", fd, res);
return res;
}

View file

@ -17,23 +17,69 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/termios.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/fmt/itoa.h"
#include "libc/paths.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
errno_t ptsname_r(int fd, char *buf, size_t size) {
int pty;
char tb[32];
if (size) {
if (!buf) return einval();
if (ioctl(fd, TIOCGPTN, &pty) == -1) return errno;
FormatInt32(stpcpy(tb, "/dev/pts/"), pty);
if (strlen(tb) + 1 >= size) return (errno = ERANGE);
stpcpy(buf, tb);
/* TODO(jart): OpenBSD OMG */
struct fiodgname_arg {
int len;
void *buf;
};
static int PtsName(int fd, char *buf, size_t size) {
if (size < 9 + 12) return erange();
if (_isptmaster(fd)) return -1;
if (IsLinux()) {
int pty;
if (sys_ioctl(fd, TIOCGPTN, &pty)) return -1;
buf[0] = '/', buf[1] = 'd', buf[2] = 'e', buf[3] = 'v';
buf[4] = '/', buf[5] = 'p', buf[6] = 't', buf[7] = 's';
buf[8] = '/', FormatInt32(buf + 9, pty);
return 0;
}
return 0;
if (IsFreebsd()) {
struct fiodgname_arg fgn = {size - 5, buf + 5};
buf[0] = '/', buf[1] = 'd';
buf[2] = 'e', buf[3] = 'v';
buf[4] = '/', buf[5] = 0;
if (sys_ioctl(fd, FIODGNAME, &fgn) == -1) {
if (errno == EINVAL) errno = ERANGE;
return -1;
}
return 0;
}
if (IsXnu()) {
char b2[128];
if (sys_ioctl(fd, TIOCPTYGNAME, b2)) return -1;
if (strlen(b2) + 1 > size) return erange();
strcpy(buf, b2);
return 0;
}
return enosys();
}
/**
* Gets name subordinate pseudoteletypewriter.
*
* @return 0 on success, or errno on error
*/
errno_t ptsname_r(int fd, char *buf, size_t size) {
int rc, e = errno;
if (!PtsName(fd, buf, size)) {
rc = 0;
} else {
rc = errno;
errno = e;
}
return rc;
}

View file

@ -116,6 +116,7 @@ i64 sys_readlinkat(int, const char *, char *, u64) hidden;
i64 sys_sendfile(i32, i32, i64 *, u64) hidden;
i64 sys_splice(i32, i64 *, i32, i64 *, u64, u32) hidden;
i64 sys_write(i32, const void *, u64) hidden;
int _isptmaster(int);
u32 sys_getegid(void) hidden;
u32 sys_geteuid(void) hidden;
u32 sys_getgid(void) hidden;

View file

@ -17,11 +17,32 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/calls/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/calls/termios.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/sysv/consts/pty.h"
#include "libc/sysv/errfuns.h"
/**
* Unlocks pseudoteletypewriter pair.
*
* @return 0 on success, or -1 w/ errno
* @raise EBADF if fd isn't open
* @raise EINVAL if fd is valid but not associated with pty
*/
int unlockpt(int fd) {
int unlock = 0;
/* TODO(jart) */
return ioctl(fd, TIOCSPTLCK, &unlock);
int rc;
if (IsFreebsd() || IsOpenbsd()) {
rc = _isptmaster(fd);
} else if (IsXnu()) {
rc = sys_ioctl(fd, TIOCPTYUNLK);
} else if (IsLinux()) {
int unlock = 0;
rc = sys_ioctl(fd, TIOCSPTLCK, &unlock);
} else {
rc = enosys();
}
STRACE("unlockpt(%d) → %d% m", fd, rc);
return rc;
}

View file

@ -16,9 +16,10 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/mem/alg.h"
#include "libc/macros.internal.h"
#include "libc/mem/alg.h"
#include "libc/nexgen32e/nexgen32e.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
static const int kCp437iMultimappings[] = {
@ -76,7 +77,7 @@ static textstartup void g_cp437i_init() {
unsigned i;
for (i = 0; i < 256; ++i) g_cp437i[i] = kCp437[i] << 8 | i;
memcpy(g_cp437i + 256, kCp437iMultimappings, sizeof(kCp437iMultimappings));
djbsort(g_cp437i, ARRAYLEN(g_cp437i));
_intsort(g_cp437i, ARRAYLEN(g_cp437i));
}
const void *const g_cp437i_ctor[] initarray = {g_cp437i_init};

View file

@ -2,6 +2,7 @@
#define LIBC_ISYSTEM_STDIO_H_
#include "libc/calls/calls.h"
#include "libc/fmt/fmt.h"
#include "libc/mem/fmt.h"
#include "libc/stdio/lock.internal.h"
#include "libc/stdio/stdio.h"
#include "libc/stdio/temp.h"

4
libc/isystem/sysexits.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef COSMOPOLITAN_LIBC_ISYSTEM_SYSEXITS_H_
#define COSMOPOLITAN_LIBC_ISYSTEM_SYSEXITS_H_
#include "libc/sysv/consts/ex.h"
#endif /* COSMOPOLITAN_LIBC_ISYSTEM_SYSEXITS_H_ */

View file

@ -1,30 +1,21 @@
#ifndef COSMOPOLITAN_LIBC_LOG_BSD_H_
#define COSMOPOLITAN_LIBC_LOG_BSD_H_
#include "libc/fmt/pflink.h"
#if !(__ASSEMBLER__ + __LINKER__ + 0)
COSMOPOLITAN_C_START_
/*───────────────────────────────────────────────────────────────────────────│─╗
cosmopolitan § logging » berkeley logger
*/
void err(int, const char *, ...) printfesque(2) wontreturn;
void errx(int, const char *, ...) printfesque(2) wontreturn;
void verr(int, const char *, va_list) paramsnonnull((3)) wontreturn;
void verrx(int, const char *, va_list) paramsnonnull((3)) wontreturn;
void vwarn(const char *, va_list) paramsnonnull((2));
void vwarnx(const char *, va_list) paramsnonnull((2));
void warn(const char *, ...) printfesque(1);
void warnx(const char *, ...) printfesque(1);
#define err(EVAL, FMT, ...) (err)(EVAL, PFLINK(FMT), ##__VA_ARGS__)
#define errx(EVAL, FMT, ...) (errx)(EVAL, PFLINK(FMT), ##__VA_ARGS__)
#define verr(EVAL, FMT, VA) (verr)(EVAL, PFLINK(FMT), VA)
#define verrx(EVAL, FMT, VA) (verrx)(EVAL, PFLINK(FMT), VA)
#define vwarn(FMT, VA) (vwarn)(PFLINK(FMT), VA)
#define vwarnx(FMT, VA) (vwarnx)(PFLINK(FMT), VA)
#define warn(FMT, ...) (warn)(PFLINK(FMT), ##__VA_ARGS__)
#define warnx(FMT, ...) (warn)(PFLINK(FMT), ##__VA_ARGS__)
void err(int, const char *, ...) wontreturn;
void verr(int, const char *, va_list) wontreturn;
void errc(int, int, const char *, ...) wontreturn;
void verrc(int, int, const char *, va_list) wontreturn;
void errx(int, const char *, ...) wontreturn;
void verrx(int, const char *, va_list) wontreturn;
void warn(const char *, ...);
void vwarn(const char *, va_list);
void warnc(int, const char *, ...);
void vwarnc(int, const char *, va_list);
void warnx(const char *, ...);
void vwarnx(const char *, va_list);
void err_set_exit(void (*)(int));
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */

View file

@ -1,26 +1,194 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
Copyright (c) 1993
The Regents of the University of California. All rights reserved.
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.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of the University nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
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.
THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
*/
#include "libc/errno.h"
#include "libc/log/bsd.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
// clang-format off
wontreturn void(err)(int eval, const char *fmt, ...) {
va_list va;
va_start(va, fmt);
(verr)(eval, fmt, va);
va_end(va);
asm(".ident\t\"\\n\\n\
FreeBSD Err (BSD-3 License)\\n\
Copyright (c) 1993\\n\
\tThe Regents of the University of California.\\n\
\tAll rights reserved.\"");
asm(".include \"libc/disclaimer.inc\"");
static FILE *err_file; /* file to use for error output */
static void (*err_exit)(int);
/*
* This is declared to take a `void *' so that the caller is not required
* to include <stdio.h> first. However, it is really a `FILE *', and the
* manual page documents it as such.
*/
void
err_set_file(void *fp)
{
if (fp)
err_file = fp;
else
err_file = stderr;
}
void
err_set_exit(void (*ef)(int))
{
err_exit = ef;
}
void
err(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrc(eval, errno, fmt, ap);
va_end(ap);
}
void
verr(int eval, const char *fmt, va_list ap)
{
verrc(eval, errno, fmt, ap);
}
void
errc(int eval, int code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrc(eval, code, fmt, ap);
va_end(ap);
}
void
verrc(int eval, int code, const char *fmt, va_list ap)
{
if (err_file == NULL)
err_set_file(NULL);
fprintf(err_file, "%s: ", program_invocation_name);
if (fmt != NULL) {
vfprintf(err_file, fmt, ap);
fprintf(err_file, ": ");
}
fprintf(err_file, "%s\n", _strerdoc(code));
if (err_exit)
err_exit(eval);
exit(eval);
}
void
errx(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
verrx(eval, fmt, ap);
va_end(ap);
}
void
verrx(int eval, const char *fmt, va_list ap)
{
if (err_file == NULL)
err_set_file(NULL);
fprintf(err_file, "%s: ", program_invocation_name);
if (fmt != NULL)
vfprintf(err_file, fmt, ap);
fprintf(err_file, "\n");
if (err_exit)
err_exit(eval);
exit(eval);
}
void
warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnc(errno, fmt, ap);
va_end(ap);
}
void
vwarn(const char *fmt, va_list ap)
{
vwarnc(errno, fmt, ap);
}
void
warnc(int code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnc(code, fmt, ap);
va_end(ap);
}
void
vwarnc(int code, const char *fmt, va_list ap)
{
int saved_errno;
saved_errno = errno;
if (err_file == NULL)
err_set_file(NULL);
fprintf(err_file, "%s: ", program_invocation_name);
if (fmt != NULL) {
vfprintf(err_file, fmt, ap);
fprintf(err_file, ": ");
}
fprintf(err_file, "%s\n", strerror(code));
errno = saved_errno;
}
void
warnx(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwarnx(fmt, ap);
va_end(ap);
}
void
vwarnx(const char *fmt, va_list ap)
{
int saved_errno;
saved_errno = errno;
if (err_file == NULL)
err_set_file(NULL);
fprintf(err_file, "%s: ", program_invocation_name);
if (fmt != NULL)
vfprintf(err_file, fmt, ap);
fprintf(err_file, "\n");
errno = saved_errno;
}

View file

@ -1,31 +0,0 @@
/*-*- 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 2020 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/log/bsd.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
wontreturn void(verrx)(int eval, const char *fmt, va_list va) {
fprintf(stderr, "%s: %s%s%s: ", program_invocation_name, RED2, "ERROR",
RESET);
if (fmt) (vfprintf)(stderr, fmt, va);
fprintf(stderr, "\n");
exit(eval);
}

View file

@ -1,30 +0,0 @@
/*-*- 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 2020 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/log/bsd.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
void(vwarn)(const char *fmt, va_list va) {
fprintf(stderr, "%s: %s%s%s[%m]: ", program_invocation_name, SUBTLE,
"WARNING", RESET);
if (fmt) (vfprintf)(stderr, fmt, va);
fprintf(stderr, "\n");
}

View file

@ -1,26 +0,0 @@
/*-*- 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 2020 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/log/bsd.h"
void(warn)(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
(vwarn)(fmt, va);
va_end(va);
}

View file

@ -1,26 +0,0 @@
/*-*- 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 2020 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/log/bsd.h"
void(warnx)(const char *fmt, ...) {
va_list va;
va_start(va, fmt);
(vwarnx)(fmt, va);
va_end(va);
}

View file

@ -1,7 +1,7 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
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
@ -16,18 +16,37 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/intrin/safemacros.internal.h"
#include "libc/log/bsd.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/runtime/utmp.h"
#include "libc/sysv/consts/termios.h"
#include "libc/sysv/errfuns.h"
wontreturn void(verr)(int eval, const char *fmt, va_list va) {
fprintf(stderr,
"%s: %s%s%s[%m]: ", firstnonnull(program_invocation_name, "unknown"),
RED2, "ERROR", RESET);
if (fmt) (vfprintf)(stderr, fmt, va);
fprintf(stderr, "\n");
exit(eval);
/**
* Prepares terminal for login.
*
* @return 0 on success, or -1 w/ errno
* @raise ENOSYS on Windows and Metal
* @raise EPERM if terminal is already controlling another sid
*/
int login_tty(int fd) {
int rc;
if (IsLinux() || IsBsd()) {
setsid();
if (!sys_ioctl(fd, TIOCSCTTY, 0)) {
for (int i = 0; i < 3; ++i) dup2(fd, i);
if (fd > 2) close(fd);
rc = 0;
} else {
rc = -1;
}
} else {
rc = enosys();
}
STRACE("login_tty(%d) → %d% m", fd, rc);
return rc;
}

View file

@ -88,12 +88,14 @@ long gethostid(void);
int sethostid(long);
char *getlogin(void);
int getlogin_r(char *, size_t);
int login_tty(int);
int getpagesize(void);
int syncfs(int);
int vhangup(void);
int getdtablesize(void);
int sethostname(const char *, size_t);
int acct(const char *);
void _intsort(int *, size_t);
void _longsort(long *, size_t);
bool _isheap(void *);
int NtGetVersion(void) pureconst;

View file

@ -1,7 +1,7 @@
/*-*- 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 2020 Justine Alexandra Roberts Tunney
Copyright 2021 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
@ -16,15 +16,32 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/log/bsd.h"
#include "libc/log/color.internal.h"
#include "libc/log/internal.h"
#include "libc/intrin/strace.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
void(vwarnx)(const char *fmt, va_list va) {
fprintf(stderr, "%s: %s%s%s: ", program_invocation_name, SUBTLE, "WARNING",
RESET);
if (fmt) (vfprintf)(stderr, fmt, va);
fprintf(stderr, "\n");
static void _intsorter(int *A, size_t n) {
int t, p;
size_t i, j;
if (n < 2) return;
for (p = A[n / 2], i = 0, j = n - 1;; i++, j--) {
while (A[i] < p) i++;
while (A[j] > p) j--;
if (i >= j) break;
t = A[i];
A[i] = A[j];
A[j] = t;
}
_intsorter(A, i);
_intsorter(A + i, n - i);
}
/**
* Tiny and reasonably fast sorting for ints.
* @see djbsort
*/
void _intsort(int *A, size_t n) {
_intsorter(A, n);
if (n > 1000) {
STRACE("_intsort(%p, %'zu)", A, n);
}
}

View file

@ -1374,7 +1374,7 @@ syscon termios TIOCCONS 0x541d 0x80047462 0x80047462 0x80047462 0x800474
syscon termios TIOCGETD 0x5424 0x4004741a 0x4004741a 0x4004741a 0x4004741a 0 # boop
syscon termios TIOCNOTTY 0x5422 0x20007471 0x20007471 0x20007471 0x20007471 0 # boop
syscon termios TIOCNXCL 0x540d 0x2000740e 0x2000740e 0x2000740e 0x2000740e 0 # boop
syscon termios TIOCSCTTY 0x540e 0x20007461 0x20007461 0x20007461 0x20007461 0 # boop
syscon termios TIOCSCTTY 0x540e 0x20007461 0x20007461 0x20007461 0x20007461 0 # makes terminal controlling terminal of calling process (see login_tty)
syscon termios TIOCSETD 0x5423 0x8004741b 0x8004741b 0x8004741b 0x8004741b 0 # boop
syscon termios TIOCSIG 0x40045436 0x2000745f 0x2004745f 0x8004745f 0x8004745f 0 # boop
syscon termios TIOCSTI 0x5412 0x80017472 0x80017472 0 0 0 # boop
@ -1409,6 +1409,10 @@ syscon termios TIOCSFLAGS 0 0 0 0x8004745c 0x8004745c 0 # boop
syscon termios TIOCSTSTAMP 0 0 0 0x8008745a 0x8008745a 0 # boop
syscon termios ENDRUNDISC 0 0 0 0x9 0x9 0 # boop
syscon termios TIOCPTMASTER 0 0 0x2000741c 0 0 0 # boop
syscon termios TIOCPTYGRANT 0 0x20007454 0 0 0 0 # xnu grantpt()
syscon termios TIOCPTYUNLK 0 0x20007452 0 0 0 0 # xnu grantpt()
syscon termios TIOCPTYGNAME 0 0x40807453 0 0 0 0 # xnu grantpt()
syscon termios FIODGNAME 0 0 0x80106678 0 0 0 # freebsd ptsname_r()
syscon termios NETGRAPHDISC 0 0 0x6 0 0 0 # boop
syscon termios H4DISC 0 0 0x7 0 0 0 # boop

View file

@ -0,0 +1,2 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon termios,FIODGNAME,0,0,0x80106678,0,0,0

View file

@ -0,0 +1,2 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon termios,TIOCPTYGNAME,0,0x40807453,0,0,0,0

View file

@ -0,0 +1,2 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon termios,TIOCPTYGRANT,0,0x20007454,0,0,0,0

View file

@ -0,0 +1,2 @@
.include "o/libc/sysv/consts/syscon.internal.inc"
.syscon termios,TIOCPTYUNLK,0,0x20007452,0,0,0,0

View file

@ -2,6 +2,7 @@
#define COSMOPOLITAN_LIBC_SYSV_CONSTS_POLL_H_
#include "libc/runtime/symbolic.h"
#define INFTIM (-1)
#define POLLERR SYMBOLIC(POLLERR)
#define POLLHUP SYMBOLIC(POLLHUP)
#define POLLIN SYMBOLIC(POLLIN)

View file

@ -178,6 +178,10 @@ extern const uint8_t VTIME;
extern const uint8_t VWERASE;
extern const uint32_t XCASE;
extern const uint32_t XTABS;
extern const uint32_t FIODGNAME;
extern const uint32_t TIOCPTYGRANT;
extern const uint32_t TIOCPTYUNLK;
extern const uint32_t TIOCPTYGNAME;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */
@ -250,6 +254,7 @@ COSMOPOLITAN_C_END_
#define EXTA SYMBOLIC(EXTA)
#define EXTB SYMBOLIC(EXTB)
#define EXTPROC SYMBOLIC(EXTPROC)
#define FIODGNAME SYMBOLIC(FIODGNAME)
#define FLUSHO SYMBOLIC(FLUSHO)
#define H4DISC SYMBOLIC(H4DISC)
#define HUPCL SYMBOLIC(HUPCL)
@ -319,6 +324,9 @@ COSMOPOLITAN_C_END_
#define TIOCNXCL SYMBOLIC(TIOCNXCL)
#define TIOCOUTQ SYMBOLIC(TIOCOUTQ)
#define TIOCPTMASTER SYMBOLIC(TIOCPTMASTER)
#define TIOCPTYGNAME SYMBOLIC(TIOCPTYGNAME)
#define TIOCPTYGRANT SYMBOLIC(TIOCPTYGRANT)
#define TIOCPTYUNLK SYMBOLIC(TIOCPTYUNLK)
#define TIOCREMOTE SYMBOLIC(TIOCREMOTE)
#define TIOCSBRK SYMBOLIC(TIOCSBRK)
#define TIOCSCTTY SYMBOLIC(TIOCSCTTY)

View file

@ -55,17 +55,16 @@ enum PosixThreadStatus {
};
struct PosixThread {
void *(*start_routine)(void *);
void *arg; // start_routine's parameter
void *rc; // start_routine's return value
bool ownstack;
int tid;
int *ctid;
char *tls;
char *altstack;
struct CosmoTib *tib;
_Atomic(enum PosixThreadStatus) status;
jmp_buf exiter;
void *(*start_routine)(void *);
void *arg; // start_routine's parameter
void *rc; // start_routine's return value
bool ownstack; // should we free it
int tid; // clone parent tid
char *altstack; // thread sigaltstack
char *tls; // bottom of tls allocation
struct CosmoTib *tib; // middle of tls allocation
jmp_buf exiter; // for pthread_exit
pthread_attr_t attr;
};
@ -82,7 +81,7 @@ void _pthread_wait(struct PosixThread *) hidden;
void _pthread_zombies_add(struct PosixThread *) hidden;
void _pthread_zombies_decimate(void) hidden;
void _pthread_zombies_harvest(void) hidden;
void _pthread_key_destruct(void *[PTHREAD_KEYS_MAX]);
void _pthread_key_destruct(void *[PTHREAD_KEYS_MAX]) hidden;
COSMOPOLITAN_C_END_
#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */