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

Last change on this file since 44 was 44, checked in by bcornec, 14 years ago

asprintf and memory management for libmondo-string.c

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