does some more cleanup

This commit is contained in:
Nathan Singer 2025-02-18 13:36:52 -05:00
parent 1eda825308
commit 6a700092f9

View File

@ -115,6 +115,26 @@ static int get_hex_lines(int len)
return out;
}
static int get_seek(int len_content)
{
uint seek = 0;
if (flags.seek >= 0) {
if (flags.seek > len_content)
exit(EXIT_SUCCESS);
seek = flags.seek;
} else {
if ((flags.seek * -1) > len_content) {
fprintf(stderr, "xxd: Sorry, cannot seek.\n");
exit(EXIT_FAILURE);
}
seek = (len_content - (flags.seek * -1));
flags.offset += seek;
}
return seek;
}
static void do_text_parse(hex_chunk_t** lines, bool interactive)
{
char* file_content = NULL;
@ -129,28 +149,15 @@ static void do_text_parse(hex_chunk_t** lines, bool interactive)
read_file_to_buf(flags.files[0], &file_content);
}
if (flags.seek >= 0) {
if (flags.seek > (int) strlen(file_content))
exit(EXIT_SUCCESS);
seek = flags.seek;
} else {
if ((flags.seek * -1) > (int) strlen(file_content)) {
fprintf(stderr, "xxd: Sorry, cannot seek.\n");
exit(EXIT_FAILURE);
}
seek = (strlen(file_content) - (flags.seek * -1));
flags.offset += seek;
}
seek = get_seek(strlen(file_content));
filesize = (flags.len == -1) ? strlen(file_content + seek) : flags.len;
flags.len = filesize;
hex_lines = get_hex_lines(filesize);
*lines = malloc(sizeof(hex_chunk_t) * (hex_lines + 1));
for (i = 0; i < hex_lines; i++) {
(*lines)[i].line = i;
add_text_to_chunk((file_content + seek) + (i * (flags.cols)), &((*lines)[i].text));
@ -189,8 +196,6 @@ static void do_display(hex_chunk_t** lines)
{
int i;
char* filename;
bool fileout = false;
FILE* stream = stdout;
@ -199,8 +204,6 @@ static void do_display(hex_chunk_t** lines)
fileout = true;
}
i = 0;
if (flags.c_style) {
parse_c_filename((!flags.c_filename) ? &(flags.files[0]) : &(flags.c_filename),
&filename);
@ -208,6 +211,7 @@ static void do_display(hex_chunk_t** lines)
printf("unsigned char %s[] = {\n", filename);
}
i = 0;
while ((*lines)[i].line != -1)
display_hex_chunk(&((*lines)[i++]), stream);