aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2021-03-12 01:25:56 -0500
committerJoseph Hunkeler <jhunkeler@gmail.com>2021-03-12 01:25:56 -0500
commitd2915dc3516ede1d24891e7ab60ef9df670b273e (patch)
treec387b8a31b2bb0ada4316c0f3967e821cc5bc6e1
parent12a1cd4e64c9684f899b624e9c48cfcae001ce84 (diff)
downloadreloc-d2915dc3516ede1d24891e7ab60ef9df670b273e.tar.gz
Allow modifying files in place if output argument is missing
-rw-r--r--main.c20
1 files changed, 15 insertions, 5 deletions
diff --git a/main.c b/main.c
index c9e9988..5481361 100644
--- a/main.c
+++ b/main.c
@@ -6,10 +6,10 @@ void show_version() {
}
void usage(char *program) {
- printf("usage: %s [-hV] <str1> <str2> <input_file> <output_file>\n"
+ printf("usage: %s [-hV] <str1> <str2> <input_file> [output_file]\n"
"\n"
"Options:\n"
- "--help (-h) - Display this help message\n"
+ "--help (-h) - Display this help message and exit\n"
"--version (-V) - Display version and exit\n"
"\n"
"Positional arguments:\n"
@@ -41,7 +41,7 @@ int main(int argc, char *argv[]) {
}
}
- if (argc < 5) {
+ if (argc < 4) {
usage(program);
exit(1);
}
@@ -49,11 +49,19 @@ int main(int argc, char *argv[]) {
char *needle = strdup(argv[1]);
char *replacement = strdup(argv[2]);
char *input_file = strdup(argv[3]);
- char *output_file = strdup(argv[4]);
+ char *output_file = NULL;
+ unsigned char in_place = 0;
size_t records = 0;
size_t replacement_length = strlen(replacement);
RelocData *info = NULL;
+ if (argc > 4) {
+ output_file = strdup(argv[4]);
+ } else {
+ output_file = input_file;
+ in_place = 1;
+ }
+
if (!(info = reloc_read(input_file))) {
reloc_perror(input_file);
exit(reloc_error);
@@ -95,7 +103,9 @@ int main(int argc, char *argv[]) {
free(needle);
free(replacement);
free(input_file);
- free(output_file);
+ input_file = NULL;
+ if (!in_place)
+ free(output_file);
reloc_deinit_data(info);
return 0;
}