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

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