source: MondoRescue/branches/2.2.5/mondo/src/common/libmondo-string.c@ 1855

Last change on this file since 1855 was 1687, checked in by Bruno Cornec, 17 years ago

First attempt to integrate USB support in mondoarchive (may not compile)

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