Arg error checking

This commit is contained in:
xamidev
2024-06-02 10:10:47 +02:00
parent 7d971f6f37
commit b5a70ff093
3 changed files with 31 additions and 11 deletions

42
hex.c
View File

@@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
// TODO: Error checking // TODO: ASCII/Position printing
// TODO: Stop appending garbage at file end
#define BYTES 1024 #define BYTES 1024
#define BYTES_PER_LINE 10 #define BYTES_PER_LINE 10
@@ -18,8 +19,14 @@ void print_hex(unsigned char* buf, int byteno)
puts(""); puts("");
} }
void main(int argc, char** argv) int main(int argc, char** argv)
{ {
if (argc != 2)
{
printf("Usage: %s <file>\n", argv[0]);
return -1;
}
FILE* f = fopen(argv[1], "r"); FILE* f = fopen(argv[1], "r");
unsigned char buf[BYTES]; unsigned char buf[BYTES];
int byteno = fread(buf, 1, BYTES, f); int byteno = fread(buf, 1, BYTES, f);
@@ -30,14 +37,27 @@ void main(int argc, char** argv)
char cmd; char cmd;
int loc; int loc;
scanf("%c%d", &cmd, &loc); scanf("%c%d", &cmd, &loc);
switch(cmd)
if (cmd == 'p') print_hex(buf + loc, BYTES_PER_LINE); {
if (cmd == 'e') scanf("%x", buf + loc); case 'p':
if (cmd == 's') break; case 'P':
print_hex(buf + loc, BYTES_PER_LINE);
break;
case 'e':
case 'E':
scanf("%hhx", buf + loc);
break;
case 's':
case 'S':
fclose(f);
f = fopen(argv[1], "w");
fwrite(buf, 1, BYTES, f);
fclose(f);
break;
case 'q':
case 'Q':
return 0;
break;
}
} }
fclose(f);
f = fopen(argv[1], "w");
fwrite(buf, 1, BYTES, f);
fclose(f);
} }

BIN
minihex

Binary file not shown.

BIN
test.txt Normal file

Binary file not shown.