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

Last change on this file since 252 was 171, checked in by bcornec, 18 years ago

memory management continues:

  • mondoarchive handled completely
  • bkpinfo, begining of dyn. alloc.
  • lot of changes around memory everywhere

=> even if it compiles, i'm pretty sure it doesn't work yet (even not tried)

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