zoukankan      html  css  js  c++  java
  • test.c

    test.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    #include <unistd.h>
    
    void dump_line(const unsigned char* buf, int w, int l)
    {
    #define YYYGET(X)       ( X >= 32 && X <= 126) ? X : '.'
        unsigned int i = 0;
        printf("%08x: ", l);
        for (; i < w; ++i)
            printf((i % 8 == 7) ? "%02x  " : "%02x ", *(buf+i));
        if (w < 0x10)
        {
            for (i = 0; i < 0x10 - w; ++i) printf("   ");
            printf("  ");
        }
        printf ("|");
        for (i = 0; i < w; ++i)
            printf ("%c", YYYGET(*(buf+i)));
        if (w < 0x10)
            for (i = 0; i < 0x10 - w; ++i) printf(" ");
        printf ("|
    ");
    #undef YYYGET
    }
    
    void dump_buffer(const unsigned char* buf, int max)
    {
        int l = max / 0x10 + ((max % 0x10) ? 1 : 0);
        printf ("l = %d
    ",l);
        int i = 0;
        int w = l - i > 1 ? 0x10 : max;
        const unsigned char* ptr = buf;
        for (; i < l; ++i,w = l - i > 1 ? 0x10 : max - 0x10 * i)
        {
            dump_line(ptr, w, i);
            ptr += w;
        }
    }
    
    
    int main(int argc, char *argv[])
    {
        if (argc < 3)
        {
            printf ("Usage: %s FileName, number.
    ", argv[0]);
            exit(1);
        }
    
        int fd = open(argv[1], O_RDONLY);
        if (fd != -1)
        {
            int s = atoi(argv[2]);
            unsigned char* buf = malloc(s+1);
            memset(buf, 0, s+1);
            read(fd, buf, s);
            dump_buffer(buf, s);
        }
        return 0;
    }
    
  • 相关阅读:
    sublime text 2安装及使用
    C陷阱与缺陷之语法陷阱
    上门洗车APP --- Androidclient开发 之 项目结构介绍
    编写语法分析程序
    TCP header
    boost事件处理
    TP-LINK无线路由器WR340G+ 54M支持WDS
    300M无线路由器 TL-WR842N
    python 2.7 支持dict comprehension
    100M 宽带办理
  • 原文地址:https://www.cnblogs.com/yangyingchao/p/3438356.html
Copyright © 2011-2022 走看看