source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-string.c @ 2315

Last change on this file since 2315 was 2315, checked in by Bruno Cornec, 11 years ago

r3326@localhost: bruno | 2009-08-01 23:53:09 +0200
resolve_naff_tokens now returns an allocated char

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