Make examples folder somewhat more focused

This commit is contained in:
Justine Tunney 2021-03-05 06:09:12 -08:00
parent 5141d00992
commit e26bdbec52
29 changed files with 133 additions and 1225 deletions

View file

@ -7,88 +7,23 @@
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/bits/pushpop.h"
#include "libc/calls/calls.h"
#include "libc/calls/ucontext.h"
#include "libc/log/log.h"
#include "libc/math.h"
#include "libc/nt/enum/errormodeflags.h"
#include "libc/nt/signals.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
/**
* @fileoverview Demonstrates SEGFAULT via NULL pointer dereference.
* @fileoverview How to print backtraces and cpu state on crash.
*
* In MODE=dbg mode, UBSAN should catch this and display a backtrace.
* In most other modes, e.g. MODE=balanced, the oncrash.c sig handler
* will either launch GDB or display a very fancy report.
*
* make -j8 -O MODE=opt o/opt/examples/crashreportdemo.com
* o/opt/examples/crashreportdemo.com
* make -j8 -O o//examples/crashreport.com
* o//examples/crashreport.com
*
* To prevent the GDB GUI from popping up:
*
* export GDB=""
* make -j8 -O MODE=opt o/opt/examples/crashreportdemo.com
* o/opt/examples/crashreportdemo.com
* export GDB=
* make -j8 -O o//examples/crashreport.com
* o//examples/crashreport.com
*/
int boo;
void boop(void);
int main(int argc, char *argv[]) {
int64_t res;
volatile int64_t x;
showcrashreports();
res = 0;
asm volatile("mov\t$0x1111111111111111,%rax\n\t"
"mov\t$0x3333333333333333,%rcx\n\t"
"mov\t$0x4444444444444444,%rdx\n\t"
"mov\t$0x8888888888888888,%r8\n\t"
"mov\t$0x9999999999999999,%r9d\n\t"
"mov\t$0xaaaaaaaaaaaaaaaa,%r10d\n\t"
"mov\t$0xbbbbbbbbbbbbbbbb,%r11d\n\t"
"mov\t$0x0000000000000000,%rax\n\t"
"movd\t%rax,%xmm0\n\t"
"mov\t$0x1111111111111111,%rax\n\t"
"push\t%rax\n\t"
"push\t%rax\n\t"
"movdqu\t(%rsp),%xmm1\n\t"
"pop\t%rax\n\t"
"pop\t%rax\n\t"
"mov\t$0x2222222222220022,%rax\n\t"
"movd\t%rax,%xmm2\n\t"
"mov\t$0x3333333333333333,%rax\n\t"
"movd\t%rax,%xmm3\n\t"
"mov\t$0x4444444444444444,%rax\n\t"
"movd\t%rax,%xmm4\n\t"
"mov\t$0x5555555555555555,%rax\n\t"
"movd\t%rax,%xmm5\n\t"
"mov\t$0x6666666666666666,%rax\n\t"
"movd\t%rax,%xmm6\n\t"
"mov\t$0x7777777777777777,%rax\n\t"
"movd\t%rax,%xmm7\n\t"
"mov\t$0x8888888888888888,%rax\n\t"
"movd\t%rax,%xmm8\n\t"
"mov\t$0x9999999999999999,%rax\n\t"
"movd\t%rax,%xmm9\n\t"
"mov\t$0xaaaaaaaaaaaaaaaa,%rax\n\t"
"movd\t%rax,%xmm10\n\t"
"mov\t$0xbbbbbbbbbbbbbbbb,%rax\n\t"
"movd\t%rax,%xmm11\n\t"
"mov\t$0xcccccccccccccccc,%rax\n\t"
"movd\t%rax,%xmm12\n\t"
"mov\t$0xdddddddddddddddd,%rax\n\t"
"movd\t%rax,%xmm13\n\t"
"mov\t$0xeeeeeeeeeeeeeeee,%rax\n\t"
"movd\t%rax,%xmm14\n\t"
"mov\t$0xffffffffffffffff,%rax\n\t"
"movd\t%rax,%xmm15\n\t"
"fldpi\n\t"
"xor\t%eax,%eax\n\t"
"div\t%eax\n\t");
return res;
return 1 / (x = 0);
}

View file

@ -32,9 +32,9 @@ int main(int argc, char *argv[]) {
ssize_t rc;
size_t got, wrote;
unsigned char buf[512];
struct sigaction saint = {.sa_handler = GotCtrlC};
fprintf(stderr, "This echos stdio until Ctrl+C is pressed.\n");
CHECK_NE(
-1, sigaction(SIGINT, &(struct sigaction){.sa_handler = GotCtrlC}, NULL));
CHECK_NE(-1, sigaction(SIGINT, &saint, NULL));
for (;;) {
rc = read(0, buf, BUFSIZ);
if (rc != -1) {

View file

@ -1,376 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "dsp/core/core.h"
#include "dsp/core/twixt8.h"
#include "dsp/scale/scale.h"
#include "dsp/tty/itoa8.h"
#include "dsp/tty/quant.h"
#include "libc/alg/arraylist2.internal.h"
#include "libc/calls/calls.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/struct/winsize.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/macros.internal.h"
#include "libc/math.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "libc/sysv/consts/termios.h"
#include "libc/x/x.h"
#include "third_party/stb/stb_image.h"
/**
* Hat tips go out to Hornet, CRTC, DESiRE, Hans Petter Jansson, Guihua
* Cui, Ming Ronnier Luoand anyone else Mister Rogers would describe as
* the The Helpers Of This World who've helped us learn more about the
* cool yet exceedingly difficult things possible to do with graphics.
*/
#define DIN99 1
#define GAMMA 4.1
#define GAMMA_A .05
#define GAMMA_B 50.
#define GAMMA_C .055
#define COLORSPACE_SRC kSrgbD65ToXyzD50
#define COLORSPACE_TTY kSrgbD65ToXyzD50
#define SQR(X) ((X) * (X))
#define DEG(X) ((X)*M_PI / 180)
const double kXyzKappa = 24389 / 27.;
const double kXyzEpsilon = 216 / 24389.;
const double kSrgbD65ToXyzD50[3][3] = {
{.4360747, .3850649, .1430804},
{.2225045, .7168786, .0606169},
{.0139322, .0971045, .7141733},
};
const double kCieRgbEToXyz[3][3] = {
{.4887180, .3106803, .2006017},
{.1762044, .8129847, .0108109},
{.0000000, .0102048, .9897952},
};
const unsigned char kShadePct[] = {0300, 0200, 0100};
const unsigned short kShadeRune[] = {u'', u'', u''};
const unsigned char kAnsiTango[16][3] = {
{46, 52, 54}, {204, 0, 0}, {78, 154, 6}, {196, 160, 0},
{52, 101, 164}, {117, 80, 123}, {6, 152, 154}, {211, 215, 207},
{85, 87, 83}, {239, 41, 41}, {138, 226, 52}, {252, 233, 79},
{114, 159, 207}, {173, 127, 168}, {52, 226, 226}, {238, 238, 236},
};
const unsigned char kCga[16][3] = {
{0, 0, 0}, {170, 0, 0}, {0, 170, 0}, {170, 85, 0},
{0, 0, 170}, {170, 0, 170}, {0, 170, 170}, {170, 170, 170},
{85, 85, 85}, {255, 85, 85}, {85, 255, 85}, {255, 255, 85},
{85, 85, 255}, {255, 85, 255}, {85, 255, 255}, {255, 255, 255},
};
struct Blends {
size_t i, n;
struct Blend {
unsigned char color[4];
unsigned char bg, fg;
unsigned short rune;
} * p;
} blends;
static double Gamma(double x) {
if (x < GAMMA_A) return x / GAMMA_B;
return pow(1 / (1 + GAMMA_C) * (x + GAMMA_C), GAMMA);
}
static void *LoadRgb(double rgb[3], const unsigned char pix[3]) {
rgb[0] = Gamma(1. / 255 * pix[0]);
rgb[1] = Gamma(1. / 255 * pix[1]);
rgb[2] = Gamma(1. / 255 * pix[2]);
return rgb;
}
static double Lab(double x) {
return x > kXyzEpsilon ? cbrtf(x) : (kXyzKappa * x + 16) / 116;
}
static void XyzToLab(double lab[3], const double d50xyz[3]) {
double Y, L, a, b;
Y = Lab(d50xyz[1]);
L = 116 * Y - 16;
a = 500 * (Lab(d50xyz[0]) - Y);
b = 200 * (Y - Lab(d50xyz[2]));
lab[0] = L, lab[1] = a, lab[2] = b;
}
static void XyzToDin99d(unsigned char din99d[3], const double d50xyz[3]) {
double e, f, G, C, h, xyz[3], lab[3];
memcpy(xyz, d50xyz, sizeof(xyz));
xyz[0] = 1.12f * xyz[0] - .12f * xyz[2];
XyzToLab(lab, xyz);
e = (+lab[1] * cosf(DEG(50)) + lab[2] * sinf(DEG(50)));
f = (-lab[1] * sinf(DEG(50)) + lab[2] + cosf(DEG(50))) * 1.14;
G = sqrtf(SQR(e) + SQR(f));
C = logf(1 + .06 * G) * 22.5f;
h = atan2f(f, e) + DEG(50);
h = fmodf(h, DEG(360));
if (h < 0) h += DEG(360);
din99d[0] = MIN(255, MAX(0, 325.22f * logf(1 + .0036 * lab[0]) * 2.5f));
din99d[1] = MIN(255, MAX(0, C * cos(h) * 2.5f + 128));
din99d[2] = MIN(255, MAX(0, C * sin(h) * 2.5f + 128));
}
static void AddColor(int bg, int fg, int rune, int r, int g, int b, int x) {
int i;
if (blends.i) {
for (i = blends.i; --i;) {
if (blends.p[i].color[0] == r && blends.p[i].color[1] == g &&
blends.p[i].color[2] == b) {
return;
}
}
}
CHECK_NE(
-1,
APPEND(&blends.p, &blends.i, &blends.n,
(&(struct Blend){
.bg = bg, .fg = fg, .rune = rune, .color = {r, g, b, x}})));
}
static void AddBlend(int bgxterm, int fgxterm, int rune, int shade,
const unsigned char bgrgb[4],
const unsigned char fgrgb[4]) {
AddColor(bgxterm, fgxterm, rune, twixt8(bgrgb[0], fgrgb[0], shade),
twixt8(bgrgb[1], fgrgb[1], shade), twixt8(bgrgb[2], fgrgb[2], shade),
0);
}
static void MakeBlends(const unsigned char palette[16][3]) {
int r, i, j, k;
double rgb[3], xyz[3];
unsigned char tab[256][4];
unsigned char cube[6] = {0, 0137, 0207, 0257, 0327, 0377};
unsigned char seqs[2][2] = {{16, 255}};
for (i = 0; i < 16; ++i) {
tab[i][0] = palette[i][0];
tab[i][1] = palette[i][1];
tab[i][2] = palette[i][2];
tab[i][3] = i;
}
for (i = 16; i < 232; ++i) {
tab[i][0] = cube[((i - 020) / 044) % 6];
tab[i][1] = cube[((i - 020) / 6) % 6];
tab[i][2] = cube[(i - 020) % 6];
tab[i][3] = i;
}
for (i = 232; i < 256; ++i) {
tab[i][0] = 8 + (i - 232) * 10;
tab[i][1] = 8 + (i - 232) * 10;
tab[i][2] = 8 + (i - 232) * 10;
tab[i][3] = i;
}
#if DIN99
for (i = 0; i < 256; ++i) {
LoadRgb(rgb, tab[i]);
vmatmul3(xyz, rgb, COLORSPACE_TTY);
XyzToDin99d(tab[i], xyz);
}
#endif
for (r = 0; r < ARRAYLEN(seqs); ++r) {
for (i = seqs[r][0]; i <= seqs[r][1]; ++i) {
for (j = seqs[r][0]; j <= seqs[r][1]; ++j) {
if (i == j) {
AddColor(i, 0, 0, tab[i][0], tab[i][1], tab[i][2], 0);
} else {
for (k = 0; k < ARRAYLEN(kShadeRune); ++k) {
AddBlend(i, j, kShadeRune[k], kShadePct[k], tab[i], tab[j]);
AddBlend(j, i, kShadeRune[k], kShadePct[k], tab[j], tab[i]);
}
}
}
}
}
}
static int PickColor(unsigned char color[3]) {
int i, best, least, dist;
best = -1;
least = INT_MAX;
for (i = 0; i < blends.i; ++i) {
dist = SQR(blends.p[i].color[0] - color[0]) +
SQR(blends.p[i].color[1] - color[1]) +
SQR(blends.p[i].color[2] - color[2]);
if (dist < least) {
least = dist;
best = i;
}
}
return best;
}
static void SetBg(int x) {
if (0 <= x && x < 16) {
if (x < 8) {
x += 40;
} else {
x -= 8;
x *= 100;
}
printf("\e[%dm", x);
} else {
/* assert(false); */
printf("\e[48;5;%dm", x);
}
}
static void SetFg(int x) {
if (0 <= x && x < 16) {
if (x < 8) {
x += 30;
} else {
x -= 8;
x *= 90;
}
printf("\e[%dm", x);
} else {
/* assert(false); */
printf("\e[38;5;%dm", x);
}
}
static void PrintImage(long yn, long xn, unsigned char src[4][yn][xn]) {
int y, x, c;
double rgb[3], xyz[3];
unsigned char din99d[3];
for (y = 0; y < yn; ++y) {
printf("\n");
for (x = 0; x < xn; ++x) {
din99d[0] = src[0][y][x];
din99d[1] = src[1][y][x];
din99d[2] = src[2][y][x];
#if DIN99
LoadRgb(rgb, din99d);
vmatmul3(xyz, rgb, COLORSPACE_SRC);
XyzToDin99d(din99d, xyz);
#endif
c = PickColor(din99d);
if (blends.p[c].rune) {
SetBg(blends.p[c].bg);
SetFg(blends.p[c].fg);
printf("%hc", blends.p[c].rune);
} else {
SetBg(blends.p[c].bg);
printf(" ");
}
}
printf("\e[0m");
}
printf("\r");
}
static void *Deinterlace(long dcn, long dyn, long dxn,
unsigned char dst[dcn][dyn][dxn], long syw, long sxw,
long scw, const unsigned char src[syw][sxw][scw],
long syn, long sxn, long sy0, long sx0, long sc0) {
long y, x, c;
for (y = 0; y < dyn; ++y) {
for (x = 0; x < dxn; ++x) {
for (c = 0; c < dcn; ++c) {
dst[c][y][x] = src[sy0 + y][sx0 + x][sc0 + c];
}
}
}
return dst;
}
static void GetTermSize(int out_rows[1], int out_cols[1]) {
struct winsize ws;
ws.ws_row = 24;
ws.ws_col = 80;
if (ioctl(STDIN_FILENO, TIOCGWINSZ, &ws) == -1) {
ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws);
}
out_rows[0] = ws.ws_row;
out_cols[0] = ws.ws_col;
}
static void ProcessImage(long syn, long sxn, unsigned char img[syn][sxn][4]) {
int dyn, dxn;
double ry, rx;
GetTermSize(&dyn, &dxn);
ry = syn, rx = sxn;
ry /= dyn, rx /= dxn;
PrintImage(
dyn, dxn,
EzGyarados(4, dyn, dxn, gc(xmemalign(32, dyn * dxn * 4)), 4, syn, sxn,
Deinterlace(4, syn, sxn, gc(xmemalign(32, syn * sxn * 4)), syn,
sxn, 4, img, syn, sxn, 0, 0, 0),
0, 4, dyn, dxn, syn, sxn, ry, rx, 0, 0));
}
static void WithImageFile(const char *path,
void fn(long yn, long xn,
unsigned char src[yn][xn][4])) {
struct stat st;
void *map, *data;
int fd, yn, xn, cn;
CHECK_NE(-1, (fd = open(path, O_RDONLY)), "%s", path);
CHECK_NE(-1, fstat(fd, &st));
CHECK_GT(st.st_size, 0);
CHECK_LE(st.st_size, INT_MAX);
CHECK_NE(MAP_FAILED,
(map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0)));
CHECK_NOTNULL(
(data = stbi_load_from_memory(map, st.st_size, &xn, &yn, NULL, 4)), "%s",
path);
CHECK_NE(-1, munmap(map, st.st_size));
CHECK_NE(-1, close(fd));
fn(yn, xn, data);
free(data);
}
static void PrintData(void) {
int i;
for (i = 0; i < blends.i; ++i) {
printf("%3d %3d %3d %3d %3d %d\n", blends.p[i].color[0],
blends.p[i].color[1], blends.p[i].color[2], blends.p[i].bg,
blends.p[i].fg, blends.p[i].rune);
}
}
void *OpenRgbMap(const char *path) {
int fd;
void *map;
size_t size;
size = 256 * 256 * 256 * 3;
CHECK_NE(-1, (fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644)));
CHECK_NE(-1, ftruncate(fd, size));
CHECK_NE(MAP_FAILED,
(map = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)));
CHECK_NE(-1, close(fd));
return map;
}
int main(int argc, char *argv[]) {
int i;
MakeBlends(kCga);
for (i = 1; i < argc; ++i) {
WithImageFile(argv[i], ProcessImage);
}
return 0;
}

View file

@ -1,44 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/fmt/itoa.h"
#include "libc/macros.internal.h"
#include "libc/runtime/e820.internal.h"
#include "libc/runtime/pc.internal.h"
#include "libc/stdio/stdio.h"
const char *DescribeMemoryType(int type) {
switch (type) {
case kMemoryUsable:
return "kMemoryUsable";
case kMemoryUnusable:
return "kMemoryUnusable";
case kMemoryAcpiReclaimable:
return "kMemoryAcpiReclaimable";
case kMemoryAcpiNvs:
return "kMemoryAcpiNvs";
case kMemoryBad:
return "kMemoryBad";
default:
return "UNKNOWN";
}
}
int main(int argc, char *argv[]) {
int i;
const char *ts;
struct mman *mm;
mm = (struct mman *)(BANE + 0x0500);
for (i = 0; i < mm->e820n; ++i) {
printf("%p-%p %s\n", mm->e820[i].addr, mm->e820[i].size,
DescribeMemoryType(mm->e820[i].type));
}
for (;;) asm("pause");
return 0;
}

View file

@ -1,17 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/runtime/runtime.h"
int main(int argc, char *argv[]) {
if (argc < 2) return 1;
execv(argv[1], argv + 1);
abort();
}

View file

@ -1,66 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/itimerval.h"
#include "libc/fmt/conv.h"
#include "libc/limits.h"
#include "libc/log/check.h"
#include "libc/log/color.internal.h"
#include "libc/log/log.h"
#include "libc/math.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/itimer.h"
#include "libc/sysv/consts/sig.h"
#include "libc/time/time.h"
static const struct itimerval kTimer = {{0, 10000}, {0, 10000}}; /* 10ms */
static volatile bool32 showprogress_;
static const char *magic_;
void OnInterrupt(int sig) {
showprogress_ = true;
}
void ShowProgress(uintmax_t i, uintmax_t j, uintmax_t n) {
fprintf(stderr, "%s%,40jd%,40jd%,40jd\r\n", magic_, i, j, n);
showprogress_ = false;
}
static bool IsPrime(uintmax_t i) {
uintmax_t j, n;
for (j = 3, n = (unsigned long)floorl(sqrtl(i)); j <= n; j += 2) {
if (showprogress_) ShowProgress(i, j, n);
if (i % j == 0) return false;
}
return true;
}
static void TryNumber(uintmax_t x, uintmax_t i) {
if (IsPrime(i)) {
printf("%s%,jd%s%,jd\n", "the prime nearest ", x, " is ", i);
exit(0);
}
}
int main(int argc, char **argv) {
uintmax_t x, i, j;
CHECK_EQ(2, argc);
signal(SIGALRM, OnInterrupt);
setitimer(ITIMER_REAL, &kTimer, NULL);
magic_ = cancolor() ? "\e[F\e[K" : "";
if ((x = strtoumax(argv[1], NULL, 0)) % 2 == 0) ++x;
TryNumber(x, x);
for (i = x, j = x;;) {
if (i < UINTMAX_MAX) TryNumber(x, i), i += 2;
if (j > 1) TryNumber(x, j), j -= 2;
}
}

View file

@ -1,18 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/stdio/stdio.h"
int main(int argc, char *argv[]) {
int pid;
pid = fork();
fprintf(stderr, "fork returned %d\n", pid);
return 0;
}

View file

@ -7,9 +7,73 @@
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/dce.h"
#include "libc/nexgen32e/nt2sysv.h"
#include "libc/nt/dll.h"
#include "libc/nt/enum/cw.h"
#include "libc/nt/enum/mb.h"
#include "libc/nt/enum/sw.h"
#include "libc/nt/enum/wm.h"
#include "libc/nt/enum/ws.h"
#include "libc/nt/events.h"
#include "libc/nt/messagebox.h"
#include "libc/nt/struct/msg.h"
#include "libc/nt/struct/wndclass.h"
#include "libc/nt/windows.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
/**
* @fileoverview GUI Example
*
* Cosmopolitan has had a razor sharp focus on making sockets and stdio
* work well across platforms. When you call a function like read(), it
* will just work on platforms like Windows too. In order to do that we
* need to support the WIN32 API, which you can use, but please note it
* isn't polyfilled on other platforms in the same way as POSIX APIs.
*/
static const char16_t kClassName[] = u"cosmopolitan";
static int64_t WindowProc(int64_t hwnd, uint32_t uMsg, uint64_t wParam,
int64_t lParam) {
switch (uMsg) {
case kNtWmDestroy:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
static void Gui(void) {
int64_t h;
struct NtMsg msg;
struct NtWndClass wc;
memset(&wc, 0, sizeof(wc));
wc.lpfnWndProc = NT2SYSV(WindowProc);
wc.hInstance = GetModuleHandle(NULL);
wc.lpszClassName = kClassName;
RegisterClass(&wc);
h = CreateWindowEx(0, kClassName, u"Hello Cosmopolitan",
kNtWsOverlappedwindow, kNtCwUsedefault, kNtCwUsedefault,
kNtCwUsedefault, kNtCwUsedefault, 0, 0, wc.hInstance, 0);
ShowWindow(h, kNtSwNormal);
/*
* NOTE: Cosmopolitan linker script is hard-coded to change the
* subsystem from CUI to GUI when GetMessage() is linked.
*/
while (GetMessage(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int main(int argc, char *argv[]) {
return MessageBox(0, u"hello world", u"cosmopolitan", kNtMbOk) ? 0 : 1;
if (!IsWindows()) {
fputs("Sorry! This GUI demo only runs on Windows\n", stderr);
fputs("https://github.com/jart/cosmopolitan/issues/57\n", stderr);
return 1;
}
Gui();
}

View file

@ -10,13 +10,6 @@
#include "libc/stdio/stdio.h"
int main() {
/*
* Cosmopolitan's "Hello World" demonstrates a best practice.
*
* Since we conditionally link heavyweight features based on which
* characters are present in the format string, it's a good idea to
* have that string consist solely of directives.
*/
printf("%s\n", "hello world");
return 0;
}

View file

@ -8,12 +8,9 @@
*/
#endif
#include "libc/errno.h"
#include "libc/runtime/gc.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/x/x.h"
#include "libc/stdio/stdio.h"
int main() {
dprintf(STDOUT_FILENO, "%s\n", gc(xasprintf("%'s", "(╯°□°)╯︵' ┻━┻")));
printf("%s \n", "hello world");
return errno;
}

View file

@ -10,7 +10,12 @@
#include "libc/errno.h"
#include "libc/stdio/stdio.h"
int main() {
printf("%s \n", "hello world");
int main(int argc, char *argv[]) {
printf("abcdefghijklmnopqrstuvwxyz "
"ABCDEFGHIJKLMNOPQRSTUVWXYZ "
"!@#$$%%^&*(){}%%* "
"0123456789 "
"%3d\n",
argc);
return errno;
}

View file

@ -1,80 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/bits/bits.h"
#include "libc/dce.h"
#include "libc/log/check.h"
#include "libc/nexgen32e/rdtsc.h"
#include "libc/nexgen32e/x86feature.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/clock.h"
#include "libc/sysv/consts/sig.h"
#include "libc/time/time.h"
#include "libc/x/x.h"
#include "third_party/gdtoa/gdtoa.h"
/**
* @fileoverview Measure CPU clock mystery constants.
* @note CPUID EAX=15h/16h is a joke
*/
const long double kInterval = 0.001L;
long double start_;
char dtoabuf_[3][32];
volatile bool isdone_;
void OnCtrlC(int sig) {
isdone_ = true;
}
long double now(void) {
return dtime(CLOCK_MONOTONIC);
}
long double GetSample(void) {
uint64_t tick1, tick2;
long double time1, time2;
time1 = now();
tick1 = rdtsc();
nanosleep(&(struct timespec){0, 100000}, NULL);
time2 = now();
tick2 = rdtsc();
return ((time2 - time1) * 1e9L) / (long double)(tick2 - tick1);
}
void MeasureNanosecondsPerAlwaysRunningTimerCycle(void) {
int i;
long double avg, samp, elapsed;
start_ = now();
for (i = 1, avg = 1.0L; !isdone_; ++i) {
samp = GetSample();
avg += (samp - avg) / i;
dsleep(kInterval);
elapsed = now() - start_;
g_xfmt_p(dtoabuf_[0], &avg, 15, 32, 0);
g_xfmt_p(dtoabuf_[1], &samp, 15, 32, 0);
g_xfmt_p(dtoabuf_[2], &elapsed, 15, 32, 0);
printf("1c = %sns (last=%sns spent=%ss)\n", dtoabuf_[0], dtoabuf_[1],
dtoabuf_[2]);
}
}
int main(int argc, char *argv[]) {
if (!X86_HAVE(INVTSC)) {
fprintf(stderr, "warning: html can reprogram your microcode\n");
}
if (X86_HAVE(HYPERVISOR)) {
fprintf(stderr, "warning: virtual machine rdtsc isn't great\n");
}
CHECK_NE(-1, xsigaction(SIGINT, OnCtrlC, 0, 0, NULL));
MeasureNanosecondsPerAlwaysRunningTimerCycle();
return 0;
}

View file

@ -1,19 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/mem/mem.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
int main(int argc, char *argv[]) {
char *s = malloc(1024 * 1024 * 2 + argc);
strcpy(s, "hello");
puts(s);
return 0;
}

View file

@ -1,50 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/macros.internal.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/prot.h"
/*
OpenBSD
00025ff61000 000000000000 65536
00026cf1d000 00000cfbc000 65536
000254d83000 ffffe7e66000 65536
0002c7bde000 000072e5b000 65536
000253ff9000 ffff8c41b000 65536
0002e96f3000 0000956fa000 65536
000236346000 ffff4cc53000 65536
0002ce744000 0000983fe000 65536
0002fe0b8000 00002f974000 65536
000225cd7000 ffff27c1f000 65536
*/
struct Mapping {
uint8_t *map;
size_t mapsize;
};
int main(int argc, char *argv[]) {
size_t i;
struct Mapping m[10];
for (i = 0; i < ARRAYLEN(m); ++i) {
m[i].mapsize = FRAMESIZE;
m[i].map = mmap(NULL, m[i].mapsize, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
}
for (i = 0; i < ARRAYLEN(m); ++i) {
printf("%p %p %zu\n", m[i].map, i ? m[i].map - m[i - 1].map : 0,
m[i].mapsize);
}
return 0;
}

View file

@ -1,39 +0,0 @@
/*-*- 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/fmt/conv.h"
#include "libc/runtime/interruptiblecall.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/sig.h"
struct InterruptibleCall icall;
static intptr_t peek(intptr_t *addr) {
return *addr;
}
int main(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; ++i) {
intptr_t addr = strtoul(argv[i], NULL, 0);
icall.sig = SIGSEGV;
printf("%#p → %#lx\n", addr,
interruptiblecall(&icall, (void *)peek, addr, 0, 0, 0));
}
return 0;
}

View file

@ -1,27 +0,0 @@
/*-*- 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/calls/calls.h"
#include "libc/stdio/stdio.h"
int main(int argc, char *argv[]) {
int pipefds[2] = {-1, -1};
int rc = pipe(pipefds);
printf("%d → %ld %ld\n", rc, pipefds[0], pipefds[1]);
return 0;
}

View file

@ -1,26 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/bits/bits.h"
#include "libc/macros.internal.h"
#include "libc/sock/sock.h"
#include "libc/stdio/stdio.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/poll.h"
int main(int argc, char *argv[]) {
int rc;
int pollms = 1;
struct pollfd fds[] = {{-1, 0}, {STDIN_FILENO, POLLIN}};
if ((rc = poll(fds, ARRAYLEN(fds), pollms)) != -1) {
printf("toto=%d\n", rc);
return 0;
}
return 0;
}

View file

@ -1,26 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "dsp/tty/tty.h"
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/time/time.h"
int main(int argc, char *argv[]) {
setenv("GDB", "nopenopenope", true);
showcrashreports();
ttyraw(kTtyLfToCrLf | kTtySigs);
printf("hi\n"), sleep(1);
if (argc > 1 && strstr(argv[1], "crash")) abort();
printf("there\n"), sleep(1);
return 0;
}

View file

@ -1,23 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/runtime/runtime.h"
int main(int argc, char *argv[]) {
int pid, wstatus;
if (argc < 2) return 1;
pid = fork();
if (!pid) {
execv(argv[1], argv + 1);
abort();
}
waitpid(pid, &wstatus, 0);
return WEXITSTATUS(wstatus);
}

View file

@ -1,18 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
int main(int argc, char *argv[]) {
struct stat st;
st.st_size = -1;
stat(argv[1], &st);
return st.st_size;
}

View file

@ -1,67 +0,0 @@
#if 0
/*─────────────────────────────────────────────────────────────────╗
To the extent possible under law, Justine Tunney has waived
all copyright and related or neighboring rights to this file,
as it is written in the following disclaimers:
http://unlicense.org/ │
http://creativecommons.org/publicdomain/zero/1.0/ │
*/
#endif
#include "libc/log/log.h"
#include "libc/runtime/runtime.h"
#include "libc/stdio/stdio.h"
#include "libc/calls/struct/utsname.h"
#include "libc/calls/calls.h"
#include "libc/sysv/consts/exit.h"
#include "third_party/getopt/getopt.h"
int main(int argc, char *argv[]) {
unsigned need = 0;
int opt;
while ((opt = getopt(argc, argv, "hasnrvmd")) != -1) {
switch (opt) {
case 'a':
need |= 0b111111;
break;
case 's':
need |= 0b000001;
break;
case 'n':
need |= 0b000010;
break;
case 'r':
need |= 0b000100;
break;
case 'v':
need |= 0b001000;
break;
case 'm':
need |= 0b010000;
break;
case 'd':
need |= 0b100000;
break;
default: /* -? or -h */
fprintf(stderr, "%s: %s [%s]\n%s", "Usage", argv[0], "FLAGS",
" -a\tprint all\n"
" -s\tprint sysname\n"
" -n\tprint nodename\n"
" -r\tprint release\n"
" -v\tprint version\n"
" -m\tprint machine\n"
" -d\tprint domainname\n");
exit(EXIT_FAILURE);
}
}
if (!need) need = 0b000001;
struct utsname data;
if (uname(&data) == -1) perror("uname"), exit(EXIT_FAILURE);
if (need & 0b000001) printf("%s ", data.sysname);
if (need & 0b000010) printf("%s ", data.nodename);
if (need & 0b000100) printf("%s ", data.release);
if (need & 0b001000) printf("%s ", data.version);
if (need & 0b010000) printf("%s ", data.machine);
if (need & 0b100000) printf("%s ", data.domainname);
printf("\n");
return 0;
}

View file

@ -16,6 +16,24 @@
/**
* @fileoverview x86 instruction length decoder by way of hex pipe.
*
* Here's an example of how you can use it to decode a NOP stream:
*
* $ make -j8 o//examples/x86split.com
* $ echo 909090 | o//examples/x86split.com
* 90
* 90
* 90
*
* If there was a XOR instruction in there, it'd do this:
*
* $ make -j8 o//examples/x86split.com
* $ echo 904531FF90 | o//examples/x86split.com
* 90 # NOP
* 4531FF # XOR R15D,R15D
* 90 # NOP
*
* Now that you're able to split x86 instructions the rest is easy.
*/
int fgethex(FILE *f) {

View file

@ -16,9 +16,7 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/nexgen32e/nexgen32e.h"
STATIC_YOINK("__errno_location"); /* needed by gdb */
#include "libc/errno.h"
/**
* Global variable for last error.
@ -33,3 +31,12 @@ STATIC_YOINK("__errno_location"); /* needed by gdb */
* @see __errno_location() stable abi
*/
int errno;
/**
* Returns address of errno.
*
* @note this is needed by gdb
*/
int *__errno_location(void) {
return &errno;
}

View file

@ -1,28 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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/macros.internal.h"
.source __FILE__
.keep.text # gdb needs it
// Returns address of errno variable.
// @note this isn't a universal definition
__errno_location:
ezlea errno,ax
ret
.endfn __errno_location,globl,hidden

View file

@ -47,6 +47,10 @@ o/$(MODE)/libc/nexgen32e/tinystrncmp.ncabi.o: \
OVERRIDE_CFLAGS += \
-Os
o/$(MODE)/libc/nexgen32e/errno.o: \
OVERRIDE_CFLAGS += \
$(NO_MAGIC)
LIBC_NEXGEN32E_LIBS = $(foreach x,$(LIBC_NEXGEN32E_ARTIFACTS),$($(x)))
LIBC_NEXGEN32E_SRCS = $(foreach x,$(LIBC_NEXGEN32E_ARTIFACTS),$($(x)_SRCS))
LIBC_NEXGEN32E_HDRS = $(foreach x,$(LIBC_NEXGEN32E_ARTIFACTS),$($(x)_HDRS))

View file

@ -1,40 +0,0 @@
/*-*- mode:unix-assembly; indent-tabs-mode:t; tab-width:8; coding:utf-8 -*-│
vi: set et ft=asm ts=8 tw=8 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 "ape/macros.internal.h"
#include "libc/nexgen32e/vidya.internal.h"
#include "libc/notice.inc"
.real
.code16 # .code32 .code64
.source __FILE__
// Clears display page.
//
// @param es:di arbitrary address within video page
// @return es:ax starting address
// @mode long,legacy,real
vcls: xchg %di,%ax
bband VIDYA_REWIND,%ah,%al
xchg %di,%ax
push %di
bbmov VIDYA_SIZE,%cx,%ch,%cl
xor %al,%al
rep stosb
pop %ax
ret
.endfn vcls,globl

View file

@ -53,8 +53,7 @@ int system(const char *cmdline) {
sigemptyset(&chldmask);
sigaddset(&chldmask, SIGCHLD);
sigprocmask(SIG_BLOCK, &chldmask, &savemask);
pid = fork();
if (!pid) {
if (!(pid = fork())) {
sigaction(SIGINT, &saveint, NULL);
sigaction(SIGQUIT, &savequit, NULL);
sigprocmask(SIG_SETMASK, &savemask, NULL);

View file

@ -16,8 +16,12 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/bits/bits.h"
#include "libc/calls/calls.h"
#include "libc/calls/struct/stat.h"
#include "libc/fmt/itoa.h"
#include "libc/macros.internal.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/runtime/runtime.h"
#include "libc/str/str.h"
#include "libc/str/tpenc.h"
@ -33,15 +37,22 @@
void bf(int fd) {
ssize_t rc;
uint64_t w;
int c, fg, fg2;
struct stat st;
size_t i, n, o;
int c, fg, fg2, bits;
char ibuf[W], obuf[12 + 1 + W * 6 + 1 + W * (2 + 11) + 11 + 1];
o = 0;
if (fstat(fd, &st) != -1 && S_ISREG(st.st_mode) && st.st_size) {
bits = bsr(roundup2pow(st.st_size));
bits = ROUNDUP(bits, 4);
} else {
bits = 48;
}
do {
if ((rc = read(fd, ibuf, W)) == -1) exit(2);
if (rc) {
n = 0;
n += uint64toarray_fixed16(o, obuf + n, 48);
n += uint64toarray_fixed16(o, obuf + n, bits);
o += rc;
obuf[n++] = ' ';
for (i = 0; i < rc; ++i) {
@ -65,21 +76,21 @@ void bf(int fd) {
}
if (fg2 != fg) {
fg = fg2;
obuf[n++] = '\e';
obuf[n++] = 033;
obuf[n++] = '[';
obuf[n++] = '3';
obuf[n++] = '8';
obuf[n++] = ';';
obuf[n++] = '5';
obuf[n++] = ';';
n += int64toarray_radix10(o, obuf + n);
n += int64toarray_radix10(fg, obuf + n);
obuf[n++] = 'm';
}
obuf[n++] = "0123456789abcdef"[c >> 4];
obuf[n++] = "0123456789abcdef"[c & 15];
/* obuf[n++] = ' '; */
}
obuf[n++] = '\e';
obuf[n++] = 033;
obuf[n++] = '[';
obuf[n++] = '0';
obuf[n++] = 'm';

View file

@ -1,161 +0,0 @@
/*-*- 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/calls/calls.h"
#include "libc/calls/ioctl.h"
#include "libc/calls/struct/stat.h"
#include "libc/calls/termios.h"
#include "libc/fmt/fmt.h"
#include "libc/log/check.h"
#include "libc/log/log.h"
#include "libc/mem/mem.h"
#include "libc/runtime/gc.h"
#include "libc/stdio/stdio.h"
#include "libc/str/str.h"
#include "libc/sysv/consts/fileno.h"
#include "libc/sysv/consts/map.h"
#include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/prot.h"
#include "third_party/zlib/zlib.h"
void ProcessFile(const char *path) {
/* int fd; */
/* void *map; */
/* z_stream zs; */
/* uint8_t *rgb; */
/* struct stat st; */
/* const char *ext; */
/* int width, height; */
/* size_t pathlen, rgbbytes; */
/* CHECK_NOTNULL((ext = memrchr(path, '-', (pathlen = strlen(path))))); */
/* CHECK_EQ(2, sscanf(ext, "-%dx%d.rgb.gz", &width, &height)); */
/* CHECK_NE(-1, (fd = open(path, O_RDONLY))); */
/* CHECK_NE(-1, fstat(fd, &st)); */
/* CHECK_NOTNULL((rgb = malloc((rgbbytes = width * height * 3)))); */
/* CHECK_NE(MAP_FAILED, */
/* (map = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0))); */
/* zs.next_in = map; */
/* zs.zalloc = Z_NULL; */
/* zs.zfree = Z_NULL; */
/* zs.avail_in = st.st_size; */
/* zs.total_in = st.st_size; */
/* zs.next_out = rgb; */
/* zs.avail_out = rgbbytes; */
/* zs.total_out = rgbbytes; */
/* CHECK_EQ(Z_OK, inflateInit2(&zs, 16 + MAX_WBITS)); */
/* CHECK_NE(Z_BUF_ERROR, inflate(&zs, Z_NO_FLUSH)); */
/* CHECK_EQ(Z_OK, inflateEnd(&zs)); */
/* struct winsize ws; */
/* struct Resizer *rz; */
/* ws.ws_row = 25; */
/* ws.ws_col = 80; */
/* LOGIFNEG1(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)); */
/* ws.ws_row *= 2; */
/* ws.ws_col /= 2; */
/* ws.ws_row /= 2; */
/* int y, x, i; */
/* uint8_t *rgb2, *u8p; */
/* float *frgba, *frgba2, *f32p; */
/* CHECK_NOTNULL((rz = NewResizer())); */
/* CHECK_NOTNULL((frgba = malloc(width * height * 4 * sizeof(float)))); */
/* CHECK_NOTNULL((frgba2 = malloc(ws.ws_row * ws.ws_col * 4 *
* sizeof(float)))); */
/* CHECK_NOTNULL((rgb2 = (uint8_t *)malloc(ws.ws_row * ws.ws_col * 3))); */
/* for (u8p = rgb, f32p = frgba, y = 0; y < height; ++y) { */
/* for (x = 0; x < width; ++x, u8p += 3, f32p += 4) { */
/* f32p[0] = (int)u8p[0], f32p[1] = (int)u8p[1]; */
/* f32p[2] = (int)u8p[2], f32p[3] = 1; */
/* for (i = 0; i < 4; ++i) f32p[i] /= 256; */
/* } */
/* } */
/* ResizeImage(rz, frgba, ws.ws_row, ws.ws_col, frgba, height, width); */
/* for (u8p = rgb2, f32p = frgba2, y = 0; y < ws.ws_row; ++y) { */
/* for (x = 0; x < ws.ws_col; ++x, u8p += 3, f32p += 4) { */
/* for (i = 0; i < 4; ++i) f32p[i] *= 256; */
/* u8p[0] = (int)f32p[0]; */
/* u8p[1] = (int)f32p[1]; */
/* u8p[2] = (int)f32p[2]; */
/* } */
/* } */
/* free(frgba2); */
/* free(frgba); */
/* int y, x; */
/* uint8_t *rgb2; */
/* CHECK_NOTNULL((rz = NewResizer())); */
/* CHECK_NOTNULL((rgb2 = (uint8_t *)malloc(ws.ws_row * ws.ws_col * 3))); */
/* printf("%d %d %d %d %d %d\n", rgb2, ws.ws_row, ws.ws_col, rgb, height,
* width); */
/* ResizeImage8(rz, rgb2, ws.ws_row, ws.ws_col, rgb, height, width); */
/* uint8_t *p; */
/* /\* printf("\e[H"); *\/ */
/* for (p = rgb2, y = 0; y < ws.ws_row / 2; ++y) { */
/* for (x = 0; x < ws.ws_col; ++x, p += 3) { */
/* printf("\e[48;2;%hhu;%hhu;%hhu;38;2;%hhu;%hhu;%hhum▄", p[0], p[1],
* p[2], */
/* p[ws.ws_col * 3 + 0], p[ws.ws_col * 3 + 1], p[ws.ws_col * 3 +
* 2]); */
/* } */
/* for (x = 0; x < ws.ws_col; ++x, p += 3) { */
/* } */
/* printf("\n"); */
/* } */
/* printf("\e[0m"); */
/* uint8_t *rgb2; */
/* struct winsize ws; */
/* struct Resizer *rz; */
/* ws.ws_row = 25; */
/* ws.ws_col = 80; */
/* LOGIFNEG1(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws)); */
/* CHECK_NOTNULL((rz = NewResizer())); */
/* CHECK_NOTNULL((rgb2 = malloc(ws.ws_row * ws.ws_col * 3))); */
/* printf("%d %d %d %d %d %d\n", rgb2, ws.ws_row, ws.ws_col, rgb, height,
* width); */
/* ResizeImage8(rz, rgb2, ws.ws_row, ws.ws_col, rgb, height, width); */
/* int y, x; */
/* uint8_t *p; */
/* printf("\e[H"); */
/* for (p = rgb2, y = 0; y < ws.ws_row; ++y) { */
/* for (x = 0; x < ws.ws_col; ++x, p += 3) { */
/* if (p[0] || p[1] || p[2]) { */
/* printf("\e[48;2;%hhu;%hhu;%hhum ", p[0], p[1], p[2]); */
/* } */
/* } */
/* printf("\n"); */
/* } */
/* printf("\e[0m"); */
/* CHECK_NE(-1, munmap(map, st.st_size)); */
/* CHECK_NE(-1, close(fd)); */
/* FreeResizer(rz); */
/* free(rgb2); */
/* free(rgb); */
}
int main(int argc, char *argv[]) {
int i;
for (i = 1; i < argc; ++i) {
ProcessFile(argv[i]);
}
return 0;
}