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

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

Huge memory management patch.
Still not finished but a lot as been done.
What remains is around some functions returning strings, and some structure members.
(Could not finish due to laptop failure !)

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