~ubuntu-branches/ubuntu/precise/open-iscsi/precise-backports

« back to all changes in this revision

Viewing changes to utils/open-isns/util.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2013-09-20 09:07:42 UTC
  • mfrom: (37.1.5 quantal-proposed)
  • Revision ID: package-import@ubuntu.com-20130920090742-rv5qfxabddc9dk2h
Tags: 2.0.873-3ubuntu5~ubuntu12.04.1
No-change backport to precise (LP: #1228046)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * util.c
 
3
 *
 
4
 * Misc utility functions
 
5
 *
 
6
 * Copyright (C) 2006, 2007 Olaf Kirch <olaf.kirch@oracle.com>
 
7
 */
 
8
 
 
9
#include <sys/stat.h>
 
10
#include <stdlib.h>
 
11
#include <string.h>
 
12
#include <err.h>
 
13
#include <errno.h>
 
14
#include "util.h"
 
15
 
 
16
unsigned long
 
17
parse_size(const char *arg)
 
18
{
 
19
    unsigned long       mult = 1, ret;
 
20
    char                *s;
 
21
 
 
22
    ret = strtol(arg, &s, 0);
 
23
 
 
24
    switch (*s++) {
 
25
    case 'g':
 
26
    case 'G':
 
27
        mult = 1024 * 1024 * 1024;
 
28
        break;
 
29
    case 'm':
 
30
    case 'M':
 
31
        mult = 1024 * 1024;
 
32
        break;
 
33
    case 'k':
 
34
    case 'K':
 
35
        mult = 1024;
 
36
        break;
 
37
 
 
38
    case '\0':
 
39
        return ret;
 
40
 
 
41
    default:
 
42
    bad:
 
43
        err(1, "parse_size: unknown unit in \"%s\"\n", arg);
 
44
    }
 
45
 
 
46
    if (*s != '\0')
 
47
            goto bad;
 
48
 
 
49
    return mult * ret;
 
50
}
 
51
 
 
52
char *
 
53
print_size(unsigned long size)
 
54
{
 
55
        static char     unit[] = "-kMG";
 
56
        static char     buffer[64];
 
57
        unsigned int    power = 0;
 
58
 
 
59
        while (size && !(size % 1024) && power < sizeof(unit)) {
 
60
                size /= 1024;
 
61
                power++;
 
62
        }
 
63
 
 
64
        if (!power) {
 
65
                snprintf(buffer, sizeof(buffer), "%lu", size);
 
66
        } else {
 
67
                snprintf(buffer, sizeof(buffer), "%lu%c",
 
68
                                size, unit[power]);
 
69
        }
 
70
        return buffer;
 
71
}
 
72
 
 
73
unsigned int
 
74
parse_count(const char *arg)
 
75
{
 
76
    unsigned long       ret;
 
77
    char                *s;
 
78
 
 
79
    ret = strtoul(arg, &s, 0);
 
80
    if (*s != '\0')
 
81
        err(1, "parse_count: unexpected character in \"%s\"\n", arg);
 
82
 
 
83
    return ret;
 
84
}
 
85
 
 
86
int
 
87
parse_int(const char *arg)
 
88
{
 
89
    long        ret;
 
90
    char        *s;
 
91
 
 
92
    ret = strtol(arg, &s, 0);
 
93
    if (*s != '\0')
 
94
        err(1, "parse_count: unexpected character in \"%s\"\n", arg);
 
95
 
 
96
    return ret;
 
97
}
 
98
 
 
99
long long
 
100
parse_longlong(const char *arg)
 
101
{
 
102
    long long   ret;
 
103
    char        *s;
 
104
 
 
105
    ret = strtoll(arg, &s, 0);
 
106
    if (*s != '\0')
 
107
        err(1, "parse_count: unexpected character in \"%s\"\n", arg);
 
108
 
 
109
    return ret;
 
110
}
 
111
 
 
112
double
 
113
parse_double(const char *arg)
 
114
{
 
115
        double  ret;
 
116
        char    *s;
 
117
 
 
118
        ret = strtod(arg, &s);
 
119
        if (*s != '\0')
 
120
                err(1, "parse_count: unexpected character in \"%s\"\n", arg);
 
121
 
 
122
        return ret;
 
123
}
 
124
 
 
125
unsigned int
 
126
parse_timeout(const char *arg)
 
127
{
 
128
        unsigned int    v, ret = 0;
 
129
        char            *s;
 
130
 
 
131
        do {
 
132
                v = strtoul(arg, &s, 10);
 
133
                switch (*s) {
 
134
                case '\0':
 
135
                        ret += v;
 
136
                        break;
 
137
                case 'd':
 
138
                        v *= 24;
 
139
                case 'h':
 
140
                        v *= 60;
 
141
                case 'm':
 
142
                        v *= 60;
 
143
                case 's':
 
144
                        ret += v;
 
145
                        ++s;
 
146
                        break;
 
147
 
 
148
                default:
 
149
                        errx(1, "parse_timeout: unexpected character in \"%s\"\n",
 
150
                                        arg);
 
151
                }
 
152
 
 
153
                arg = s;
 
154
        } while (*arg);
 
155
 
 
156
        return ret;
 
157
}
 
158
 
 
159
void
 
160
isns_string_array_append(struct string_array *array, const char *val)
 
161
{
 
162
        if (!(array->count % 32)) {
 
163
                array->list = isns_realloc(array->list,
 
164
                                (array->count + 32) * sizeof(val));
 
165
        }
 
166
        array->list[array->count++] = val? isns_strdup(val) : NULL;
 
167
}
 
168
 
 
169
void
 
170
isns_string_array_destroy(struct string_array *array)
 
171
{
 
172
        unsigned int    i;
 
173
 
 
174
        for (i = 0; i < array->count; ++i)
 
175
                isns_free(array->list[i]);
 
176
        isns_free(array->list);
 
177
        memset(array, 0, sizeof(*array));
 
178
}
 
179
 
 
180
void
 
181
isns_assign_string(char **var, const char *val)
 
182
{
 
183
        char    *s = NULL;
 
184
 
 
185
        if (val && !(s = isns_strdup(val)))
 
186
                errx(1, "out of memory");
 
187
 
 
188
        if (*var)
 
189
                isns_free(*var);
 
190
        *var = s;
 
191
}
 
192
 
 
193
/*
 
194
 * Recursively create a directory
 
195
 */
 
196
int
 
197
isns_mkdir_recursive(const char *pathname)
 
198
{
 
199
        const char *orig_pathname = pathname;
 
200
        char    *squirrel[64];
 
201
        char    *copy = NULL, *s;
 
202
        int     ns = 0;
 
203
 
 
204
        if (!pathname || !strcmp(pathname, "."))
 
205
                return 0;
 
206
        while (1) {
 
207
                if (mkdir(pathname, 0755) >= 0) {
 
208
                        if (ns == 0)
 
209
                                break;
 
210
                        *squirrel[--ns] = '/';
 
211
                        continue;
 
212
                }
 
213
 
 
214
                if (errno == EEXIST)
 
215
                        goto good;
 
216
                if (errno != ENOENT)
 
217
                        goto bad;
 
218
 
 
219
                if (copy == NULL) {
 
220
                        copy = isns_strdup(pathname);
 
221
                        pathname = copy;
 
222
                }
 
223
 
 
224
                s = strrchr(copy, '/');
 
225
                while (s > copy && s[-1] == '/')
 
226
                        --s;
 
227
                *s = '\0';
 
228
 
 
229
                isns_assert(ns < 64);
 
230
                squirrel[ns++] = s;
 
231
 
 
232
                if (s == copy)
 
233
                        goto bad;
 
234
        }
 
235
 
 
236
good:   if (copy)
 
237
                isns_free(copy);
 
238
        errno = 0;
 
239
        return 0;
 
240
 
 
241
bad:    if (copy)
 
242
                isns_free(copy);
 
243
        perror(orig_pathname);
 
244
        return -1;
 
245
}
 
246
 
 
247
/*
 
248
 * This one differs from POSIX dirname; it does not
 
249
 * modify its argument
 
250
 */
 
251
const char *
 
252
isns_dirname(const char *pathname)
 
253
{
 
254
        static char     buffer[4096];
 
255
        char            *s;
 
256
 
 
257
        strcpy(buffer, pathname);
 
258
        if ((s = strrchr(buffer, '/')) != NULL) {
 
259
                *s = '\0';
 
260
                return buffer;
 
261
        }
 
262
        return ".";
 
263
}