source: MondoRescue/branches/3.2/mondo/src/common/libmondo-string.c@ 3510

Last change on this file since 3510 was 3413, checked in by Bruno Cornec, 9 years ago

Fix remaining compilation warnings

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