-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress-dd.c
247 lines (194 loc) · 5.51 KB
/
progress-dd.c
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#define _POSIX_C_SOURCE 200112L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <err.h>
/* Может упасть с сообщением No space left. Хорошо бы сделать, чтобы это не было ошибкой */
void human(ssize_t bytes)
{
if (bytes < 0){
fprintf(stderr, " Negative");
}else
{
fprintf(stderr, "%4zd Gib %4zd Mib %4zd Kib %4zd bytes (%16zd bytes)", bytes / (1024 * 1024 * 1024), bytes / (1024 * 1024) % 1024, bytes / 1024 % 1024, bytes % 1024, bytes);
}
}
void human_time (long long usec)
{
long long sec = (usec + 500000) / 1000000;
if (sec < 0){
fprintf(stderr, " Negative");
}else
{
fprintf(stderr, " %2lld h %2lld m %2lld s (%6lld s)", sec / 3600, sec / 60 % 60, sec % 60, sec);
}
}
int main(int argc, char *argv[])
{
const char *input_file = NULL;
const char *output_file = NULL;
size_t bs = 1;
size_t fs = 0;
ssize_t count = -1;
for (int i = 1; argv[i] != NULL; ++i){
if (strncmp(argv[i], "if=", 3) == 0){
input_file = argv[i] + 3;
}else if (strncmp(argv[i], "of=", 3) == 0){
output_file = argv[i] + 3;
}else if (strncmp(argv[i], "bs=", 3) == 0){
char *endptr;
if (argv[i][3] == '\0'){
errx (EXIT_FAILURE, "no bs");
}
errno = 0;
bs = (size_t)strtoll(argv[i] + 3, &endptr, 10);
if (*endptr == '\0')
;
else if (strcmp(endptr, "K") == 0){
bs *= 1024;
}else if (strcmp(endptr, "M") == 0){
bs *= 1024 * 1024;
}else if (strcmp(endptr, "G") == 0){
bs *= 1024 * 1024 * 1024;
}else{
errx (EXIT_FAILURE, "wrong bs");
}
if (errno != 0){
err (EXIT_FAILURE, "wrong bs");
}
}else if (strncmp(argv[i], "fs=", 3) == 0){
char *endptr;
if (argv[i][3] == '\0'){
errx (EXIT_FAILURE, "no fs");
}
errno = 0;
fs = (size_t)strtoll(argv[i] + 3, &endptr, 10);
if (*endptr == '\0')
;
else if (strcmp(endptr, "K") == 0){
fs *= 1024;
}else if (strcmp(endptr, "M") == 0){
fs *= 1024 * 1024;
}else if (strcmp(endptr, "G") == 0){
fs *= 1024 * 1024 * 1024;
}else{
errx (EXIT_FAILURE, "wrong fs");
}
if (errno != 0){
err (EXIT_FAILURE, "wrong fs");
}
}else if (strncmp(argv[i], "count=", 6) == 0){
char *endptr;
if (argv[i][6] == '\0'){
errx (EXIT_FAILURE, "no count");
}
errno = 0;
count = (ssize_t)strtoll(argv[i] + 6, &endptr, 10);
if (*endptr == '\0')
;
else{
errx (EXIT_FAILURE, "wrong count");
}
if (errno != 0){
err (EXIT_FAILURE, "wrong count");
}
fs = bs * count;
}else
{
errx (EXIT_FAILURE, "%s: wrong argument", argv[1]);
}
}
if (fs == 0)
{
errx (EXIT_FAILURE, "usage: %s [if=FILE] [of=FILE] [bs=BLOCK-SIZE] fs=FULL-SIZE|count=COUNT (use K, M, G)", argv[0]);
}
int src, dest;
if (input_file == NULL){
src = 0;
}else{
src = open(input_file, O_RDONLY);
if (src == -1)
{
err (EXIT_FAILURE, "%s", input_file);
}
}
if (output_file == NULL){
dest = 1;
}else{
dest = creat(output_file, 0666);
if (dest == -1)
{
err (EXIT_FAILURE, "%s", output_file);
}
}
char *buf = (char *)malloc(bs);
struct timeval start;
gettimeofday(&start, NULL);
/* Цикл скопирован из Mini */
size_t data_done = 0;
for (ssize_t i = 0; i != count; ++i){
ssize_t size;
ssize_t write_result;
if (i % 1 == 0){
struct timeval now;
gettimeofday(&now, NULL);
long long time_done = ((long long)now.tv_sec - start.tv_sec) * 1000000 + ((long long)now.tv_usec - start.tv_usec);
fprintf(stderr, "\033[H\033[2J");
fprintf(stderr, " | Done | Left | Total\n");
fprintf(stderr, "Data | ");
human(data_done);
fprintf(stderr, " | ");
human(fs - data_done);
fprintf(stderr, " | ");
human(fs);
fputc('\n', stderr);
fprintf(stderr, "Time | ");
human_time (time_done);
fprintf(stderr, " | ");
human_time (llround(((double)fs * time_done) / data_done - time_done));
fprintf(stderr, " | ");
human_time (llround(((double)fs * time_done) / data_done));
fputc('\n', stderr);
}
size = read(src, buf, bs);
if (size == 0)
{
break;
}
if (size == -1){
perror("read(...) failed");
return -1;
}
{
char *left = buf;
while (size > 0){
write_result = write(dest, left, size);
if (write_result == -1){
perror("write(...) failed");
return -1;
}
left += write_result;
size -= write_result;
data_done += write_result;
}
}
}
free(buf);
if (close(src) == -1)
{
err (EXIT_FAILURE, "%s: cannot close", input_file);
}
if (close(dest) == -1)
{
err (EXIT_FAILURE, "%s: cannot close", output_file);
}
fprintf(stderr, "OK\n");
exit (EXIT_SUCCESS);
}