source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-string.c@ 1585

Last change on this file since 1585 was 1585, checked in by Bruno Cornec, 17 years ago

Fix bug #187 (Scott Cummings rsc_at_usfamily.net)

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