summaryrefslogtreecommitdiff
path: root/command/dd.c
diff options
context:
space:
mode:
authorTyler Murphy <tylerm@tylerm.dev>2023-06-07 21:33:44 -0400
committerTyler Murphy <tylerm@tylerm.dev>2023-06-07 21:33:44 -0400
commit47e9c333620a0ca186efc00d2495b44fc887882f (patch)
treeb94c4d0238d186d0f8d3a7fb5485d0cf784714de /command/dd.c
parentslight refactor and start documenting (diff)
downloadlazysphere-47e9c333620a0ca186efc00d2495b44fc887882f.tar.gz
lazysphere-47e9c333620a0ca186efc00d2495b44fc887882f.tar.bz2
lazysphere-47e9c333620a0ca186efc00d2495b44fc887882f.zip
more documentation
Diffstat (limited to 'command/dd.c')
-rw-r--r--command/dd.c51
1 files changed, 34 insertions, 17 deletions
diff --git a/command/dd.c b/command/dd.c
index e298c69..d628f9f 100644
--- a/command/dd.c
+++ b/command/dd.c
@@ -1,8 +1,17 @@
+/**
+ * file: dd.c
+ * command: dd
+ * author: Tyler Murphy
+ */
+
#include "command.h"
#include "lslib.h"
#include <stdlib.h>
+/**
+ * Help function for dd
+ */
static void help(void) {
printf("Usage: dd [if=FILE] [of=FILE] [bs=N] [count=N]\n\n");
printf("Copy a file with converting and formatting\n\n");
@@ -12,49 +21,57 @@ static void help(void) {
printf("\tcount=N\t\tCopy only N input blocks\n");
}
+/**
+* Copys a file with converting and formatting
+*/
COMMAND(dd_main) {
- FILE* in_file = stdin;
- FILE* out_file = stdout;
- int bs = 1024;
- int count = -1;
+ FILE* in_file = stdin; /* the file to copy from */
+ FILE* out_file = stdout; /* the file to copy to */
+ int bs = 1024; /* the default bs */
+ int count = -1; /* amount of bs's to copy, if negative go until EOF */
int i;
- char* buffer;
+ char* buffer; /* buffer to hold copy data */
size_t read;
+ /* parse arguments and only check for --help */
parse_help(argc, argv, help);
+ /* dd doesnt use standard arguments, parse though each argument and match each arg */
for (i = 0; i < argc; i++) {
- if (prefix("if=", argv[i])) {
- char* path = argv[i] + 3;
+ if (prefix("if=", argv[i])) { /* sets the input file */
+ char* path = argv[i] + 3; /* get data after = */
in_file = get_file(path, "rb");
- } else if (prefix("of=", argv[i])) {
- char* path = argv[i] + 3;
+ } else if (prefix("of=", argv[i])) { /* sets the output file */
+ char* path = argv[i] + 3; /* get data after = */
out_file = get_file(path, "wb");
- } else if (prefix("bs=", argv[i])) {
- char* str = argv[i] + 3;
- bs = get_number(str);
- if (bs < 1) {
+ } else if (prefix("bs=", argv[i])) { /* sets the blocksize */
+ char* str = argv[i] + 3; /* get data after = */
+ bs = get_blkm(str);
+ if (bs < 1) { /* cannot have a negative bs */
error("block size must be greater than 0");
}
- } else if (prefix("count=", argv[i])) {
- char* str = argv[i] + 6;
+ } else if (prefix("count=", argv[i])) { /* sets the count of bs to do */
+ char* str = argv[i] + 6; /* get data after = */
count = get_number(str);
if (count < 1) {
error("count must be greather than 0");
}
- } else {
+ } else { /* error and exit since arg is invalid */
error("unkown option %s", argv[i]);
}
}
-
+
+ /* allocate buffer of set bs */
buffer = xalloc(bs);
+ /* read data until EOF from and to buf */
while ((read = fread(buffer, 1, bs, in_file)) != 0) {
fwrite(buffer, 1, read, out_file);
if (--count, count == 0) break;
}
+ /* cleanup */
free(buffer);
fclose(in_file);
fclose(out_file);