aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ssl/c_src/esock_utils.c
blob: 0098a4f5f686910e2860ff71e35a9803ead10b6b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 *  %CopyrightBegin%
 *  
 *  Copyright Ericsson AB 1999-2009. All Rights Reserved.
 *  
 *  The contents of this file are subject to the Erlang Public License,
 *  Version 1.1, (the "License"); you may not use this file except in
 *  compliance with the License. You should have received a copy of the
 *  Erlang Public License along with this software. If not, it can be
 *  retrieved online at http://www.erlang.org/.
 *  
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 *  the License for the specific language governing rights and limitations
 *  under the License.
 *  
 *  %CopyrightEnd%
 */

/*
 * Purpose: Safe memory allocation and other utilities.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "esock_utils.h"

static char *strtok_quote(char *s1, const char *s2);


void *esock_malloc(size_t size)
{
    void *p;

    p = malloc(size);
    if (!p) {
	fprintf(stderr, "esock_malloc: cannot alloc %d bytes\n", size);
	exit(EXIT_FAILURE);
    }
    return p;
}

void *esock_realloc(void *p, size_t size)
{
    void *np;

    np = realloc(p, size);
    if (!np) {
	fprintf(stderr, "esock_realloc: cannot realloc %d bytes\n", size);
	exit(EXIT_FAILURE);
    }
    return np;
}

void esock_free(void *p)
{
    free(p);
}

/* Builds an argv array from cmd. Spaces and tabs within double quotes
 * are not considered delimiters. Double quotes are removed.
 * 
 * The return value is argc, and the pointer to char ** is set. argc 
 * is non-negative, argv[0], ..., argv[argc - 1] are pointers to
 * strings, and argv[argc] == NULL.  All argv[0], ..., argv[argc - 1]
 * must be freed by the user, and also the argv pointer itself. 
 *
 * Example: cmd = abc"/program files/"olle nisse, results in 
 * argv[0] = abc/program files/olle, argv[1] = nisse, argc = 2.
 *
 */
int esock_build_argv(char *cmd, char ***argvp)
{
    int argvsize = 10, argc = 0;
    char *args, *tokp, *argp;
    char **argv;

    argv = esock_malloc(argvsize * sizeof(char *));
    args = esock_malloc(strlen(cmd) + 1);
    strcpy(args, cmd);
    tokp = strtok_quote(args, " \t");
    while (tokp != NULL) {
	if (argc + 1 >= argvsize) {
	    argvsize += 10;
	    argv = esock_realloc(argv, argvsize * sizeof(char *));
	}
	argp = esock_malloc(strlen(tokp) + 1);
	strcpy(argp, tokp);
	argv[argc++] = argp;
	tokp = strtok_quote(NULL, " \t");
    }
    esock_free(args);
    argv[argc] = NULL;
    *argvp = argv;
    return argc;
}

/* strtok_quote
 * Works as strtok, but characters within pairs of double quotes are not 
 * considered as delimiters. Quotes are removed.
 */
static char *strtok_quote(char *s1, const char *s2)
{
    static char *last;
    char *s, *t, *u;

    s = (s1) ? s1 : last;
    if (!s) 
	return last = NULL;

    while (*s != '"' && *s != '\0' && strchr(s2, *s))
	s++;
    t = s;

    while (1) {
	if (*t == '"') {
	    t++;
	    while (*t != '"' && *t != '\0')
		t++;
	    if (*t == '\0') {
		last = NULL;
		goto end;
	    }
	    t++;
	}
	while(*t != '"' && *t != '\0' && !strchr(s2, *t))
	    t++;
	if (*t == '\0') {
	    last = NULL;
	    goto end;
	} else if (*t != '"') {
	    *t = '\0';
	    last = t + 1;
	    goto end;
	}
    }
end:
    /* Remove quotes */
    u = t = s;
    while (*u) {
	if (*u == '"')
	    u++;
	else 
	    *t++ = *u++;
    }
    *t = '\0';
    return s;
}