mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 19:43:32 +00:00
f7ff77d865
- Invent iso8601us() for faster timestamps - Improve --strace descriptions of sigset_t - Rebuild the Landlock Make bootstrap binary - Introduce MODE=sysv for non-Windows builds - Permit OFD fcntl() locks under pledge(flock) - redbean can now protect your kernel from ddos - Have vfork() fallback to sys_fork() not fork() - Change kmalloc() to not die when out of memory - Improve documentation for some termios functions - Rewrite putenv() and friends to conform to POSIX - Fix linenoise + strace verbosity issue on Windows - Fix regressions in our ability to show backtraces - Change redbean SetHeader() to no-op if value is nil - Improve fcntl() so SQLite locks work in non-WAL mode - Remove some unnecessary work during fork() on Windows - Create redbean-based SSL reverse proxy for IPv4 TurfWar - Fix ape/apeinstall.sh warning when using non-bash shells - Add ProgramTrustedIp(), and IsTrustedIp() APIs to redbean - Support $PWD, $UID, $GID, and $EUID in command interpreter - Introduce experimental JTqFpD APE prefix for non-Windows builds - Invent blackhole daemon for firewalling IP addresses via UNIX named socket - Add ProgramTokenBucket(), AcquireToken(), and CountTokens() APIs to redbean
102 lines
3.5 KiB
C
102 lines
3.5 KiB
C
/*-*- 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/intrin/_getenv.internal.h"
|
|
#include "libc/intrin/kmalloc.h"
|
|
#include "libc/intrin/strace.internal.h"
|
|
#include "libc/macros.internal.h"
|
|
#include "libc/mem/internal.h"
|
|
#include "libc/runtime/runtime.h"
|
|
|
|
#define ToUpper(c) ((c) >= 'a' && (c) <= 'z' ? (c) - 'a' + 'A' : (c))
|
|
|
|
static char **expected;
|
|
static size_t capacity;
|
|
|
|
static size_t GetEnvironLen(char **env) {
|
|
char **p = env;
|
|
while (*p) ++p;
|
|
return p - env;
|
|
}
|
|
|
|
static char **GrowEnviron(char **a) {
|
|
size_t n, c;
|
|
char **b, **p;
|
|
if (!a) a = environ;
|
|
n = a ? GetEnvironLen(a) : 0;
|
|
c = MAX(16ul, n) << 1;
|
|
if ((b = kmalloc(c * sizeof(char *)))) {
|
|
if (a) {
|
|
for (p = b; *a;) {
|
|
*p++ = *a++;
|
|
}
|
|
}
|
|
environ = b;
|
|
expected = b;
|
|
capacity = c;
|
|
return b;
|
|
} else {
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
int PutEnvImpl(char *s, bool overwrite) {
|
|
char **p;
|
|
struct Env e;
|
|
if (!(p = environ)) {
|
|
if (!(p = GrowEnviron(0))) {
|
|
return -1;
|
|
}
|
|
}
|
|
e = _getenv(p, s);
|
|
if (e.s && !overwrite) {
|
|
return 0;
|
|
}
|
|
if (e.s) {
|
|
p[e.i] = s;
|
|
return 0;
|
|
}
|
|
if (p != expected) {
|
|
capacity = e.i;
|
|
}
|
|
if (e.i + 1 >= capacity) {
|
|
if (!(p = GrowEnviron(p))) {
|
|
return -1;
|
|
}
|
|
}
|
|
p[e.i + 1] = 0;
|
|
p[e.i] = s;
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Emplaces environment key=value.
|
|
*
|
|
* @param s should be a string that looks like `"name=value"` and it'll
|
|
* become part of the environment; changes to its memory will change
|
|
* the environment too
|
|
* @return 0 on success, or non-zero w/ errno on error
|
|
* @raise ENOMEM if we require more vespene gas
|
|
* @see setenv(), getenv()
|
|
*/
|
|
int putenv(char *s) {
|
|
int rc;
|
|
rc = PutEnvImpl(s, true);
|
|
STRACE("putenv(%#s) → %d% m", s, rc);
|
|
return rc;
|
|
}
|