Subversion Repositories HelenOS

Rev

Rev 3316 | Rev 3346 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3316 Rev 3345
Line 44... Line 44...
44
 
44
 
45
#include <stdio.h>
45
#include <stdio.h>
46
#include <string.h>
46
#include <string.h>
47
#include <stdarg.h>
47
#include <stdarg.h>
48
#include <stdlib.h>
48
#include <stdlib.h>
-
 
49
#include <stdarg.h>
49
 
50
 
50
#include "config.h"
51
#include "config.h"
51
#include "errors.h"
52
#include "errors.h"
52
#include "util.h"
53
#include "util.h"
53
 
54
 
54
 
-
 
55
/* some platforms do not have strdup, implement it here */
55
/* some platforms do not have strdup, implement it here.
-
 
56
 * Returns a pointer to an allocated string or NULL on failure */
56
char * cli_strdup(const char *s1)
57
char * cli_strdup(const char *s1)
57
{
58
{
58
    size_t len = strlen(s1) + 1;
59
    size_t len = strlen(s1) + 1;
59
    void *ret = malloc(len);
60
    void *ret = malloc(len);
60
 
61
 
Line 62... Line 63...
62
        return (char *) NULL;
63
        return (char *) NULL;
63
 
64
 
64
    return (char *) memcpy(ret, s1, len);
65
    return (char *) memcpy(ret, s1, len);
65
}
66
}
66
 
67
 
-
 
68
/*
-
 
69
 * Take a previously allocated string (s1), re-size it to accept s2 and copy
-
 
70
 * the contents of s2 into s1.
-
 
71
 * Return -1 on failure, or the length of the copied string on success.
-
 
72
 */
-
 
73
int cli_redup(char **s1, const char *s2)
-
 
74
{
-
 
75
    size_t len = strlen(s2) + 1;
-
 
76
 
-
 
77
    if (! len)
-
 
78
        return -1;
-
 
79
 
-
 
80
    *s1 = realloc(*s1, len);
-
 
81
 
-
 
82
    if (*s1 == NULL)
-
 
83
        return -1;
-
 
84
 
-
 
85
    memset(*s1, 0, sizeof(*s1));
-
 
86
    memcpy(*s1, s2, len);
-
 
87
    return (int) len;
-
 
88
}
-
 
89
 
-
 
90
/* An asprintf() for concantenating paths. Allocates the system PATH_MAX value,
-
 
91
 * expands the formatted string and re-sizes the block s1 points to accordingly.
-
 
92
 *
-
 
93
 * Returns the length of the new s1 on success, -1 on failure. On failure, an
-
 
94
 * attempt is made to return s1 unmodified for sanity, in this case 0 is returned.
-
 
95
 * to indicate that s1 was not modified.
-
 
96
 *
-
 
97
 * FIXME: ugly hack to get around asprintf(), if you use this, CHECK ITS VALUE! */
-
 
98
int cli_psprintf(char **s1, const char *fmt, ...)
-
 
99
{
-
 
100
    va_list ap;
-
 
101
    size_t needed, base = PATH_MAX + 1;
-
 
102
    char *tmp = (char *) malloc(base);
-
 
103
 
-
 
104
    if (NULL == tmp)
-
 
105
        return -1;
-
 
106
 
-
 
107
    char *orig = *s1;
-
 
108
 
-
 
109
    memset(tmp, 0, sizeof(tmp));
-
 
110
    va_start(ap, fmt);
-
 
111
    vsnprintf(tmp, base, fmt, ap);
-
 
112
    va_end(ap);
-
 
113
    needed = strlen(tmp) + 1;
-
 
114
    *s1 = realloc(*s1, needed);
-
 
115
    if (NULL == *s1) {
-
 
116
        *s1 = realloc(*s1, strlen(orig) + 1);
-
 
117
        if (NULL == *s1) {
-
 
118
            free(tmp);
-
 
119
            return -1;
-
 
120
        }
-
 
121
        memset(*s1, 0, sizeof(*s1));
-
 
122
        memcpy(*s1, orig, strlen(orig) + 1);
-
 
123
        free(tmp);
-
 
124
        return 0;
-
 
125
    }
-
 
126
    memset(*s1, 0, sizeof(*s1));
-
 
127
    memcpy(*s1, tmp, needed);
-
 
128
    free(tmp);
-
 
129
 
-
 
130
    return (int) needed;
-
 
131
}
-
 
132
   
67
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
133
/* Ported from FBSD strtok.c 8.1 (Berkeley) 6/4/93 */
68
char * cli_strtok_r(char *s, const char *delim, char **last)
134
char * cli_strtok_r(char *s, const char *delim, char **last)
69
{
135
{
70
    char *spanp, *tok;
136
    char *spanp, *tok;
71
    int c, sc;
137
    int c, sc;