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

Last change on this file since 1081 was 1081, checked in by Bruno Cornec, 17 years ago

merge -r1078:1080 $SVN_M/branches/stable

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