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

Last change on this file since 899 was 863, checked in by Bruno Cornec, 19 years ago

merge -r838:862 $SVN_M/branches/stable

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