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.
22 #include <sys/types.h>
25 #define VERSION "0.01"
27 int main(int argc
, char* argv
[])
29 char* input_path
= NULL
;
30 char* output_path
= NULL
;
32 uint32_t bs_modifier
= 1;
33 unsigned int count
= 0;
34 unsigned int r_in
= 0;
35 unsigned int r_out
= 0;
39 for (arg
= 1; arg
< argc
; arg
++)
41 if (strstr(argv
[arg
], "if=")) {
42 input_path
= strstr(argv
[arg
], "if=") + strlen("if=");
43 } else if (strstr(argv
[arg
], "of=")) {
44 output_path
= strstr(argv
[arg
], "of=") + strlen("of=");
45 } else if (strstr(argv
[arg
], "bs=")) {
46 char* bs_value
= strstr(argv
[arg
], "bs=") + strlen("bs=");
47 char bs_unit
= bs_value
[strlen(bs_value
) - 1];
48 // if last character isn't a digit
49 if (!isdigit(bs_unit
)) {
58 bs_modifier
= 1024 * 1024;
62 bs_modifier
= 1024 * 1024 * 1024;
65 fprintf(stderr
, "%s: invalid unit: '%c'\n",
69 printf("%c\n", bs_unit
);
70 // delete the unit, leaving just the number
71 bs_value
[strlen(bs_value
) - 1] = '\0';
75 for (size_t i
= 0; i
< strlen(bs_value
); i
++)
77 if (!isdigit(bs_value
[i
])) {
78 fprintf(stderr
, "%s: invalid number: '%s'\n",
86 fprintf(stderr
, "%s: invalid number: '%zu'\n",
91 } else if (strstr(argv
[arg
], "count=")) {
92 char* count_value
= strstr(argv
[arg
], "count=") + strlen("count=");
95 for (size_t i
= 0; i
< strlen(count_value
); i
++)
97 if (!isdigit(count_value
[i
])) {
98 fprintf(stderr
, "%s: invalid number: '%s'\n",
99 argv
[0], count_value
);
104 count
= atoi(count_value
);
106 fprintf(stderr
, "0+0 records in\n"
108 "0 bytes (0 B) copied\n");
111 } else if (strcmp("--help", argv
[arg
]) == 0 || strcmp("-h", argv
[arg
]) == 0) {
112 printf("Usage: %s\n", argv
[0]);
113 printf("[delim] must be a character and [field] starts at 1.\n"
115 " -h, --help Print this message.\n"
116 " -v, --version Show version info.\n");
118 } else if (strcmp("--version", argv
[arg
]) == 0 || strcmp("-v", argv
[arg
]) == 0) {
119 printf("dd (mutos) v"VERSION
"\n");
122 // no more flags left
127 FILE* input_file
= NULL
;
128 FILE* output_file
= NULL
;
132 if (!input_path
|| strcmp(input_path
, "-") == 0) {
135 struct stat input_stat
;
136 rc
= stat(input_path
, &input_stat
);
138 fprintf(stderr
, "%s: %s: %s\n",
139 argv
[0], strerror(errno
), input_path
);
142 input_file
= fopen(input_path
, "r");
146 fprintf(stderr
, "%s: %s: %s\n",
147 argv
[0], strerror(errno
), input_path
);
152 output_file
= stdout
;
154 struct stat output_stat
;
155 rc
= stat(output_path
, &output_stat
);
156 if (rc
< 0 && errno
!= ENOENT
) {
157 fprintf(stderr
, "%s: %s: %s\n",
158 argv
[0], strerror(errno
), input_path
);
161 output_file
= fopen(output_path
, "w");
165 fprintf(stderr
, "%s: %s: %s\n",
166 argv
[0], strerror(errno
), output_path
);
173 c
= fgetc(input_file
);
177 if (bytes
% (bs
*bs_modifier
) == 0) {
180 fputc(c
, output_file
);
181 if (bytes
% (bs
*bs_modifier
) == 0) {
184 c
= fgetc(input_file
);
187 fprintf(stderr
, "%d+0 records in\n"