1 /* @(#) $Id: ./src/shared/string_op.c, 2011/11/01 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
12 * License details at the LICENSE file included with OSSEC or
13 * online at: http://www.ossec.net/en/licensing.html
21 * Trims the cr and/or LF from the last positions of a string
23 void os_trimcrlf(char *str)
30 while (str[len]=='\n' || str[len]=='\r')
37 /* Remove offending char (e.g., double quotes) from source */
38 char *os_strip_char(char *source, char remove) {
40 char *iterator = source;
44 // Figure out how much memory to allocate
45 for( ; *iterator; iterator++ ) {
46 if ( *iterator != remove ) {
51 // Allocate the memory
52 if( (clean = malloc( length + 1 )) == NULL ) {
56 memset(clean, '\0', length + 1);
58 // Remove the characters
60 for( i=0; *iterator; iterator++ ) {
61 if ( *iterator != remove ) {
71 int os_substr(char *dest, const char *src, int position, int length) {
75 // Unsupported negative length string
81 if( position >= strlen(src) ) {
85 strncat(dest, (src + position), length);
90 /* Escape a set of characters */
91 char *os_shell_escape(const char *src) {
92 // Maximum Length of the String is 2xthe current length
93 char shell_escapes[] = { '\\', '"', '\'', ' ', '\t', ';', '`', '>', '<', '|', '#',
94 '*', '[', ']', '{', '}', '&', '$', '!', ':', '(', ')' };
103 // Determine how long the string will be
104 const char *iterator = src;
105 for (; *iterator; iterator++) {
106 if( strchr(shell_escapes, *iterator) ) {
111 // Allocate the memory
112 if( (escaped_string = calloc(1, length + 1 )) == NULL ) {
117 // Escape the escapable characters
119 for( i=0; *iterator; iterator++ ) {
120 if ( strchr(shell_escapes, *iterator) ) {
121 escaped_string[i] = '\\';
124 escaped_string[i] = *iterator;
128 return escaped_string;