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

Last change on this file since 1081 was 1081, checked in by bruno, 13 years ago

merge -r1078:1080 $SVN_M/branches/stable

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