source: MondoRescue/trunk/mondo/mondo/common/libmondo-string.c@ 75

Last change on this file since 75 was 58, checked in by bcornec, 19 years ago

Trunk: This version executes correctly mondoarchive
with the new memory management system (r57 didn't)

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