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

Last change on this file since 668 was 668, checked in by andree, 18 years ago

Explicitely terminate token after strncpy() in mr_strok() as strncpy()
does not do this. Fixes mdstat parsing errors.

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