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

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