cosmopolitan/tool/build/mv.c
Justine Tunney 4778cd4d27
Fix bugs in termios library and cleanup code
This change fixes an issue with the tcflow() magic numbers that was
causing bash to freeze up on Linux. While auditing termios polyfills,
several other issues were identified with XNU/BSD compatibility.

Out of an abundance of caution this change undefines as much surface
area from libc/calls/struct/termios.h as possible, so that autoconf
scripts are less likely to detect non-POSIX teletypewriter APIs that
haven't been polyfilled by Cosmopolitan.

This is a *breaking change* for your static archives in /opt/cosmos if
you use the cosmocc toolchain. That's because this change disables the
ioctl() undiamonding trick for code outside the monorepo, specifically
because it'll lead to brittle ABI breakages like this. If you're using
the cosmocc toolchain, you'll need to rebuild libraries like ncurses,
readline, etc. Yes diamonds cause bloat. To work around that, consider
using tcgetwinsize() instead of ioctl(TIOCGWINSZ) since it'll help you
avoid pulling every single ioctl-related polyfill into the linkage.

The cosmocc script was specifying -DNDEBUG for some reason. It's fixed.
2023-06-14 19:30:52 -07:00

232 lines
6.3 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 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/struct/stat.h"
#include "libc/errno.h"
#include "libc/fmt/conv.h"
#include "libc/fmt/fmt.h"
#include "libc/fmt/magnumstrs.internal.h"
#include "libc/mem/gc.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/at.h"
#include "libc/sysv/consts/ok.h"
#include "libc/sysv/consts/s.h"
#include "libc/x/x.h"
#include "third_party/getopt/getopt.h"
#include "third_party/musl/ftw.h"
#define USAGE \
" SRC... DST\n\
\n\
SYNOPSIS\n\
\n\
Moves Files\n\
\n\
FLAGS\n\
\n\
-?\n\
-h help\n\
-f force\n\
-r recursive\n\
\n"
bool force;
int striplen;
bool recursive;
const char *prog;
char mkbuf[PATH_MAX];
char srcdir[PATH_MAX];
char dstdir[PATH_MAX];
char srcfile[PATH_MAX];
char dstfile[PATH_MAX];
char linkbuf[PATH_MAX];
void Mv(char *, char *);
nullterminated() void Print(int fd, const char *s, ...) {
va_list va;
char buf[2048];
va_start(va, s);
buf[0] = 0;
do {
strlcat(buf, s, sizeof(buf));
} while ((s = va_arg(va, const char *)));
write(fd, buf, strlen(buf));
va_end(va);
}
wontreturn void Die(const char *path, const char *reason) {
Print(2, path, ": ", reason, "\n", NULL);
exit(1);
}
wontreturn void SysExit(const char *path, const char *func) {
const char *errstr;
if (!(errstr = _strerdoc(errno))) errstr = "EUNKNOWN";
Print(2, path, ": ", func, "() failed with ", errstr, "\n", NULL);
exit(1);
}
bool IsDirectory(const char *path) {
int e;
bool res;
struct stat st;
e = errno;
res = stat(path, &st) != -1 && S_ISDIR(st.st_mode);
errno = e;
return res;
}
bool IsSymlink(const char *path) {
int e;
bool res;
struct stat st;
e = errno;
res = fstatat(AT_FDCWD, path, &st, AT_SYMLINK_NOFOLLOW) != -1 &&
S_ISLNK(st.st_mode);
errno = e;
return res;
}
wontreturn void PrintUsage(int rc, int fd) {
Print(fd, "usage: ", prog, USAGE, NULL);
exit(rc);
}
void GetOpts(int argc, char *argv[]) {
int opt;
while ((opt = getopt(argc, argv, "?hfrR")) != -1) {
switch (opt) {
case 'f':
force = true;
break;
case 'r':
case 'R':
recursive = true;
break;
case 'h':
case '?':
PrintUsage(0, 1);
default:
PrintUsage(1, 2);
}
}
}
int Visit(const char *fpath, const struct stat *sb, int tflag,
struct FTW *ftwbuf) {
char *src;
strcpy(srcfile, fpath);
src = srcfile + striplen;
strcpy(dstfile, dstdir);
if (!_endswith(dstfile, "/")) {
strcat(dstfile, "/");
}
strcat(dstfile, src);
strcpy(srcfile, fpath);
switch (tflag) {
case FTW_D:
return 0;
case FTW_F:
case FTW_SL:
case FTW_SLN:
Mv(srcfile, dstfile);
return 0;
default:
Die(fpath, "can't handle file type");
}
}
char *Join(const char *a, const char *b) {
size_t n, m;
n = strlen(a);
m = strlen(b);
if (n + 1 + m + 1 > sizeof(dstfile)) {
Print(2, "error: mv: path too long\n", NULL);
exit(1);
}
stpcpy(stpcpy(stpcpy(dstfile, a), "/"), b);
return dstfile;
}
void Mv(char *src, char *dst) {
ssize_t rc;
const char *s, *d;
if (strlen(src) + 1 > PATH_MAX) _Exit(2);
if (strlen(dst) + 1 > PATH_MAX) _Exit(2);
basename(src);
basename(dst);
if (IsDirectory(src)) {
if (!recursive) {
Die(prog, "won't move directory without -r flag.");
}
strcpy(dstdir, dst);
if (IsDirectory(dst)) {
strcpy(srcdir, src);
basename(srcdir);
striplen = 0;
strcpy(srcdir, basename(src));
} else {
strcpy(srcdir, src);
basename(srcdir);
striplen = strlen(srcdir);
strcpy(srcdir, "");
}
if (nftw(src, Visit, 20, 0) == -1) {
SysExit(src, "nftw");
}
return;
}
if (IsDirectory(dst)) {
dst = Join(dst, basename(src));
}
if (!force && access(dst, W_OK) == -1 && errno != ENOENT) {
SysExit(dst, "access");
}
strcpy(mkbuf, dst);
if (makedirs((d = dirname(mkbuf)), 0755) == -1) {
SysExit(d, "makedirs");
}
if (IsSymlink(src)) {
if ((rc = readlink(src, linkbuf, sizeof(linkbuf) - 1)) == -1) {
SysExit(src, "readlink");
}
linkbuf[rc] = 0;
if (symlink(linkbuf, dst)) {
SysExit(dst, "symlink");
}
} else {
if (rename(src, dst)) {
SysExit(src, "rename");
}
}
}
int main(int argc, char *argv[]) {
int i;
prog = argc > 0 ? argv[0] : "mv.com";
GetOpts(argc, argv);
if (argc - optind < 2) PrintUsage(1, 2);
for (i = optind; i < argc - 1; ++i) {
Mv(argv[i], argv[argc - 1]);
}
return 0;
}