Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for open-vm-tools-10.0.0-3000743 #2

Open
wants to merge 1 commit into
base: origin-open-vm-tools-10.0.0-3000743-1733280279
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions open-vm-tools/lib/include/strutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,6 @@ void StrUtil_SafeStrcatF(char **prefix, const char *fmt, ...) PRINTF_DECL(2, 3);

char *StrUtil_TrimWhitespace(const char *str);

char *StrUtil_ReplaceAll(const char *orig, const char *what, const char *with);

#endif /* STRUTIL_H */
87 changes: 87 additions & 0 deletions open-vm-tools/lib/misc/strutil.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,30 @@
#include "util.h"


/*
*-----------------------------------------------------------------------------
*
* StrUtil_IsEmpty --
*
* Test if a non-NULL string is empty.
*
* Results:
* TRUE if the string has length 0, FALSE otherwise.
*
* Side effects:
* None
*
*-----------------------------------------------------------------------------
*/

static INLINE Bool
StrUtil_IsEmpty(const char *str) // IN:
{
ASSERT(str != NULL);
return str[0] == '\0';
}


/*
*-----------------------------------------------------------------------------
*
Expand Down Expand Up @@ -1235,3 +1259,66 @@ StrUtil_TrimWhitespace(const char *str) // IN

return res;
}


/*
*-----------------------------------------------------------------------------
*
* StrUtil_ReplaceAll --
*
* Replaces all occurrences of a non-empty substring with non-NULL pattern
* in non-NULL string.
*
* Results:
* Returns pointer to the allocated resulting string. The caller is
* responsible for freeing it.
*
*-----------------------------------------------------------------------------
*/

char *
StrUtil_ReplaceAll(const char *orig, // IN
const char *what, // IN
const char *with) // IN
{
char *result;
const char *current;
char *tmp;
size_t lenWhat;
size_t lenWith;
size_t lenBefore;
size_t occurrences = 0;
size_t lenNew;

ASSERT(orig != NULL);
ASSERT(!StrUtil_IsEmpty(what));
ASSERT(with != NULL);

lenWhat = strlen(what);
lenWith = strlen(with);

current = orig;
while ((tmp = strstr(current, what)) != NULL) {
current = tmp + lenWhat;
++occurrences;
}

lenNew = strlen(orig) + (lenWith - lenWhat) * occurrences;
tmp = Util_SafeMalloc(lenNew + 1);
result = tmp;

while (occurrences--) {
current = strstr(orig, what);
lenBefore = current - orig;
tmp = memcpy(tmp, orig, lenBefore);
tmp += lenBefore;
tmp = memcpy(tmp, with, lenWith);
tmp += lenWith;
orig += lenBefore + lenWhat;
}
memcpy(tmp, orig, strlen(orig));

result[lenNew] = '\0';

return result;
}
Loading