Skip to content

Instantly share code, notes, and snippets.

@bennydictor
Created October 17, 2017 07:44
Show Gist options
  • Save bennydictor/22e1abd6d8b14bbd823b53294431513d to your computer and use it in GitHub Desktop.
Save bennydictor/22e1abd6d8b14bbd823b53294431513d to your computer and use it in GitHub Desktop.
#include <unistd.h>
#define BUF_MAX (4 * 1024)
int read_char(void) {
static char buf[BUF_MAX];
static unsigned int size = 0;
static unsigned int ptr = 0;
if (ptr == size) {
size = read(0, buf, BUF_MAX);
ptr = 0;
if (size == 0)
return -1;
}
return buf[ptr++];
}
int read_int(void) {
int ret = 0;
int rd = read_char();
while (rd < '0' || '9' < rd) {
rd = read_char();
if (rd == -1)
return -1;
}
while ('0' <= rd && rd <= '9') {
ret = 10 * ret + rd - '0';
rd = read_char();
}
return ret;
}
static char wbuf[BUF_MAX];
static int wptr = 0;
void write_char(int c) {
wbuf[wptr++] = c;
if (wptr == BUF_MAX) {
write(1, wbuf, BUF_MAX);
wptr = 0;
}
}
void write_int(int a) {
char ibuf[20];
int i = 0;
while (a > 0) {
ibuf[i++] = a % 10;
a = a / 10;
}
--i;
while (i >= 0) {
write_char(ibuf[i] + '0');
--i;
}
}
void write_flush(void) {
write(1, wbuf, wptr);
wptr = 0;
}
int main(void) {
int a = read_int();
while (a != -1) {
write_int(a);
write_char('\n');
a = read_int();
}
write_flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment