source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-string.c@ 2316

Last change on this file since 2316 was 2316, checked in by Bruno Cornec, 15 years ago

r3327@localhost: bruno | 2009-08-04 00:17:46 +0200
popup_and_get_string and build_partition_name now return an allocated string

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