-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtac.c
More file actions
173 lines (162 loc) · 4.06 KB
/
tac.c
File metadata and controls
173 lines (162 loc) · 4.06 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* =====================================================================================
*
* Filename: tac.c
*
* Description: tac
*
* Version: 1.0
* Created: 2014年08月08日 10时21分44秒
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define likely(x) __builtin_expect(!!(x), 1)
#define unlikely(x) __builtin_expect(!!(x), 0)
#define LINE_BUF 8192
void tac(const char *file)
{
if (!file) {
fprintf(stderr, "%s", "filename empty!");
return;
}
int fd = open(file, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open file failed:%s\n", strerror(errno));
return;
}
int len = lseek(fd, 0, SEEK_END);
if (len < 1) {
return;
}
if (errno != 0) {
if (errno == EBADF) {
fprintf(stderr, "%s", "stream not seekable");
goto __err_out;
}
goto __err_out;
}
int total_len = LINE_BUF * 2;
char *big_buf = (char *) malloc(total_len);
if (!big_buf) {
fprintf(stderr, "oom");
goto __err_out;
}
char *buf, *left, *end;
buf = big_buf;
left = big_buf + LINE_BUF;
int file_pos = len - len % LINE_BUF;
if (file_pos == len) {
file_pos -= LINE_BUF;
}
ssize_t r_len = 0;
size_t n_left = 0;
while (file_pos >= 0) {
lseek(fd, file_pos, SEEK_SET);
r_len = read(fd, buf, LINE_BUF);
if (r_len == 0) {
// eof
break;
}
char *p = buf + r_len - 1;
char *pre = p;
while (p >= buf) {
if (*p != '\n') {
--p;
continue;
}
if (pre - p > 0) {
fwrite(p + 1, 1, pre - p, stdout);
if (n_left) {
fwrite(left, 1, n_left, stdout);
n_left = 0;
}
} else if (n_left) {
fwrite(left, 1, n_left, stdout);
n_left = 0;
}
pre = p--;
}
if (pre >= buf) {
n_left += pre - buf + 1;
if (unlikely(total_len - LINE_BUF < n_left)) {
total_len *= 2;
big_buf = realloc(big_buf, total_len);
if (!big_buf) {
fprintf(stderr, "oom\n");
goto __err_out;
}
buf = big_buf;
left = big_buf + LINE_BUF;
}
memcpy(left, buf, n_left);
} else {
n_left = 0;
}
file_pos -= LINE_BUF;
}
if (n_left) {
fwrite(left, 1, n_left, stdout);
}
#if 0
for (i = len - 1; i >= 0; --i) {
fseek(f, i, SEEK_SET);
unsigned char c = fgetc(f);
if (c == '\n' && *pstr) {
printf("%s\n", pstr);
pstr = buf + buf_len - 1;
} else {
if (c == '\n') {
continue;
}
--pstr;
if (pstr == buf) { // full
//printf("out of buffer");
char *new_buf = NULL;
new_buf = (char *) malloc(buf_len * 2);
if (!new_buf) {
fprintf(stderr, "oom");
free(buf);
goto __err_out;
}
memcpy(new_buf + buf_len, buf, buf_len);
buf = new_buf;
buf_len *= 2;
pstr = buf + buf_len/2;
}
*pstr = c;
}
}
if (*pstr) {
printf("%s\n", pstr);
}
#endif
//fclose(f);
close(fd);
//free(buf);
//free(left);
free(big_buf);
return;
__err_out:
//fclose(f);
close(fd);
return;
}
int main(int argc, char *argv[])
{
tac(argv[1]);
return 0;
}