MyOd

MyOd

代码

"head.h"

#ifndef HEAD_H_
#define HEAD_H_

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>

long Mtx(FILE *fp, bool recovery);
long Mtc(FILE *fp, bool flag);

#endif

"mian.c"

#include "head.h"

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

    const char *filename;
    if(argc == 3)
    {
        filename = argv[2];
    }
    else if(argc == 4)
    {
        filename = argv[3];
    }
    else
    {
        fprintf(stderr, "the argument is less.
	eg:%s [option] <file>
",argv[0]);
        return 1;
    }

    FILE *fp = fopen(filename, "rt+");
    if (!fp)
    {
        fprintf(stderr, "open file %s was failed:%s
", argv[1], strerror(errno));
        return 1;
    }
    long current_pos = 0;
    while (!feof(fp))
    {
        printf("%08o ", (unsigned int)current_pos);
        if(argc == 4)
        {
            if(strcmp(argv[1], "-tx") == 0)
            {
                Mtx(fp,true);
                current_pos = Mtc(fp,true);
            }
        }
        else if (argc == 3 && (strcmp(argv[1], "-tx") == 0))
        {
            current_pos = Mtx(fp, false);
        }
        else if (argc == 3 && (strcmp(argv[1], "-tc") == 0))
        {
            current_pos = Mtc(fp,false);
        }

    }
    printf("
%08o 
", (unsigned int)current_pos);
    fclose(fp);
    return 0;
}

"tx.c"

#include "head.h"

long Mtx(FILE *fp, bool recovery)
{
    long ptr = ftell(fp);
    char buffer[5] = {0};
    char c;
    for (int i = 1; (c = fgetc(fp)) != EOF || i % 4 != 0; i++)
    {
        buffer[(i - 1) % 4] = c;
        if (i % 4 == 0 )
        {
            printf("        %02x%02x%02x%02x ", buffer[3], buffer[2], buffer[1], buffer[0]);
        }
        if (i % 16 == 0)
        {
            printf("
");
            break;
        }
    }
    if (recovery)
    {
        fseek(fp, ptr, SEEK_SET);
    }
    return ftell(fp);
}

"tc.c"

#include "head.h"

long Mtc(FILE *fp, bool flag)
{
    char c;
    int i;
    if(flag)
    {
        printf("            ");
    }
    long ptr = ftell(fp);
    fseek(fp, 0L, SEEK_END);
    long end = ftell(fp) - ptr;
     fseek(fp, ptr, SEEK_SET);
     if(end < 16)
     {
         printf("
            ");
     }
    for (i = 1; (c = fgetc(fp)) != EOF; i++)
    {

        if (c == '
')
        {
            printf("\n");
            printf("   ");
        }
        else
        {
            putchar(c);
            printf("   ");
        }
        if (i % 16 == 0)
        {
            printf("
");
            break;
        }
    }
    return ftell(fp);
}

库制作

动态库制作:

makefile:

MyodD: src/main.c libdynamic.so  
	gcc -Iinclude src/main.c -Llib -ldynamic -o $@
	export LD_LIBRARY_PATH=./lib
libdynamic.so: tx.o tc.o 
	gcc -shared -o lib/libdynamic.so bin/dynamic/*.o

tx.o: src/tx.c include/head.h
	gcc -Iinclude -c -fPIC $< -o bin/dynamic/$@

tc.o: src/tc.c include/head.h
	gcc -Iinclude -c -fPIC $< -o bin/dynamic/$@

过程截图:

静态库制作:

makefile:

MyodS: src/main.c libstatic.a  
	gcc -Iinclude -static src/main.c -Llib -lstatic -o $@

libstatic.a: tx.o tc.o 
	ar rcvs -o  lib/$@ bin/static/*.o

tx.o: src/tx.c include/head.h
	gcc -Iinclude -c $< -o bin/static/$@

tc.o: src/tc.c include/head.h
	gcc -Iinclude -c $< -o bin/static/$@

过程截图:

运行结果

原文地址:https://www.cnblogs.com/WANGYUHAN/p/15339554.html