2 Copyright © 2013 Alastair Stuart
4 This program is open source software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
23 #include <sys/types.h>
26 #define VERSION "0.01"
28 int main(int argc
, char* argv
[])
30 char* input_path
= NULL
;
31 char* output_path
= NULL
;
33 uint32_t bs_modifier
= 1;
34 unsigned int count
= 0;
35 unsigned int r_in
= 0;
36 unsigned int r_out
= 0;
40 for (arg
= 1; arg
< argc
; arg
++)
42 if (strstr(argv
[arg
], "if=")) {
43 input_path
= strstr(argv
[arg
], "if=") + strlen("if=");
44 } else if (strstr(argv
[arg
], "of=")) {
45 output_path
= strstr(argv
[arg
], "of=") + strlen("of=");
46 } else if (strstr(argv
[arg
], "bs=")) {
47 char* bs_value
= strstr(argv
[arg
], "bs=") + strlen("bs=");
48 char bs_unit
= bs_value
[strlen(bs_value
) - 1];
49 // if last character isn't a digit
50 if (!isdigit(bs_unit
)) {
59 bs_modifier
= 1024 * 1024;
63 bs_modifier
= 1024 * 1024 * 1024;
66 fprintf(stderr
, "%s: invalid unit: '%c'\n",
70 printf("%c\n", bs_unit
);
71 // delete the unit, leaving just the number
72 bs_value
[strlen(bs_value
) - 1] = '\0';
76 for (size_t i
= 0; i
< strlen(bs_value
); i
++)
78 if (!isdigit(bs_value
[i
])) {
79 fprintf(stderr
, "%s: invalid number: '%s'\n",
87 fprintf(stderr
, "%s: invalid number: '%"PRIu64
"'\n",
92 } else if (strstr(argv
[arg
], "count=")) {
93 char* count_value
= strstr(argv
[arg
], "count=") + strlen("count=");
96 for (size_t i
= 0; i
< strlen(count_value
); i
++)
98 if (!isdigit(count_value
[i
])) {
99 fprintf(stderr
, "%s: invalid number: '%s'\n",
100 argv
[0], count_value
);
105 count
= atoi(count_value
);
107 fprintf(stderr
, "0+0 records in\n"
109 "0 bytes (0 B) copied\n");
112 } else if (strcmp("--help", argv
[arg
]) == 0 || strcmp("-h", argv
[arg
]) == 0) {
113 printf("Usage: %s\n", argv
[0]);
114 printf("[delim] must be a character and [field] starts at 1.\n"
116 " -h, --help Print this message.\n"
117 " -v, --version Show version info.\n");
119 } else if (strcmp("--version", argv
[arg
]) == 0 || strcmp("-v", argv
[arg
]) == 0) {
120 printf("dd (mutos) v"VERSION
"\n");
123 // no more flags left
128 FILE* input_file
= NULL
;
129 FILE* output_file
= NULL
;
133 if (!input_path
|| strcmp(input_path
, "-") == 0) {
136 struct stat input_stat
;
137 rc
= stat(input_path
, &input_stat
);
139 fprintf(stderr
, "%s: %s: %s\n",
140 argv
[0], strerror(errno
), input_path
);
143 input_file
= fopen(input_path
, "r");
147 fprintf(stderr
, "%s: %s: %s\n",
148 argv
[0], strerror(errno
), input_path
);
153 output_file
= stdout
;
155 struct stat output_stat
;
156 rc
= stat(output_path
, &output_stat
);
157 if (rc
< 0 && errno
!= ENOENT
) {
158 fprintf(stderr
, "%s: %s: %s\n",
159 argv
[0], strerror(errno
), input_path
);
162 output_file
= fopen(output_path
, "w");
166 fprintf(stderr
, "%s: %s: %s\n",
167 argv
[0], strerror(errno
), output_path
);
174 c
= fgetc(input_file
);
178 if (bytes
% (bs
*bs_modifier
) == 0) {
181 fputc(c
, output_file
);
182 if (bytes
% (bs
*bs_modifier
) == 0) {
185 c
= fgetc(input_file
);
188 fprintf(stderr
, "%d+0 records in\n"