source: MondoRescue/branches/2.2.10/mondo/src/common/libmondo-string.c@ 2405

Last change on this file since 2405 was 2405, checked in by Bruno Cornec, 15 years ago

Removes some malloc_string static allocation

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