2
* Copyright (C) 2004,2005 Heinz Mauelshagen, Red Hat GmbH.
5
* Copyright (C) 2007 Intel Corporation. All rights reserved.
6
* November, 2007 - additions for Create, Delete, Rebuild & Raid 10.
8
* See file LICENSE at the top of this source tree for license information.
14
/* Prompt for a yes/no answer */
16
yes_no_prompt(struct lib_context *lc, const char *prompt, ...)
21
/* Use getc() for klibc compatibility. */
27
log_print_nnl(lc, " ? [y/n] :");
29
} while ((c = tolower(getc(stdin))) && c != 'y' && c != 'n');
32
while (getc(stdin) != '\n');
37
/* Return the basename of a path. */
39
get_basename(struct lib_context *lc, char *str)
41
char *ret = strrchr(str, '/');
43
return ret ? ++ret : str;
46
/* Return the dirname of a path. */
48
get_dirname(struct lib_context *lc, char *str)
50
char *ret = strrchr(str, '/');
51
size_t len = ret ? ret - str : strlen(str);
53
if ((ret = dbg_malloc(len + 1)))
54
strncpy(ret, str, len);
59
/* Convert a numeric string to alpha. */
61
mk_alpha(struct lib_context *lc, char *str, size_t len)
63
for (; len && *str; len--, str++) {
69
/* Remove any whitespace from a string. */
71
remove_white_space(struct lib_context *lc, char *str, size_t size)
74
char *in = str, *out = str;
87
/* Remove any whitespace at the tail of a string */
89
remove_tail_space(char *str)
91
char *s = str + strlen(str);
93
while (s-- > str && isspace(*s))
97
/* Remove/add a delimiter character. */
99
remove_delimiter(char *ptr, char c)
103
if (ptr && (ret = strchr(ptr, (int) c)))
110
add_delimiter(char **ptr, char c)
119
replace_delimiter(char *str, char delim, char c)
123
while ((s = remove_delimiter(s, delim)))
124
add_delimiter(&s, c);
131
grow_string(struct lib_context *lc, char **string, const char *s)
136
len = strlen(s) + (tmp ? strlen(tmp) + 1 : 1);
137
if ((*string = dbg_realloc(tmp, len))) {
144
return *string ? 1 : 0;
149
free_string(struct lib_context *lc, char **string)
157
/* Push a string onto the end of another. */
159
p_str(struct lib_context *lc, char **string, const char *s)
163
if ((ret = grow_string(lc, string, s)))
169
/* Push a string defined by a start and end pointer onto the end of another. */
171
p_str_str(struct lib_context *lc, char **string, char *begin, char *end)
177
return p_str(lc, string, begin);
180
/* Push an uint64_t in ascii onto the end of a string. */
182
p_u64(struct lib_context *lc, char **string, const uint64_t u)
186
sprintf(buffer, "%" PRIu64, u);
187
return p_str(lc, string, buffer);
190
/* Push an uint_t in ascii onto the end of a string. */
192
p_u(struct lib_context *lc, char **string, const unsigned int u)
194
return p_u64(lc, string, (uint64_t) u);
197
/* Push an uint_t in ascii onto the end of a string. */
199
p_d(struct lib_context *lc, char **string, const int d)
203
sprintf(buffer, "%d", d);
205
return p_str(lc, string, buffer);
208
/* Push a format string defined list of arguments onto a string. */
210
p_fmt(struct lib_context *lc, char **string, const char *fmt, ...)
216
if (!(f = f_sav = dbg_strdup((char *) fmt)))
220
while (ret && *(b = f++)) {
221
if (!(f = strchr(b, '%'))) {
222
/* No '%' -> just print string. */
223
ret = p_str(lc, string, b);
227
if (!(ret = p_str_str(lc, string, b, f)))
232
ret = p_d(lc, string, va_arg(ap, int));
236
ret = p_str(lc, string, va_arg(ap, char *));
240
ret = p_u(lc, string, va_arg(ap, unsigned int));
244
ret = p_u64(lc, string, va_arg(ap, uint64_t));
248
log_err(lc, "%s: unknown format identifier %%%c",
250
free_string(lc, string);
265
led(const char *path, int status)
268
#ifdef DMRAID_INTEL_LED
271
static char com[100];
273
/* Check if sgpio app is installed. */
274
if ((fd = popen("which sgpio", "r"))) {
275
sgpio = fscanf(fd, "%s", com);
280
printf("sgpio app not found\n");
286
sprintf(com, "sgpio -d %s -s rebuild", path);
290
sprintf(com, "sgpio -d %s -s off", path);
294
printf("Unknown LED status\n");
298
if (system(com) == -1) {
299
printf("Call to sgpio app (%s) failed\n", com);