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

Last change on this file since 561 was 561, checked in by bcornec, 18 years ago

merge -r 542:560 $SVN_M/branches/stable

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