aboutsummaryrefslogtreecommitdiff
path: root/src/relocation.c
diff options
context:
space:
mode:
authorJoseph Hunkeler <jhunkeler@gmail.com>2024-04-02 13:49:18 -0400
committerJoseph Hunkeler <jhunkeler@gmail.com>2024-04-02 18:55:47 -0400
commited9974ba3f6353f7e2532bafa55a32ba01f41b4c (patch)
tree2b78b273545643c15a22201bd9771b7be1b62b90 /src/relocation.c
parent7ee5e046b510af8473a2793173c0cbb3499da869 (diff)
downloadstasis-ed9974ba3f6353f7e2532bafa55a32ba01f41b4c.tar.gz
Add REPLACE_TRUNCATE_AFTER_MATCH mode to replace_text
Diffstat (limited to 'src/relocation.c')
-rw-r--r--src/relocation.c14
1 files changed, 11 insertions, 3 deletions
diff --git a/src/relocation.c b/src/relocation.c
index 8b18efd..2f29d69 100644
--- a/src/relocation.c
+++ b/src/relocation.c
@@ -23,7 +23,7 @@
* @param replacement string value
* @return 0 on success, -1 on error
*/
-int replace_text(char *original, const char *target, const char *replacement) {
+int replace_text(char *original, const char *target, const char *replacement, unsigned flags) {
char buffer[OMC_BUFSIZ];
char *pos = original;
char *match = NULL;
@@ -58,6 +58,12 @@ int replace_text(char *original, const char *target, const char *replacement) {
// target consumed. jump to the end of the substring.
pos += target_len;
}
+ if (flags & REPLACE_TRUNCATE_AFTER_MATCH) {
+ if (strstr(pos, LINE_SEP)) {
+ strcat(buffer, LINE_SEP);
+ }
+ break;
+ }
// find more matches
if (!(match = strstr(pos, target))) {
// no more matches
@@ -67,6 +73,8 @@ int replace_text(char *original, const char *target, const char *replacement) {
break;
}
}
+ } else {
+ return 0;
}
buffer_len = strlen(buffer);
@@ -94,7 +102,7 @@ int replace_text(char *original, const char *target, const char *replacement) {
* @param replacement string
* @return 0 on success, -1 on error
*/
-int file_replace_text(const char* filename, const char* target, const char* replacement) {
+int file_replace_text(const char* filename, const char* target, const char* replacement, unsigned flags) {
int result;
char buffer[OMC_BUFSIZ];
char tempfilename[] = "tempfileXXXXXX";
@@ -118,7 +126,7 @@ int file_replace_text(const char* filename, const char* target, const char* repl
result = 0;
while (fgets(buffer, sizeof(buffer), fp)) {
if (strstr(buffer, target)) {
- if (replace_text(buffer, target, replacement)) {
+ if (replace_text(buffer, target, replacement, flags)) {
result = -1;
}
}