source: MondoRescue/branches/stable/mondo/src/common/libmondo-string.c@ 1107

Last change on this file since 1107 was 1080, checked in by Bruno Cornec, 17 years ago
  • Fix mindi install messages (reported by Andree Leidenfrost)
  • remove paranoid_free/free for mr_free
  • mr_asprintf used everywhere
  • mr_malloc used everywhere
  • Property svn:keywords set to Id
File size: 29.4 KB
Line 
1/* $Id: libmondo-string.c 1080 2007-01-28 18:04:41Z 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 1080 2007-01-28 18:04:41Z bruno $";
20
21extern int g_current_media_number;
22extern long long g_tape_posK;
23
24
25/**
26 * @addtogroup stringGroup
27 * @{
28 */
29/**
30 * Build a partition name from a drive and a partition number.
31 * @param drive The drive basename of the partition name (e.g. /dev/hda)
32 * @param partno The partition number (e.g. 1)
33 * @param partition Where to put the partition name (e.g. /dev/hda1)
34 * @return @p partition.
35 * @note If @p drive ends in a digit, then 'p' (on Linux) or 's' (on *BSD) is added before @p partno.
36 */
37char *build_partition_name(char *partition, const char *drive, int partno)
38{
39 char *p, *c;
40
41 assert(partition != NULL);
42 assert_string_is_neither_NULL_nor_zerolength(drive);
43 assert(partno >= 0);
44
45 p = strcpy(partition, drive);
46 /* is this a devfs device path? */
47 c = strrchr(partition, '/');
48 if (c && strncmp(c, "/disc", 5) == 0) {
49 /* yup it's devfs, return the "part" path */
50 strcpy(c + 1, "part");
51 p = c + 5;
52 } else {
53 p += strlen(p);
54 if (isdigit(p[-1])) {
55 *p++ =
56#ifdef BSD
57 's';
58#else
59 'p';
60#endif
61 }
62 }
63 sprintf(p, "%d", partno);
64 return (partition);
65}
66
67
68
69
70
71
72
73
74
75
76
77
78
79/**
80 * Pad a string on both sides so it appears centered.
81 * @param in_out The string to be center-padded (modified).
82 * @param width The width of the final result.
83 */
84void center_string(char *in_out, int width)
85{
86 char scratch[MAX_STR_LEN];
87 char *p;
88 int i; /* purpose */
89 int len; /* purpose */
90 int mid; /* purpose */
91 int x; /* purpose */
92
93 assert(in_out != NULL);
94 assert(width > 2);
95
96 if (strlen(in_out) == 0) {
97 return;
98 }
99 for (p = in_out; *p == ' '; p++);
100 strcpy(scratch, p);
101 strip_spaces (scratch);
102 len = (int) strlen(scratch);
103 mid = width / 2;
104 x = mid - len / 2;
105 for (i = 0; i < x; i++) {
106 in_out[i] = ' ';
107 }
108 in_out[i] = '\0';
109 strcat(in_out, scratch);
110 for (i = x + len ; i < width - 1; i++) {
111 in_out[i] = ' ';
112 }
113 in_out[i] = '\0';
114}
115
116
117
118
119inline void turn_wildcard_chars_into_literal_chars(char *sout, char *sin)
120{
121 char *p, *q;
122
123 for (p = sin, q = sout; *p != '\0'; *(q++) = *(p++)) {
124 if (strchr("[]*?", *p)) {
125 *(q++) = '\\';
126 }
127 }
128 *q = *p; // for the final '\0'
129}
130
131
132
133/**
134 * Add commas every third place in @p input.
135 * @param input The string to commarize.
136 * @return The string with commas.
137 * @note The returned string points to static storage that will be overwritten with each call.
138 */
139char *commarize(char *input)
140{
141 char pos_w_commas[MAX_STR_LEN];
142 static char output[MAX_STR_LEN];
143 char tmp[MAX_STR_LEN];
144 int j;
145
146 assert(input != NULL);
147
148 strcpy(tmp, input);
149 if (strlen(tmp) > 6) {
150 strcpy(pos_w_commas, tmp);
151 j = (int) strlen(pos_w_commas);
152 tmp[j - 6] = ',';
153 strcpy(tmp + j - 5, pos_w_commas + j - 6);
154// tmp[j-2] = ',';
155// strcpy(tmp+j-1, pos_w_commas+j-3);
156 strcpy(pos_w_commas, tmp);
157 }
158 if (strlen(tmp) > 3) {
159 j = (int) strlen(tmp);
160 strcpy(pos_w_commas, tmp);
161 pos_w_commas[j - 3] = ',';
162 strcpy(pos_w_commas + j - 2, tmp + j - 3);
163 } else {
164 strcpy(pos_w_commas, tmp);
165 }
166 strcpy(output, pos_w_commas);
167 return (output);
168}
169
170
171
172
173
174
175
176
177
178/**
179 * Turn an entry from the RAID editor's disklist into a GUI-friendly string.
180 * The format is: the device left-aligned and padded to 24 places, followed by a space and the
181 * index, right-aligned and padded to eight places. The total string length
182 * is exactly 33.
183 * @param disklist The disklist to operate on.
184 * @param lino The line number from @p disklist to convert to a string.
185 * @return The string form of the disklist entry.
186 * @note The returned string points to static storage and will be overwritten with each call.
187 */
188char *disklist_entry_to_string(struct list_of_disks *disklist, int lino)
189{
190
191 /*@ buffers ********************************************************** */
192 static char output[MAX_STR_LEN];
193
194 assert(disklist != NULL);
195
196 sprintf(output, "%-24s %8d", disklist->el[lino].device,
197 disklist->el[lino].index);
198 return (output);
199}
200
201
202
203
204
205/**
206 * Turn a "friendly" sizestring into a number of megabytes.
207 * Supports the suffixes 'k'/'K', 'm'/'M', and 'g'/'G'. Calls
208 * fatal_error() if an unknown suffix is encountered.
209 * @param incoming The sizestring to convert (e.g. "40m", "2g").
210 * @return The size in megabytes.
211 */
212long friendly_sizestr_to_sizelong(char *incoming)
213{
214 long outval;
215 int i;
216 char *tmp;
217 char ch;
218
219 assert_string_is_neither_NULL_nor_zerolength(incoming);
220
221 malloc_string(tmp);
222 if (!incoming[0]) {
223 mr_free(tmp);
224 return (0);
225 }
226 if (strchr(incoming, '.')) {
227 fatal_error("Please use integers only. No decimal points.");
228 }
229 strcpy(tmp, incoming);
230 i = (int) strlen(tmp);
231 if (tmp[i - 1] == 'B' || tmp[i - 1] == 'b') {
232 tmp[i - 1] = '\0';
233 }
234 for (i = 0; i < (int) strlen(tmp) && isdigit(tmp[i]); i++);
235 ch = tmp[i];
236 tmp[i] = '\0';
237 outval = atol(tmp);
238 if (ch == 'g' || ch == 'G') {
239 outval = outval * 1024;
240 } else if (ch == 'k' || ch == 'K') {
241 outval = outval / 1024;
242 } else if (ch == 't' || ch == 'T') // terabyte
243 {
244 outval *= 1048576;
245 } else if (ch == 'Y' || ch == 'y') // yottabyte - the biggest measure in the info file
246 {
247 log_it
248 ("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?!?!");
249 popup_and_OK
250 ("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 :-)");
251 fatal_error("Integer overflow.");
252 } else if (ch != 'm' && ch != 'M') {
253 sprintf(tmp, "Re: parameter '%s' - bad multiplier ('%c')",
254 incoming, ch);
255 fatal_error(tmp);
256 }
257 mr_free(tmp);
258 return (outval);
259}
260
261
262
263/**
264 * Add spaces to the right of @p incoming to make it @p width characters wide.
265 * @param incoming The string to left-pad.
266 * @param width The width to pad it to.
267 * @return The left-padded string.
268 * @note The returned string points to static storage that will be overwritten with each call.
269 * @bug Why does center_string() modify its argument but leftpad_string() returns a modified copy?
270 */
271char *leftpad_string(char *incoming, int width)
272{
273 /*@ buffers ***************************************************** */
274 static char output[MAX_STR_LEN];
275
276 /*@ ints ******************************************************** */
277 int i;
278
279 /*@ end vars **************************************************** */
280 assert(incoming != NULL);
281 assert(width > 2);
282
283 strcpy(output, incoming);
284 for (i = (int) strlen(output); i < width; i++) {
285 output[i] = ' ';
286 }
287 output[i] = '\0';
288 return (output);
289}
290
291
292
293/**
294 * Turn a marker byte (e.g. BLK_START_OF_BACKUP) into a string (e.g. "BLK_START_OF_BACKUP").
295 * Unknown markers are identified as "BLK_UNKNOWN (%d)" where %d is the decimal value.
296 * @param marker The marker byte to stringify.
297 * @return @p marker as a string.
298 * @note The returned string points to static storage that will be overwritten with each call.
299 */
300char *marker_to_string(int marker)
301{
302 /*@ buffer ****************************************************** */
303 static char outstr[MAX_STR_LEN];
304
305
306 /*@ end vars *************************************************** */
307
308 switch (marker) {
309 case BLK_START_OF_BACKUP:
310 strcpy(outstr, "BLK_START_OF_BACKUP");
311 break;
312 case BLK_START_OF_TAPE:
313 strcpy(outstr, "BLK_START_OF_TAPE");
314 break;
315 case BLK_START_AN_AFIO_OR_SLICE:
316 strcpy(outstr, "BLK_START_AN_AFIO_OR_SLICE");
317 break;
318 case BLK_STOP_AN_AFIO_OR_SLICE:
319 strcpy(outstr, "BLK_STOP_AN_AFIO_OR_SLICE");
320 break;
321 case BLK_START_AFIOBALLS:
322 strcpy(outstr, "BLK_START_AFIOBALLS");
323 break;
324 case BLK_STOP_AFIOBALLS:
325 strcpy(outstr, "BLK_STOP_AFIOBALLS");
326 break;
327 case BLK_STOP_BIGGIEFILES:
328 strcpy(outstr, "BLK_STOP_BIGGIEFILES");
329 break;
330 case BLK_START_A_NORMBIGGIE:
331 strcpy(outstr, "BLK_START_A_NORMBIGGIE");
332 break;
333 case BLK_START_A_PIHBIGGIE:
334 strcpy(outstr, "BLK_START_A_PIHBIGGIE");
335 break;
336 case BLK_START_EXTENDED_ATTRIBUTES:
337 strcpy(outstr, "BLK_START_EXTENDED_ATTRIBUTES");
338 break;
339 case BLK_STOP_EXTENDED_ATTRIBUTES:
340 strcpy(outstr, "BLK_STOP_EXTENDED_ATTRIBUTES");
341 break;
342 case BLK_START_EXAT_FILE:
343 strcpy(outstr, "BLK_START_EXAT_FILE");
344 break;
345 case BLK_STOP_EXAT_FILE:
346 strcpy(outstr, "BLK_STOP_EXAT_FILE");
347 break;
348 case BLK_START_BIGGIEFILES:
349 strcpy(outstr, "BLK_START_BIGGIEFILES");
350 break;
351 case BLK_STOP_A_BIGGIE:
352 strcpy(outstr, "BLK_STOP_A_BIGGIE");
353 break;
354 case BLK_END_OF_TAPE:
355 strcpy(outstr, "BLK_END_OF_TAPE");
356 break;
357 case BLK_END_OF_BACKUP:
358 strcpy(outstr, "BLK_END_OF_BACKUP");
359 break;
360 case BLK_ABORTED_BACKUP:
361 strcpy(outstr, "BLK_ABORTED_BACKUP");
362 break;
363 case BLK_START_FILE:
364 strcpy(outstr, "BLK_START_FILE");
365 break;
366 case BLK_STOP_FILE:
367 strcpy(outstr, "BLK_STOP_FILE");
368 break;
369 default:
370 sprintf(outstr, "BLK_UNKNOWN (%d)", marker);
371 break;
372 }
373 return (outstr);
374}
375
376
377
378
379/**
380 * Turn a line from the mountlist into a GUI-friendly string.
381 * The format is as follows: the left-aligned @p device field padded to 24 places,
382 * a space, the left-aligned @p mountpoint field again padded to 24 places, a space,
383 * the left-aligned @p format field padded to 10 places, a space, and the right-aligned
384 * @p size field (in MB) padded to 8 places. The total string length is exactly 69.
385 * @param mountlist The mountlist to operate on.
386 * @param lino The line number in @p mountlist to stringify.
387 * @return The string form of <tt>mountlist</tt>-\>el[<tt>lino</tt>].
388 * @note The returned string points to static storage and will be overwritten with each call.
389 */
390char *mountlist_entry_to_string(struct mountlist_itself *mountlist,
391 int lino)
392{
393
394 /*@ buffer *********************************************************** */
395 static char output[MAX_STR_LEN];
396
397 assert(mountlist != NULL);
398
399 sprintf(output, "%-24s %-24s %-10s %8lld", mountlist->el[lino].device,
400 mountlist->el[lino].mountpoint, mountlist->el[lino].format,
401 mountlist->el[lino].size / 1024);
402 return (output);
403}
404
405
406
407
408
409
410/**
411 * Generate a friendly string containing "X blah blah disk(s)"
412 * @param noof_disks The number of disks (the X).
413 * @param label The "blah blah" part in the middle. If you leave this blank
414 * there will be a weird double space in the middle, so pass *something*.
415 * @return The string containing "X blah blah disk(s)".
416 * @note The returned string points to static storage and will be overwritten with each call.
417 */
418char *number_of_disks_as_string(int noof_disks, char *label)
419{
420
421 /*@ buffers ********************************************************* */
422 static char output[MAX_STR_LEN];
423
424 /*@ char ******************************************************** */
425 char p;
426
427 assert(label != NULL);
428
429 if (noof_disks > 1) {
430 p = 's';
431 } else {
432 p = ' ';
433 }
434 sprintf(output, "%d %s disk%c", noof_disks, label, p);
435 while (strlen(output) < 14) {
436 strcat(output, " ");
437 }
438 return (output);
439}
440
441
442
443/**
444 * Change @p i into a friendly string. If @p i is \<= 10 then write out the
445 * number (e.g. "one", "two", ..., "nine", "ten", "11", ...).
446 * @param i The number to stringify.
447 * @return The string form of @p i.
448 * @note The returned value points to static strorage that will be overwritten with each call.
449 */
450char *number_to_text(int i)
451{
452
453 /*@ buffers ***************************************************** */
454 static char output[MAX_STR_LEN];
455
456
457 /*@ end vars *************************************************** */
458
459 switch (i) {
460 case 0:
461 strcpy(output, "zero");
462 break;
463 case 1:
464 strcpy(output, "one");
465 break;
466 case 2:
467 strcpy(output, "two");
468 break;
469 case 3:
470 strcpy(output, "three");
471 break;
472 case 4:
473 strcpy(output, "four");
474 break;
475 case 5:
476 strcpy(output, "five");
477 break;
478 case 6:
479 strcpy(output, "six");
480 break;
481 case 7:
482 strcpy(output, "seven");
483 break;
484 case 8:
485 strcpy(output, "eight");
486 break;
487 case 9:
488 strcpy(output, "nine");
489 case 10:
490 strcpy(output, "ten");
491 default:
492 sprintf(output, "%d", i);
493 }
494 return (output);
495}
496
497
498
499
500/**
501 * Replace all occurences of @p token with @p value while copying @p ip to @p output.
502 * @param ip The input string containing zero or more <tt>token</tt>s.
503 * @param output The output string written with the <tt>token</tt>s replaced by @p value.
504 * @param token The token to be relaced with @p value.
505 * @param value The value to replace @p token.
506 */
507void resolve_naff_tokens(char *output, char *ip, char *value, char *token)
508{
509 /*@ buffers *** */
510 char *input;
511
512 /*@ pointers * */
513 char *p;
514
515 input = mr_malloc(2000);
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 mr_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 mr_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 partiton. 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 mr_free(filename);
1022 mr_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 mr_free(pos_w_commas);
1116 mr_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 case usb:
1160 strcpy(type_of_backup, "USB");
1161 break;
1162 default:
1163 strcpy(type_of_backup, "ISO");
1164 }
1165 return (type_of_backup);
1166}
Note: See TracBrowser for help on using the repository browser.