summaryrefslogtreecommitdiff
path: root/support/wordexp/wordexp.c
blob: faa2d656be10532f20911ced4758dd29e54e6c65 (plain)
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
#include <sys/types.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "wordexp.h"

int wordexp(const char * words, wordexp_t * we, int flags)
{
	int error=0;

	assert(we != NULL);
	assert(words != NULL);

    we->we_wordc = 1;
	we->we_wordv = NULL;
	we->we_strings = NULL;
	we->we_nbytes = 0;

    we->we_wordv = malloc( we->we_wordc * sizeof( char* ) );

    we->we_nbytes = strlen( words ) + 1;
    we->we_strings = malloc( we->we_nbytes );

    we->we_wordv[0] = &we->we_strings[0];

    // copy string & terminate
    memcpy( we->we_strings, words, we->we_nbytes -1 );
    we->we_strings[ we->we_nbytes -1 ] = '\0';

	return error;
}

void wordfree(wordexp_t *we)
{
	assert(we != NULL);

	if ( we->we_wordv )
	{
        free(we->we_wordv);
	}
	if ( we->we_strings )
	{
        free(we->we_strings);
	}

	we->we_wordv = NULL;
	we->we_strings = NULL;
	we->we_nbytes = 0;
	we->we_wordc = 0;
}