~psusi/ubuntu/precise/dmraid/fix-gpt

« back to all changes in this revision

Viewing changes to 1.0.0.rc15/lib/misc/misc.c

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona
  • Date: 2010-02-04 21:34:22 UTC
  • mfrom: (1.1.4 upstream) (2.4.3 sid)
  • Revision ID: james.westby@ubuntu.com-20100204213422-tdag8lcxpr7ahmg4
Tags: 1.0.0.rc16-3ubuntu1
* Merge from debian testing. (LP: #503136)  Remaining changes:
  - debian/dmraid-activate: Remove the special-casing of the root
    device which breaks in many situations and leaves the raw devices
    exposed. This was introduced in Debian to accommodate some broken
    configurations which wanted to access "partitions" on the raid
    raw devices. In Ubuntu, broken configurations has not been supported.
  - debian/dmraid.postinst: Comment out "udevadm trigger" call in postinst
    for now as it has severeconsequences when mountall is installed
    (clears /tmp).  If dmraid is installed, then presumably the important
    system devices are up and one canbe bothered with a reboot to take 
    the change into account. Let update-initramfs flag the system
    as needing a reboot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2004,2005  Heinz Mauelshagen, Red Hat GmbH.
3
 
 *                          All rights reserved.
4
 
 *
5
 
 * Copyright (C) 2007   Intel Corporation. All rights reserved.
6
 
 * November, 2007 - additions for Create, Delete, Rebuild & Raid 10. 
7
 
 * 
8
 
 * See file LICENSE at the top of this source tree for license information.
9
 
 */
10
 
 
11
 
#include <stdarg.h>
12
 
#include "internal.h"
13
 
 
14
 
/* Prompt for a yes/no answer */
15
 
int
16
 
yes_no_prompt(struct lib_context *lc, const char *prompt, ...)
17
 
{
18
 
        int c = '\n';
19
 
        va_list ap;
20
 
 
21
 
        /* Use getc() for klibc compatibility. */
22
 
        do {
23
 
                if (c == '\n') {
24
 
                        va_start(ap, prompt);
25
 
                        vprintf(prompt, ap);
26
 
                        va_end(ap);
27
 
                        log_print_nnl(lc, " ? [y/n] :");
28
 
                }
29
 
        } while ((c = tolower(getc(stdin))) && c != 'y' && c != 'n');
30
 
 
31
 
        /* Ignore rest. */
32
 
        while (getc(stdin) != '\n');
33
 
 
34
 
        return c == 'y';
35
 
}
36
 
 
37
 
/* Return the basename of a path. */
38
 
char *
39
 
get_basename(struct lib_context *lc, char *str)
40
 
{
41
 
        char *ret = strrchr(str, '/');
42
 
 
43
 
        return ret ? ++ret : str;
44
 
}
45
 
 
46
 
/* Return the dirname of a path. */
47
 
char *
48
 
get_dirname(struct lib_context *lc, char *str)
49
 
{
50
 
        char *ret = strrchr(str, '/');
51
 
        size_t len = ret ? ret - str : strlen(str);
52
 
 
53
 
        if ((ret = dbg_malloc(len + 1)))
54
 
                strncpy(ret, str, len);
55
 
 
56
 
        return ret;
57
 
}
58
 
 
59
 
/* Convert a numeric string to alpha. */
60
 
void
61
 
mk_alpha(struct lib_context *lc, char *str, size_t len)
62
 
{
63
 
        for (; len && *str; len--, str++) {
64
 
                if (isdigit(*str))
65
 
                        *str += 'a' - '0';
66
 
        }
67
 
}
68
 
 
69
 
/* Remove any whitespace from a string. */
70
 
char *
71
 
remove_white_space(struct lib_context *lc, char *str, size_t size)
72
 
{
73
 
        int c;
74
 
        char *in = str, *out = str;
75
 
 
76
 
        in[size] = 0;
77
 
        while ((c = *in++)) {
78
 
                if (!isspace(c))
79
 
                        *out++ = c;
80
 
        }
81
 
 
82
 
        *out = 0;
83
 
        return str;
84
 
 
85
 
}
86
 
 
87
 
/* Remove any whitespace at the tail of a string */
88
 
void
89
 
remove_tail_space(char *str)
90
 
{
91
 
        char *s = str + strlen(str);
92
 
 
93
 
        while (s-- > str && isspace(*s))
94
 
                *s = 0;
95
 
}
96
 
 
97
 
/* Remove/add a delimiter character. */
98
 
char *
99
 
remove_delimiter(char *ptr, char c)
100
 
{
101
 
        char *ret = NULL;
102
 
 
103
 
        if (ptr && (ret = strchr(ptr, (int) c)))
104
 
                *ret = 0;
105
 
 
106
 
        return ret;
107
 
}
108
 
 
109
 
void
110
 
add_delimiter(char **ptr, char c)
111
 
{
112
 
        if (ptr && *ptr) {
113
 
                **ptr = c;
114
 
                (*ptr)++;
115
 
        }
116
 
}
117
 
 
118
 
char *
119
 
replace_delimiter(char *str, char delim, char c)
120
 
{
121
 
        char *s = str;
122
 
 
123
 
        while ((s = remove_delimiter(s, delim)))
124
 
                add_delimiter(&s, c);
125
 
 
126
 
        return str;
127
 
}
128
 
 
129
 
/* Grow a string. */
130
 
static int
131
 
grow_string(struct lib_context *lc, char **string, const char *s)
132
 
{
133
 
        size_t len;
134
 
        char *tmp = *string;
135
 
 
136
 
        len = strlen(s) + (tmp ? strlen(tmp) + 1 : 1);
137
 
        if ((*string = dbg_realloc(tmp, len))) {
138
 
                if (!tmp)
139
 
                        **string = '\0';
140
 
        }
141
 
        else if (tmp)
142
 
                dbg_free(tmp);
143
 
 
144
 
        return *string ? 1 : 0;
145
 
}
146
 
 
147
 
/* Free a string. */
148
 
void
149
 
free_string(struct lib_context *lc, char **string)
150
 
{
151
 
        if (*string) {
152
 
                dbg_free(*string);
153
 
                *string = NULL;
154
 
        }
155
 
}
156
 
 
157
 
/* Push a string onto the end of another. */
158
 
static int
159
 
p_str(struct lib_context *lc, char **string, const char *s)
160
 
{
161
 
        int ret;
162
 
 
163
 
        if ((ret = grow_string(lc, string, s)))
164
 
                strcat(*string, s);
165
 
 
166
 
        return ret;
167
 
}
168
 
 
169
 
/* Push a string defined by a start and end pointer onto the end of another. */
170
 
static int
171
 
p_str_str(struct lib_context *lc, char **string, char *begin, char *end)
172
 
{
173
 
        if (end == begin)
174
 
                return 1;
175
 
 
176
 
        *end = 0;
177
 
        return p_str(lc, string, begin);
178
 
}
179
 
 
180
 
/* Push an uint64_t in ascii onto the end of a string. */
181
 
static int
182
 
p_u64(struct lib_context *lc, char **string, const uint64_t u)
183
 
{
184
 
        char buffer[22];
185
 
 
186
 
        sprintf(buffer, "%" PRIu64, u);
187
 
        return p_str(lc, string, buffer);
188
 
}
189
 
 
190
 
/* Push an uint_t in ascii onto the end of a string. */
191
 
static int
192
 
p_u(struct lib_context *lc, char **string, const unsigned int u)
193
 
{
194
 
        return p_u64(lc, string, (uint64_t) u);
195
 
}
196
 
 
197
 
/* Push an uint_t in ascii onto the end of a string. */
198
 
static int
199
 
p_d(struct lib_context *lc, char **string, const int d)
200
 
{
201
 
        char buffer[12];
202
 
 
203
 
        sprintf(buffer, "%d", d);
204
 
 
205
 
        return p_str(lc, string, buffer);
206
 
}
207
 
 
208
 
/* Push a format string defined list of arguments onto a string. */
209
 
int
210
 
p_fmt(struct lib_context *lc, char **string, const char *fmt, ...)
211
 
{
212
 
        int ret = 1;
213
 
        char *b, *f, *f_sav;
214
 
        va_list ap;
215
 
 
216
 
        if (!(f = f_sav = dbg_strdup((char *) fmt)))
217
 
                return 0;
218
 
 
219
 
        va_start(ap, fmt);
220
 
        while (ret && *(b = f++)) {
221
 
                if (!(f = strchr(b, '%'))) {
222
 
                        /* No '%' -> just print string. */
223
 
                        ret = p_str(lc, string, b);
224
 
                        break;
225
 
                }
226
 
 
227
 
                if (!(ret = p_str_str(lc, string, b, f)))
228
 
                        break;
229
 
 
230
 
                switch (*++f) {
231
 
                case 'd':
232
 
                        ret = p_d(lc, string, va_arg(ap, int));
233
 
                        break;
234
 
 
235
 
                case 's':
236
 
                        ret = p_str(lc, string, va_arg(ap, char *));
237
 
                        break;
238
 
 
239
 
                case 'u':
240
 
                        ret = p_u(lc, string, va_arg(ap, unsigned int));
241
 
                        break;
242
 
 
243
 
                case 'U':
244
 
                        ret = p_u64(lc, string, va_arg(ap, uint64_t));
245
 
                        break;
246
 
 
247
 
                default:
248
 
                        log_err(lc, "%s: unknown format identifier %%%c",
249
 
                                __func__, *f);
250
 
                        free_string(lc, string);
251
 
                        ret = 0;
252
 
                }
253
 
 
254
 
                f++;
255
 
        }
256
 
 
257
 
        va_end(ap);
258
 
        dbg_free(f_sav);
259
 
 
260
 
        return ret;
261
 
}
262
 
 
263
 
#ifdef DMRAID_LED
264
 
int
265
 
led(const char *path, int status)
266
 
{
267
 
 
268
 
#ifdef DMRAID_INTEL_LED
269
 
        FILE *fd;
270
 
        int sgpio = 0;
271
 
        static char com[100];
272
 
 
273
 
        /* Check if sgpio app is installed. */
274
 
        if ((fd = popen("which sgpio", "r"))) {
275
 
                sgpio = fscanf(fd, "%s", com);
276
 
                close(fd);
277
 
        }
278
 
 
279
 
        if (sgpio != 1) {
280
 
                printf("sgpio app not found\n");
281
 
                return 1;
282
 
        }
283
 
 
284
 
        switch (status) {
285
 
        case LED_REBUILD:
286
 
                sprintf(com, "sgpio -d %s -s rebuild", path);
287
 
                break;
288
 
 
289
 
        case LED_OFF:
290
 
                sprintf(com, "sgpio -d %s -s off", path);
291
 
                break;
292
 
 
293
 
        default:
294
 
                printf("Unknown LED status\n");
295
 
                return 2;
296
 
        }
297
 
 
298
 
        if (system(com) == -1) {
299
 
                printf("Call to sgpio app (%s) failed\n", com);
300
 
                return 4;
301
 
        }
302
 
#endif
303
 
 
304
 
        return 0;
305
 
 
306
 
}
307
 
#endif