source: branches/stable/mondo/src/common/libmondo-string.c @ 1156

Last change on this file since 1156 was 1148, checked in by Bruno Cornec, 13 years ago

rewrite of strip_spaces (way shorter, maybe less complete, but valgrind clean)

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