C/Linux :: 리눅스에서 파일 덤프… dump.c

리눅스 프로그래밍 과제로 작성했던 dump 프로그램입니다.

#include <sys/types.h>
#include <sys/stat.h>

#include <fcntl.h>
#include <unistd.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BUFSIZE 16

char * getDump (char *data, int size, char *out)
{
    int offs;

    for (offs = 0; offs < size; offs++)
    {
        sprintf (&out[offs * 3], "%02x ", (unsigned char)data[offs]);
    }

    return out;
}

char * getASCII (char *data, int size, char *out)
{
    int offs;

    for (offs = 0; offs < size; offs++)
    {
        if (data[offs] < 0x20 || data[offs] >= 0x7f)
            sprintf (&out[offs], "%c", '.');
        else
            sprintf (&out[offs], "%c", data[offs]);
    }

    return out;
}

int main (int argc, char **argv)
{
    char buf[BUFSIZE + 1] = {0};
    char dumpbuf[BUFSIZE * 3 + 1] = {0};
    char asciibuf[BUFSIZE + 1] = {0};
    int fdes;
    int offs = 0;

    if (argc < 2)
    {
        printf ("[*] USAGE : $%s [file]\n", argv[0]);
        exit (1);
    }

    fdes = open (argv[1], O_RDONLY);
    if (fdes == -1)
    {
        perror ("[*] FILE");
        exit (1);
    }

    while (read (fdes, buf, BUFSIZE) > 0)
    {
        printf ("%08x:  %s  %s\n", \
                (offs++) * BUFSIZE, \
                getDump (buf, BUFSIZE, dumpbuf), \
                getASCII (buf, BUFSIZE, asciibuf));
        memset (buf, 0, BUFSIZE);
    }

    close (fdes);
    return 0;
}


사용자 삽입 이미지

Similar Posts

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다