~ubuntu-branches/ubuntu/vivid/cctools/vivid

« back to all changes in this revision

Viewing changes to dttools/src/stringtools.c

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2012-03-30 12:40:01 UTC
  • mfrom: (9.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20120330124001-ze0lhxm5uwq2e3mo
Tags: 3.4.2-2
Added patch to handle a missing CFLAGS variable in Python's sysconfig
report (Closes: #661658).

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
See the file COPYING for details.
6
6
*/
7
7
 
 
8
#include "debug.h"
8
9
#include "stringtools.h"
9
10
#include "timestamp.h"
 
11
#include "xmalloc.h"
10
12
 
 
13
#include <assert.h>
 
14
#include <errno.h>
11
15
#include <stdio.h>
12
16
#include <string.h>
13
17
#include <math.h>
20
24
#define STRINGTOOLS_BUFFER_SIZE 256
21
25
#define METRIC_POWER_COUNT 6
22
26
 
 
27
char *escape_shell_string (const char *str)
 
28
{
 
29
        if (str == NULL) str = "";
 
30
        char *escaped_string = malloc(strlen(str)*3+1);
 
31
        if (escaped_string == NULL) return NULL;
 
32
        const char *old = str;
 
33
        char *current = escaped_string;
 
34
        strcpy(current, "'");
 
35
        current += 1;
 
36
        for (; *old; old++) {
 
37
                if (*old == '\'') {
 
38
                        strcpy(current, "'\\''");
 
39
                        current += 3;
 
40
                } else {
 
41
                        *current = *old;
 
42
                        current += 1;
 
43
                }
 
44
        }
 
45
        strcpy(current, "'");
 
46
        return escaped_string;
 
47
}
 
48
 
23
49
void string_from_ip_address(const unsigned char *bytes, char *str)
24
50
{
25
51
        sprintf(str, "%u.%u.%u.%u", (unsigned) bytes[0], (unsigned) bytes[1], (unsigned) bytes[2], (unsigned) bytes[3]);
93
119
        if(!pattern || !text)
94
120
                return 0;
95
121
 
96
 
        new_pattern = (char *) malloc(sizeof(char) * (strlen(pattern) + 2));
 
122
        new_pattern = (char *) malloc(sizeof(char) * (strlen(pattern) + 3));
97
123
        if(!new_pattern)
98
124
                return 0;
99
125
 
865
891
        else
866
892
                return 1;
867
893
}
 
894
 
 
895
char *string_format (const char *fmt, ...)
 
896
{
 
897
        va_list va;
 
898
 
 
899
        va_start(va, fmt);
 
900
        int n = vsnprintf(NULL, 0, fmt, va);
 
901
        va_end(va);
 
902
 
 
903
        if (n < 0)
 
904
                return NULL;
 
905
 
 
906
        char *str = xxmalloc((n+1)*sizeof(char));
 
907
        va_start(va, fmt);
 
908
        n = vsnprintf(str, n+1, fmt, va);
 
909
        assert(n >= 0);
 
910
        va_end(va);
 
911
 
 
912
        return str;
 
913
}
 
914
 
 
915
char *string_getcwd (void)
 
916
{
 
917
        char *result = NULL;
 
918
        size_t size = 1024;
 
919
        result = xxrealloc(result, size);
 
920
 
 
921
        while (getcwd(result, size) == NULL) {
 
922
                if (errno == ERANGE) {
 
923
                        size *= 2;
 
924
                        result = xxrealloc(result, size);
 
925
                } else {
 
926
                        fatal("couldn't getcwd: %s", strerror(errno));
 
927
                        return NULL;
 
928
                }
 
929
        }
 
930
    return result;
 
931
}