简体   繁体   中英

Having trouble with hexdump program in C

I can't seem to get any characters to print, in fact, it seems like the program won't read any file correctly, as all the hex values are zeros when I give it a file.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <argp.h>
#include <sys/types.h>
#include <sys/stat.h>

static error_t parse_opt(int key, char *arg, struct argp_state *state);
static void dump_hex(char *f, size_t l);

static struct argp_option options[] = {
    {"filepath",    'f', "PATH",    0, "uses filepath provided by user" },
    { 0 }
};

struct arguments {
    char *path;
    /* int *column_size; */
};

static error_t parse_opt(int key, char *arg, struct argp_state *state) {

    struct arguments *arguments = state->input;

    switch(key) {
        case 'f':
            arguments->path = arg;
            break;

        default:
            return ARGP_ERR_UNKNOWN;
        }

    return 0;
}

static struct argp argp = { options, parse_opt, NULL, NULL };

static void dump_hex(char *f, size_t l) {

    FILE *fd;
    size_t i;
    unsigned char *b = (unsigned char *)malloc(16 * sizeof(unsigned char));

    memset(b, 0, 16 * sizeof(unsigned char));

    if((fd = fopen(f, "r"))) {
        for(i = 0; i <= l; i++) {
            if((i % 8) == 0) {
                if(i != 0) {
                    printf("| %s\n", b);
                }
                /* print the offset */
                printf("%05lx: ", i);
            }
            /* check if ASCII is printable */
            b[i % 16] = isprint(b[i]) ? b[i] : '.';

            /* print ASCII */
            printf("%02x ", b[i]);
        }

        /* print remaining ASCII */
        printf("| %s\n", b);
    }

    fclose(fd);
    free(b);
}

int main(int argc, char *argv[]) {

    struct arguments arguments;
    struct stat sb;
    off_t size;

    arguments.path = "-";

    argp_parse(&argp, argc, argv, 0, 0, &arguments);

    stat(arguments.path, &sb);
    size = sb.st_size;

    dump_hex(arguments.path, size);

    return 0;
}

Here is the output I receive upon giving it an argument to a binary:

./hexdump --filepath=/tmp/a.out

212d0: 00 00 00 00 00 00 00 00 | ................
212d8: 00 00 00 00 00 00 00 00 | ................
212e0: 00 00 00 00 00 00 00 00 | ................
212e8: 00 00 00 00 00 00 00 00 | ................
212f0: 00 00 00 00 00 00 00 00 | ................
212f8: 00 00 00 00 00 00 00 00 | ................
Segmentation fault

Also, any tips on making the code a bit more concise would be very beneficial.

The reason why you are getting zeros is that you don't read the content of your file and b is initialized with 0x00 . Your just opening and closing the file.

Another problem (this could be the reason for the segmentation fault) is that b has a size of 16 bytes. On some places you are using i % 16 but not on all (on some places you use i directly). You should definitely check this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM