~ubuntu-branches/ubuntu/trusty/cctools/trusty

« back to all changes in this revision

Viewing changes to dttools/src/stringtools.c

  • Committer: Package Import Robot
  • Author(s): Michael Hanke
  • Date: 2012-06-15 08:30:02 UTC
  • mfrom: (9.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20120615083002-r5nq7iowcv6bm35n
Tags: 3.5.0-1
* New upstream release.
* Remove obsolete patches 'missing_cflags' and 'python_compat'. They have
  been merged upstream.
* Remove DM-upload flag, not needed anymore.
* Bumped Standards-version to 3.9.3, no changes necessary.
* Do not install the new 'apps' binaries. They carry language-specific
  filename extensions, and upstream was asked if renaming is possible. Until
  this is decided they won't be installed to avoid changing the API twice.

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
#include "debug.h"
9
9
#include "stringtools.h"
10
10
#include "timestamp.h"
11
 
#include "xmalloc.h"
 
11
#include "xxmalloc.h"
12
12
 
13
13
#include <assert.h>
14
14
#include <errno.h>
24
24
#define STRINGTOOLS_BUFFER_SIZE 256
25
25
#define METRIC_POWER_COUNT 6
26
26
 
27
 
char *escape_shell_string (const char *str)
 
27
char *escape_shell_string(const char *str)
28
28
{
29
 
        if (str == NULL) str = "";
30
 
        char *escaped_string = malloc(strlen(str)*3+1);
31
 
        if (escaped_string == NULL) return NULL;
 
29
        if(str == NULL)
 
30
                str = "";
 
31
        char *escaped_string = malloc(strlen(str) * 3 + 1);
 
32
        if(escaped_string == NULL)
 
33
                return NULL;
32
34
        const char *old = str;
33
35
        char *current = escaped_string;
34
36
        strcpy(current, "'");
35
37
        current += 1;
36
 
        for (; *old; old++) {
37
 
                if (*old == '\'') {
 
38
        for(; *old; old++) {
 
39
                if(*old == '\'') {
38
40
                        strcpy(current, "'\\''");
39
41
                        current += 3;
40
42
                } else {
115
117
int whole_string_match_regex(const char *text, char *pattern)
116
118
{
117
119
        char *new_pattern;
 
120
        int result;
118
121
 
119
122
        if(!pattern || !text)
120
123
                return 0;
130
133
        if(text[strlen(pattern) - 1] != '$')
131
134
                strncat(new_pattern, "$", 1);
132
135
 
133
 
        return string_match_regex(text, new_pattern);
 
136
        result = string_match_regex(text, new_pattern);
 
137
        free(new_pattern);
 
138
 
 
139
        return result;
134
140
}
135
141
 
136
142
 
892
898
                return 1;
893
899
}
894
900
 
895
 
char *string_format (const char *fmt, ...)
 
901
char *string_format(const char *fmt, ...)
896
902
{
897
903
        va_list va;
898
904
 
900
906
        int n = vsnprintf(NULL, 0, fmt, va);
901
907
        va_end(va);
902
908
 
903
 
        if (n < 0)
 
909
        if(n < 0)
904
910
                return NULL;
905
911
 
906
 
        char *str = xxmalloc((n+1)*sizeof(char));
 
912
        char *str = xxmalloc((n + 1) * sizeof(char));
907
913
        va_start(va, fmt);
908
 
        n = vsnprintf(str, n+1, fmt, va);
 
914
        n = vsnprintf(str, n + 1, fmt, va);
909
915
        assert(n >= 0);
910
916
        va_end(va);
911
917
 
912
918
        return str;
913
919
}
914
920
 
915
 
char *string_getcwd (void)
 
921
char *string_getcwd(void)
916
922
{
917
923
        char *result = NULL;
918
924
        size_t size = 1024;
919
925
        result = xxrealloc(result, size);
920
926
 
921
 
        while (getcwd(result, size) == NULL) {
922
 
                if (errno == ERANGE) {
 
927
        while(getcwd(result, size) == NULL) {
 
928
                if(errno == ERANGE) {
923
929
                        size *= 2;
924
930
                        result = xxrealloc(result, size);
925
931
                } else {
927
933
                        return NULL;
928
934
                }
929
935
        }
930
 
    return result;
 
936
        return result;
931
937
}