-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathccxxd.c
More file actions
126 lines (111 loc) · 3.33 KB
/
Copy pathccxxd.c
File metadata and controls
126 lines (111 loc) · 3.33 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This challenge is to build your own version of xxd. Xxd is a Unix command line tool to create hexdumps and to convert a hexdump back into it’s binary form.
ccxxd files.tar
pass a file to the utility. we gotta open this file in read mode and start reading.
*/
#include <unistd.h> // for getopts
#include <stdio.h>
#include <ctype.h> // for isprint function
#include <stdlib.h> // atoi
FILE *open_file_ro_mode(const char *filename);
void print_hex(unsigned char *buffer, size_t num_read, unsigned char col_size, unsigned char group_size);
void print_ascii(unsigned char *buffer, size_t num_read);
void hex_dump_loop(FILE *file, unsigned char col_size, unsigned char group_size, int len_octets);
int main(int argc, char **argv) {
int opt;
int col_size = 16;
int group_size = 16;
// default size for dumping the whole file
int len_octets = 0;
while ((opt = getopt(argc, argv, "c:g:l:")) != -1) {
switch (opt) {
case 'c':
col_size = atoi(optarg);
if (col_size < 1 || col_size > 30) {
printf("Warning: column size should be between 1 and 30. Using default column size 16.\n");
col_size = 16;
}
break;
case 'g':
group_size = atoi(optarg);
if (group_size < 1 || group_size > 30) {
printf("Warning: group size should be between 1 and 30. Using default group size 16.\n");
group_size = 16;
}
break;
case 'l':
len_octets = atoi(optarg);
if (len_octets < 1) {
printf("Warning: length should be a positive number. Using default size of infinite.\n");
len_octets = 0;
}
break;
case '?':
printf("Unknown option: %c\n", optopt);
break;
}
}
if (optind >= argc) {
printf("Please provide a filename to the hexdump utility as an argument.\n");
return 1;
}
printf("Hex dumping the file: %s...\n", argv[optind]);
FILE *fp = open_file_ro_mode(argv[optind]);
hex_dump_loop(fp, col_size, group_size, len_octets);
fclose(fp);
return 0;
}
FILE *open_file_ro_mode(const char *filename) {
FILE *file_to_dump = fopen(filename, "rb");
if (file_to_dump == NULL) {
printf("Error opening the file: %s\n", filename);
return NULL;
}
return file_to_dump;
}
void print_hex(unsigned char *buffer, size_t num_read, unsigned char col_size, unsigned char group_size) {
for (size_t i = 0; i < col_size; i++) {
if (i < num_read) {
printf("%02x", buffer[i]);
if ((i+1) % group_size == 0) {
printf(" ");
}
}
else {
if (i % group_size == group_size - 1) {
printf(" ");
} else {
printf(" ");
}
}
}
printf(" ");
}
void print_ascii(unsigned char *buffer, size_t num_read) {
for (size_t i = 0; i < num_read; i++) {
if (isprint(buffer[i])) {
printf("%c", buffer[i]);
}
else {
printf(".");
}
}
printf("\n");
}
void hex_dump_loop(FILE *file, unsigned char col_size, unsigned char group_size, int len_octets) {
unsigned char buffer[col_size];
size_t total_read = 0;
while (!feof(file) && !ferror(file)) {
size_t bytes_to_read = col_size;
if (len_octets > 0 && total_read + bytes_to_read > len_octets) {
bytes_to_read = len_octets - total_read;
}
size_t num_read = fread(buffer, sizeof(*buffer), bytes_to_read, file);
total_read += num_read;
printf("%08zx: ", total_read - num_read);
print_hex(buffer, num_read, col_size, group_size);
print_ascii(buffer, num_read);
if (len_octets > 0 && total_read >= len_octets) {
break;
}
}
}