1 | /*
|
---|
2 | $Id: libmondo-string.c 1180 2007-02-18 15:41:30Z bruno $
|
---|
3 | */
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * @file
|
---|
7 | * Functions for handling strings.
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include "my-stuff.h"
|
---|
11 | #include "mr_mem.h"
|
---|
12 | #include "mr_str.h"
|
---|
13 |
|
---|
14 | #include "mondostructures.h"
|
---|
15 | #include "libmondo-string.h"
|
---|
16 | #include "libmondo-files-EXT.h"
|
---|
17 | #include "newt-specific-EXT.h"
|
---|
18 | #include "libmondo-tools-EXT.h"
|
---|
19 | #include <math.h>
|
---|
20 |
|
---|
21 | /*@unused@*/
|
---|
22 | //static char cvsid[] = "$Id: libmondo-string.c 1180 2007-02-18 15:41:30Z bruno $";
|
---|
23 |
|
---|
24 | extern int g_current_media_number;
|
---|
25 | extern long long g_tape_posK;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @addtogroup stringGroup
|
---|
29 | * @{
|
---|
30 | */
|
---|
31 | /**
|
---|
32 | * Build a partition name from a drive and a partition number.
|
---|
33 | * @param drive The drive basename of the partition name (e.g. /dev/hda)
|
---|
34 | * @param partno The partition number (e.g. 1)
|
---|
35 | * @param partition Where to put the partition name (e.g. /dev/hda1)
|
---|
36 | * @return @p partition.
|
---|
37 | * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno.
|
---|
38 | */
|
---|
39 | char *build_partition_name(char *partition, const char *drive, int partno)
|
---|
40 | {
|
---|
41 | char *p = NULL;
|
---|
42 | char *c = NULL;
|
---|
43 |
|
---|
44 | assert(partition != NULL);
|
---|
45 | assert_string_is_neither_NULL_nor_zerolength(drive);
|
---|
46 | assert(partno >= 0);
|
---|
47 |
|
---|
48 | p = strcpy(partition, drive);
|
---|
49 | /* is this a devfs device path? */
|
---|
50 | c = strrchr(partition, '/');
|
---|
51 | if (c && strncmp(c, "/disc", 5) == 0) {
|
---|
52 | /* yup it's devfs, return the "part" path */
|
---|
53 | /* format /dev/.../disc */
|
---|
54 | strcpy(c + 1, "part");
|
---|
55 | p = c + 5;
|
---|
56 | } else {
|
---|
57 | p += strlen(p);
|
---|
58 | if (isdigit(p[-1])) {
|
---|
59 | *p++ =
|
---|
60 | #ifdef BSD
|
---|
61 | 's';
|
---|
62 | #else
|
---|
63 | 'p';
|
---|
64 | #endif
|
---|
65 | }
|
---|
66 | }
|
---|
67 | sprintf(p, "%d", partno);
|
---|
68 | return (partition);
|
---|
69 | }
|
---|
70 |
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Pad a string on both sides so it appears centered.
|
---|
74 | * @param in_out The string to be center-padded (modified).
|
---|
75 | * @param width The width of the final result.
|
---|
76 | */
|
---|
77 | void center_string(char *in_out, int width)
|
---|
78 | {
|
---|
79 | char *scratch = NULL;
|
---|
80 | int i = 0; /* purpose */
|
---|
81 | int len = 0; /* purpose */
|
---|
82 | int mid = 0; /* purpose */
|
---|
83 | int x = 0; /* purpose */
|
---|
84 |
|
---|
85 | assert(in_out != NULL);
|
---|
86 | assert(width > 2);
|
---|
87 |
|
---|
88 | if (strlen(in_out) == 0) {
|
---|
89 | return;
|
---|
90 | }
|
---|
91 | mr_asprintf(&scratch, in_out);
|
---|
92 | mr_strip_spaces(scratch);
|
---|
93 | len = (int) strlen(scratch);
|
---|
94 | mid = width / 2;
|
---|
95 | x = mid - len / 2;
|
---|
96 | for (i = 0; i < x; i++) {
|
---|
97 | in_out[i] = ' ';
|
---|
98 | }
|
---|
99 | in_out[i] = '\0';
|
---|
100 | strcat(in_out, scratch);
|
---|
101 | for (i = x + len ; i < width - 1; i++) {
|
---|
102 | in_out[i] = ' ';
|
---|
103 | }
|
---|
104 | in_out[i] = '\0';
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | inline void turn_wildcard_chars_into_literal_chars(char *strout, char *strin)
|
---|
109 | {
|
---|
110 | char *p, *q;
|
---|
111 |
|
---|
112 | for (p = strin, q = strout; *p != '\0'; *(q++) = *(p++)) {
|
---|
113 | if (strchr("[]*?", *p)) {
|
---|
114 | *(q++) = '\\';
|
---|
115 | }
|
---|
116 | }
|
---|
117 | *q = *p; // for the final '\0'
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Turn an entry from the RAID editor's disklist into a GUI-friendly string.
|
---|
123 | * The format is: the device left-aligned and padded to 24 places, followed by a space and the
|
---|
124 | * index, right-aligned and padded to eight places. The total string length
|
---|
125 | * is exactly 33.
|
---|
126 | * @param disklist The disklist to operate on.
|
---|
127 | * @param lino The line number from @p disklist to convert to a string.
|
---|
128 | * @return The string form of the disklist entry.
|
---|
129 | * @note The returned string points to static storage and will be overwritten with each call.
|
---|
130 | */
|
---|
131 | char *disklist_entry_to_string(struct list_of_disks *disklist, int lino)
|
---|
132 | {
|
---|
133 |
|
---|
134 | /*@ buffers ********************************************************** */
|
---|
135 | static char output[MAX_STR_LEN];
|
---|
136 |
|
---|
137 | assert(disklist != NULL);
|
---|
138 |
|
---|
139 | sprintf(output, "%-24s %8d", disklist->el[lino].device,
|
---|
140 | disklist->el[lino].index);
|
---|
141 | return (output);
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Turn a "friendly" sizestring into a number of megabytes.
|
---|
147 | * Supports the suffixes 'k'/'K', 'm'/'M', and 'g'/'G'. Calls
|
---|
148 | * fatal_error() if an unknown suffix is encountered.
|
---|
149 | * @param incoming The sizestring to convert (e.g. "40m", "2g").
|
---|
150 | * @return The size in megabytes.
|
---|
151 | */
|
---|
152 | long friendly_sizestr_to_sizelong(char *incoming)
|
---|
153 | {
|
---|
154 | long outval = 0L;
|
---|
155 | int i = 0;
|
---|
156 | char *tmp = NULL;
|
---|
157 | char ch = ' ';
|
---|
158 |
|
---|
159 | assert_string_is_neither_NULL_nor_zerolength(incoming);
|
---|
160 |
|
---|
161 | if (!incoming[0]) {
|
---|
162 | return(0L);
|
---|
163 | }
|
---|
164 | if (strchr(incoming, '.')) {
|
---|
165 | fatal_error("Please use integers only. No decimal points.");
|
---|
166 | }
|
---|
167 | mr_asprintf(&tmp, "%s", incoming);
|
---|
168 | i = (int) strlen(tmp);
|
---|
169 | if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') {
|
---|
170 | tmp[i - 1] = '\0';
|
---|
171 | }
|
---|
172 | for (i = 0; i < (int) strlen(tmp) && isdigit(tmp[i]); i++);
|
---|
173 | ch = tmp[i];
|
---|
174 | tmp[i] = '\0';
|
---|
175 | outval = atol(tmp);
|
---|
176 | mr_free(tmp);
|
---|
177 |
|
---|
178 | if (ch == 'g' || ch == 'G') {
|
---|
179 | outval = outval * 1024;
|
---|
180 | } else if (ch == 'k' || ch == 'K') {
|
---|
181 | outval = outval / 1024;
|
---|
182 | } else if (ch == 't' || ch == 'T') // terabyte
|
---|
183 | {
|
---|
184 | outval *= 1048576;
|
---|
185 | } else if (ch == 'Y' || ch == 'y') // yottabyte - the biggest measure in the info file
|
---|
186 | {
|
---|
187 | log_it
|
---|
188 | ("Oh my gosh. You actually think a YOTTABYTE will get you anywhere? What're you going to do with 1,208,925,819,614,629,174,706,176 bytes of data?!?!");
|
---|
189 | popup_and_OK
|
---|
190 | (_("That sizespec is more than 1,208,925,819,614,629,174,706,176 bytes. You have a shocking amount of data. Please send a screenshot to the list :-)"));
|
---|
191 | fatal_error("Integer overflow.");
|
---|
192 | } else if (ch != 'm' && ch != 'M') {
|
---|
193 | mr_asprintf(&tmp, "Re: parameter '%s' - bad multiplier ('%c')",
|
---|
194 | incoming, ch);
|
---|
195 | fatal_error(tmp);
|
---|
196 | }
|
---|
197 | return (outval);
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | /**
|
---|
202 | * Turn a marker byte (e.g. BLK_START_OF_BACKUP) into a string (e.g. "BLK_START_OF_BACKUP").
|
---|
203 | * Unknown markers are identified as "BLK_UNKNOWN (%d)" where %d is the decimal value.
|
---|
204 | * @param marker The marker byte to stringify.
|
---|
205 | * @return @p marker as a string.
|
---|
206 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
207 | */
|
---|
208 | char *marker_to_string(int marker)
|
---|
209 | {
|
---|
210 | /*@ buffer ****************************************************** */
|
---|
211 | static char outstr[MAX_STR_LEN];
|
---|
212 |
|
---|
213 |
|
---|
214 | /*@ end vars *************************************************** */
|
---|
215 |
|
---|
216 | switch (marker) {
|
---|
217 | case BLK_START_OF_BACKUP:
|
---|
218 | strcpy(outstr, "BLK_START_OF_BACKUP");
|
---|
219 | break;
|
---|
220 | case BLK_START_OF_TAPE:
|
---|
221 | strcpy(outstr, "BLK_START_OF_TAPE");
|
---|
222 | break;
|
---|
223 | case BLK_START_AN_AFIO_OR_SLICE:
|
---|
224 | strcpy(outstr, "BLK_START_AN_AFIO_OR_SLICE");
|
---|
225 | break;
|
---|
226 | case BLK_STOP_AN_AFIO_OR_SLICE:
|
---|
227 | strcpy(outstr, "BLK_STOP_AN_AFIO_OR_SLICE");
|
---|
228 | break;
|
---|
229 | case BLK_START_AFIOBALLS:
|
---|
230 | strcpy(outstr, "BLK_START_AFIOBALLS");
|
---|
231 | break;
|
---|
232 | case BLK_STOP_AFIOBALLS:
|
---|
233 | strcpy(outstr, "BLK_STOP_AFIOBALLS");
|
---|
234 | break;
|
---|
235 | case BLK_STOP_BIGGIEFILES:
|
---|
236 | strcpy(outstr, "BLK_STOP_BIGGIEFILES");
|
---|
237 | break;
|
---|
238 | case BLK_START_A_NORMBIGGIE:
|
---|
239 | strcpy(outstr, "BLK_START_A_NORMBIGGIE");
|
---|
240 | break;
|
---|
241 | case BLK_START_A_PIHBIGGIE:
|
---|
242 | strcpy(outstr, "BLK_START_A_PIHBIGGIE");
|
---|
243 | break;
|
---|
244 | case BLK_START_EXTENDED_ATTRIBUTES:
|
---|
245 | strcpy(outstr, "BLK_START_EXTENDED_ATTRIBUTES");
|
---|
246 | break;
|
---|
247 | case BLK_STOP_EXTENDED_ATTRIBUTES:
|
---|
248 | strcpy(outstr, "BLK_STOP_EXTENDED_ATTRIBUTES");
|
---|
249 | break;
|
---|
250 | case BLK_START_EXAT_FILE:
|
---|
251 | strcpy(outstr, "BLK_START_EXAT_FILE");
|
---|
252 | break;
|
---|
253 | case BLK_STOP_EXAT_FILE:
|
---|
254 | strcpy(outstr, "BLK_STOP_EXAT_FILE");
|
---|
255 | break;
|
---|
256 | case BLK_START_BIGGIEFILES:
|
---|
257 | strcpy(outstr, "BLK_START_BIGGIEFILES");
|
---|
258 | break;
|
---|
259 | case BLK_STOP_A_BIGGIE:
|
---|
260 | strcpy(outstr, "BLK_STOP_A_BIGGIE");
|
---|
261 | break;
|
---|
262 | case BLK_END_OF_TAPE:
|
---|
263 | strcpy(outstr, "BLK_END_OF_TAPE");
|
---|
264 | break;
|
---|
265 | case BLK_END_OF_BACKUP:
|
---|
266 | strcpy(outstr, "BLK_END_OF_BACKUP");
|
---|
267 | break;
|
---|
268 | case BLK_ABORTED_BACKUP:
|
---|
269 | strcpy(outstr, "BLK_ABORTED_BACKUP");
|
---|
270 | break;
|
---|
271 | case BLK_START_FILE:
|
---|
272 | strcpy(outstr, "BLK_START_FILE");
|
---|
273 | break;
|
---|
274 | case BLK_STOP_FILE:
|
---|
275 | strcpy(outstr, "BLK_STOP_FILE");
|
---|
276 | break;
|
---|
277 | default:
|
---|
278 | sprintf(outstr, "BLK_UNKNOWN (%d)", marker);
|
---|
279 | break;
|
---|
280 | }
|
---|
281 | return (outstr);
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Turn a line from the mountlist into a GUI-friendly string.
|
---|
287 | * The format is as follows: the left-aligned @p device field padded to 24 places,
|
---|
288 | * a space, the left-aligned @p mountpoint field again padded to 24 places, a space,
|
---|
289 | * the left-aligned @p format field padded to 10 places, a space, and the right-aligned
|
---|
290 | * @p size field (in MB) padded to 8 places. The total string length is exactly 69.
|
---|
291 | * @param mountlist The mountlist to operate on.
|
---|
292 | * @param lino The line number in @p mountlist to stringify.
|
---|
293 | * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>].
|
---|
294 | * @note The returned string points to static storage and will be overwritten with each call.
|
---|
295 | */
|
---|
296 | char *mountlist_entry_to_string(struct mountlist_itself *mountlist,
|
---|
297 | int lino)
|
---|
298 | {
|
---|
299 |
|
---|
300 | /*@ buffer *********************************************************** */
|
---|
301 | static char output[MAX_STR_LEN];
|
---|
302 |
|
---|
303 | assert(mountlist != NULL);
|
---|
304 |
|
---|
305 | sprintf(output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device,
|
---|
306 | mountlist->el[lino].mountpoint, mountlist->el[lino].format,
|
---|
307 | mountlist->el[lino].size / 1024);
|
---|
308 | return (output);
|
---|
309 | }
|
---|
310 |
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Generate a friendly string containing "X blah blah disk(s)"
|
---|
314 | * @param noof_disks The number of disks (the X).
|
---|
315 | * @param label The "blah blah" part in the middle. If you leave this blank
|
---|
316 | * there will be a weird double space in the middle, so pass *something*.
|
---|
317 | * @return The string containing "X blah blah disk(s)".
|
---|
318 | * @note The returned string points to static storage and will be overwritten with each call.
|
---|
319 | */
|
---|
320 | char *number_of_disks_as_string(int noof_disks, char *label)
|
---|
321 | {
|
---|
322 |
|
---|
323 | /*@ buffers ********************************************************* */
|
---|
324 | static char output[MAX_STR_LEN];
|
---|
325 |
|
---|
326 | /*@ char ******************************************************** */
|
---|
327 | char p = ' ';
|
---|
328 |
|
---|
329 | assert(label != NULL);
|
---|
330 |
|
---|
331 | if (noof_disks > 1) {
|
---|
332 | p = 's';
|
---|
333 | } else {
|
---|
334 | p = ' ';
|
---|
335 | }
|
---|
336 | sprintf(output, "%d %s disk%c", noof_disks, label, p);
|
---|
337 | while (strlen(output) < 14) {
|
---|
338 | strcat(output, " ");
|
---|
339 | }
|
---|
340 | return (output);
|
---|
341 | }
|
---|
342 |
|
---|
343 |
|
---|
344 |
|
---|
345 | /**
|
---|
346 | * Change @p i into a friendly string. If @p i is \<= 10 then write out the
|
---|
347 | * number (e.g. "one", "two", ..., "nine", "ten", "11", ...).
|
---|
348 | * @param i The number to stringify.
|
---|
349 | * @return The string form of @p i.
|
---|
350 | * @note The returned value points to static strorage that will be overwritten with each call.
|
---|
351 | */
|
---|
352 | char *number_to_text(int i)
|
---|
353 | {
|
---|
354 |
|
---|
355 | /*@ buffers ***************************************************** */
|
---|
356 | static char output[MAX_STR_LEN];
|
---|
357 |
|
---|
358 |
|
---|
359 | /*@ end vars *************************************************** */
|
---|
360 |
|
---|
361 | switch (i) {
|
---|
362 | case 0:
|
---|
363 | strcpy(output, "zero");
|
---|
364 | break;
|
---|
365 | case 1:
|
---|
366 | strcpy(output, "one");
|
---|
367 | break;
|
---|
368 | case 2:
|
---|
369 | strcpy(output, "two");
|
---|
370 | break;
|
---|
371 | case 3:
|
---|
372 | strcpy(output, "three");
|
---|
373 | break;
|
---|
374 | case 4:
|
---|
375 | strcpy(output, "four");
|
---|
376 | break;
|
---|
377 | case 5:
|
---|
378 | strcpy(output, "five");
|
---|
379 | break;
|
---|
380 | case 6:
|
---|
381 | strcpy(output, "six");
|
---|
382 | break;
|
---|
383 | case 7:
|
---|
384 | strcpy(output, "seven");
|
---|
385 | break;
|
---|
386 | case 8:
|
---|
387 | strcpy(output, "eight");
|
---|
388 | break;
|
---|
389 | case 9:
|
---|
390 | strcpy(output, "nine");
|
---|
391 | case 10:
|
---|
392 | strcpy(output, "ten");
|
---|
393 | default:
|
---|
394 | sprintf(output, "%d", i);
|
---|
395 | }
|
---|
396 | return (output);
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | /**
|
---|
401 | * Replace all occurences of @p token with @p value while copying @p ip to @p output.
|
---|
402 | * @param ip The input string containing zero or more <tt>token</tt>s.
|
---|
403 | * @param output The output string written with the <tt>token</tt>s replaced by @p value.
|
---|
404 | * @param token The token to be relaced with @p value.
|
---|
405 | * @param value The value to replace @p token.
|
---|
406 | */
|
---|
407 | void resolve_naff_tokens(char *output, char *ip, char *value, char *token)
|
---|
408 | {
|
---|
409 | /*@ buffers *** */
|
---|
410 | char *input;
|
---|
411 |
|
---|
412 | /*@ pointers * */
|
---|
413 | char *p;
|
---|
414 |
|
---|
415 | input = mr_malloc(2000);
|
---|
416 | assert_string_is_neither_NULL_nor_zerolength(ip);
|
---|
417 | assert_string_is_neither_NULL_nor_zerolength(token);
|
---|
418 | assert(value != NULL);
|
---|
419 |
|
---|
420 | strcpy(output, ip); /* just in case the token doesn't appear in string at all */
|
---|
421 | for (strcpy(input, ip); strstr(input, token); strcpy(input, output)) {
|
---|
422 | strcpy(output, input);
|
---|
423 | p = strstr(output, token);
|
---|
424 | *p = '\0';
|
---|
425 | strcat(output, value);
|
---|
426 | p = strstr(input, token) + strlen(token);
|
---|
427 | strcat(output, p);
|
---|
428 | }
|
---|
429 | mr_free(input);
|
---|
430 | }
|
---|
431 |
|
---|
432 |
|
---|
433 | /**
|
---|
434 | * Generate the filename of slice @p sliceno of biggiefile @p bigfileno
|
---|
435 | * in @p path with suffix @p s. The format is as follows: @p path, followed
|
---|
436 | * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by
|
---|
437 | * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the
|
---|
438 | * suffix. The string is a minimum of 24 characters long.
|
---|
439 | * @param bigfileno The biggiefile number. Starts from 0.
|
---|
440 | * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header"
|
---|
441 | * slice (no suffix) containing the biggiestruct, then are the compressed
|
---|
442 | * slices, then an empty uncompressed "trailer" slice.
|
---|
443 | * @param path The path to append (with a / in the middle) to the slice filename.
|
---|
444 | * @param s If not "" then add a "." and this to the end.
|
---|
445 | * @return The slice filename.
|
---|
446 | * @note The returned value points to static storage and will be overwritten with each call.
|
---|
447 | */
|
---|
448 | char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
|
---|
449 | {
|
---|
450 |
|
---|
451 | /*@ buffers **************************************************** */
|
---|
452 | static char output[MAX_STR_LEN];
|
---|
453 | char *suffix = NULL;
|
---|
454 |
|
---|
455 | /*@ end vars *************************************************** */
|
---|
456 |
|
---|
457 | assert_string_is_neither_NULL_nor_zerolength(path);
|
---|
458 | if (s[0] != '\0') {
|
---|
459 | mr_asprintf(&suffix, ".%s", s);
|
---|
460 | } else {
|
---|
461 | mr_asprintf(&suffix, "");
|
---|
462 | }
|
---|
463 | sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno,
|
---|
464 | suffix);
|
---|
465 | mr_free(suffix);
|
---|
466 | return (output);
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /**
|
---|
471 | * Generate a spinning symbol based on integer @p i.
|
---|
472 | * The symbol rotates through the characters / - \ | to form an ASCII "spinner"
|
---|
473 | * if successively written to the same location on screen.
|
---|
474 | * @param i The amount of progress or whatever else to use to determine the character
|
---|
475 | * for this iteration of the spinner.
|
---|
476 | * @return The character for this iteration.
|
---|
477 | */
|
---|
478 | int special_dot_char(int i)
|
---|
479 | {
|
---|
480 | switch (i % 4) {
|
---|
481 | case 0:
|
---|
482 | return ('/');
|
---|
483 | case 1:
|
---|
484 | return ('-');
|
---|
485 | case 2:
|
---|
486 | return ('\\');
|
---|
487 | case 3:
|
---|
488 | return ('|');
|
---|
489 | default:
|
---|
490 | return ('.');
|
---|
491 | }
|
---|
492 | return ('.');
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | /**
|
---|
497 | * Wrap @p flaws_str across three lines. The first two are no more than 74 characters wide.
|
---|
498 | * @param flaws_str The original string to split.
|
---|
499 | * @param flaws_str_A Where to put the first 74-or-less characters.
|
---|
500 | * @param flaws_str_B Where to put the second 74-or-less characters.
|
---|
501 | * @param flaws_str_C Where to put the rest.
|
---|
502 | * @param res The result of the original evaluate_mountlist() operation.
|
---|
503 | * @return TRUE if res == 0, FALSE otherwise.
|
---|
504 | */
|
---|
505 | bool
|
---|
506 | spread_flaws_across_three_lines(char *flaws_str, char *flaws_str_A,
|
---|
507 | char *flaws_str_B, char *flaws_str_C,
|
---|
508 | int res)
|
---|
509 | {
|
---|
510 |
|
---|
511 | /*@ int ************************************************************* */
|
---|
512 | int i = 0;
|
---|
513 |
|
---|
514 | /*@ initialize ****************************************************** */
|
---|
515 | assert(flaws_str_A != NULL);
|
---|
516 | assert(flaws_str_B != NULL);
|
---|
517 | assert(flaws_str_C != NULL);
|
---|
518 | assert(flaws_str != NULL);
|
---|
519 |
|
---|
520 | flaws_str_A[0] = flaws_str_B[0] = flaws_str_C[0] = '\0';
|
---|
521 |
|
---|
522 |
|
---|
523 | if (!res && !strlen(flaws_str)) {
|
---|
524 | return (TRUE);
|
---|
525 | }
|
---|
526 | if (strlen(flaws_str) > 0) {
|
---|
527 | sprintf(flaws_str_A, "%s", flaws_str + 1);
|
---|
528 | }
|
---|
529 | if (strlen(flaws_str_A) >= 74) {
|
---|
530 | for (i = 74; flaws_str_A[i] != ' '; i--);
|
---|
531 | strcpy(flaws_str_B, flaws_str_A + i + 1);
|
---|
532 | flaws_str_A[i] = '\0';
|
---|
533 | }
|
---|
534 | if (strlen(flaws_str_B) >= 74) {
|
---|
535 | for (i = 74; flaws_str_B[i] != ' '; i--);
|
---|
536 | strcpy(flaws_str_C, flaws_str_B + i + 1);
|
---|
537 | flaws_str_B[i] = '\0';
|
---|
538 | }
|
---|
539 | if (res) {
|
---|
540 | return (FALSE);
|
---|
541 | } else {
|
---|
542 | return (TRUE);
|
---|
543 | }
|
---|
544 | }
|
---|
545 |
|
---|
546 |
|
---|
547 | /**
|
---|
548 | * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
|
---|
549 | * up to the digits on the end but a numerical sort for the digits on the end.
|
---|
550 | * @param stringA The first string to compare.
|
---|
551 | * @param stringB The second string to compare.
|
---|
552 | * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B.
|
---|
553 | * @note This function only does a numerical sort on the @e last set of numbers. If
|
---|
554 | * there are any in the middle those will be sorted ASCIIbetically.
|
---|
555 | */
|
---|
556 | int strcmp_inc_numbers(char *stringA, char *stringB)
|
---|
557 | {
|
---|
558 | /*@ int ********************************************************* */
|
---|
559 | int i;
|
---|
560 | int start_of_numbers_in_A;
|
---|
561 | int start_of_numbers_in_B;
|
---|
562 | int res;
|
---|
563 |
|
---|
564 | /*@ long ******************************************************* */
|
---|
565 | long numA;
|
---|
566 | long numB;
|
---|
567 |
|
---|
568 | /*@ end vars *************************************************** */
|
---|
569 | assert(stringA != NULL);
|
---|
570 | assert(stringB != NULL);
|
---|
571 |
|
---|
572 | if (strlen(stringA) == strlen(stringB)) {
|
---|
573 | return (strcmp(stringA, stringB));
|
---|
574 | }
|
---|
575 | for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
|
---|
576 | if (i == (int) strlen(stringA)) {
|
---|
577 | return (strcmp(stringA, stringB));
|
---|
578 | }
|
---|
579 | start_of_numbers_in_A = i;
|
---|
580 | for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
|
---|
581 | if (i == (int) strlen(stringB)) {
|
---|
582 | return (strcmp(stringA, stringB));
|
---|
583 | }
|
---|
584 | start_of_numbers_in_B = i;
|
---|
585 | if (start_of_numbers_in_A != start_of_numbers_in_B) {
|
---|
586 | return (strcmp(stringA, stringB));
|
---|
587 | }
|
---|
588 | res = strncmp(stringA, stringB, (size_t) i);
|
---|
589 | if (res) {
|
---|
590 | return (res);
|
---|
591 | }
|
---|
592 | numA = atol(stringA + start_of_numbers_in_A);
|
---|
593 | numB = atol(stringB + start_of_numbers_in_B);
|
---|
594 | return ((int) (numA - numB));
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 |
|
---|
599 | /**
|
---|
600 | * Strip excess baggage from @p input, which should be a line from afio.
|
---|
601 | * For now this copies the whole line unless it finds a set of quotes, in which case
|
---|
602 | * it copies their contents only.
|
---|
603 | * @param input The input line (presumably from afio).
|
---|
604 | * @return The stripped line.
|
---|
605 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
606 | */
|
---|
607 | char *strip_afio_output_line(char *input)
|
---|
608 | {
|
---|
609 | /*@ buffer ****************************************************** */
|
---|
610 | static char output[MAX_STR_LEN];
|
---|
611 |
|
---|
612 | /*@ pointers **************************************************** */
|
---|
613 | char *p;
|
---|
614 | char *q;
|
---|
615 | /*@ end vars *************************************************** */
|
---|
616 |
|
---|
617 | assert(input != NULL);
|
---|
618 | strcpy(output, input);
|
---|
619 | p = strchr(input, '\"');
|
---|
620 | if (p) {
|
---|
621 | q = strchr(++p, '\"');
|
---|
622 | if (q) {
|
---|
623 | strcpy(output, p);
|
---|
624 | *(strchr(output, '\"')) = '\0';
|
---|
625 | }
|
---|
626 | }
|
---|
627 | return (output);
|
---|
628 | }
|
---|
629 |
|
---|
630 |
|
---|
631 | /**
|
---|
632 | * If there are double quotes "" around @p incoming then remove them.
|
---|
633 | * This does not affect other quotes that may be embedded within the string.
|
---|
634 | * @param incoming The string to trim quotes from (modified).
|
---|
635 | * @return @p incoming.
|
---|
636 | */
|
---|
637 | char *trim_empty_quotes(char *incoming)
|
---|
638 | {
|
---|
639 | /*@ buffer ****************************************************** */
|
---|
640 | static char outgoing[MAX_STR_LEN];
|
---|
641 |
|
---|
642 | /*@ end vars *************************************************** */
|
---|
643 | assert(incoming != NULL);
|
---|
644 |
|
---|
645 | if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
|
---|
646 | strcpy(outgoing, incoming + 1);
|
---|
647 | outgoing[strlen(outgoing) - 1] = '\0';
|
---|
648 | } else {
|
---|
649 | strcpy(outgoing, incoming);
|
---|
650 | }
|
---|
651 | return (outgoing);
|
---|
652 | }
|
---|
653 |
|
---|
654 |
|
---|
655 |
|
---|
656 |
|
---|
657 | /**
|
---|
658 | * Remove any partition info from @p partition, leaving just the drive name.
|
---|
659 | * @param partition The partition name soon-to-become drive name. (modified)
|
---|
660 | * @return @p partition.
|
---|
661 | */
|
---|
662 | char *truncate_to_drive_name(char *partition)
|
---|
663 | {
|
---|
664 | int i = strlen(partition) - 1;
|
---|
665 | char *c = NULL;
|
---|
666 |
|
---|
667 | #ifdef __FreeBSD__
|
---|
668 |
|
---|
669 | if (islower(partition[i])) // BSD subpartition
|
---|
670 | i--;
|
---|
671 | if (partition[i - 1] == 's') {
|
---|
672 | while (isdigit(partition[i]))
|
---|
673 | i--;
|
---|
674 | i--;
|
---|
675 | }
|
---|
676 | partition[i + 1] = '\0';
|
---|
677 |
|
---|
678 | #else
|
---|
679 |
|
---|
680 | assert_string_is_neither_NULL_nor_zerolength(partition);
|
---|
681 | /* first see if it's a devfs style device */
|
---|
682 | c = strrchr(partition, '/');
|
---|
683 | if (c && strncmp(c, "/part", 5) == 0) {
|
---|
684 | /* yup it's devfs, return the "disc" path */
|
---|
685 | strncpy(c + 1, "disc", (size_t)5);
|
---|
686 | return partition;
|
---|
687 | }
|
---|
688 |
|
---|
689 | for (i = strlen(partition); isdigit(partition[i - 1]); i--)
|
---|
690 | continue;
|
---|
691 | if (partition[i - 1] == 'p' && isdigit(partition[i - 2])) {
|
---|
692 | i--;
|
---|
693 | }
|
---|
694 | partition[i] = '\0';
|
---|
695 |
|
---|
696 | #endif
|
---|
697 |
|
---|
698 | return partition;
|
---|
699 | }
|
---|
700 |
|
---|
701 |
|
---|
702 | /**
|
---|
703 | * Turn a RAID level number (-1 to 5) into a friendly string. The string
|
---|
704 | * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level)
|
---|
705 | * for anything else.
|
---|
706 | * @param raid_level The RAID level to stringify.
|
---|
707 | * @return The string form of @p raid_level.
|
---|
708 | * @note The returned value points to static storage that will be overwritten with each call.
|
---|
709 | */
|
---|
710 | char *turn_raid_level_number_to_string(int raid_level)
|
---|
711 | {
|
---|
712 |
|
---|
713 | /*@ buffer ********************************************************** */
|
---|
714 | static char output[MAX_STR_LEN];
|
---|
715 |
|
---|
716 |
|
---|
717 |
|
---|
718 | if (raid_level >= 0) {
|
---|
719 | sprintf(output, " RAID %-2d ", raid_level);
|
---|
720 | } else {
|
---|
721 | sprintf(output, "Linear RAID");
|
---|
722 | }
|
---|
723 | return (output);
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | /**
|
---|
728 | * Determine the severity (1-3, 1 being low) of the fact that
|
---|
729 | * @p fn changed in the live filesystem (verify/compare).
|
---|
730 | * @param fn The filename that changed.
|
---|
731 | * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here.
|
---|
732 | * @return The severity (1-3).
|
---|
733 | */
|
---|
734 | int severity_of_difference(char *fn, char *out_reason)
|
---|
735 | {
|
---|
736 | int sev = 0;
|
---|
737 | char *reason = NULL;
|
---|
738 | char *filename = NULL;
|
---|
739 |
|
---|
740 | // out_reason might be null on purpose, so don't bomb if it is :) OK?
|
---|
741 | assert_string_is_neither_NULL_nor_zerolength(fn);
|
---|
742 | if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
|
---|
743 | mr_asprintf(&filename, fn + strlen(MNT_RESTORING));
|
---|
744 | } else if (fn[0] != '/') {
|
---|
745 | mr_asprintf(&filename, "/%s", fn);
|
---|
746 | } else {
|
---|
747 | mr_asprintf(&filename, fn);
|
---|
748 | }
|
---|
749 |
|
---|
750 | if (!strncmp(filename, "/var/", 5)) {
|
---|
751 | sev = 2;
|
---|
752 | mr_asprintf(&reason,
|
---|
753 | _("/var's contents will change regularly, inevitably."));
|
---|
754 | }
|
---|
755 | if (!strncmp(filename, "/home", 5)) {
|
---|
756 | sev = 2;
|
---|
757 | mr_asprintf(&reason,
|
---|
758 | _("It's in your /home partiton. Therefore, it is important."));
|
---|
759 | }
|
---|
760 | if (!strncmp(filename, "/usr/", 5)) {
|
---|
761 | sev = 3;
|
---|
762 | mr_asprintf(&reason,
|
---|
763 | _("You may have installed/removed software during the backup."));
|
---|
764 | }
|
---|
765 | if (!strncmp(filename, "/etc/", 5)) {
|
---|
766 | sev = 3;
|
---|
767 | mr_asprintf(&reason,
|
---|
768 | _("Do not edit config files while backing up your PC."));
|
---|
769 | }
|
---|
770 | if (!strcmp(filename, "/etc/adjtime")
|
---|
771 | || !strcmp(filename, "/etc/mtab")) {
|
---|
772 | sev = 1;
|
---|
773 | mr_asprintf(&reason, _("This file changes all the time. It's OK."));
|
---|
774 | }
|
---|
775 | if (!strncmp(filename, "/root/", 6)) {
|
---|
776 | sev = 3;
|
---|
777 | mr_asprintf(&reason,
|
---|
778 | _("Were you compiling/editing something in /root?"));
|
---|
779 | }
|
---|
780 | if (!strncmp(filename, "/root/.", 7)) {
|
---|
781 | sev = 2;
|
---|
782 | mr_asprintf(&reason, _("Temp or 'dot' files changed in /root."));
|
---|
783 | }
|
---|
784 | if (!strncmp(filename, "/var/lib/", 9)) {
|
---|
785 | sev = 2;
|
---|
786 | mr_asprintf(&reason, _("Did you add/remove software during backing?"));
|
---|
787 | }
|
---|
788 | if (!strncmp(filename, "/var/lib/rpm", 12)) {
|
---|
789 | sev = 3;
|
---|
790 | mr_asprintf(&reason, _("Did you add/remove software during backing?"));
|
---|
791 | }
|
---|
792 | if (!strncmp(filename, "/var/lib/slocate", 16)) {
|
---|
793 | sev = 1;
|
---|
794 | mr_asprintf(&reason,
|
---|
795 | _("The 'update' daemon ran during backup. This does not affect the integrity of your backup."));
|
---|
796 | }
|
---|
797 | if (!strncmp(filename, "/var/log/", 9)
|
---|
798 | || strstr(filename, "/.xsession")
|
---|
799 | || !strcmp(filename + strlen(filename) - 4, ".log")) {
|
---|
800 | sev = 1;
|
---|
801 | mr_asprintf(&reason,
|
---|
802 | _("Log files change frequently as the computer runs. Fret not."));
|
---|
803 | }
|
---|
804 | if (!strncmp(filename, "/var/spool", 10)) {
|
---|
805 | sev = 1;
|
---|
806 | mr_asprintf(&reason,
|
---|
807 | _("Background processes or printers were active. This does not affect the integrity of your backup."));
|
---|
808 | }
|
---|
809 | if (!strncmp(filename, "/var/spool/mail", 10)) {
|
---|
810 | sev = 2;
|
---|
811 | mr_asprintf(&reason, _("Mail was sent/received during backup."));
|
---|
812 | }
|
---|
813 | if (filename[strlen(filename) - 1] == '~') {
|
---|
814 | sev = 1;
|
---|
815 | mr_asprintf(&reason,
|
---|
816 | _("Backup copy of another file which was modified recently."));
|
---|
817 | }
|
---|
818 | if (strstr(filename, "cache")) {
|
---|
819 | sev = 1;
|
---|
820 | mr_asprintf(&reason,
|
---|
821 | _("Part of a cache of data. Caches change from time to time. Don't worry."));
|
---|
822 | }
|
---|
823 | if (!strncmp(filename, "/var/run/", 9)
|
---|
824 | || !strncmp(filename, "/var/lock", 8)
|
---|
825 | || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
|
---|
826 | || strstr(filename, "/.Xauthority")) {
|
---|
827 | sev = 1;
|
---|
828 | mr_asprintf(&reason,
|
---|
829 | _("Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence."));
|
---|
830 | }
|
---|
831 | mr_free(filename);
|
---|
832 |
|
---|
833 | if (sev == 0) {
|
---|
834 | sev = 3;
|
---|
835 | mr_asprintf(&reason,
|
---|
836 | _("Changed since backup. Consider running a differential backup in a day or two."));
|
---|
837 | }
|
---|
838 | if (out_reason) {
|
---|
839 | strcpy(out_reason, reason);
|
---|
840 | }
|
---|
841 |
|
---|
842 | mr_free(reason);
|
---|
843 | return (sev);
|
---|
844 | }
|
---|
845 |
|
---|
846 |
|
---|
847 | /**
|
---|
848 | * Compare the filenames in two filelist entries (s_filelist_entry*) casted
|
---|
849 | * to void*.
|
---|
850 | * @param va The first filelist entry, cast as a @c void pointer.
|
---|
851 | * @param vb The second filelist entry, cast as a @c void pointer.
|
---|
852 | * @return The return value of strcmp().
|
---|
853 | */
|
---|
854 | int compare_two_filelist_entries(void *va, void *vb)
|
---|
855 | {
|
---|
856 | static int res;
|
---|
857 | struct s_filelist_entry *fa, *fb;
|
---|
858 |
|
---|
859 | assert(va != NULL);
|
---|
860 | assert(vb != NULL);
|
---|
861 | fa = (struct s_filelist_entry *) va;
|
---|
862 | fb = (struct s_filelist_entry *) vb;
|
---|
863 | res = strcmp(fa->filename, fb->filename);
|
---|
864 | return (res);
|
---|
865 | }
|
---|
866 |
|
---|
867 |
|
---|
868 | /**
|
---|
869 | * Generate a line intended to be passed to update_evalcall_form(), indicating
|
---|
870 | * the current media fill percentage (or number of kilobytes if size is not known).
|
---|
871 | * @param bkpinfo The backup media structure. Fields used:
|
---|
872 | * - @c bkpinfo->backup_media_type
|
---|
873 | * - @c bkpinfo->media_size
|
---|
874 | * - @c bkpinfo->scratchdir
|
---|
875 | * @return The string indicating media fill.
|
---|
876 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
877 | */
|
---|
878 | char *percent_media_full_comment(struct s_bkpinfo *bkpinfo)
|
---|
879 | {
|
---|
880 | /*@ int *********************************************** */
|
---|
881 | int percentage = 0;
|
---|
882 | int j = 0;
|
---|
883 |
|
---|
884 | /*@ buffers ******************************************* */
|
---|
885 | static char outstr[MAX_STR_LEN];
|
---|
886 |
|
---|
887 | assert(bkpinfo != NULL);
|
---|
888 |
|
---|
889 | if (bkpinfo->media_size[g_current_media_number] <= 0) {
|
---|
890 | sprintf(outstr, _("Volume %d: %'lld kilobytes archived so far"),
|
---|
891 | g_current_media_number, g_tape_posK);
|
---|
892 | return (outstr);
|
---|
893 | }
|
---|
894 |
|
---|
895 | /* update screen */
|
---|
896 | if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
|
---|
897 | percentage =
|
---|
898 | (int) (g_tape_posK / 10 /
|
---|
899 | bkpinfo->media_size[g_current_media_number]);
|
---|
900 | if (percentage > 100) {
|
---|
901 | percentage = 100;
|
---|
902 | }
|
---|
903 | sprintf(outstr, _("Volume %d: ["), g_current_media_number);
|
---|
904 | } else {
|
---|
905 | percentage =
|
---|
906 | (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
|
---|
907 | bkpinfo->media_size[g_current_media_number]);
|
---|
908 | sprintf(outstr, "%s %d: [",
|
---|
909 | bkpinfo->backup_media_string,
|
---|
910 | g_current_media_number);
|
---|
911 | }
|
---|
912 | for (j = 0; j < percentage; j += 5) {
|
---|
913 | strcat(outstr, "*");
|
---|
914 | }
|
---|
915 | for (; j < 100; j += 5) {
|
---|
916 | strcat(outstr, ".");
|
---|
917 | }
|
---|
918 | j = (int) strlen(outstr);
|
---|
919 | sprintf(outstr + j, "] %d%% used", percentage);
|
---|
920 | return (outstr);
|
---|
921 | }
|
---|
922 |
|
---|
923 |
|
---|
924 |
|
---|
925 | /**
|
---|
926 | * Get a string form of @p type_of_bkp.
|
---|
927 | * @param type_of_bkp The backup type to stringify.
|
---|
928 | * @return The stringification of @p type_of_bkp.
|
---|
929 | * @note The returned string points to static storage that will be overwritten with each call.
|
---|
930 | */
|
---|
931 | char *media_descriptor_string(t_bkptype type_of_bkp)
|
---|
932 | {
|
---|
933 | static char *type_of_backup = NULL;
|
---|
934 |
|
---|
935 | if (!type_of_backup) {
|
---|
936 | malloc_string(type_of_backup);
|
---|
937 | }
|
---|
938 |
|
---|
939 | switch (type_of_bkp) {
|
---|
940 | case dvd:
|
---|
941 | strcpy(type_of_backup, "DVD");
|
---|
942 | break;
|
---|
943 | case cdr:
|
---|
944 | strcpy(type_of_backup, "CDR");
|
---|
945 | break;
|
---|
946 | case cdrw:
|
---|
947 | strcpy(type_of_backup, "CDRW");
|
---|
948 | break;
|
---|
949 | case tape:
|
---|
950 | strcpy(type_of_backup, "tape");
|
---|
951 | break;
|
---|
952 | case cdstream:
|
---|
953 | strcpy(type_of_backup, "CDR");
|
---|
954 | break;
|
---|
955 | case udev:
|
---|
956 | strcpy(type_of_backup, "udev");
|
---|
957 | break;
|
---|
958 | case iso:
|
---|
959 | strcpy(type_of_backup, "ISO");
|
---|
960 | break;
|
---|
961 | case nfs:
|
---|
962 | strcpy(type_of_backup, "nfs");
|
---|
963 | break;
|
---|
964 | case usb:
|
---|
965 | strcpy(type_of_backup, "USB");
|
---|
966 | break;
|
---|
967 | default:
|
---|
968 | strcpy(type_of_backup, "ISO");
|
---|
969 | }
|
---|
970 | return (type_of_backup);
|
---|
971 | }
|
---|
972 |
|
---|