mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-07 19:58:30 +00:00
Work towards improving signals and processes
This commit is contained in:
parent
de703b182c
commit
d7ac16a9ed
96 changed files with 1474 additions and 427 deletions
|
@ -8,10 +8,12 @@
|
|||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/errno.h"
|
||||
#include "libc/log/check.h"
|
||||
#include "libc/log/color.internal.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/mem/mem.h"
|
||||
#include "libc/nt/thread.h"
|
||||
#include "libc/runtime/gc.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
|
@ -20,48 +22,41 @@
|
|||
#include "libc/sysv/consts/fileno.h"
|
||||
#include "libc/sysv/consts/sig.h"
|
||||
|
||||
#define kTutorialMessage "This echos stdio until Ctrl+C is pressed.\n"
|
||||
#define kVictoryMessage "\rGot Ctrl+C and longjmp() ran dtors (>'.')>\n"
|
||||
|
||||
jmp_buf jb;
|
||||
volatile bool gotctrlc;
|
||||
|
||||
void GotCtrlC(int sig) {
|
||||
gclongjmp(jb, 1);
|
||||
unreachable;
|
||||
}
|
||||
|
||||
size_t HowManyBytesOfHeapMemoryAreAllocated(void) {
|
||||
return mallinfo().uordblks;
|
||||
}
|
||||
|
||||
void ReadAndPrintLinesLoop(void) {
|
||||
ssize_t got;
|
||||
unsigned char *buf;
|
||||
buf = gc(malloc(BUFSIZ));
|
||||
CHECK_NE(0, HowManyBytesOfHeapMemoryAreAllocated());
|
||||
for (;;) {
|
||||
CHECK_NE(-1, (got = read(STDIN_FILENO, buf, BUFSIZ)));
|
||||
CHECK_EQ(got, write(STDOUT_FILENO, buf, got));
|
||||
}
|
||||
gotctrlc = true;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int rc;
|
||||
rc = 0;
|
||||
showcrashreports();
|
||||
if (cancolor()) {
|
||||
CHECK_EQ(0 /* cosmo runtime doesn't link malloc */,
|
||||
HowManyBytesOfHeapMemoryAreAllocated());
|
||||
puts("This echos stdio until Ctrl+C is pressed.");
|
||||
if (!(rc = setjmp(jb))) {
|
||||
CHECK_NE(SIG_ERR, signal(SIGINT, GotCtrlC));
|
||||
ReadAndPrintLinesLoop();
|
||||
unreachable;
|
||||
ssize_t rc;
|
||||
size_t got, wrote;
|
||||
unsigned char buf[512];
|
||||
fprintf(stderr, "This echos stdio until Ctrl+C is pressed.\n");
|
||||
CHECK_NE(
|
||||
-1, sigaction(SIGINT, &(struct sigaction){.sa_handler = GotCtrlC}, NULL));
|
||||
for (;;) {
|
||||
rc = read(0, buf, BUFSIZ);
|
||||
if (rc != -1) {
|
||||
got = rc;
|
||||
} else {
|
||||
--rc;
|
||||
CHECK_EQ(EINTR, errno);
|
||||
break;
|
||||
}
|
||||
CHECK_EQ(0, HowManyBytesOfHeapMemoryAreAllocated());
|
||||
puts("\rGot Ctrl+C and longjmp() ran dtors (>'.')>");
|
||||
if (!got) break;
|
||||
rc = write(1, buf, got);
|
||||
if (rc != -1) {
|
||||
wrote = rc;
|
||||
} else {
|
||||
CHECK_EQ(EINTR, errno);
|
||||
break;
|
||||
}
|
||||
CHECK_EQ(got, wrote);
|
||||
}
|
||||
return rc;
|
||||
if (gotctrlc) {
|
||||
fprintf(stderr, "Got Ctrl+C\n");
|
||||
} else {
|
||||
fprintf(stderr, "Got EOF\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -51,6 +51,7 @@ EXAMPLES_DIRECTDEPS = \
|
|||
LIBC_LOG_ASAN \
|
||||
LIBC_MEM \
|
||||
LIBC_NEXGEN32E \
|
||||
LIBC_NT_KERNEL32 \
|
||||
LIBC_NT_NTDLL \
|
||||
LIBC_NT_USER32 \
|
||||
LIBC_NT_WS2_32 \
|
||||
|
|
61
examples/sleep.c
Normal file
61
examples/sleep.c
Normal file
|
@ -0,0 +1,61 @@
|
|||
#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"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/str/str.h"
|
||||
#include "libc/time/time.h"
|
||||
#include "libc/x/x.h"
|
||||
|
||||
/**
|
||||
* 16kb sleep executable that runs on all operating systems.
|
||||
* https://justine.lol/cosmopolitan/demos/sleep.c
|
||||
* https://justine.lol/cosmopolitan/demos/sleep.com
|
||||
* https://justine.lol/cosmopolitan/index.html
|
||||
*/
|
||||
|
||||
#define WRITE(s) write(2, s, strlen(s))
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
char *p;
|
||||
long double x, y;
|
||||
if (argc != 2) {
|
||||
WRITE("Usage: ");
|
||||
WRITE(argv[0]);
|
||||
WRITE(" SECONDS\n");
|
||||
exit(1);
|
||||
}
|
||||
x = 0;
|
||||
for (p = argv[1]; isdigit(*p); ++p) {
|
||||
x *= 10;
|
||||
x += *p - '0';
|
||||
}
|
||||
if (*p == '.') {
|
||||
y = 1;
|
||||
for (++p; isdigit(*p); ++p) {
|
||||
x += (*p - '0') * (y /= 10);
|
||||
}
|
||||
}
|
||||
switch (*p) {
|
||||
case 'm':
|
||||
x *= 60;
|
||||
break;
|
||||
case 'h':
|
||||
x *= 60 * 60;
|
||||
break;
|
||||
case 'd':
|
||||
x *= 60 * 60 * 24;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
dsleep(x);
|
||||
return 0;
|
||||
}
|
|
@ -617,10 +617,10 @@
|
|||
|
||||
#define setlocate(l, s) 0
|
||||
#define equal(s1, s2) (strcmp(s1, s2) == 0)
|
||||
#define getenv(p) bltinlookup((p), 0)
|
||||
#define isodigit(c) ((c) >= '0' && (c) <= '7')
|
||||
#define octtobin(c) ((c) - '0')
|
||||
#define scopy(s1, s2) ((void)strcpy(s2, s1))
|
||||
/* #define getenv(p) bltinlookup((p), 0) */
|
||||
#define isodigit(c) ((c) >= '0' && (c) <= '7')
|
||||
#define octtobin(c) ((c) - '0')
|
||||
#define scopy(s1, s2) ((void)strcpy(s2, s1))
|
||||
|
||||
#define TRACE(param)
|
||||
/* #define TRACE(param) \ */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue