source: branches/2.2.9/mondo/src/common/libmondo-string.c @ 2230

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