-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhexdump.c
More file actions
162 lines (151 loc) · 3.37 KB
/
Copy pathhexdump.c
File metadata and controls
162 lines (151 loc) · 3.37 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include<stdio.h>
#include<stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include "util.h"
#include <string.h>
#include <signal.h>
#include <errno.h>
int main(int argc, char *argv[]){
struct stat file_stat;
int fd;
char *file_buffer;
//ssize_t file_size;
//ssize_t file_bytes;
char *hex_data;
ssize_t bytes_written;
char *stdin_buffer;
ssize_t bytes;
//handle no file provided
if (argc == 1){
int max = 16;
stdin_buffer = malloc(sizeof(max));
ssize_t byte_counter = 0;
char temp_buffer[16];
// process running here
while(1){
bytes = read(0, stdin_buffer, max);
if(bytes > 0){
//copy read bytes into byte-counter
for (ssize_t i=0; i < bytes; i++){
temp_buffer[byte_counter++] = stdin_buffer[i];
if(byte_counter == 16){
hex_data = binary_to_hex(temp_buffer, 16); //convert binary to hex
if(hex_data == NULL){
perror("binary_to_hex");
return 1;
}
//write data to stdout
if((bytes_written = write(1, hex_data, strlen(hex_data))) == -1){
perror("write");
free(stdin_buffer);
free(hex_data);
return 1;
}
//free buffers to continue reading
free(hex_data);
//reset
byte_counter = 0;
}
}
} else if (bytes == 0){
break;
} else if(errno == EINTR){
continue;
}else{
perror("read");
free(stdin_buffer);
return 1;
}
}
//flush buffer to stdout
if(byte_counter > 0){
hex_data = binary_to_hex(temp_buffer, byte_counter);
if(hex_data == NULL){
perror("binary_to_hex");
free(stdin_buffer);
return 1;
}
if(write(1, hex_data, strlen(hex_data)) == -1){
perror("write");
free(stdin_buffer);
free(hex_data);
return 1;
}
free(hex_data);
}
free(stdin_buffer);
}else{
//read file
if((fd = open(argv[1], O_RDONLY)) == -1){
return 1;
}
if(stat(argv[1], &file_stat) == -1){
perror("stat");
close(fd);
return 1;
}
int max = 1024;
ssize_t byte_counter = 0;
//file_size = file_stat.st_size;
// file_buffer = malloc(file_size); //old code
file_buffer = malloc(max);
char temp_buffer[16];
//new code start here
while(1){
bytes = read(fd, file_buffer, max);
if(bytes > 0){
//copy read bytes into byte-counter
ssize_t i = 0;
for (; i < bytes; i++){
temp_buffer[byte_counter++] = file_buffer[i];
if(byte_counter == 16){
hex_data = binary_to_hex(temp_buffer, 16); //convert binary to hex
if(hex_data == NULL){
perror("binary_to_hex");
return 1;
}
//write data to stdout
if((bytes_written = write(1, hex_data, strlen(hex_data))) == -1){
perror("write");
free(hex_data);
return 1;
}
//free buffers to continue reading
free(hex_data);
//reset
byte_counter = 0;
}
}
} else if (bytes == 0){
break;
}else{
perror("read");
free(file_buffer);
return 1;
}
}
//flush buffer to stdout
if(byte_counter > 0){
hex_data = binary_to_hex(temp_buffer, byte_counter);
if(hex_data == NULL){
perror("binary_to_hex");
free(file_buffer);
return 1;
}
if(write(1, hex_data, strlen(hex_data)) == -1){
perror("write");
free(file_buffer);
free(hex_data);
return 1;
}
free(hex_data);
}
//new code ends here
free(file_buffer);
close(fd);
}
return 0;
}