Skip to content

Instantly share code, notes, and snippets.

@b4284
Created November 30, 2016 10:14
Show Gist options
  • Select an option

  • Save b4284/0b6cde4ce12fe0e1bbb896ae1526376e to your computer and use it in GitHub Desktop.

Select an option

Save b4284/0b6cde4ce12fe0e1bbb896ae1526376e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <inttypes.h>
void print_bit(uint8_t c, FILE* s) {
uint_fast8_t i = 8;
AGAIN:
if (i > 0) {
putc(((c & 0x80) ? '1' : '0'), s);
c <<= 1;
i -= 1;
goto AGAIN;
}
}
void pipe_stream(FILE *s1, FILE *s2) {
uint8_t buf[4096];
while (!feof(s1)) {
size_t r = fread(buf, 1, sizeof buf, s1);
fwrite(buf, 1, r, s2);
}
}
int main(void) {
char buf[512];
FILE *bufs = fmemopen(buf, sizeof buf, "w+");
if (bufs != NULL) {
print_bit(0x46, bufs);
putc('\n', bufs);
}
fseek(bufs, 0, SEEK_SET);
pipe_stream(bufs, stderr);
fclose(bufs);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment