1 /* Copyright (C) 2009 Trend Micro Inc.
4 * This program is a free software; you can redistribute it
5 * and/or modify it under the terms of the GNU General Public
6 * License (version 2) as published by the FSF - Free Software
14 /* Trim CR and/or LF from the last positions of a string */
15 void os_trimcrlf(char *str)
22 while (str[len] == '\n' || str[len] == '\r') {
28 /* Remove offending char (e.g., double quotes) from source */
29 char *os_strip_char(const char *source, char remove)
32 const char *iterator = source;
36 /* Figure out how much memory to allocate */
37 for ( ; *iterator; iterator++ ) {
38 if ( *iterator != remove ) {
43 /* Allocate the memory */
44 if ( (clean = (char *) malloc( length + 1 )) == NULL ) {
48 memset(clean, '\0', length + 1);
50 /* Remove the characters */
52 for ( i = 0; *iterator; iterator++ ) {
53 if ( *iterator != remove ) {
63 int os_substr(char *dest, const char *src, size_t position, ssize_t length)
68 /* Unsupported negative length string */
74 if ( position >= strlen(src) ) {
78 strncat(dest, (src + position), (size_t) length);
83 /* Escape a set of characters */
84 char *os_shell_escape(const char *src)
86 /* Maximum Length of the String is 2 times the current length */
87 char shell_escapes[] = { '\\', '"', '\'', ' ', '\t', ';', '`', '>', '<', '|', '#',
88 '*', '[', ']', '{', '}', '&', '$', '!', ':', '(', ')'
99 /* Determine how long the string will be */
100 const char *iterator = src;
101 for (; *iterator; iterator++) {
102 if ( strchr(shell_escapes, *iterator) ) {
107 /* Allocate memory */
108 if ( (escaped_string = (char *) calloc(1, length + 1 )) == NULL ) {
113 /* Escape the escapable characters */
115 for ( i = 0; *iterator; iterator++ ) {
116 if ( strchr(shell_escapes, *iterator) ) {
117 escaped_string[i] = '\\';
120 escaped_string[i] = *iterator;
124 return escaped_string;