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

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

strip_spaces => mr_strip_spaces in mr_str.c and corrected at the same time :-)

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