使用C

时间:2018-10-21 19:04:50

标签: c hexdump

我似乎无法打印任何字符,实际上,该程序似乎无法正确读取任何文件,因为当我给它一个字符时,所有十六进制值均为零文件。

#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;
}

这是我给它提供二进制参数时收到的输出:

./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

此外,任何使代码更简洁的提示都将非常有益。

1 个答案:

答案 0 :(得分:2)

得到零的原因是您不读取文件的内容,而b则用0x00进行了初始化。您只需打开和关闭文件即可。

另一个问题(这可能是分段错误的原因)是b的大小为16个字节。在某些地方,您正在使用i % 16,但不是全部(在某些地方,您直接使用i)。您绝对应该检查一下。