source: MondoRescue/branches/3.2/mondo/src/common/libmondo-string.c@ 3289

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