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

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