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

Last change on this file since 863 was 863, checked in by Bruno Cornec, 14 years ago

merge -r838:862 $SVN_M/branches/stable

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