mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-28 13:30:29 +00:00
Remove plenty of makefile misconfigurations
This commit is contained in:
parent
9172fd42a0
commit
8b469389f6
186 changed files with 1408 additions and 901 deletions
|
@ -63,8 +63,10 @@ APE shell script format. This is useful on UNIX operating systems when\n\
|
|||
you want to use your APE programs as script interpreter or for setuid.\n\
|
||||
"
|
||||
|
||||
#define MODE_ELF 1
|
||||
#define MODE_MACHO 2
|
||||
#define MODE_NATIVE 0
|
||||
#define MODE_ELF 1
|
||||
#define MODE_MACHO 2
|
||||
#define MODE_PE 3
|
||||
|
||||
int g_mode;
|
||||
bool g_force;
|
||||
|
@ -94,11 +96,13 @@ void GetOpts(int argc, char *argv[]) {
|
|||
exit(64);
|
||||
}
|
||||
}
|
||||
if (!g_mode) {
|
||||
if (g_mode == MODE_NATIVE) {
|
||||
if (IsXnu()) {
|
||||
g_mode = MODE_MACHO;
|
||||
} else {
|
||||
} else if (IsLinux() || IsFreebsd() || IsNetbsd() || IsOpenbsd()) {
|
||||
g_mode = MODE_ELF;
|
||||
} else {
|
||||
g_mode = MODE_PE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -233,6 +237,20 @@ void Assimilate(void) {
|
|||
kprintf("%s: mmap failed: %m\n", prog);
|
||||
exit(16);
|
||||
}
|
||||
if (g_mode == MODE_PE) {
|
||||
if (READ16LE(p) == READ16LE("MZ")) {
|
||||
if (!g_force) {
|
||||
kprintf("%s: program is already an elf binary\n", prog);
|
||||
if (g_mode != MODE_ELF) {
|
||||
exitcode = 1;
|
||||
}
|
||||
}
|
||||
goto Finish;
|
||||
} else {
|
||||
kprintf("%s: currently cannot back-convert to pe\n", prog);
|
||||
exit(17);
|
||||
}
|
||||
}
|
||||
if (READ32LE(p) == READ32LE("\177ELF")) {
|
||||
if (!g_force) {
|
||||
kprintf("%s: program is already an elf binary\n", prog);
|
||||
|
|
|
@ -85,14 +85,6 @@ o/$(MODE)/tool/build/%.com.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/build/blinkenlights.com.dbg: \
|
||||
$(TOOL_BUILD_DEPS) \
|
||||
o/$(MODE)/tool/build/build.pkg \
|
||||
o/$(MODE)/tool/build/blinkenlights.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
.PRECIOUS: o/$(MODE)/tool/build/blinkenlights.com
|
||||
o/$(MODE)/tool/build/blinkenlights.com: \
|
||||
o/$(MODE)/tool/build/blinkenlights.com.dbg \
|
||||
|
@ -104,50 +96,27 @@ o/$(MODE)/tool/build/blinkenlights.com: \
|
|||
@$(COMPILE) -AZIP -T$@ o/$(MODE)/third_party/zip/zip.com -9qj $@ \
|
||||
o/$(MODE)/tool/build/.blinkenlights/.symtab
|
||||
|
||||
o/$(MODE)/tool/build/ar.com.dbg: \
|
||||
$(TOOL_BUILD_DEPS) \
|
||||
o/$(MODE)/tool/build/build.pkg \
|
||||
o/$(MODE)/tool/build/ar.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/build/package.com.dbg: \
|
||||
$(TOOL_BUILD_DEPS) \
|
||||
o/$(MODE)/tool/build/build.pkg \
|
||||
o/$(MODE)/tool/build/package.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/build/mkdeps.com.dbg: \
|
||||
$(TOOL_BUILD_DEPS) \
|
||||
o/$(MODE)/tool/build/build.pkg \
|
||||
o/$(MODE)/tool/build/mkdeps.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/build/compile.com.dbg: \
|
||||
$(TOOL_BUILD_DEPS) \
|
||||
o/$(MODE)/tool/build/build.pkg \
|
||||
o/$(MODE)/tool/build/compile.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/build/zipobj.com.dbg: \
|
||||
$(TOOL_BUILD_DEPS) \
|
||||
o/$(MODE)/tool/build/build.pkg \
|
||||
o/$(MODE)/tool/build/zipobj.o \
|
||||
$(CRT) \
|
||||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/build/emulator.o: \
|
||||
OVERRIDE_COPTS += \
|
||||
-fno-sanitize=pointer-overflow
|
||||
|
||||
o/$(MODE)/tool/build/mkdir.zip.o: o/$(MODE)/tool/build/mkdir
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/chmod.zip.o: o/$(MODE)/tool/build/chmod
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/cp.zip.o: o/$(MODE)/tool/build/cp
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/mv.zip.o: o/$(MODE)/tool/build/mv
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/echo.zip.o: o/$(MODE)/tool/build/echo
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/gzip.zip.o: o/$(MODE)/tool/build/gzip
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/printf.zip.o: o/$(MODE)/tool/build/printf
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
o/$(MODE)/tool/build/dd.zip.o: o/$(MODE)/tool/build/dd
|
||||
@$(COMPILE) -AZIPOBJ $(ZIPOBJ) $(ZIPOBJ_FLAGS) -0 -B -Pbin $(OUTPUT_OPTION) $<
|
||||
|
||||
.PHONY: o/$(MODE)/tool/build
|
||||
o/$(MODE)/tool/build: \
|
||||
o/$(MODE)/tool/build/emucrt \
|
||||
|
|
120
tool/build/chmod.c
Normal file
120
tool/build/chmod.c
Normal file
|
@ -0,0 +1,120 @@
|
|||
/*-*- 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/dirent.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/ex.h"
|
||||
#include "libc/sysv/consts/exit.h"
|
||||
#include "third_party/getopt/getopt.h"
|
||||
|
||||
#define USAGE \
|
||||
" OCTAL DST...\n\
|
||||
\n\
|
||||
SYNOPSIS\n\
|
||||
\n\
|
||||
Changes File Mode Bits\n\
|
||||
\n\
|
||||
FLAGS\n\
|
||||
\n\
|
||||
-?\n\
|
||||
-h help\n\
|
||||
\n"
|
||||
|
||||
const char *prog;
|
||||
|
||||
wontreturn void PrintUsage(int rc, FILE *f) {
|
||||
fputs("usage: ", f);
|
||||
fputs(prog, f);
|
||||
fputs(USAGE, f);
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
void GetOpts(int argc, char *argv[]) {
|
||||
int opt;
|
||||
while ((opt = getopt(argc, argv, "?h")) != -1) {
|
||||
switch (opt) {
|
||||
case 'h':
|
||||
case '?':
|
||||
PrintUsage(EXIT_SUCCESS, stdout);
|
||||
default:
|
||||
PrintUsage(EX_USAGE, stderr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i, mode;
|
||||
char buf[PATH_MAX];
|
||||
|
||||
{
|
||||
printf("curdir %s\n", getcwd(buf, sizeof(buf)));
|
||||
printf("tmp:");
|
||||
struct dirent *e;
|
||||
DIR *d;
|
||||
if ((d = opendir("tmp"))) {
|
||||
while ((e = readdir(d))) {
|
||||
printf(" %s", e->d_name);
|
||||
}
|
||||
closedir(d);
|
||||
} else {
|
||||
printf(" dir not found");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
{
|
||||
printf("curdir %s\n", getcwd(buf, sizeof(buf)));
|
||||
printf("bin:");
|
||||
struct dirent *e;
|
||||
DIR *d;
|
||||
if ((d = opendir("bin"))) {
|
||||
while ((e = readdir(d))) {
|
||||
printf(" %s", e->d_name);
|
||||
}
|
||||
closedir(d);
|
||||
} else {
|
||||
printf(" dir not found");
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
prog = argc > 0 ? argv[0] : "mv.com";
|
||||
GetOpts(argc, argv);
|
||||
if (argc - optind < 2) {
|
||||
PrintUsage(EX_USAGE, stderr);
|
||||
}
|
||||
mode = strtol(argv[optind], 0, 8) & 07777;
|
||||
for (i = optind + 1; i < argc; ++i) {
|
||||
if (chmod(argv[i], mode) == -1) {
|
||||
const char *s = strerdoc(errno);
|
||||
fputs(prog, stderr);
|
||||
fputs(": ", stderr);
|
||||
fputs(argv[i], stderr);
|
||||
fputs(": ", stderr);
|
||||
fputs(s, stderr);
|
||||
fputs("\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
160
tool/build/dd.c
Normal file
160
tool/build/dd.c
Normal file
|
@ -0,0 +1,160 @@
|
|||
/*-*- 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/bits/safemacros.internal.h"
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/fmt/conv.h"
|
||||
#include "libc/limits.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
|
||||
char buf[65536];
|
||||
|
||||
wontreturn void SysFail(const char *func, const char *file) {
|
||||
int e = errno;
|
||||
fputs("dd: ", stderr);
|
||||
fputs(func, stderr);
|
||||
fputs(" failed: ", stderr);
|
||||
fputs(file, stderr);
|
||||
fputs(": ", stderr);
|
||||
fputs(nulltoempty(strerdoc(e)), stderr);
|
||||
fputs("\n", stderr);
|
||||
exit(__COUNTER__ + 1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
long i;
|
||||
char *p;
|
||||
long skip = 0;
|
||||
long count = LONG_MAX;
|
||||
long blocksize = 1;
|
||||
int oflags = O_WRONLY | O_TRUNC | O_CREAT;
|
||||
const char *infile = "/dev/stdin";
|
||||
const char *oufile = "/dev/stdout";
|
||||
|
||||
for (i = 1; i < argc; ++i) {
|
||||
|
||||
if (argv[i][0] == 'b' && //
|
||||
argv[i][1] == 's' && //
|
||||
argv[i][2] == '=') {
|
||||
blocksize = strtol(argv[i] + 3 + (argv[i][3] == '"'), 0, 10);
|
||||
if (!(0 < blocksize && blocksize <= sizeof(buf))) {
|
||||
fputs("dd: bad block size\n", stderr);
|
||||
return __COUNTER__ + 1;
|
||||
}
|
||||
|
||||
} else if (argv[i][0] == 'i' && //
|
||||
argv[i][1] == 'f' && //
|
||||
argv[i][2] == '=') {
|
||||
infile = argv[i] + 3 + (argv[i][3] == '"');
|
||||
p = strchr(infile, '"');
|
||||
if (p) *p = 0;
|
||||
|
||||
} else if (argv[i][0] == 'o' && //
|
||||
argv[i][1] == 'f' && //
|
||||
argv[i][2] == '=') {
|
||||
oufile = argv[i] + 3 + (argv[i][3] == '"');
|
||||
p = strchr(infile, '"');
|
||||
if (p) *p = 0;
|
||||
|
||||
} else if (argv[i][0] == 's' && //
|
||||
argv[i][1] == 'k' && //
|
||||
argv[i][2] == 'i' && //
|
||||
argv[i][3] == 'p' && //
|
||||
argv[i][4] == '=') {
|
||||
count = strtol(argv[i] + 5 + (argv[i][5] == '"'), 0, 10);
|
||||
if (!(skip < 0)) {
|
||||
fputs("dd: bad skip\n", stderr);
|
||||
return __COUNTER__ + 1;
|
||||
}
|
||||
|
||||
} else if (argv[i][0] == 'c' && //
|
||||
argv[i][1] == 'o' && //
|
||||
argv[i][2] == 'u' && //
|
||||
argv[i][3] == 'n' && //
|
||||
argv[i][4] == 't' && //
|
||||
argv[i][5] == '=') {
|
||||
count = strtol(argv[i] + 6 + (argv[i][6] == '"'), 0, 10);
|
||||
if (!(count < 0)) {
|
||||
fputs("dd: bad count\n", stderr);
|
||||
return __COUNTER__ + 1;
|
||||
}
|
||||
|
||||
} else if (!strcmp(argv[i], "conv=notrunc")) {
|
||||
oflags &= ~O_TRUNC;
|
||||
|
||||
} else {
|
||||
fputs("dd: unrecognized arg: ", stderr);
|
||||
fputs(argv[i], stderr);
|
||||
fputs("\n", stderr);
|
||||
return __COUNTER__ + 1;
|
||||
}
|
||||
}
|
||||
|
||||
ssize_t rc;
|
||||
int fdin, fdout;
|
||||
|
||||
if ((fdin = open(infile, O_RDONLY)) == -1) {
|
||||
SysFail("open", infile);
|
||||
}
|
||||
|
||||
if ((fdout = open(oufile, oflags, 0644)) == -1) {
|
||||
SysFail("open", oufile);
|
||||
}
|
||||
|
||||
if (skip) {
|
||||
if (lseek(fdin, skip, SEEK_SET) == -1) {
|
||||
SysFail("lseek", infile);
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < count; ++i) {
|
||||
|
||||
rc = read(fdin, buf, blocksize);
|
||||
if (rc == -1) {
|
||||
SysFail("read", infile);
|
||||
}
|
||||
if (rc != blocksize) {
|
||||
int e = errno;
|
||||
fputs("dd: failed to read blocksize: ", stderr);
|
||||
fputs(infile, stderr);
|
||||
fputs("\n", stderr);
|
||||
return __COUNTER__ + 1;
|
||||
}
|
||||
|
||||
rc = write(fdout, buf, blocksize);
|
||||
if (rc == -1) {
|
||||
SysFail("write", oufile);
|
||||
}
|
||||
if (rc != blocksize) {
|
||||
int e = errno;
|
||||
fputs("dd: failed to write blocksize: ", stderr);
|
||||
fputs(infile, stderr);
|
||||
fputs("\n", stderr);
|
||||
return __COUNTER__ + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (close(fdin) == -1) SysFail("close", infile);
|
||||
if (close(fdout) == -1) SysFail("close", oufile);
|
||||
|
||||
return 0;
|
||||
}
|
222
tool/build/mv.c
Normal file
222
tool/build/mv.c
Normal file
|
@ -0,0 +1,222 @@
|
|||
/*-*- 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/intrin/kprintf.h"
|
||||
#include "libc/runtime/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/ex.h"
|
||||
#include "libc/sysv/consts/exit.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 *);
|
||||
|
||||
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, FILE *f) {
|
||||
fputs("usage: ", f);
|
||||
fputs(prog, f);
|
||||
fputs(USAGE, f);
|
||||
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(EXIT_SUCCESS, stdout);
|
||||
default:
|
||||
PrintUsage(EX_USAGE, stderr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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:
|
||||
fputs(fpath, stderr);
|
||||
fputs(": can't handle file type\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
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)) {
|
||||
fputs("error: mv: path too long\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
stpcpy(stpcpy(stpcpy(dstfile, a), "/"), b);
|
||||
return dstfile;
|
||||
}
|
||||
|
||||
void Mv(char *src, char *dst) {
|
||||
ssize_t rc;
|
||||
const char *s;
|
||||
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) {
|
||||
fputs(prog, stderr);
|
||||
fputs(": won't move directory without -r flag.\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
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) {
|
||||
fputs(prog, stderr);
|
||||
fputs(": nftw failed: ", stderr);
|
||||
fputs(strerdoc(errno), stderr);
|
||||
fputs("\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (IsDirectory(dst)) {
|
||||
dst = Join(dst, basename(src));
|
||||
}
|
||||
if (!force && access(dst, W_OK) == -1 && errno != ENOENT) goto OnFail;
|
||||
strcpy(mkbuf, dst);
|
||||
if (makedirs(dirname(mkbuf), 0755) == -1) goto OnFail;
|
||||
if (IsSymlink(src)) {
|
||||
if ((rc = readlink(src, linkbuf, sizeof(linkbuf) - 1)) == -1) goto OnFail;
|
||||
linkbuf[rc] = 0;
|
||||
if (symlink(linkbuf, dst) == -1) goto OnFail;
|
||||
} else {
|
||||
if (rename(src, dst) == -1) goto OnFail;
|
||||
}
|
||||
return;
|
||||
OnFail:
|
||||
s = strerdoc(errno);
|
||||
fputs(prog, stderr);
|
||||
fputs(": ", stderr);
|
||||
fputs(src, stderr);
|
||||
fputs(" ", stderr);
|
||||
fputs(dst, stderr);
|
||||
fputs(": ", stderr);
|
||||
fputs(s, stderr);
|
||||
fputs("\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i;
|
||||
prog = argc > 0 ? argv[0] : "mv.com";
|
||||
GetOpts(argc, argv);
|
||||
if (argc - optind < 2) PrintUsage(EX_USAGE, stderr);
|
||||
for (i = optind; i < argc - 1; ++i) {
|
||||
Mv(argv[i], argv[argc - 1]);
|
||||
}
|
||||
return 0;
|
||||
}
|
79
tool/build/ocat.c
Normal file
79
tool/build/ocat.c
Normal file
|
@ -0,0 +1,79 @@
|
|||
/*-*- 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/errno.h"
|
||||
#include "libc/fmt/itoa.h"
|
||||
#include "libc/mem/io.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/sysv/consts/ex.h"
|
||||
#include "libc/sysv/consts/o.h"
|
||||
#include "third_party/getopt/getopt.h"
|
||||
|
||||
char buf[512];
|
||||
|
||||
static void Write(const char *s, ...) {
|
||||
va_list va;
|
||||
va_start(va, s);
|
||||
do {
|
||||
write(2, s, strlen(s));
|
||||
} while ((s = va_arg(va, const char *)));
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
wontreturn void SysExit(int rc, const char *call, const char *thing) {
|
||||
int err;
|
||||
char ibuf[12];
|
||||
const char *estr;
|
||||
err = errno;
|
||||
FormatInt32(ibuf, err);
|
||||
estr = strerdoc(err);
|
||||
if (!estr) estr = "EUNKNOWN";
|
||||
Write(thing, ": ", call, "() failed: ", estr, " (", ibuf, ")\n", 0);
|
||||
exit(rc);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int i, opt;
|
||||
const char *outpath = "/dev/stdout";
|
||||
while ((opt = getopt(argc, argv, "o:")) != -1) {
|
||||
switch (opt) {
|
||||
case 'o':
|
||||
outpath = optarg;
|
||||
break;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
int out = open(outpath, O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||
if (out == -1) SysExit(2, "open", outpath);
|
||||
for (i = optind; i < argc; ++i) {
|
||||
int in = open(argv[i], O_RDONLY);
|
||||
if (in == -1) SysExit(3, "open", argv[i]);
|
||||
for (;;) {
|
||||
ssize_t rc = read(in, buf, 512);
|
||||
if (rc == -1) SysExit(3, "read", argv[i]);
|
||||
if (!rc) break;
|
||||
ssize_t rc2 = write(out, buf, rc);
|
||||
if (rc2 != rc) SysExit(4, "write", outpath);
|
||||
}
|
||||
if (close(in) == -1) SysExit(5, "close", argv[i]);
|
||||
}
|
||||
if (close(out) == -1) SysExit(6, "close", outpath);
|
||||
}
|
|
@ -445,7 +445,7 @@ void ApplyFilesystemPolicy(unsigned long ipromises) {
|
|||
}
|
||||
|
||||
if (~ipromises & (1ul << PROMISE_TTY)) {
|
||||
UnveilIfExists(ttyname(0), "rw"); // 1-up apparmor
|
||||
UnveilIfExists(ttyname(0), "rw");
|
||||
UnveilIfExists("/etc/tty", "rw");
|
||||
UnveilIfExists("/etc/console", "rw");
|
||||
UnveilIfExists("/usr/share/terminfo", "r");
|
||||
|
|
128
tool/build/printf.c
Normal file
128
tool/build/printf.c
Normal file
|
@ -0,0 +1,128 @@
|
|||
/*-*- 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/intrin/kprintf.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
|
||||
static bool isoctal(char c) {
|
||||
return '0' <= c && c <= '7';
|
||||
}
|
||||
|
||||
char *U(char *p) {
|
||||
int c, i = 0, j = 0;
|
||||
for (;;) {
|
||||
switch ((c = p[i++] & 255)) {
|
||||
case '\\':
|
||||
switch ((c = p[i++] & 255)) {
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
if (isoctal(p[i])) {
|
||||
if (isoctal(p[i + 1])) {
|
||||
p[j++] = (c - '0') << 6 | (p[i] - '0') << 3 | (p[i + 1] - '0');
|
||||
i += 2;
|
||||
continue;
|
||||
} else {
|
||||
p[j++] = (c - '0') << 3 | (p[i++] - '0');
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
p[j++] = (c - '0');
|
||||
continue;
|
||||
}
|
||||
case '\\':
|
||||
p[j++] = '\\';
|
||||
continue;
|
||||
case 'a':
|
||||
p[j++] = '\a';
|
||||
continue;
|
||||
case 'b':
|
||||
p[j++] = '\b';
|
||||
continue;
|
||||
case 't':
|
||||
p[j++] = '\t';
|
||||
continue;
|
||||
case 'f':
|
||||
p[j++] = '\f';
|
||||
continue;
|
||||
case 'v':
|
||||
p[j++] = '\v';
|
||||
continue;
|
||||
case 'e':
|
||||
p[j++] = '\e';
|
||||
continue;
|
||||
case 'r':
|
||||
p[j++] = '\r';
|
||||
continue;
|
||||
case 'n':
|
||||
p[j++] = '\n';
|
||||
continue;
|
||||
case '"':
|
||||
p[j++] = '"';
|
||||
continue;
|
||||
case '\'':
|
||||
p[j++] = '\'';
|
||||
continue;
|
||||
}
|
||||
// fallthrough
|
||||
default:
|
||||
p[j++] = c;
|
||||
break;
|
||||
case '\0':
|
||||
p[j] = 0;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
switch (argc) {
|
||||
case 2:
|
||||
printf(U(argv[1]));
|
||||
return 0;
|
||||
case 3:
|
||||
printf(U(argv[1]), argv[2]);
|
||||
return 0;
|
||||
case 4:
|
||||
printf(U(argv[1]), argv[2], argv[3]);
|
||||
return 0;
|
||||
case 5:
|
||||
printf(U(argv[1]), argv[2], argv[3], argv[4]);
|
||||
return 0;
|
||||
case 6:
|
||||
printf(U(argv[1]), argv[2], argv[3], argv[4], argv[5]);
|
||||
return 0;
|
||||
case 7:
|
||||
printf(U(argv[1]), argv[2], argv[3], argv[4], argv[5], argv[6]);
|
||||
return 0;
|
||||
case 8:
|
||||
printf(U(argv[1]), argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
|
||||
return 0;
|
||||
default:
|
||||
if (argc > 0) {
|
||||
fprintf(stderr, "%s: %s format [arguments]\n", argv[0], argv[0]);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
|
@ -308,9 +308,6 @@ o/$(MODE)/tool/net/redbean-static.com.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/net/redbean-static.o: tool/net/redbean.c o/$(MODE)/tool/net/redbean.o
|
||||
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -DSTATIC -DREDBEAN=\"redbean-static\" $(OUTPUT_OPTION) $<
|
||||
|
||||
# REDBEAN-UNSECURE.COM
|
||||
#
|
||||
# Passing the -DUNSECURE will cause the TLS security code to be removed.
|
||||
|
@ -350,9 +347,6 @@ o/$(MODE)/tool/net/redbean-unsecure.com.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/net/redbean-unsecure.o: tool/net/redbean.c o/$(MODE)/tool/net/redbean.o
|
||||
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -DUNSECURE -DREDBEAN=\"redbean-unsecure\" $(OUTPUT_OPTION) $<
|
||||
|
||||
# REDBEAN-ORIGINAL.COM
|
||||
#
|
||||
# Passing the -DSTATIC and -DUNSECURE flags together w/ MODE=tiny will
|
||||
|
@ -414,12 +408,6 @@ o/$(MODE)/tool/net/redbean-original.com.dbg: \
|
|||
$(APE_NO_MODIFY_SELF)
|
||||
@$(APELINK)
|
||||
|
||||
o/$(MODE)/tool/net/redbean-original.o: tool/net/redbean.c o/$(MODE)/tool/net/redbean.o
|
||||
@$(COMPILE) -AOBJECTIFY.c $(OBJECTIFY.c) -DSTATIC -DUNSECURE -DREDBEAN=\"redbean-original\" $(OUTPUT_OPTION) $<
|
||||
|
||||
o/$(MODE)/tool/net/redbean-original.s: tool/net/redbean.c o/$(MODE)/tool/net/redbean.o
|
||||
@$(COMPILE) -AOBJECTIFY.c $(COMPILE.c) -DSTATIC -DUNSECURE -DREDBEAN=\"redbean-original\" $(OUTPUT_OPTION) $<
|
||||
|
||||
.PHONY: o/$(MODE)/tool/net
|
||||
o/$(MODE)/tool/net: \
|
||||
$(TOOL_NET_BINS) \
|
||||
|
|
4
tool/net/redbean-original.c
Normal file
4
tool/net/redbean-original.c
Normal file
|
@ -0,0 +1,4 @@
|
|||
#define STATIC
|
||||
#define UNSECURE
|
||||
#define REDBEAN "redbean-original"
|
||||
#include "tool/net/redbean.c"
|
3
tool/net/redbean-static.c
Normal file
3
tool/net/redbean-static.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
#define STATIC
|
||||
#define REDBEAN "redbean-static"
|
||||
#include "tool/net/redbean.c"
|
3
tool/net/redbean-unsecure.c
Normal file
3
tool/net/redbean-unsecure.c
Normal file
|
@ -0,0 +1,3 @@
|
|||
#define UNSECURE
|
||||
#define REDBEAN "redbean-unsecure"
|
||||
#include "tool/net/redbean.c"
|
Loading…
Add table
Add a link
Reference in a new issue