mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +00:00
Introduce new tinyprint() api
This commit is contained in:
parent
053681cb97
commit
963e10b9bf
2 changed files with 69 additions and 0 deletions
|
@ -229,6 +229,7 @@ int unveil(const char *, const char *);
|
||||||
long ptrace(int, ...);
|
long ptrace(int, ...);
|
||||||
ssize_t copyfd(int, int, size_t);
|
ssize_t copyfd(int, int, size_t);
|
||||||
ssize_t readansi(int, char *, size_t);
|
ssize_t readansi(int, char *, size_t);
|
||||||
|
ssize_t tinyprint(int, const char *, ...) nullterminated();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
|
|
68
libc/calls/tinyprint.c
Normal file
68
libc/calls/tinyprint.c
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
/*-*- 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 2023 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"
|
||||||
|
|
||||||
|
static bool tinyflush(int fd, char *buf, size_t n, ssize_t *toto) {
|
||||||
|
ssize_t rc;
|
||||||
|
if ((rc = write(fd, buf, n)) != -1) {
|
||||||
|
*toto += rc;
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
if (!*toto) {
|
||||||
|
--*toto;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Writes strings to file descriptor, e.g.
|
||||||
|
*
|
||||||
|
* tinyprint(1, "hello", " ", "world", "\n", NULL);
|
||||||
|
*
|
||||||
|
* This function is intended so that small command line programs (e.g.
|
||||||
|
* build utilities) can print succinct error messages automatically to
|
||||||
|
* standard io without linking too much binary footprint.
|
||||||
|
*
|
||||||
|
* @param fd is file descriptor
|
||||||
|
* @param s is NUL-terminated string, or NULL
|
||||||
|
* @return bytes written on success, or -1 w/ errno
|
||||||
|
*/
|
||||||
|
ssize_t tinyprint(int fd, const char *s, ...) {
|
||||||
|
int c;
|
||||||
|
size_t n;
|
||||||
|
va_list va;
|
||||||
|
ssize_t toto;
|
||||||
|
char buf[512];
|
||||||
|
va_start(va, s);
|
||||||
|
for (toto = n = 0; s; s = va_arg(va, const char *)) {
|
||||||
|
while ((c = *s++)) {
|
||||||
|
buf[n++] = c;
|
||||||
|
if (n == sizeof(buf)) {
|
||||||
|
if (tinyflush(fd, buf, n, &toto)) {
|
||||||
|
return toto;
|
||||||
|
}
|
||||||
|
n = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
va_end(va);
|
||||||
|
tinyflush(fd, buf, n, &toto);
|
||||||
|
return toto;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue