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

Last change on this file since 2314 was 2314, checked in by bruno, 10 years ago

r3325@localhost: bruno | 2009-08-01 22:25:29 +0200
number_of_disks_as_string now alloctes memory

  • Property svn:keywords set to Id
File size: 29.9 KB
Line 
1/* $Id: libmondo-string.c 2314 2009-08-18 12:29:14Z 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 2314 2009-08-18 12:29:14Z 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.
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 */
508void resolve_naff_tokens(char *output, char *ip, char *value, char *token)
509{
510    /*@ buffers *** */
511    char *input;
512
513    /*@ pointers * */
514    char *p;
515
516    input = malloc(2000);
517    // BERLIOS: seems to cause invalid errors
518    //assert_string_is_neither_NULL_nor_zerolength(ip);
519    assert_string_is_neither_NULL_nor_zerolength(token);
520    assert(value != NULL);
521
522    strcpy(output, ip);         /* just in case the token doesn't appear in string at all */
523    for (strcpy(input, ip); strstr(input, token); strcpy(input, output)) {
524        strcpy(output, input);
525        p = strstr(output, token);
526        *p = '\0';
527        strcat(output, value);
528        p = strstr(input, token) + strlen(token);
529        strcat(output, p);
530    }
531    paranoid_free(input);
532}
533
534
535
536
537
538/**
539 * Generate the filename of slice @p sliceno of biggiefile @p bigfileno
540 * in @p path with suffix @p s. The format is as follows: @p path, followed
541 * by "/slice-" and @p bigfileno zero-padded to 7 places, followed by
542 * a dot and @p sliceno zero-padded to 5 places, followed by ".dat" and the
543 * suffix. The string is a minimum of 24 characters long.
544 * @param bigfileno The biggiefile number. Starts from 0.
545 * @param sliceno The slice number of biggiefile @p bigfileno. 0 is a "header"
546 * slice (no suffix) containing the biggiestruct, then are the compressed
547 * slices, then an empty uncompressed "trailer" slice.
548 * @param path The path to append (with a / in the middle) to the slice filename.
549 * @param s If not "" then add a "." and this to the end.
550 * @return The slice filename.
551 * @note The returned value points to static storage and will be overwritten with each call.
552 */
553char *slice_fname(long bigfileno, long sliceno, char *path, char *s)
554{
555
556    /*@ buffers **************************************************** */
557    static char output[MAX_STR_LEN];
558    static char suffix[MAX_STR_LEN];
559
560    /*@ end vars *************************************************** */
561
562    assert_string_is_neither_NULL_nor_zerolength(path);
563    if (s[0] != '\0') {
564        sprintf(suffix, ".%s", s);
565    } else {
566        suffix[0] = '\0';
567    }
568    sprintf(output, "%s/slice-%07ld.%05ld.dat%s", path, bigfileno, sliceno,
569            suffix);
570    return (output);
571}
572
573
574/**
575 * Generate a spinning symbol based on integer @p i.
576 * The symbol rotates through the characters / - \ | to form an ASCII "spinner"
577 * if successively written to the same location on screen.
578 * @param i The amount of progress or whatever else to use to determine the character
579 * for this iteration of the spinner.
580 * @return The character for this iteration.
581 */
582int special_dot_char(int i)
583{
584    switch (i % 4) {
585    case 0:
586        return ('/');
587    case 1:
588        return ('-');
589    case 2:
590        return ('\\');
591    case 3:
592        return ('|');
593    default:
594        return ('.');
595    }
596    return ('.');
597}
598
599
600
601
602/**
603 * Wrap @p flaws_str across three lines. The first two are no more than 74 characters wide.
604 * @param flaws_str The original string to split.
605 * @param flaws_str_A Where to put the first 74-or-less characters.
606 * @param flaws_str_B Where to put the second 74-or-less characters.
607 * @param flaws_str_C Where to put the rest.
608 * @param res The result of the original evaluate_mountlist() operation.
609 * @return TRUE if res == 0, FALSE otherwise.
610 */
611bool
612spread_flaws_across_three_lines(char *flaws_str, char *flaws_str_A,
613                                char *flaws_str_B, char *flaws_str_C,
614                                int res)
615{
616
617    /*@ int ************************************************************* */
618    int i = 0;
619
620    /*@ initialize ****************************************************** */
621    assert(flaws_str_A != NULL);
622    assert(flaws_str_B != NULL);
623    assert(flaws_str_C != NULL);
624    assert(flaws_str != NULL);
625
626    if (!res && !strlen(flaws_str)) {
627        return (TRUE);
628    }
629    if (strlen(flaws_str) > 0) {
630        paranoid_free(flaws_str_A);
631        mr_asprintf(&flaws_str_A, "%s", flaws_str + 1);
632    }
633    if (strlen(flaws_str_A) >= 74) {
634        for (i = 74; flaws_str_A[i] != ' '; i--);
635        paranoid_free(flaws_str_B);
636        mr_asprintf(&flaws_str_B, "%s", flaws_str_A + i + 1);
637        flaws_str_A[i] = '\0';
638    }
639    if (strlen(flaws_str_B) >= 74) {
640        for (i = 74; flaws_str_B[i] != ' '; i--);
641        paranoid_free(flaws_str_C);
642        mr_asprintf(&flaws_str_C, "%s", flaws_str_B + i + 1);
643        flaws_str_B[i] = '\0';
644    }
645    if (res) {
646        return (FALSE);
647    } else {
648        return (TRUE);
649    }
650}
651
652
653
654/**
655 * Compare @p stringA and @p stringB. This uses an ASCII sort for everything
656 * up to the digits on the end but a numerical sort for the digits on the end.
657 * @param stringA The first string to compare.
658 * @param stringB The second string to compare.
659 * @return The same as strcmp() - <0 if A<B, 0 if A=B, >0 if A>B.
660 * @note This function only does a numerical sort on the @e last set of numbers. If
661 * there are any in the middle those will be sorted ASCIIbetically.
662 */
663int strcmp_inc_numbers(char *stringA, char *stringB)
664{
665    /*@ int ********************************************************* */
666    int i;
667    int start_of_numbers_in_A;
668    int start_of_numbers_in_B;
669    int res;
670
671    /*@ long ******************************************************* */
672    long numA;
673    long numB;
674
675    /*@ end vars *************************************************** */
676    assert(stringA != NULL);
677    assert(stringB != NULL);
678
679    if (strlen(stringA) == strlen(stringB)) {
680        return (strcmp(stringA, stringB));
681    }
682    for (i = (int) strlen(stringA); i > 0 && isdigit(stringA[i - 1]); i--);
683    if (i == (int) strlen(stringA)) {
684        return (strcmp(stringA, stringB));
685    }
686    start_of_numbers_in_A = i;
687    for (i = (int) strlen(stringB); i > 0 && isdigit(stringB[i - 1]); i--);
688    if (i == (int) strlen(stringB)) {
689        return (strcmp(stringA, stringB));
690    }
691    start_of_numbers_in_B = i;
692    if (start_of_numbers_in_A != start_of_numbers_in_B) {
693        return (strcmp(stringA, stringB));
694    }
695    res = strncmp(stringA, stringB, (size_t) i);
696    if (res) {
697        return (res);
698    }
699    numA = atol(stringA + start_of_numbers_in_A);
700    numB = atol(stringB + start_of_numbers_in_B);
701    /*
702       sprintf(tmp,"Comparing %s and %s --> %ld,%ld\n",stringA,stringB,numA,numB);
703       log_to_screen(tmp);
704     */
705    return ((int) (numA - numB));
706}
707
708
709
710/**
711 * Strip excess baggage from @p input, which should be a line from afio.
712 * For now this copies the whole line unless it finds a set of quotes, in which case
713 * it copies their contents only.
714 * @param input The input line (presumably from afio).
715 * @return The stripped line.
716 * @note The returned string points to static storage that will be overwritten with each call.
717 */
718char *strip_afio_output_line(char *input)
719{
720    /*@ buffer ****************************************************** */
721    static char output[MAX_STR_LEN];
722
723    /*@ pointers **************************************************** */
724    char *p;
725    char *q;
726    /*@ end vars *************************************************** */
727
728    assert(input != NULL);
729    strcpy(output, input);
730    p = strchr(input, '\"');
731    if (p) {
732        q = strchr(++p, '\"');
733        if (q) {
734            strcpy(output, p);
735            *(strchr(output, '\"')) = '\0';
736        }
737    }
738    return (output);
739}
740
741
742
743/**
744 * Remove all characters whose ASCII value is less than or equal to 32
745 * (spaces and control characters) from both sides of @p in_out.
746 * @param in_out The string to strip spaces/control characters from (modified).
747 */
748void strip_spaces(char *in_out)
749{
750    /*@ buffers ***************************************************** */
751    char *tmp;
752
753    /*@ pointers **************************************************** */
754    char *p;
755
756    /*@ int ******************************************************** */
757    int i;
758    int original_incoming_length;
759
760    /*@ end vars *************************************************** */
761
762    assert(in_out != NULL);
763    malloc_string(tmp);
764    original_incoming_length = (int) strlen(in_out);
765    for (i = 0; in_out[i] <= ' ' && i < (int) strlen(in_out); i++);
766    strcpy(tmp, in_out + i);
767    for (i = (int) strlen(tmp); i > 0 && tmp[i - 1] <= 32; i--);
768    tmp[i] = '\0';
769    for (i = 0; i < original_incoming_length && MAX_STR_LEN; i++) {
770        in_out[i] = ' ';
771    }
772    in_out[i] = '\0';
773    i = 0;
774    p = tmp;
775    while (*p != '\0') {
776        in_out[i] = *(p++);
777        in_out[i + 1] = '\0';
778        if (in_out[i] < 32 && i > 0) {
779            if (in_out[i] == 8) {
780                i--;
781            } else if (in_out[i] == 9) {
782                in_out[i++] = ' ';
783            } else if (in_out[i] == '\r')   // added 1st October 2003 -- FIXME
784            {
785                strcpy(tmp, in_out + i);
786                strcpy(in_out, tmp);
787                i = -1;
788                continue;
789            } else if (in_out[i] == '\t') {
790                for (i++; i % 5; i++);
791            } else if (in_out[i] >= 10 && in_out[i] <= 13) {
792                break;
793            } else {
794                i--;
795            }
796        } else {
797            i++;
798        }
799    }
800    in_out[i] = '\0';
801    paranoid_free(tmp);
802}
803
804
805/**
806 * If there are double quotes "" around @p incoming then remove them.
807 * This does not affect other quotes that may be embedded within the string.
808 * @param incoming The string to trim quotes from (modified).
809 * @return @p incoming.
810 */
811char *trim_empty_quotes(char *incoming)
812{
813    /*@ buffer ****************************************************** */
814    static char outgoing[MAX_STR_LEN];
815
816    /*@ end vars *************************************************** */
817    assert(incoming != NULL);
818
819    if (incoming[0] == '\"' && incoming[strlen(incoming) - 1] == '\"') {
820        strcpy(outgoing, incoming + 1);
821        outgoing[strlen(outgoing) - 1] = '\0';
822    } else {
823        strcpy(outgoing, incoming);
824    }
825    return (outgoing);
826}
827
828
829
830
831/**
832 * Remove any partition info from @p partition, leaving just the drive name.
833 * @param partition The partition name soon-to-become drive name. (modified)
834 * @return @p partition.
835 */
836char *truncate_to_drive_name(char *partition)
837{
838    int i = strlen(partition) - 1;
839    char *c;
840    char *trunc = NULL;
841
842    assert_string_is_neither_NULL_nor_zerolength(partition);
843    mr_asprintf(&trunc, "%s", partition);
844
845#ifdef __FreeBSD__
846
847    if (islower(trunc[i]))  // BSD subpartition
848        i--;
849    if (trunc[i-1] == 's') {
850        while (isdigit(trunc[i]))
851            i--;
852        i--;
853    }
854    trunc[i+1] = '\0';
855
856#else
857
858    /* first see if it's a devfs style device */
859    c = strrchr(trunc, '/');
860    if (c && strncmp(c, "/part", 5) == 0) {
861        /* yup it's devfs, return the "disc" path */
862        strcpy(c + 1, "disc");
863        return trunc;
864    }
865    /* then see if it's a dm style device */
866    if (c && strncmp(c, "/dm-", 4) == 0) {
867        /* yup it's dm, return the full path */
868        return trunc;
869    }
870
871
872    for (i = strlen(trunc); isdigit(trunc[i-1]); i--)
873        continue;
874    if (trunc[i-1] == 'p' && isdigit(trunc[i-2])) {
875        i--;
876    } else {
877        /* Some full devices like this /dev/mapper/mpath0
878           /dev/cciss/c0d0 may be used as partition names */
879        if ((strstr(trunc,"/dev/mapper/mpath") != NULL) ||
880            (strstr(trunc,"/dev/cciss/c") != NULL) ||
881            (strstr(trunc,"/dev/rd/") != NULL)) {
882                return trunc;
883            }
884    }
885    trunc[i] = '\0';
886
887#endif
888
889    return trunc;
890}
891
892
893
894
895
896/**
897 * Turn a RAID level number (-1 to 5) into a friendly string. The string
898 * is either "Linear RAID" for -1, or " RAID %-2d " (%d = @p raid_level)
899 * for anything else.
900 * @param raid_level The RAID level to stringify.
901 * @return The string form of @p raid_level.
902 * @note The returned value points to static storage that will be overwritten with each call.
903 */
904char *turn_raid_level_number_to_string(int raid_level)
905{
906
907    /*@ buffer ********************************************************** */
908    static char output[MAX_STR_LEN];
909
910
911
912    if (raid_level >= 0) {
913        sprintf(output, " RAID %-2d ", raid_level);
914    } else {
915        sprintf(output, "Linear RAID");
916    }
917    return (output);
918}
919
920
921
922
923
924
925
926
927
928/**
929 * Determine the severity (1-3, 1 being low) of the fact that
930 * @p fn changed in the live filesystem (verify/compare).
931 * @param fn The filename that changed.
932 * @param out_reason If non-NULL, a descriptive reason for the difference will be copied here.
933 * @return The severity (1-3).
934 */
935int severity_of_difference(char *fn, char *out_reason)
936{
937    int sev;
938    char *reason;
939    char *filename;
940
941    malloc_string(reason);
942    malloc_string(filename);
943   
944    // out_reason might be null on purpose, so don't bomb if it is :) OK?
945    assert_string_is_neither_NULL_nor_zerolength(fn);
946    if (!strncmp(fn, MNT_RESTORING, strlen(MNT_RESTORING))) {
947        strcpy(filename, fn + strlen(MNT_RESTORING));
948    } else if (fn[0] != '/') {
949        sprintf(filename, "/%s", fn);
950    } else {
951        strcpy(filename, fn);
952    }
953
954    sev = 3;
955    sprintf(reason,
956            "Changed since backup. Consider running a differential backup in a day or two.");
957    if (!strncmp(filename, "/var/", 5)) {
958        sev = 2;
959        sprintf(reason,
960                "/var's contents will change regularly, inevitably.");
961    }
962    if (!strncmp(filename, "/home", 5)) {
963        sev = 2;
964        sprintf(reason,
965                "It's in your /home directory. Therefore, it is important.");
966    }
967    if (!strncmp(filename, "/usr/", 5)) {
968        sev = 3;
969        sprintf(reason,
970                "You may have installed/removed software during the backup.");
971    }
972    if (!strncmp(filename, "/etc/", 5)) {
973        sev = 3;
974        sprintf(reason,
975                "Do not edit config files while backing up your PC.");
976    }
977    if (!strcmp(filename, "/etc/adjtime")
978        || !strcmp(filename, "/etc/mtab")) {
979        sev = 1;
980        sprintf(reason, "This file changes all the time. It's OK.");
981    }
982    if (!strncmp(filename, "/root/", 6)) {
983        sev = 3;
984        sprintf(reason, "Were you compiling/editing something in /root?");
985    }
986    if (!strncmp(filename, "/root/.", 7)) {
987        sev = 2;
988        sprintf(reason, "Temp or 'dot' files changed in /root.");
989    }
990    if (!strncmp(filename, "/var/lib/", 9)) {
991        sev = 2;
992        sprintf(reason, "Did you add/remove software during backing?");
993    }
994    if (!strncmp(filename, "/var/lib/rpm", 12)) {
995        sev = 3;
996        sprintf(reason, "Did you add/remove software during backing?");
997    }
998    if (!strncmp(filename, "/var/lib/slocate", 16)) {
999        sev = 1;
1000        sprintf(reason,
1001                "The 'update' daemon ran during backup. This does not affect the integrity of your backup.");
1002    }
1003    if (!strncmp(filename, "/var/log/", 9)
1004        || strstr(filename, "/.xsession")
1005        || !strcmp(filename + strlen(filename) - 4, ".log")) {
1006        sev = 1;
1007        sprintf(reason,
1008                "Log files change frequently as the computer runs. Fret not.");
1009    }
1010    if (!strncmp(filename, "/var/spool", 10)) {
1011        sev = 1;
1012        sprintf(reason,
1013                "Background processes or printers were active. This does not affect the integrity of your backup.");
1014    }
1015    if (!strncmp(filename, "/var/spool/mail", 10)) {
1016        sev = 2;
1017        sprintf(reason, "Mail was sent/received during backup.");
1018    }
1019    if (filename[strlen(filename) - 1] == '~') {
1020        sev = 1;
1021        sprintf(reason,
1022                "Backup copy of another file which was modified recently.");
1023    }
1024    if (strstr(filename, "cache")) {
1025        sev = 1;
1026        sprintf(reason,
1027                "Part of a cache of data. Caches change from time to time. Don't worry.");
1028    }
1029    if (!strncmp(filename, "/var/run/", 9)
1030        || !strncmp(filename, "/var/lock", 8)
1031        || strstr(filename, "/.DCOPserver") || strstr(filename, "/.MCOP")
1032        || strstr(filename, "/.Xauthority")) {
1033        sev = 1;
1034        sprintf(reason,
1035                "Temporary file (a lockfile, perhaps) used by software such as X or KDE to register its presence.");
1036    }
1037    if (out_reason) {
1038        strcpy(out_reason, reason);
1039    }
1040    paranoid_free(filename);
1041    paranoid_free(reason);
1042    return (sev);
1043}
1044
1045
1046
1047/**
1048 * Compare the filenames in two filelist entries (s_filelist_entry*) casted
1049 * to void*.
1050 * @param va The first filelist entry, cast as a @c void pointer.
1051 * @param vb The second filelist entry, cast as a @c void pointer.
1052 * @return The return value of strcmp().
1053 */
1054int compare_two_filelist_entries(void *va, void *vb)
1055{
1056    static int res;
1057    struct s_filelist_entry *fa, *fb;
1058
1059    assert(va != NULL);
1060    assert(vb != NULL);
1061    fa = (struct s_filelist_entry *) va;
1062    fb = (struct s_filelist_entry *) vb;
1063    res = strcmp(fa->filename, fb->filename);
1064    return (res);
1065}
1066
1067
1068
1069
1070
1071
1072
1073/**
1074 * Generate a line intended to be passed to update_evalcall_form(), indicating
1075 * the current media fill percentage (or number of kilobytes if size is not known).
1076 * @param bkpinfo The backup media structure. Fields used:
1077 * - @c bkpinfo->backup_media_type
1078 * - @c bkpinfo->media_size
1079 * - @c bkpinfo->scratchdir
1080 * @return The string indicating media fill.
1081 * @note The returned string points to static storage that will be overwritten with each call.
1082 */
1083char *percent_media_full_comment()
1084{
1085    /*@ int *********************************************** */
1086    int percentage;
1087
1088    /*@ buffers ******************************************* */
1089    char *outstr = NULL;
1090    char *pos_w_commas, *tmp;
1091    char *mds = NULL;
1092
1093    assert(bkpinfo != NULL);
1094
1095    if (bkpinfo->media_size[g_current_media_number] <= 0) {
1096        malloc_string(pos_w_commas);
1097        malloc_string(tmp);
1098        sprintf(tmp, "%lld", g_tape_posK);
1099        strcpy(pos_w_commas, commarize(tmp));
1100        paranoid_free(tmp);
1101        mr_asprintf(&outstr, "Volume %d: %s kilobytes archived so far", g_current_media_number, pos_w_commas);
1102        paranoid_free(pos_w_commas);
1103        return (outstr);
1104    }
1105
1106/* update screen */
1107    if (IS_THIS_A_STREAMING_BACKUP(bkpinfo->backup_media_type)) {
1108        percentage =
1109            (int) (g_tape_posK / 10 /
1110                   bkpinfo->media_size[g_current_media_number]);
1111        if (percentage > 100) {
1112            percentage = 100;
1113        }
1114        mr_asprintf(&outstr, "Volume %d: [", g_current_media_number);
1115    } else {
1116        percentage =
1117            (int) (space_occupied_by_cd(bkpinfo->scratchdir) * 100 / 1024 /
1118                   bkpinfo->media_size[g_current_media_number]);
1119        mds = media_descriptor_string(bkpinfo->backup_media_type);
1120        mr_asprintf(&outstr, "%s %d: [", mds, g_current_media_number);
1121        mr_free(mds);
1122    }
1123    for (j = 0; j < percentage; j += 5) {
1124        mr_strcat(outstr, "*");
1125    }
1126    for (; j < 100; j += 5) {
1127        mr_strcat(outstr, ".");
1128    }
1129    mr_strcat(outstr, "] %d%% used", percentage);
1130    return (outstr);
1131}
1132
1133/**
1134 * Get a string form of @p type_of_bkp.
1135 * @param type_of_bkp The backup type to stringify.
1136 * @return The stringification of @p type_of_bkp.
1137 * @note The returned string points to static storage that will be overwritten with each call.
1138 */
1139/* BCO/BERLIOS change that function to allocate memory, return a pointer and modify caller to free the mem */
1140char *media_descriptor_string(t_bkptype type_of_bkp) {
1141
1142    char *type_of_backup = NULL;
1143
1144    switch (type_of_bkp) {
1145    case dvd:
1146        mr_asprintf(&type_of_backup, "DVD");
1147        break;
1148    case cdr:
1149        mr_asprintf(&type_of_backup, "CDR");
1150        break;
1151    case cdrw:
1152        mr_asprintf(&type_of_backup, "CDRW");
1153        break;
1154    case tape:
1155        mr_asprintf(&type_of_backup, "tape");
1156        break;
1157    case cdstream:
1158        mr_asprintf(&type_of_backup, "CDR");
1159        break;
1160    case udev:
1161        mr_asprintf(&type_of_backup, "udev");
1162        break;
1163    case iso:
1164        mr_asprintf(&type_of_backup, "ISO");
1165        break;
1166    case nfs:
1167        mr_asprintf(&type_of_backup, "nfs");
1168        break;
1169    case usb:
1170        mr_asprintf(&type_of_backup, "USB");
1171        break;
1172    default:
1173        mr_asprintf(&type_of_backup, "ISO");
1174    }
1175    return (type_of_backup);
1176}
Note: See TracBrowser for help on using the repository browser.