lazysphere/command/dd.c

64 lines
1.7 KiB
C
Raw Normal View History

2023-05-06 04:39:44 +00:00
#include "command.h"
#include "lslib.h"
#include <stdlib.h>
2023-04-27 18:38:16 +00:00
2023-05-01 22:43:32 +00:00
static void help(void) {
2023-04-27 18:38:16 +00:00
printf("Usage: dd [if=FILE] [of=FILE] [bs=N] [count=N]\n\n");
printf("Copy a file with converting and formatting\n\n");
printf("\tif=FILE\t\tRead from FILE instead of stdin\n");
printf("\tof=FILE\t\tWrite to FILE instead of stdout\n");
printf("\tbs=N\t\tRead and write N bytes at a time\n");
printf("\tcount=N\t\tCopy only N input blocks\n");
2023-04-27 18:38:16 +00:00
}
2023-05-15 14:57:33 +00:00
COMMAND(dd_main) {
2023-04-27 18:38:16 +00:00
FILE* in_file = stdin;
FILE* out_file = stdout;
int bs = 1024;
int count = -1;
2023-05-04 20:10:37 +00:00
int i;
char* buffer;
size_t read;
2023-04-27 18:38:16 +00:00
2023-05-01 22:43:32 +00:00
parse_help(argc, argv, help);
2023-05-04 20:10:37 +00:00
for (i = 0; i < argc; i++) {
2023-04-27 18:38:16 +00:00
if (prefix("if=", argv[i])) {
char* path = argv[i] + 3;
in_file = get_file(path, "rb");
} else if (prefix("of=", argv[i])) {
char* path = argv[i] + 3;
out_file = get_file(path, "wb");
} else if (prefix("bs=", argv[i])) {
char* str = argv[i] + 3;
bs = get_number(str);
if (bs < 1) {
2023-05-03 16:17:56 +00:00
error("block size must be greater than 0");
2023-04-27 18:38:16 +00:00
}
} else if (prefix("count=", argv[i])) {
char* str = argv[i] + 6;
count = get_number(str);
if (count < 1) {
2023-05-03 16:17:56 +00:00
error("count must be greather than 0");
2023-04-27 18:38:16 +00:00
}
} else {
2023-05-03 16:17:56 +00:00
error("unkown option %s", argv[i]);
2023-04-27 18:38:16 +00:00
}
}
2023-05-15 01:43:02 +00:00
buffer = xalloc(bs);
2023-05-04 20:10:37 +00:00
2023-04-27 18:38:16 +00:00
while ((read = fread(buffer, 1, bs, in_file)) != 0) {
fwrite(buffer, 1, read, out_file);
if (--count, count == 0) break;
}
free(buffer);
fclose(in_file);
fclose(out_file);
return EXIT_SUCCESS;
}