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;

38
libc/calls/isptmaster.c Normal file
View file

@ -0,0 +1,38 @@
/*-*- 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/calls.h"
#include "libc/calls/syscall-sysv.internal.h"
#include "libc/dce.h"
#include "libc/errno.h"
#include "libc/sysv/consts/termios.h"
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;
}