mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 06:48:31 +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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue