summaryrefslogtreecommitdiff
path: root/hex.c
blob: 1f9ba088c88850a3bf54b40f94981b3f6460ee3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>


static void Dump(char *fn,FILE *fp)
{
    char s[17];
    int p;
    int f;
    int b;

    printf("File:%s\n",fn);

    b=!EOF;
    p=0;

    while(b!=EOF)
    {
	strcpy(s,"                ");

	for(f=0;f<16;f++)
	{
	    if((b!=EOF)&&((b=getc(fp))!=EOF))
	    {
		if (f == 0)
		{
		    printf("%6.6x:   ",p);
		    p+=16;
		}

		printf("%2.2x ",(unsigned char)b);

		if (isprint(b))
		{
		    s[f]=b;
		}
		else
		{
		    s[f]='.';
		}
	    }
	    else
	    {
		if (f == 0)
		{
		    return;
		}

		printf("** ");
	    }
	}

	printf("   %s\n",s);
    }
}

int main(int argc,char *argv[])
{
    FILE *fp;
    int f;
    int ret = EXIT_SUCCESS;

    if (argc==1)
    {
    	Dump("stdin",stdin);
    }
    else
    {
	for(f=1;f<argc;f++)
	{
	    if ((fp=fopen(argv[f],"r")))
	    {
		Dump(argv[f],fp);
		fclose(fp);
	    }
	    else
	    {
	    	fprintf(stderr,"Couldn't open %s\n",argv[f]);
		ret = EXIT_FAILURE;
	    }
	}
    }

    return ret;
}