2020-06-15 14:18:57 +00:00
|
|
|
/*-*- 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 │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ 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. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2020-12-01 11:43:40 +00:00
|
|
|
#include "libc/calls/internal.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/calls/ntmagicpaths.internal.h"
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
#include "libc/calls/strace.internal.h"
|
2022-04-24 16:59:22 +00:00
|
|
|
#include "libc/macros.internal.h"
|
2021-06-24 19:31:26 +00:00
|
|
|
#include "libc/nt/systeminfo.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/str/oldutf16.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2020-11-25 16:19:00 +00:00
|
|
|
#include "libc/str/tpdecode.internal.h"
|
2020-10-27 10:39:46 +00:00
|
|
|
#include "libc/sysv/consts/o.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
2021-08-18 21:21:30 +00:00
|
|
|
static inline bool IsSlash(char c) {
|
|
|
|
return c == '/' || c == '\\';
|
|
|
|
}
|
|
|
|
|
2020-10-27 10:39:46 +00:00
|
|
|
textwindows static const char *FixNtMagicPath(const char *path,
|
|
|
|
unsigned flags) {
|
|
|
|
const struct NtMagicPaths *mp = &kNtMagicPaths;
|
|
|
|
asm("" : "+r"(mp));
|
2021-08-18 21:21:30 +00:00
|
|
|
if (!IsSlash(path[0])) return path;
|
2020-12-01 11:43:40 +00:00
|
|
|
if (strcmp(path, mp->devtty) == 0) {
|
2020-10-27 10:39:46 +00:00
|
|
|
if ((flags & O_ACCMODE) == O_RDONLY) {
|
|
|
|
return mp->conin;
|
|
|
|
} else if ((flags & O_ACCMODE) == O_WRONLY) {
|
|
|
|
return mp->conout;
|
|
|
|
}
|
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
if (strcmp(path, mp->devnull) == 0) return mp->nul;
|
|
|
|
if (strcmp(path, mp->devstdin) == 0) return mp->conin;
|
|
|
|
if (strcmp(path, mp->devstdout) == 0) return mp->conout;
|
2020-10-27 10:39:46 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-12-01 11:43:40 +00:00
|
|
|
textwindows int __mkntpath(const char *path,
|
2022-04-28 16:42:36 +00:00
|
|
|
char16_t path16[hasatleast PATH_MAX]) {
|
2020-12-01 11:43:40 +00:00
|
|
|
return __mkntpath2(path, path16, -1);
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:18:57 +00:00
|
|
|
/**
|
|
|
|
* Copies path for Windows NT.
|
|
|
|
*
|
|
|
|
* This entails (1) UTF-8 to UTF-16 conversion; (2) replacing
|
|
|
|
* forward-slashes with backslashes; and (3) remapping several
|
|
|
|
* well-known paths (e.g. /dev/null → NUL) for convenience.
|
|
|
|
*
|
2020-10-27 10:39:46 +00:00
|
|
|
* @param flags is used by open()
|
2020-06-15 14:18:57 +00:00
|
|
|
* @param path16 is shortened so caller can prefix, e.g. \\.\pipe\, and
|
|
|
|
* due to a plethora of special-cases throughout the Win32 API
|
|
|
|
* @return short count excluding NUL on success, or -1 w/ errno
|
|
|
|
* @error ENAMETOOLONG
|
|
|
|
*/
|
2020-12-01 11:43:40 +00:00
|
|
|
textwindows int __mkntpath2(const char *path,
|
2022-04-28 16:42:36 +00:00
|
|
|
char16_t path16[hasatleast PATH_MAX], int flags) {
|
2020-06-15 14:18:57 +00:00
|
|
|
/*
|
2022-04-24 16:59:22 +00:00
|
|
|
* 1. Need +1 for NUL-terminator
|
|
|
|
* 2. Need +1 for UTF-16 overflow
|
|
|
|
* 3. Need ≥2 for SetCurrentDirectory trailing slash requirement
|
|
|
|
* 5. Need ≥13 for mkdir() i.e. 1+8+3+1, e.g. "\\ffffffff.xxx\0"
|
|
|
|
* which is an "8.3 filename" from the DOS days
|
2020-06-15 14:18:57 +00:00
|
|
|
*/
|
2021-06-24 19:31:26 +00:00
|
|
|
char16_t *p;
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
const char *q;
|
2021-06-24 19:31:26 +00:00
|
|
|
size_t i, n, m, z;
|
2021-02-07 14:11:44 +00:00
|
|
|
if (!path) return efault();
|
2020-10-27 10:39:46 +00:00
|
|
|
path = FixNtMagicPath(path, flags);
|
2021-06-24 19:31:26 +00:00
|
|
|
p = path16;
|
|
|
|
q = path;
|
2022-04-24 16:59:22 +00:00
|
|
|
if (IsSlash(path[0]) && IsSlash(path[1]) && path[2] == '?' &&
|
|
|
|
IsSlash(path[3])) {
|
|
|
|
z = MIN(32767, PATH_MAX);
|
|
|
|
} else {
|
|
|
|
z = MIN(260, PATH_MAX);
|
|
|
|
}
|
2021-08-18 21:21:30 +00:00
|
|
|
if (IsSlash(q[0]) && q[1] == 't' && q[2] == 'm' && q[3] == 'p' &&
|
|
|
|
(IsSlash(q[4]) || !q[4])) {
|
2021-06-24 19:31:26 +00:00
|
|
|
m = GetTempPath(z, p);
|
|
|
|
if (!q[4]) return m;
|
|
|
|
q += 5;
|
|
|
|
p += m;
|
|
|
|
z -= m;
|
|
|
|
} else {
|
|
|
|
m = 0;
|
|
|
|
}
|
|
|
|
n = tprecode8to16(p, z, q).ax;
|
2022-04-28 16:42:36 +00:00
|
|
|
if (n >= z - 1) {
|
Introduce --strace flag for system call tracing
This is similar to the --ftrace (c function call trace) flag, except
it's less noisy since it only logs system calls to stderr. Having this
flag is valuable because (1) system call tracing tells us a lot about
the behavior of complex programs and (2) it's usually very hard to get
system call tracing on various operating systems, e.g. strace, ktrace,
dtruss, truss, nttrace, etc. Especially on Apple platforms where even
with the special boot trick, debuggers still aren't guaranteed to work.
make -j8 o//examples
o//examples/hello.com --strace
This is enabled by default in MODE=, MODE=opt, and MODE=dbg. In MODE=dbg
extra information will be printed.
make -j8 MODE=dbg o/dbg/examples
o/dbg/examples/hello.com --strace |& less
This change also changes:
- Rename IsText() → _istext()
- Rename IsUtf8() → _isutf8()
- Fix madvise() on Windows NT
- Fix empty string case of inet_ntop()
- vfork() wrapper now saves and restores errno
- Update xsigaction() to yoink syscall support
2022-03-19 01:07:28 +00:00
|
|
|
STRACE("path too long for windows: %#s", path);
|
2021-10-14 00:27:13 +00:00
|
|
|
return enametoolong();
|
|
|
|
}
|
2020-12-01 11:43:40 +00:00
|
|
|
for (i = 0; i < n; ++i) {
|
2021-06-24 19:31:26 +00:00
|
|
|
if (p[i] == '/') {
|
|
|
|
p[i] = '\\';
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-24 19:31:26 +00:00
|
|
|
return m + n;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|