aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2019-11-08 00:39:19 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2019-11-08 08:41:30 -0500
commit2729eb493218429f84beab04bad3c383135fb85c (patch)
treea0098666a068da56764e3ea5e482a2993af01748
parentc4c62db41d3aa70ae0c99b53d3d697285a7f0e18 (diff)
downloadreloc-2729eb493218429f84beab04bad3c383135fb85c.tar.gz
Handle OS-specific size_t printf formatting
-rw-r--r--main.c14
-rw-r--r--reloc.h2
2 files changed, 11 insertions, 5 deletions
diff --git a/main.c b/main.c
index 535b277..5da1ba9 100644
--- a/main.c
+++ b/main.c
@@ -9,7 +9,7 @@ RelocMatch *reloc_match(char *haystack, const char *needle) {
// Search the needle in the data
if (!(memcmp(data, needle, needle_size))) {
if (!(match = calloc(1, sizeof(RelocMatch)))) {
- fprintf(stderr,"Failed to allocate FindStat structure: %s\n", strerror(errno));
+ fprintf(stderr,"Failed to allocate RelocMatch structure: %s\n", strerror(errno));
exit(1);
}
size_t data_end = strlen(data);
@@ -28,6 +28,7 @@ RelocData *reloc_read(const char *filename) {
char *data = NULL;
FILE *fp = NULL;
size_t size = 0;
+ RelocData *result;
// Open file for reading in binary mode
if (!(fp = fopen(filename, "rb"))) {
@@ -41,13 +42,16 @@ RelocData *reloc_read(const char *filename) {
rewind(fp);
if (!(data = calloc(sizeof(char), size + 1))) {
- fprintf(stderr, "Cannot allocate data array: %s\n", strerror(errno));
+ fprintf(stderr, "Failed to allocate data array: %s\n", strerror(errno));
exit(1);
}
// Read data into array
fread(data, sizeof(char), size, fp);
- RelocData *result = (RelocData *) malloc(sizeof(RelocData));
+ if (!(result = (RelocData *)malloc(sizeof(RelocData)))) {
+ fprintf(stderr, "Failed to allocate RelocData structure: %s\n", strerror(errno));
+ exit(1);
+ }
result->size = size;
result->data = data;
result->path = strdup(filename);
@@ -82,7 +86,7 @@ void reloc_deinit_data(RelocData *finfo) {
void reloc_replace(RelocMatch *match, const char *rstr) {
size_t rstr_length = strlen(rstr);
if (rstr_length > match->length) {
- fprintf(stderr, "Replacement value is too long (%lu > %lu)\n", match->length, rstr_length);
+ fprintf(stderr, "Replacement string is too long ("SIZE_T_FMT " > " SIZE_T_FMT ")\n", rstr_length, match->length);
return;
}
char *data = match->begin;
@@ -147,7 +151,7 @@ int main(int argc, char *argv[]) {
}
reloc_write(info, output_file);
- printf("%lu\n", records);
+ printf(SIZE_T_FMT"\n", records);
reloc_deinit_data(info);
return 0;
diff --git a/reloc.h b/reloc.h
index bebd8bb..aaafbdd 100644
--- a/reloc.h
+++ b/reloc.h
@@ -14,8 +14,10 @@
#if defined(_WIN32) || defined(_WIN64)
#define DIRSEP '\\'
+#define SIZE_T_FMT "%I64u"
#elif defined(__linux__) || defined(__unix__) || defined(__APPLE__) || defined(__MACH__)
#define DIRSEP '/'
+#define SIZE_T_FMT "%lu"
#endif