2022-07-21 16:16:38 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
2023-12-08 03:11:56 +00:00
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
2022-07-21 16:16:38 +00:00
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ 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"
|
2023-06-18 12:39:31 +00:00
|
|
|
#include "libc/fmt/libgen.h"
|
2023-06-09 08:23:18 +00:00
|
|
|
#include "libc/fmt/magnumstrs.internal.h"
|
2023-09-06 10:54:42 +00:00
|
|
|
#include "libc/limits.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/mem/gc.h"
|
2022-07-21 16:16:38 +00:00
|
|
|
#include "libc/runtime/runtime.h"
|
2023-08-14 03:31:27 +00:00
|
|
|
#include "libc/stdio/ftw.h"
|
2022-07-21 16:16:38 +00:00
|
|
|
#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"
|
2023-07-03 02:57:43 +00:00
|
|
|
#include "third_party/getopt/getopt.internal.h"
|
2022-07-21 16:16:38 +00:00
|
|
|
|
|
|
|
#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 *);
|
|
|
|
|
2023-06-15 00:02:57 +00:00
|
|
|
wontreturn void Die(const char *path, const char *reason) {
|
2023-07-03 09:47:05 +00:00
|
|
|
tinyprint(2, path, ": ", reason, "\n", NULL);
|
2023-06-15 00:02:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2023-07-03 09:47:05 +00:00
|
|
|
wontreturn void SysDie(const char *path, const char *func) {
|
2023-06-15 00:02:57 +00:00
|
|
|
const char *errstr;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!(errstr = _strerdoc(errno)))
|
|
|
|
errstr = "EUNKNOWN";
|
2023-07-03 09:47:05 +00:00
|
|
|
tinyprint(2, path, ": ", func, ": ", errstr, "\n", NULL);
|
2023-06-15 00:02:57 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:16:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-06-15 00:02:57 +00:00
|
|
|
wontreturn void PrintUsage(int rc, int fd) {
|
2023-07-03 09:47:05 +00:00
|
|
|
tinyprint(fd, "usage: ", prog, USAGE, NULL);
|
2022-07-21 16:16:38 +00:00
|
|
|
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 '?':
|
2023-06-15 00:02:57 +00:00
|
|
|
PrintUsage(0, 1);
|
2022-07-21 16:16:38 +00:00
|
|
|
default:
|
2023-06-15 00:02:57 +00:00
|
|
|
PrintUsage(1, 2);
|
2022-07-21 16:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2023-08-14 03:31:27 +00:00
|
|
|
if (!endswith(dstfile, "/")) {
|
2022-07-21 16:16:38 +00:00
|
|
|
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:
|
2023-06-15 00:02:57 +00:00
|
|
|
Die(fpath, "can't handle file type");
|
2022-07-21 16:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2023-07-03 09:47:05 +00:00
|
|
|
tinyprint(2, "error: mv: path too long\n", NULL);
|
2022-07-21 16:16:38 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
stpcpy(stpcpy(stpcpy(dstfile, a), "/"), b);
|
|
|
|
return dstfile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Mv(char *src, char *dst) {
|
|
|
|
ssize_t rc;
|
2023-09-02 03:49:13 +00:00
|
|
|
const char *d;
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (strlen(src) + 1 > PATH_MAX)
|
|
|
|
_Exit(2);
|
|
|
|
if (strlen(dst) + 1 > PATH_MAX)
|
|
|
|
_Exit(2);
|
2022-07-21 16:16:38 +00:00
|
|
|
basename(src);
|
|
|
|
basename(dst);
|
|
|
|
if (IsDirectory(src)) {
|
|
|
|
if (!recursive) {
|
2023-06-15 00:02:57 +00:00
|
|
|
Die(prog, "won't move directory without -r flag.");
|
2022-07-21 16:16:38 +00:00
|
|
|
}
|
|
|
|
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) {
|
2023-07-03 09:47:05 +00:00
|
|
|
SysDie(src, "nftw");
|
2022-07-21 16:16:38 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (IsDirectory(dst)) {
|
|
|
|
dst = Join(dst, basename(src));
|
|
|
|
}
|
2023-06-15 00:02:57 +00:00
|
|
|
if (!force && access(dst, W_OK) == -1 && errno != ENOENT) {
|
2023-07-03 09:47:05 +00:00
|
|
|
SysDie(dst, "access");
|
2023-06-15 00:02:57 +00:00
|
|
|
}
|
2022-07-21 16:16:38 +00:00
|
|
|
strcpy(mkbuf, dst);
|
2023-06-15 00:02:57 +00:00
|
|
|
if (makedirs((d = dirname(mkbuf)), 0755) == -1) {
|
2023-07-03 09:47:05 +00:00
|
|
|
SysDie(d, "makedirs");
|
2023-06-15 00:02:57 +00:00
|
|
|
}
|
2022-07-21 16:16:38 +00:00
|
|
|
if (IsSymlink(src)) {
|
2023-06-15 00:02:57 +00:00
|
|
|
if ((rc = readlink(src, linkbuf, sizeof(linkbuf) - 1)) == -1) {
|
2023-07-03 09:47:05 +00:00
|
|
|
SysDie(src, "readlink");
|
2023-06-15 00:02:57 +00:00
|
|
|
}
|
2022-07-21 16:16:38 +00:00
|
|
|
linkbuf[rc] = 0;
|
2023-06-15 00:02:57 +00:00
|
|
|
if (symlink(linkbuf, dst)) {
|
2023-07-03 09:47:05 +00:00
|
|
|
SysDie(dst, "symlink");
|
2023-06-15 00:02:57 +00:00
|
|
|
}
|
2022-07-21 16:16:38 +00:00
|
|
|
} else {
|
2023-06-15 00:02:57 +00:00
|
|
|
if (rename(src, dst)) {
|
2023-07-03 09:47:05 +00:00
|
|
|
SysDie(src, "rename");
|
2023-06-15 00:02:57 +00:00
|
|
|
}
|
2022-07-21 16:16:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int i;
|
2023-07-03 09:47:05 +00:00
|
|
|
prog = argv[0];
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (!prog)
|
|
|
|
prog = "mv";
|
2022-07-21 16:16:38 +00:00
|
|
|
GetOpts(argc, argv);
|
Apply clang-format update to repo (#1154)
Commit bc6c183 introduced a bunch of discrepancies between what files
look like in the repo and what clang-format says they should look like.
However, there were already a few discrepancies prior to that. Most of
these discrepancies seemed to be unintentional, but a few of them were
load-bearing (e.g., a #include that violated header ordering needing
something to have been #defined by a 'later' #include.)
I opted to take what I hope is a relatively smooth-brained approach: I
reverted the .clang-format change, ran clang-format on the whole repo,
reapplied the .clang-format change, reran clang-format again, and then
reverted the commit that contained the first run. Thus the full effect
of this PR should only be to apply the changed formatting rules to the
repo, and from skimming the results, this seems to be the case.
My work can be checked by applying the short, manual commits, and then
rerunning the command listed in the autogenerated commits (those whose
messages I have prefixed auto:) and seeing if your results agree.
It might be that the other diffs should be fixed at some point but I'm
leaving that aside for now.
fd '\.c(c|pp)?$' --print0| xargs -0 clang-format -i
2024-04-25 17:38:00 +00:00
|
|
|
if (argc - optind < 2)
|
|
|
|
PrintUsage(1, 2);
|
2022-07-21 16:16:38 +00:00
|
|
|
for (i = optind; i < argc - 1; ++i) {
|
|
|
|
Mv(argv[i], argv[argc - 1]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|