blob: d4c47e4da448834d6e1055a1f98ebfe66dcb5c49 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
/*
* Print stdin fully reversed. Unlike util-linux rev(1), this program does not
* reverse lines, but buffers everything fully and then writes it on the end.
* Author: Peter Wu <lekensteyn@gmail.com>
* Date: 2012-11-07
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#define READ_BLOCK 1024
#define SIZE_INC (4 * READ_BLOCK)
int main(void) {
unsigned len = 0; /* length of data */
unsigned str_size = SIZE_INC; /* size of buffer */
char *str = malloc(str_size);
if (!str) {
perror("malloc");
return 1;
}
int n = 0;
do {
if (str_size - len < SIZE_INC) {
str_size += SIZE_INC;
str = realloc(str, str_size);
if (!str) {
fprintf(stderr, "Failed to enlarge buffer to %u: %s\n", str_size, strerror(errno));
abort();
}
}
n = read(STDIN_FILENO, str + len, READ_BLOCK);
if (n < 0) {
perror("read");
goto free_str;
} else if (n) {
len += n;
}
} while (n);
/* print in reverse */
while (len)
putchar(str[--len]);
free(str);
return 0;
free_str:
free(str);
return 1;
}
|